File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.452: download - view: text, annotated - select for diffs
Tue May 13 04:07:07 2025 UTC (7 weeks, 4 days ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Use Crypt::CBC->new(-pass => $privkey, -cipher => 'Crypt::DES') to create
  cipher in all cases. [Default cipher in Crypt::CBC was changed from
  Crypt::DES to Crypt::Cipher::AES between v2 and v3 by module author.]

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.452 2025/05/13 04:07:07 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, textbook, placement, and lti).  
  107: In each case the radio buttons allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use Apache::courseprefs();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: use Crypt::CBC;
  181: 
  182: my $registered_cleanup;
  183: my $modified_urls;
  184: 
  185: sub handler {
  186:     my $r=shift;
  187:     if ($r->header_only) {
  188:         &Apache::loncommon::content_type($r,'text/html');
  189:         $r->send_http_header;
  190:         return OK;
  191:     }
  192: 
  193:     my $context = 'domain';
  194:     my $dom = $env{'request.role.domain'};
  195:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  196:     if (&Apache::lonnet::allowed('mau',$dom)) {
  197:         &Apache::loncommon::content_type($r,'text/html');
  198:         $r->send_http_header;
  199:     } else {
  200:         $env{'user.error.msg'}=
  201:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  202:         return HTTP_NOT_ACCEPTABLE;
  203:     }
  204: 
  205:     $registered_cleanup=0;
  206:     @{$modified_urls}=();
  207: 
  208:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                             ['phase','actions']);
  211:     my $phase = 'pickactions';
  212:     if ( exists($env{'form.phase'}) ) {
  213:         $phase = $env{'form.phase'};
  214:     }
  215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  216:     my %domconfig =
  217:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  218:                 'quotas','autoenroll','autoupdate','autocreate',
  219:                 'directorysrch','usercreation','usermodification',
  220:                 'contacts','defaults','scantron','coursecategories',
  221:                 'serverstatuses','requestcourses','helpsettings',
  222:                 'coursedefaults','usersessions','loadbalancing',
  223:                 'requestauthor','selfenrollment','inststatus',
  224:                 'ltitools','toolsec','ssl','trust','lti','ltisec',
  225:                 'privacy','passwords','proctoring','wafproxy',
  226:                 'ipaccess','authordefaults'],$dom);
  227:     my %encconfig =
  228:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  229:     my ($checked_is_home,$is_home);
  230:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  231:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  232:             my $home = &Apache::lonnet::domain($dom,'primary');
  233:             unless (($home eq 'no_host') || ($home eq '')) {
  234:                 my @ids=&Apache::lonnet::current_machine_ids();
  235:                 if (grep(/^\Q$home\E$/,@ids)) {
  236:                     $is_home = 1;
  237:                 }
  238:             }
  239:             $checked_is_home = 1;
  240:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  241:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  243:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  244:                     if (($is_home) && ($phase eq 'process')) {
  245:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'lti'}) eq 'HASH') {
  252:         if (ref($encconfig{'lti'}) eq 'HASH') {
  253:             unless ($checked_is_home) {
  254:                 my $home = &Apache::lonnet::domain($dom,'primary');
  255:                 unless (($home eq 'no_host') || ($home eq '')) {
  256:                     my @ids=&Apache::lonnet::current_machine_ids();
  257:                     if (grep(/^\Q$home\E$/,@ids)) {
  258:                         $is_home = 1;
  259:                     }
  260:                 }
  261:                 $checked_is_home = 1;
  262:             }
  263:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  264:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  265:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  266:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  267:                     if (($is_home) && ($phase eq 'process')) {
  268:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  269:                     }
  270:                 }
  271:             }
  272:         }
  273:     }
  274:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  275:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  276:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  277:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  278:                     unless ($id =~ /^\d+$/) {
  279:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  280:                     }
  281:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  282:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  283:                         foreach my $item ('key','secret') {
  284:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  285:                         }
  286:                     }
  287:                 }
  288:             }
  289:         }
  290:     }
  291:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  292:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  293:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  294:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  295:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  296:                     foreach my $item ('key','secret') {
  297:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  298:                     }
  299:                 }
  300:             }
  301:         }
  302:     }
  303:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  304:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  305:                        'contacts','privacy','usercreation','selfcreation',
  306:                        'usermodification','scantron','requestcourses','requestauthor',
  307:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  308:                        'authordefaults','ltitools','proctoring','selfenrollment',
  309:                        'usersessions','ssl','trust','lti');
  310:     my %existing;
  311:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  312:         %existing = %{$domconfig{'loadbalancing'}};
  313:     }
  314:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  315:         push(@prefs_order,'loadbalancing');
  316:     }
  317:     my %prefs = (
  318:         'rolecolors' =>
  319:                    { text => 'Default color schemes',
  320:                      help => 'Domain_Configuration_Color_Schemes',
  321:                      header => [{col1 => 'Student Settings',
  322:                                  col2 => '',},
  323:                                 {col1 => 'Coordinator Settings',
  324:                                  col2 => '',},
  325:                                 {col1 => 'Author Settings',
  326:                                  col2 => '',},
  327:                                 {col1 => 'Administrator Settings',
  328:                                  col2 => '',}],
  329:                       print => \&print_rolecolors,
  330:                       modify => \&modify_rolecolors,
  331:                     },
  332:         'login' =>
  333:                     { text => 'Log-in page options',
  334:                       help => 'Domain_Configuration_Login_Page',
  335:                       header => [{col1 => 'Log-in Page Items',
  336:                                   col2 => '',},
  337:                                  {col1 => 'Log-in Help',
  338:                                   col2 => 'Value'},
  339:                                  {col1 => 'Custom HTML in document head',
  340:                                   col2 => 'Value'},
  341:                                  {col1 => 'SSO',
  342:                                   col2 => 'Dual login: SSO and non-SSO options'},
  343:                                 ],
  344:                       print => \&print_login,
  345:                       modify => \&modify_login,
  346:                     },
  347:         'defaults' => 
  348:                     { text => 'Default authentication/language/timezone/portal/types',
  349:                       help => 'Domain_Configuration_LangTZAuth',
  350:                       header => [{col1 => 'Setting',
  351:                                   col2 => 'Value'},
  352:                                  {col1 => 'Institutional user types',
  353:                                   col2 => 'Name displayed'},
  354:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  355:                                   col2 => 'Rules in use'}],
  356:                       print => \&print_defaults,
  357:                       modify => \&modify_defaults,
  358:                     },
  359:         'wafproxy' =>
  360:                     { text => 'Web Application Firewall/Reverse Proxy',
  361:                       help => 'Domain_Configuration_WAF_Proxy',
  362:                       header => [{col1 => 'Domain(s)',
  363:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  364:                                  },
  365:                                  {col1 => 'Domain(s)',
  366:                                   col2 => 'WAF Configuration',}],
  367:                       print => \&print_wafproxy,
  368:                       modify => \&modify_wafproxy,
  369:                     },
  370:         'passwords' =>
  371:                     { text => 'Passwords (Internal authentication)',
  372:                       help => 'Domain_Configuration_Passwords',
  373:                       header => [{col1 => 'Resetting Forgotten Password',
  374:                                   col2 => 'Settings'},
  375:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  376:                                   col2 => 'Settings'},
  377:                                  {col1 => 'Rules for LON-CAPA Passwords',
  378:                                   col2 => 'Settings'},
  379:                                  {col1 => 'Course Owner Changing Student Passwords',
  380:                                   col2 => 'Settings'}],
  381:                       print => \&print_passwords,
  382:                       modify => \&modify_passwords,
  383:                     },
  384:         'quotas' => 
  385:                     { text => 'Blogs, personal pages/timezones, portfolio/quotas',
  386:                       help => 'Domain_Configuration_Quotas',
  387:                       header => [{col1 => 'User affiliation',
  388:                                   col2 => 'Available tools',
  389:                                   col3 => 'Portfolio quota (MB)',}],
  390:                       print => \&print_quotas,
  391:                       modify => \&modify_quotas,
  392:                     },
  393:         'autoenroll' =>
  394:                    { text => 'Auto-enrollment settings',
  395:                      help => 'Domain_Configuration_Auto_Enrollment',
  396:                      header => [{col1 => 'Configuration setting',
  397:                                  col2 => 'Value(s)'}],
  398:                      print => \&print_autoenroll,
  399:                      modify => \&modify_autoenroll,
  400:                    },
  401:         'autoupdate' => 
  402:                    { text => 'Auto-update settings',
  403:                      help => 'Domain_Configuration_Auto_Updates',
  404:                      header => [{col1 => 'Setting',
  405:                                  col2 => 'Value',},
  406:                                 {col1 => 'Setting',
  407:                                  col2 => 'Affiliation'},
  408:                                 {col1 => 'User population',
  409:                                  col2 => 'Updatable user data'}],
  410:                      print => \&print_autoupdate,
  411:                      modify => \&modify_autoupdate,
  412:                   },
  413:         'autocreate' => 
  414:                   { text => 'Auto-course creation settings',
  415:                      help => 'Domain_Configuration_Auto_Creation',
  416:                      header => [{col1 => 'Configuration Setting',
  417:                                  col2 => 'Value',}],
  418:                      print => \&print_autocreate,
  419:                      modify => \&modify_autocreate,
  420:                   },
  421:         'directorysrch' => 
  422:                   { text => 'Directory searches',
  423:                     help => 'Domain_Configuration_InstDirectory_Search',
  424:                     header => [{col1 => 'Institutional Directory Setting',
  425:                                 col2 => 'Value',},
  426:                                {col1 => 'LON-CAPA Directory Setting',
  427:                                 col2 => 'Value',}],
  428:                     print => \&print_directorysrch,
  429:                     modify => \&modify_directorysrch,
  430:                   },
  431:         'contacts' =>
  432:                   { text => 'E-mail addresses and helpform',
  433:                     help => 'Domain_Configuration_Contact_Info',
  434:                     header => [{col1 => 'Default e-mail addresses',
  435:                                 col2 => 'Value',},
  436:                                {col1 => 'Recipient(s) for notifications',
  437:                                 col2 => 'Value',},
  438:                                {col1 => 'Nightly status check e-mail',
  439:                                 col2 => 'Settings',},
  440:                                {col1 => 'Ask helpdesk form settings',
  441:                                 col2 => 'Value',},],
  442:                     print => \&print_contacts,
  443:                     modify => \&modify_contacts,
  444:                   },
  445:         'usercreation' => 
  446:                   { text => 'User creation',
  447:                     help => 'Domain_Configuration_User_Creation',
  448:                     header => [{col1 => 'Format rule type',
  449:                                 col2 => 'Format rules in force'},
  450:                                {col1 => 'User account creation',
  451:                                 col2 => 'Usernames which may be created',},
  452:                                {col1 => 'Context',
  453:                                 col2 => 'Assignable authentication types'}],
  454:                     print => \&print_usercreation,
  455:                     modify => \&modify_usercreation,
  456:                   },
  457:         'selfcreation' => 
  458:                   { text => 'Users self-creating accounts',
  459:                     help => 'Domain_Configuration_Self_Creation', 
  460:                     header => [{col1 => 'Self-creation with institutional username',
  461:                                 col2 => 'Enabled?'},
  462:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  463:                                 col2 => 'Information user can enter'},
  464:                                {col1 => 'Self-creation with e-mail verification',
  465:                                 col2 => 'Settings'}],
  466:                     print => \&print_selfcreation,
  467:                     modify => \&modify_selfcreation,
  468:                   },
  469:         'usermodification' =>
  470:                   { text => 'User modification',
  471:                     help => 'Domain_Configuration_User_Modification',
  472:                     header => [{col1 => 'Target user has role',
  473:                                 col2 => 'User information updatable in author context'},
  474:                                {col1 => 'Target user has role',
  475:                                 col2 => 'User information updatable by co-author manager'},
  476:                                {col1 => 'Target user has role',
  477:                                 col2 => 'User information updatable in course context'}],
  478:                     print => \&print_usermodification,
  479:                     modify => \&modify_usermodification,
  480:                   },
  481:         'scantron' =>
  482:                   { text => 'Bubblesheet format',
  483:                     help => 'Domain_Configuration_Scantron_Format',
  484:                     header => [ {col1 => 'Bubblesheet format file',
  485:                                  col2 => ''},
  486:                                 {col1 => 'Bubblesheet data upload formats',
  487:                                  col2 => 'Settings'}],
  488:                     print => \&print_scantron,
  489:                     modify => \&modify_scantron,
  490:                   },
  491:         'requestcourses' => 
  492:                  {text => 'Request creation of courses',
  493:                   help => 'Domain_Configuration_Request_Courses',
  494:                   header => [{col1 => 'User affiliation',
  495:                               col2 => 'Availability/Processing of requests',},
  496:                              {col1 => 'Setting',
  497:                               col2 => 'Value'},
  498:                              {col1 => 'Available textbooks',
  499:                               col2 => ''},
  500:                              {col1 => 'Available templates',
  501:                               col2 => ''},
  502:                              {col1 => 'Validation (not official courses)',
  503:                               col2 => 'Value'},],
  504:                   print => \&print_quotas,
  505:                   modify => \&modify_quotas,
  506:                  },
  507:         'requestauthor' =>
  508:                  {text => 'Request Authoring Space',
  509:                   help => 'Domain_Configuration_Request_Author',
  510:                   header => [{col1 => 'User affiliation',
  511:                               col2 => 'Availability/Processing of requests',},
  512:                              {col1 => 'Setting',
  513:                               col2 => 'Value'}],
  514:                   print => \&print_quotas,
  515:                   modify => \&modify_quotas,
  516:                  },
  517:         'coursecategories' =>
  518:                   { text => 'Cataloging of courses/communities',
  519:                     help => 'Domain_Configuration_Cataloging_Courses',
  520:                     header => [{col1 => 'Catalog type/availability',
  521:                                 col2 => '',},
  522:                                {col1 => 'Category settings for standard catalog',
  523:                                 col2 => '',},
  524:                                {col1 => 'Categories',
  525:                                 col2 => '',
  526:                                }],
  527:                     print => \&print_coursecategories,
  528:                     modify => \&modify_coursecategories,
  529:                   },
  530:         'serverstatuses' =>
  531:                  {text   => 'Access to server status pages',
  532:                   help   => 'Domain_Configuration_Server_Status',
  533:                   header => [{col1 => 'Status Page',
  534:                               col2 => 'Other named users',
  535:                               col3 => 'Specific IPs',
  536:                             }],
  537:                   print => \&print_serverstatuses,
  538:                   modify => \&modify_serverstatuses,
  539:                  },
  540:         'helpsettings' =>
  541:                  {text   => 'Support settings',
  542:                   help   => 'Domain_Configuration_Help_Settings',
  543:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  544:                               col2 => 'Value'},
  545:                              {col1 => 'Helpdesk Roles',
  546:                               col2 => 'Settings'},],
  547:                   print  => \&print_helpsettings,
  548:                   modify => \&modify_helpsettings,
  549:                  },
  550:         'coursedefaults' => 
  551:                  {text => 'Course/Community defaults',
  552:                   help => 'Domain_Configuration_Course_Defaults',
  553:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  554:                               col2 => 'Value',},
  555:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  556:                               col2 => 'Value',},],
  557:                   print => \&print_coursedefaults,
  558:                   modify => \&modify_coursedefaults,
  559:                  },
  560:         'selfenrollment' => 
  561:                  {text   => 'Self-enrollment in Course/Community',
  562:                   help   => 'Domain_Configuration_Selfenrollment',
  563:                   header => [{col1 => 'Configuration Rights',
  564:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  565:                              {col1 => 'Defaults',
  566:                               col2 => 'Value'},
  567:                              {col1 => 'Self-enrollment validation (optional)',
  568:                               col2 => 'Value'},],
  569:                   print => \&print_selfenrollment,
  570:                   modify => \&modify_selfenrollment,
  571:                  },
  572:         'privacy' => 
  573:                  {text   => 'Role assignments and user privacy',
  574:                   help   => 'Domain_Configuration_User_Privacy',
  575:                   header => [{col1 => 'Role assigned in different domain',
  576:                               col2 => 'Approval options'},
  577:                              {col1 => 'Role assigned in different domain to user of type',
  578:                               col2 => 'User information available in that domain'},
  579:                              {col1 => "Role assigned in user's domain",
  580:                               col2 => 'Information viewable by privileged user'},
  581:                              {col1 => "Role assigned in user's domain",
  582:                               col2 => 'Information viewable by unprivileged user'}],
  583:                   print => \&print_privacy,
  584:                   modify => \&modify_privacy,
  585:                  },
  586:         'usersessions' =>
  587:                  {text  => 'User session hosting/offloading',
  588:                   help  => 'Domain_Configuration_User_Sessions',
  589:                   header => [{col1 => 'Domain server',
  590:                               col2 => 'Servers to offload sessions to when busy'},
  591:                              {col1 => 'Hosting of users from other domains',
  592:                               col2 => 'Rules'},
  593:                              {col1 => "Hosting domain's own users elsewhere",
  594:                               col2 => 'Rules'}],
  595:                   print => \&print_usersessions,
  596:                   modify => \&modify_usersessions,
  597:                  },
  598:         'loadbalancing' =>
  599:                  {text  => 'Dedicated Load Balancer(s)',
  600:                   help  => 'Domain_Configuration_Load_Balancing',
  601:                   header => [{col1 => 'Balancers',
  602:                               col2 => 'Default destinations',
  603:                               col3 => 'User affiliation',
  604:                               col4 => 'Overrides'},
  605:                             ],
  606:                   print => \&print_loadbalancing,
  607:                   modify => \&modify_loadbalancing,
  608:                  },
  609:         'ltitools' =>
  610:                  {text => 'External Tools (LTI)',
  611:                   help => 'Domain_Configuration_LTI_Tools',
  612:                   header => [{col1 => 'Encryption of shared secrets',
  613:                               col2 => 'Settings'},
  614:                              {col1 => 'Rules for shared secrets',
  615:                               col2 => 'Settings'},
  616:                              {col1 => 'Providers',
  617:                               col2 => 'Settings',}],
  618:                   print => \&print_ltitools,
  619:                   modify => \&modify_ltitools,
  620:                  },
  621:         'proctoring' =>
  622:                  {text => 'Remote Proctoring Integration',
  623:                   help => 'Domain_Configuration_Proctoring',
  624:                   header => [{col1 => 'Name',
  625:                               col2 => 'Configuration'}],
  626:                   print => \&print_proctoring,
  627:                   modify => \&modify_proctoring,
  628:                  },
  629:         'ssl' =>
  630:                  {text  => 'LON-CAPA Network (SSL)',
  631:                   help  => 'Domain_Configuration_Network_SSL',
  632:                   header => [{col1 => 'Server',
  633:                               col2 => 'Certificate Status'},
  634:                              {col1 => 'Connections to other servers',
  635:                               col2 => 'Rules'},
  636:                              {col1 => 'Connections from other servers',
  637:                               col2 => 'Rules'},
  638:                              {col1 => "Replicating domain's published content",
  639:                               col2 => 'Rules'}],
  640:                   print => \&print_ssl,
  641:                   modify => \&modify_ssl,
  642:                  },
  643:         'trust' =>
  644:                  {text   => 'Trust Settings',
  645:                   help   => 'Domain_Configuration_Trust',
  646:                   header => [{col1 => "Access to this domain's content by others",
  647:                               col2 => 'Rules'},
  648:                              {col1 => "Access to other domain's content by this domain",
  649:                               col2 => 'Rules'},
  650:                              {col1 => "Enrollment in this domain's courses by others",
  651:                               col2 => 'Rules',},
  652:                              {col1 => "Co-author roles in this domain for others",
  653:                               col2 => 'Rules',},
  654:                              {col1 => "Co-author roles for this domain's users elsewhere",
  655:                               col2 => 'Rules',},
  656:                              {col1 => "Domain roles in this domain assignable to others",
  657:                               col2 => 'Rules'},
  658:                              {col1 => "Course catalog for this domain displayed elsewhere",
  659:                               col2 => 'Rules'},
  660:                              {col1 => "Requests for creation of courses in this domain by others",
  661:                               col2 => 'Rules'},
  662:                              {col1 => "Users in other domains can send messages to this domain",
  663:                               col2 => 'Rules'},],
  664:                   print => \&print_trust,
  665:                   modify => \&modify_trust,
  666:                  },
  667:         'lti' =>
  668:                  {text => 'LTI Link Protection and LTI Consumers',
  669:                   help => 'Domain_Configuration_LTI_Provider',
  670:                   header => [{col1 => 'Encryption of shared secrets',
  671:                               col2 => 'Settings'},
  672:                              {col1 => 'Rules for shared secrets',
  673:                               col2 => 'Settings'},
  674:                              {col1 => 'Link Protectors in Courses',
  675:                               col2 => 'Values'},
  676:                              {col1 => 'Link Protectors',
  677:                               col2 => 'Settings'},
  678:                              {col1 => 'Consumers',
  679:                               col2 => 'Settings'},],
  680:                   print => \&print_lti,
  681:                   modify => \&modify_lti,
  682:                  },
  683:         'ipaccess' =>
  684:                        {text => 'IP-based access control',
  685:                         help => 'Domain_Configuration_IP_Access',
  686:                         header => [{col1 => 'Setting',
  687:                                     col2 => 'Value'},],
  688:                         print  => \&print_ipaccess,
  689:                         modify => \&modify_ipaccess,
  690:                        },
  691:         'authordefaults' =>
  692:                             {text => 'Authoring Space defaults',
  693:                              help => 'Domain_Configuration_Author_Defaults',
  694:                              header => [{col1 => 'Defaults which can be overridden by Author',
  695:                                          col2 => 'Settings',},
  696:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  697:                                          col2 => 'Settings',},],
  698:                              print => \&print_authordefaults,
  699:                              modify => \&modify_authordefaults,
  700:                             },
  701:     );
  702:     if (keys(%servers) > 1) {
  703:         $prefs{'login'}  = { text   => 'Log-in page options',
  704:                              help   => 'Domain_Configuration_Login_Page',
  705:                             header => [{col1 => 'Log-in Service',
  706:                                         col2 => 'Server Setting',},
  707:                                        {col1 => 'Log-in Page Items',
  708:                                         col2 => 'Settings'},
  709:                                        {col1 => 'Log-in Help',
  710:                                         col2 => 'Value'},
  711:                                        {col1 => 'Custom HTML in document head',
  712:                                         col2 => 'Value'},
  713:                                        {col1 => 'SSO',
  714:                                         col2 => 'Dual login: SSO and non-SSO options'},
  715:                                       ],
  716:                             print => \&print_login,
  717:                             modify => \&modify_login,
  718:                            };
  719:     }
  720: 
  721:     my @roles = ('student','coordinator','author','admin');
  722:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  723:     &Apache::lonhtmlcommon::add_breadcrumb
  724:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  725:       text=>"Settings to display/modify"});
  726:     my $confname = $dom.'-domainconfig';
  727: 
  728:     if ($phase eq 'process') {
  729:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  730:                                                               \%prefs,\%domconfig,$confname,\@roles);
  731:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  732:             $r->rflush();
  733:             &devalidate_remote_domconfs($dom,$result);
  734:         }
  735:     } elsif ($phase eq 'display') {
  736:         my $js = &recaptcha_js().
  737:                  &toggle_display_js();
  738:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  739:             my ($othertitle,$usertypes,$types) =
  740:                 &Apache::loncommon::sorted_inst_types($dom);
  741:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  742:                                           $domconfig{'loadbalancing'}).
  743:                    &new_spares_js().
  744:                    &common_domprefs_js().
  745:                    &Apache::loncommon::javascript_array_indexof();
  746:         }
  747:         if (grep(/^requestcourses$/,@actions)) {
  748:             my $javascript_validations;
  749:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  750:             $js .= <<END;
  751: <script type="text/javascript">
  752: $javascript_validations
  753: </script>
  754: $coursebrowserjs
  755: END
  756:         } elsif (grep(/^ipaccess$/,@actions)) {
  757:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  758:         }
  759:         if (grep(/^selfcreation$/,@actions)) {
  760:             $js .= &selfcreate_javascript();
  761:         }
  762:         if (grep(/^contacts$/,@actions)) {
  763:             $js .= &contacts_javascript();
  764:         }
  765:         if (grep(/^scantron$/,@actions)) {
  766:             $js .= &scantron_javascript();
  767:         }
  768:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  769:     } else {
  770: # check if domconfig user exists for the domain.
  771:         my $servadm = $r->dir_config('lonAdmEMail');
  772:         my ($configuserok,$author_ok,$switchserver) =
  773:             &config_check($dom,$confname,$servadm);
  774:         unless ($configuserok eq 'ok') {
  775:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  776:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  777:                           $confname).
  778:                       '<br />'
  779:             );
  780:             if ($switchserver) {
  781:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  782:                           '<br />'.
  783:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  784:                           '<br />'.
  785:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  786:                           '<br />'.
  787:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  788:                 );
  789:             } else {
  790:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  791:                           '<br />'.
  792:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  793:                 );
  794:             }
  795:             $r->print(&Apache::loncommon::end_page());
  796:             return OK;
  797:         }
  798:         if (keys(%domconfig) == 0) {
  799:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  800:             my @ids=&Apache::lonnet::current_machine_ids();
  801:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  802:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  803:                 my @loginimages = ('img','logo','domlogo','login');
  804:                 my $custom_img_count = 0;
  805:                 foreach my $img (@loginimages) {
  806:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  807:                         $custom_img_count ++;
  808:                     }
  809:                 }
  810:                 foreach my $role (@roles) {
  811:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  812:                         $custom_img_count ++;
  813:                     }
  814:                 }
  815:                 if ($custom_img_count > 0) {
  816:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  817:                     my $switch_server = &check_switchserver($dom,$confname);
  818:                     $r->print(
  819:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  820:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  821:     &mt("Thereafter, (with a Domain Coordinator role selected in the domain) you will be able to update settings when logged in to any server in the LON-CAPA network.").'<br />'.
  822:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  823:                     if ($switch_server) {
  824:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  825:                     }
  826:                     $r->print(&Apache::loncommon::end_page());
  827:                     return OK;
  828:                 }
  829:             }
  830:         }
  831:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  832:     }
  833:     return OK;
  834: }
  835: 
  836: sub process_changes {
  837:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  838:     my %domconfig;
  839:     if (ref($values) eq 'HASH') {
  840:         %domconfig = %{$values};
  841:     }
  842:     my $output;
  843:     if ($action eq 'login') {
  844:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  845:     } elsif ($action eq 'rolecolors') {
  846:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  847:                                      $lastactref,%domconfig);
  848:     } elsif ($action eq 'quotas') {
  849:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  850:     } elsif ($action eq 'autoenroll') {
  851:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  852:     } elsif ($action eq 'autoupdate') {
  853:         $output = &modify_autoupdate($dom,%domconfig);
  854:     } elsif ($action eq 'autocreate') {
  855:         $output = &modify_autocreate($dom,%domconfig);
  856:     } elsif ($action eq 'directorysrch') {
  857:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  858:     } elsif ($action eq 'usercreation') {
  859:         $output = &modify_usercreation($dom,%domconfig);
  860:     } elsif ($action eq 'selfcreation') {
  861:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  862:     } elsif ($action eq 'usermodification') {
  863:         $output = &modify_usermodification($dom,%domconfig);
  864:     } elsif ($action eq 'contacts') {
  865:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  866:     } elsif ($action eq 'defaults') {
  867:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  868:     } elsif ($action eq 'scantron') {
  869:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  870:     } elsif ($action eq 'coursecategories') {
  871:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  872:     } elsif ($action eq 'serverstatuses') {
  873:         $output = &modify_serverstatuses($dom,%domconfig);
  874:     } elsif ($action eq 'requestcourses') {
  875:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  876:     } elsif ($action eq 'requestauthor') {
  877:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  878:     } elsif ($action eq 'helpsettings') {
  879:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  880:     } elsif ($action eq 'coursedefaults') {
  881:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  882:     } elsif ($action eq 'selfenrollment') {
  883:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  884:     } elsif ($action eq 'usersessions') {
  885:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  886:     } elsif ($action eq 'loadbalancing') {
  887:         $output = &modify_loadbalancing($dom,%domconfig);
  888:     } elsif ($action eq 'ltitools') {
  889:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  890:     } elsif ($action eq 'proctoring') {
  891:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  892:     } elsif ($action eq 'ssl') {
  893:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  894:     } elsif ($action eq 'trust') {
  895:         $output = &modify_trust($dom,$lastactref,%domconfig);
  896:     } elsif ($action eq 'lti') {
  897:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  898:     } elsif ($action eq 'privacy') {
  899:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  900:     } elsif ($action eq 'passwords') {
  901:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  902:     } elsif ($action eq 'wafproxy') {
  903:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  904:     } elsif ($action eq 'ipaccess') {
  905:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  906:     } elsif ($action eq 'authordefaults') {
  907:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  908:     }
  909:     return $output;
  910: }
  911: 
  912: sub print_config_box {
  913:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  914:     my $rowtotal = 0;
  915:     my $output;
  916:     if ($action eq 'coursecategories') {
  917:         $output = &coursecategories_javascript($settings);
  918:     } elsif ($action eq 'defaults') {
  919:         $output = &defaults_javascript($settings); 
  920:     } elsif ($action eq 'passwords') {
  921:         $output = &passwords_javascript($action);
  922:     } elsif ($action eq 'helpsettings') {
  923:         my (%privs,%levelscurrent);
  924:         my %full=();
  925:         my %levels=(
  926:                      course => {},
  927:                      domain => {},
  928:                      system => {},
  929:                    );
  930:         my $context = 'domain';
  931:         my $crstype = 'Course';
  932:         my $formname = 'display';
  933:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  934:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  935:         $output =
  936:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  937:                                                       \@templateroles);
  938:     } elsif ($action eq 'ltitools') {
  939:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  940:     } elsif ($action eq 'lti') {
  941:         $output .= &passwords_javascript('ltisecrets')."\n".
  942:                    &lti_javascript($dom,$settings);
  943:     } elsif ($action eq 'proctoring') {
  944:         $output .= &proctoring_javascript($settings);
  945:     } elsif ($action eq 'wafproxy') {
  946:         $output .= &wafproxy_javascript($dom);
  947:     } elsif ($action eq 'autoupdate') {
  948:         $output .= &autoupdate_javascript();
  949:     } elsif ($action eq 'autoenroll') {
  950:         $output .= &autoenroll_javascript();
  951:     } elsif ($action eq 'login') {
  952:         $output .= &saml_javascript();
  953:     } elsif ($action eq 'ipaccess') {
  954:         $output .= &ipaccess_javascript($settings);
  955:     } elsif ($action eq 'authordefaults') {
  956:         $output .= &authordefaults_javascript();
  957:     }
  958:     $output .=
  959:          '<table class="LC_nested_outer">
  960:           <tr>
  961:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  962:            &mt($item->{text}).'&nbsp;'.
  963:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  964:           '</tr>';
  965:     $rowtotal ++;
  966:     my $numheaders = 1;
  967:     if (ref($item->{'header'}) eq 'ARRAY') {
  968:         $numheaders = scalar(@{$item->{'header'}});
  969:     }
  970:     if ($numheaders > 1) {
  971:         my $colspan = '';
  972:         my $rightcolspan = '';
  973:         my $leftnobr = '';
  974:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  975:             ($action eq 'directorysrch') ||
  976:             (($action eq 'login') && ($numheaders < 5))) {
  977:             $colspan = ' colspan="2"';
  978:         }
  979:         if ($action eq 'usersessions') {
  980:             $rightcolspan = ' colspan="3"'; 
  981:         }
  982:         if ($action eq 'passwords') {
  983:             $leftnobr = ' LC_nobreak';
  984:         }
  985:         $output .= '
  986:           <tr>
  987:            <td>
  988:             <table class="LC_nested">
  989:              <tr class="LC_info_row">
  990:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  991:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  992:              </tr>';
  993:         $rowtotal ++;
  994:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  995:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  996:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  997:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  998:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  999:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
 1000:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
 1001:         } elsif ($action eq 'passwords') {
 1002:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'coursecategories') {
 1004:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
 1005:         } elsif ($action eq 'scantron') {
 1006:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
 1007:         } elsif ($action eq 'login') {
 1008:             if ($numheaders == 5) {
 1009:                 $colspan = ' colspan="2"';
 1010:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
 1011:             } else {
 1012:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
 1013:             }
 1014:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 1015:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1016:         } elsif ($action eq 'rolecolors') {
 1017:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1018:         }
 1019:         $output .= '
 1020:            </table>
 1021:           </td>
 1022:          </tr>
 1023:          <tr>
 1024:            <td>
 1025:             <table class="LC_nested">
 1026:              <tr class="LC_info_row">
 1027:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1028:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1029:              </tr>';
 1030:             $rowtotal ++;
 1031:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1032:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1033:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1034:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1035:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1036:             ($action eq 'ltitools') || ($action eq 'usermodification')) {
 1037:             if ($action eq 'coursecategories') {
 1038:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1039:                 $colspan = ' colspan="2"';
 1040:             } elsif ($action eq 'trust') {
 1041:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1042:             } elsif ($action eq 'passwords') {
 1043:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1044:             } elsif ($action eq 'lti') {
 1045:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
 1046:                            </table>
 1047:                           </td>
 1048:                          </tr>
 1049:                          <tr>
 1050:                           <td>
 1051:                           <table class="LC_nested">
 1052:                            <tr class="LC_info_row">
 1053:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1054:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1055:                            </tr>'."\n".
 1056:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1057:             } else {
 1058:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1059:             }
 1060:             if ($action eq 'trust') {
 1061:                 $output .= '
 1062:             </table>
 1063:           </td>
 1064:          </tr>';
 1065:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1066:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1067:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1068:                     $output .= '
 1069:          <tr>
 1070:            <td>
 1071:             <table class="LC_nested">
 1072:              <tr class="LC_info_row">
 1073:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1074:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1075:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1076:             </table>
 1077:           </td>
 1078:          </tr>';
 1079:                 }
 1080:                 $output .= '
 1081:          <tr>
 1082:            <td>
 1083:             <table class="LC_nested">
 1084:              <tr class="LC_info_row">
 1085:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1086:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1087:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1088:             } else {
 1089:                 my $hdridx = 2;
 1090:                 if ($action eq 'lti') {
 1091:                     $hdridx = 3;
 1092:                 }
 1093:                 $output .= '
 1094:            </table>
 1095:           </td>
 1096:          </tr>
 1097:          <tr>
 1098:            <td>
 1099:             <table class="LC_nested">
 1100:              <tr class="LC_info_row">
 1101:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1102:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
 1103:              </tr>'."\n";
 1104:                 if ($action eq 'coursecategories') {
 1105:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1106:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1107:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1108:                     if ($action eq 'passwords') {
 1109:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1110:                     } else {
 1111:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1112:                     }
 1113:                     $hdridx ++;
 1114:                     $output .= '
 1115:              </tr>
 1116:             </table>
 1117:            </td>
 1118:           </tr>
 1119:           <tr>
 1120:            <td>
 1121:             <table class="LC_nested">
 1122:              <tr class="LC_info_row">
 1123:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1124:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
 1125:                     if ($action eq 'passwords') {
 1126:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1127:                     } else {
 1128:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1129:                     }
 1130:                     $output .= '
 1131:             </table>
 1132:           </td>
 1133:          </tr>
 1134:          <tr>';
 1135:                 } else {
 1136:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1137:                 }
 1138:             }
 1139:             $rowtotal ++;
 1140:         } elsif (($action eq 'coursedefaults') || ($action eq 'authordefaults') ||
 1141:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1142:                  ($action eq 'wafproxy')) {
 1143:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1144:         } elsif ($action eq 'scantron') {
 1145:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1146:         } elsif ($action eq 'ssl') {
 1147:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1148:             </table>
 1149:           </td>
 1150:          </tr>
 1151:          <tr>
 1152:            <td>
 1153:             <table class="LC_nested">
 1154:              <tr class="LC_info_row">
 1155:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1156:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1157:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1158:             </table>
 1159:           </td>
 1160:          </tr>
 1161:          <tr>
 1162:            <td>
 1163:             <table class="LC_nested">
 1164:              <tr class="LC_info_row">
 1165:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1166:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1167:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1168:         } elsif ($action eq 'login') {
 1169:             if ($numheaders == 5) {
 1170:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1171:            </table>
 1172:           </td>
 1173:          </tr>
 1174:          <tr>
 1175:            <td>
 1176:             <table class="LC_nested">
 1177:              <tr class="LC_info_row">
 1178:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1179:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1180:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1181:                 $rowtotal ++;
 1182:             } else {
 1183:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1184:             }
 1185:             $output .= '
 1186:            </table>
 1187:           </td>
 1188:          </tr>
 1189:          <tr>
 1190:            <td>
 1191:             <table class="LC_nested">
 1192:              <tr class="LC_info_row">';
 1193:             if ($numheaders == 5) {
 1194:                 $output .= '
 1195:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1196:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1197:              </tr>';
 1198:             } else {
 1199:                 $output .= '
 1200:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1201:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1202:              </tr>';
 1203:             }
 1204:             $rowtotal ++;
 1205:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1206:            </table>
 1207:           </td>
 1208:          </tr>
 1209:          <tr>
 1210:            <td>
 1211:             <table class="LC_nested">
 1212:              <tr class="LC_info_row">';
 1213:             if ($numheaders == 5) {
 1214:                 $output .= '
 1215:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1216:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1217:              </tr>';
 1218:             } else {
 1219:                 $output .= '
 1220:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1221:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1222:              </tr>';
 1223:             }
 1224:             $rowtotal ++;
 1225:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1226:         } elsif ($action eq 'requestcourses') {
 1227:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1228:             $rowtotal ++;
 1229:             $output .= &print_studentcode($settings,\$rowtotal).'
 1230:            </table>
 1231:           </td>
 1232:          </tr>
 1233:          <tr>
 1234:            <td>
 1235:             <table class="LC_nested">
 1236:              <tr class="LC_info_row">
 1237:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1238:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1239:                        &textbookcourses_javascript($settings).
 1240:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1241:             </table>
 1242:            </td>
 1243:           </tr>
 1244:          <tr>
 1245:            <td>
 1246:             <table class="LC_nested">
 1247:              <tr class="LC_info_row">
 1248:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1249:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1250:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1251:             </table>
 1252:            </td>
 1253:           </tr>
 1254:           <tr>
 1255:            <td>
 1256:             <table class="LC_nested">
 1257:              <tr class="LC_info_row">
 1258:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1259:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1260:              </tr>'.
 1261:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1262:         } elsif ($action eq 'requestauthor') {
 1263:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1264:             $rowtotal ++;
 1265:         } elsif ($action eq 'rolecolors') {
 1266:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1267:            </table>
 1268:           </td>
 1269:          </tr>
 1270:          <tr>
 1271:            <td>
 1272:             <table class="LC_nested">
 1273:              <tr class="LC_info_row">
 1274:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1275:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1276:               <td class="LC_right_item" style="vertical-align: top">'.
 1277:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1278:              </tr>'.
 1279:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1280:            </table>
 1281:           </td>
 1282:          </tr>
 1283:          <tr>
 1284:            <td>
 1285:             <table class="LC_nested">
 1286:              <tr class="LC_info_row">
 1287:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1288:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1289:              </tr>'.
 1290:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1291:             $rowtotal += 2;
 1292:         }
 1293:     } else {
 1294:         $output .= '
 1295:           <tr>
 1296:            <td>
 1297:             <table class="LC_nested">
 1298:              <tr class="LC_info_row">';
 1299:         if ($action eq 'login') {
 1300:             $output .= '  
 1301:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1302:         } elsif ($action eq 'serverstatuses') {
 1303:             $output .= '
 1304:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1305:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1306: 
 1307:         } else {
 1308:             $output .= '
 1309:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1310:         }
 1311:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1312:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1313:                        &mt($item->{'header'}->[0]->{'col2'});
 1314:             if ($action eq 'serverstatuses') {
 1315:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1316:             } 
 1317:         } else {
 1318:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1319:                        &mt($item->{'header'}->[0]->{'col2'});
 1320:         }
 1321:         $output .= '</td>';
 1322:         if ($item->{'header'}->[0]->{'col3'}) {
 1323:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1324:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1325:                             &mt($item->{'header'}->[0]->{'col3'});
 1326:             } else {
 1327:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1328:                            &mt($item->{'header'}->[0]->{'col3'});
 1329:             }
 1330:             if ($action eq 'serverstatuses') {
 1331:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1332:             }
 1333:             $output .= '</td>';
 1334:         }
 1335:         if ($item->{'header'}->[0]->{'col4'}) {
 1336:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1337:                        &mt($item->{'header'}->[0]->{'col4'});
 1338:         }
 1339:         $output .= '</tr>';
 1340:         $rowtotal ++;
 1341:         if ($action eq 'quotas') {
 1342:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1343:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1344:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1345:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1346:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1347:         }
 1348:     }
 1349:     $output .= '
 1350:    </table>
 1351:   </td>
 1352:  </tr>
 1353: </table><br />';
 1354:     return ($output,$rowtotal);
 1355: }
 1356: 
 1357: sub print_login {
 1358:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1359:     my ($css_class,$datatable,$switchserver,%lt);
 1360:     my %choices = &login_choices();
 1361:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1362:         %lt = &login_file_options();
 1363:         $switchserver = &check_switchserver($dom,$confname);
 1364:     }
 1365:     if ($caller eq 'service') {
 1366:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1367:         my $choice = $choices{'disallowlogin'};
 1368:         $css_class = ' class="LC_odd_row"';
 1369:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1370:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1371:                       '<th>'.$choices{'server'}.'</th>'.
 1372:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1373:                       '<th>'.$choices{'custompath'}.'</th>'.
 1374:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1375:         my %disallowed;
 1376:         if (ref($settings) eq 'HASH') {
 1377:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1378:                %disallowed = %{$settings->{'loginvia'}};
 1379:             }
 1380:         }
 1381:         foreach my $lonhost (sort(keys(%servers))) {
 1382:             my $direct = 'selected="selected"';
 1383:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1384:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1385:                     $direct = '';
 1386:                 }
 1387:             }
 1388:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1389:                           '<td><select name="'.$lonhost.'_server">'.
 1390:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1391:                           '</option>';
 1392:             foreach my $hostid (sort(keys(%servers))) {
 1393:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1394:                 my $selected = '';
 1395:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1396:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1397:                         $selected = 'selected="selected"';
 1398:                     }
 1399:                 }
 1400:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1401:                               $servers{$hostid}.'</option>';
 1402:             }
 1403:             $datatable .= '</select></td>'.
 1404:                           '<td><select name="'.$lonhost.'_serverpath">';
 1405:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1406:                 my $pathname = $path;
 1407:                 if ($path eq 'custom') {
 1408:                     $pathname = &mt('Custom Path').' ->';
 1409:                 }
 1410:                 my $selected = '';
 1411:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1412:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1413:                         $selected = 'selected="selected"';
 1414:                     }
 1415:                 } elsif ($path eq '') {
 1416:                     $selected = 'selected="selected"';
 1417:                 }
 1418:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1419:             }
 1420:             $datatable .= '</select></td>';
 1421:             my ($custom,$exempt);
 1422:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1423:                 $custom = $disallowed{$lonhost}{'custompath'};
 1424:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1425:             }
 1426:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1427:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1428:                           '</tr>';
 1429:         }
 1430:         $datatable .= '</table></td></tr>';
 1431:         return $datatable;
 1432:     } elsif ($caller eq 'page') {
 1433:         my %defaultchecked = ( 
 1434:                                'coursecatalog' => 'on',
 1435:                                'helpdesk'      => 'on',
 1436:                                'adminmail'     => 'off',
 1437:                                'newuser'       => 'off',
 1438:                              );
 1439:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1440:         my (%checkedon,%checkedoff);
 1441:         foreach my $item (@toggles) {
 1442:             if ($defaultchecked{$item} eq 'on') { 
 1443:                 $checkedon{$item} = ' checked="checked" ';
 1444:                 $checkedoff{$item} = ' ';
 1445:             } elsif ($defaultchecked{$item} eq 'off') {
 1446:                 $checkedoff{$item} = ' checked="checked" ';
 1447:                 $checkedon{$item} = ' ';
 1448:             }
 1449:         }
 1450:         my @images = ('img','logo','domlogo','login');
 1451:         my @alttext = ('img','logo','domlogo');
 1452:         my @logintext = ('textcol','bgcol');
 1453:         my @bgs = ('pgbg','mainbg','sidebg');
 1454:         my @links = ('link','alink','vlink');
 1455:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1456:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1457:         my (%is_custom,%designs);
 1458:         my %defaults = (
 1459:                        font => $defaultdesign{'login.font'},
 1460:                        );
 1461:         foreach my $item (@images) {
 1462:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1463:             $defaults{'showlogo'}{$item} = 1;
 1464:         }
 1465:         foreach my $item (@bgs) {
 1466:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1467:         }
 1468:         foreach my $item (@logintext) {
 1469:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1470:         }
 1471:         foreach my $item (@links) {
 1472:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1473:         }
 1474:         if (ref($settings) eq 'HASH') {
 1475:             foreach my $item (@toggles) {
 1476:                 if ($settings->{$item} eq '1') {
 1477:                     $checkedon{$item} =  ' checked="checked" ';
 1478:                     $checkedoff{$item} = ' ';
 1479:                 } elsif ($settings->{$item} eq '0') {
 1480:                     $checkedoff{$item} =  ' checked="checked" ';
 1481:                     $checkedon{$item} = ' ';
 1482:                 }
 1483:             }
 1484:             foreach my $item (@images) {
 1485:                 if (defined($settings->{$item})) {
 1486:                     $designs{$item} = $settings->{$item};
 1487:                     $is_custom{$item} = 1;
 1488:                 }
 1489:                 if (defined($settings->{'showlogo'}{$item})) {
 1490:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1491:                 }
 1492:             }
 1493:             foreach my $item (@alttext) {
 1494:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1495:                     if ($settings->{'alttext'}->{$item} ne '') {
 1496:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1497:                     }
 1498:                 }
 1499:             }
 1500:             foreach my $item (@logintext) {
 1501:                 if ($settings->{$item} ne '') {
 1502:                     $designs{'logintext'}{$item} = $settings->{$item};
 1503:                     $is_custom{$item} = 1;
 1504:                 }
 1505:             }
 1506:             if ($settings->{'font'} ne '') {
 1507:                 $designs{'font'} = $settings->{'font'};
 1508:                 $is_custom{'font'} = 1;
 1509:             }
 1510:             foreach my $item (@bgs) {
 1511:                 if ($settings->{$item} ne '') {
 1512:                     $designs{'bgs'}{$item} = $settings->{$item};
 1513:                     $is_custom{$item} = 1;
 1514:                 }
 1515:             }
 1516:             foreach my $item (@links) {
 1517:                 if ($settings->{$item} ne '') {
 1518:                     $designs{'links'}{$item} = $settings->{$item};
 1519:                     $is_custom{$item} = 1;
 1520:                 }
 1521:             }
 1522:         } else {
 1523:             if ($designhash{$dom.'.login.font'} ne '') {
 1524:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1525:                 $is_custom{'font'} = 1;
 1526:             }
 1527:             foreach my $item (@images) {
 1528:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1529:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1530:                     $is_custom{$item} = 1;
 1531:                 }
 1532:             }
 1533:             foreach my $item (@bgs) {
 1534:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1535:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1536:                     $is_custom{$item} = 1;
 1537:                 }
 1538:             }
 1539:             foreach my $item (@links) {
 1540:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1541:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1542:                     $is_custom{$item} = 1;
 1543:                 }
 1544:             }
 1545:         }
 1546:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1547:                                                       logo => 'Institution Logo',
 1548:                                                       domlogo => 'Domain Logo',
 1549:                                                       login => 'Login box');
 1550:         my $itemcount = 1;
 1551:         foreach my $item (@toggles) {
 1552:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1553:             $datatable .=  
 1554:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1555:                 '</td><td>'.
 1556:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1557:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1558:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1559:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1560:                 '</tr>';
 1561:             $itemcount ++;
 1562:         }
 1563:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1564:         $datatable .= '</tr></table></td></tr>';
 1565:     } elsif ($caller eq 'help') {
 1566:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1567:         my $itemcount = 1;
 1568:         $defaulturl = '/adm/loginproblems.html';
 1569:         $defaulttype = 'default';
 1570:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1571:         my @currlangs;
 1572:         if (ref($settings) eq 'HASH') {
 1573:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1574:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1575:                     next if ($settings->{'helpurl'}{$key} eq '');
 1576:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1577:                     $type{$key} = 'custom';
 1578:                     unless ($key eq 'nolang') {
 1579:                         push(@currlangs,$key);
 1580:                     }
 1581:                 }
 1582:             } elsif ($settings->{'helpurl'} ne '') {
 1583:                 $type{'nolang'} = 'custom';
 1584:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1585:             }
 1586:         }
 1587:         foreach my $lang ('nolang',sort(@currlangs)) {
 1588:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1589:             $datatable .= '<tr'.$css_class.'>';
 1590:             if ($url{$lang} eq '') {
 1591:                 $url{$lang} = $defaulturl;
 1592:             }
 1593:             if ($type{$lang} eq '') {
 1594:                 $type{$lang} = $defaulttype;
 1595:             }
 1596:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1597:             if ($lang eq 'nolang') {
 1598:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1599:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1600:             } else {
 1601:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1602:                                   $langchoices{$lang},
 1603:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1604:             }
 1605:             $datatable .= '</span></td>'."\n".
 1606:                           '<td class="LC_left_item">';
 1607:             if ($type{$lang} eq 'custom') {
 1608:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1609:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1610:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1611:             } else {
 1612:                 $datatable .= $lt{'upl'};
 1613:             }
 1614:             $datatable .='<br />';
 1615:             if ($switchserver) {
 1616:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1617:             } else {
 1618:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1619:             }
 1620:             $datatable .= '</td></tr>';
 1621:             $itemcount ++;
 1622:         }
 1623:         my @addlangs;
 1624:         foreach my $lang (sort(keys(%langchoices))) {
 1625:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1626:             push(@addlangs,$lang);
 1627:         }
 1628:         if (@addlangs > 0) {
 1629:             my %toadd;
 1630:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1631:             $toadd{''} = &mt('Select');
 1632:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1633:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1634:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1635:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1636:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1637:             if ($switchserver) {
 1638:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1639:             } else {
 1640:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1641:             }
 1642:             $datatable .= '</td></tr>';
 1643:             $itemcount ++;
 1644:         }
 1645:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1646:     } elsif ($caller eq 'headtag') {
 1647:         my %domservers = &Apache::lonnet::get_servers($dom);
 1648:         my $choice = $choices{'headtag'};
 1649:         $css_class = ' class="LC_odd_row"';
 1650:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1651:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1652:                       '<th>'.$choices{'current'}.'</th>'.
 1653:                       '<th>'.$choices{'action'}.'</th>'.
 1654:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1655:         my (%currurls,%currexempt);
 1656:         if (ref($settings) eq 'HASH') {
 1657:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1658:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1659:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1660:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1661:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1662:                     }
 1663:                 }
 1664:             }
 1665:         }
 1666:         foreach my $lonhost (sort(keys(%domservers))) {
 1667:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1668:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1669:             if ($currurls{$lonhost}) {
 1670:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1671:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1672:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1673:                               '">'.$lt{'curr'}.'</a></td>'.
 1674:                               '<td><span class="LC_nobreak"><label>'.
 1675:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1676:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1677:             } else {
 1678:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1679:             }
 1680:             $datatable .='<br />';
 1681:             if ($switchserver) {
 1682:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1683:             } else {
 1684:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1685:             }
 1686:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1687:         }
 1688:         $datatable .= '</table></td></tr>';
 1689:     } elsif ($caller eq 'saml') {
 1690:         my %domservers = &Apache::lonnet::get_servers($dom);
 1691:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1692:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1693:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1694:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1695:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1696:         foreach my $lonhost (keys(%domservers)) {
 1697:             $samlurl{$lonhost} = '/adm/sso';
 1698:             $styleon{$lonhost} = 'display:none';
 1699:             $styleoff{$lonhost} = '';
 1700:         }
 1701:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1702:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1703:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1704:                     $saml{$lonhost} = 1;
 1705:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1706:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1707:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1708:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1709:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1710:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1711:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1712:                     $styleon{$lonhost} = '';
 1713:                     $styleoff{$lonhost} = 'display:none';
 1714:                 } else {
 1715:                     $styleon{$lonhost} = 'display:none';
 1716:                     $styleoff{$lonhost} = '';
 1717:                 }
 1718:             }
 1719:         }
 1720:         my $itemcount = 1;
 1721:         foreach my $lonhost (sort(keys(%domservers))) {
 1722:             my $samlon = ' ';
 1723:             my $samloff = ' checked="checked" ';
 1724:             if ($saml{$lonhost}) {
 1725:                 $samlon = $samloff;
 1726:                 $samloff = ' ';
 1727:             }
 1728:             my $samlwinon = '';
 1729:             my $samlwinoff = ' checked="checked"';
 1730:             if ($samlwindow{$lonhost}) {
 1731:                 $samlwinon = $samlwinoff;
 1732:                 $samlwinoff = '';
 1733:             }
 1734:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1735:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1736:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1737:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1738:                           &mt('No').'</label>'.('&nbsp;'x2).
 1739:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1740:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1741:                           &mt('Yes').'</label></span></td>'.
 1742:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1743:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1744:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1745:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1746:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1747:                           $samltext{$lonhost}.'" /></td><td>';
 1748:             if ($samlimg{$lonhost}) {
 1749:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1750:                               '<span class="LC_nobreak"><label>'.
 1751:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1752:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1753:             } else {
 1754:                 $datatable .= $lt{'upl'};
 1755:             }
 1756:             $datatable .='<br />';
 1757:             if ($switchserver) {
 1758:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1759:             } else {
 1760:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1761:             }
 1762:             $datatable .= '</td>'.
 1763:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1764:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1765:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1766:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1767:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1768:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1769:                           '<tr'.$css_class.'>'.
 1770:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1771:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1772:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1773:                           $samltitle{$lonhost}.'</textarea></td>'.
 1774:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1775:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1776:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1777:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1778:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1779:                           '</table></td>'.
 1780:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1781:            $itemcount ++;
 1782:         }
 1783:         $datatable .= '</table></td></tr>';
 1784:     }
 1785:     return $datatable;
 1786: }
 1787: 
 1788: sub login_choices {
 1789:     my %choices =
 1790:         &Apache::lonlocal::texthash (
 1791:             coursecatalog => 'Display Course/Community Catalog link?',
 1792:             adminmail     => "Display Administrator's E-mail Address?",
 1793:             helpdesk      => 'Display "Contact Helpdesk" link',
 1794:             disallowlogin => "Login page requests redirected",
 1795:             hostid        => "Server",
 1796:             server        => "Redirect to:",
 1797:             serverpath    => "Path",
 1798:             custompath    => "Custom", 
 1799:             exempt        => "Exempt IP(s)",
 1800:             directlogin   => "No redirect",
 1801:             newuser       => "Link to create a user account",
 1802:             img           => "Header",
 1803:             logo          => "Main Logo",
 1804:             domlogo       => "Domain Logo",
 1805:             login         => "Log-in Header", 
 1806:             textcol       => "Text color",
 1807:             bgcol         => "Box color",
 1808:             bgs           => "Background colors",
 1809:             links         => "Link colors",
 1810:             font          => "Font color",
 1811:             pgbg          => "Header",
 1812:             mainbg        => "Page",
 1813:             sidebg        => "Login box",
 1814:             link          => "Link",
 1815:             alink         => "Active link",
 1816:             vlink         => "Visited link",
 1817:             headtag       => "Custom markup",
 1818:             action        => "Action",
 1819:             current       => "Current",
 1820:             samllanding   => "Dual login?",
 1821:             samloptions   => "Options",
 1822:             alttext       => "Alt text",
 1823:         );
 1824:     return %choices;
 1825: }
 1826: 
 1827: sub login_file_options {
 1828:       return &Apache::lonlocal::texthash(
 1829:                                            del     => 'Delete?',
 1830:                                            rep     => 'Replace:',
 1831:                                            upl     => 'Upload:',
 1832:                                            curr    => 'View contents',
 1833:                                            default => 'Default',
 1834:                                            custom  => 'Custom',
 1835:                                            none    => 'None',
 1836:       );
 1837: }
 1838: 
 1839: sub print_ipaccess {
 1840:     my ($dom,$settings,$rowtotal) = @_;
 1841:     my $css_class;
 1842:     my $itemcount = 0;
 1843:     my $datatable;
 1844:     my %ordered;
 1845:     if (ref($settings) eq 'HASH') {
 1846:         foreach my $item (keys(%{$settings})) {
 1847:             if (ref($settings->{$item}) eq 'HASH') {
 1848:                 my $num = $settings->{$item}{'order'};
 1849:                 if ($num eq '') {
 1850:                     $num = scalar(keys(%{$settings}));
 1851:                 }
 1852:                 $ordered{$num} = $item;
 1853:             }
 1854:         }
 1855:     }
 1856:     my $maxnum = scalar(keys(%ordered));
 1857:     if (keys(%ordered)) {
 1858:         my @items = sort { $a <=> $b } keys(%ordered);
 1859:         for (my $i=0; $i<@items; $i++) {
 1860:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1861:             my $item = $ordered{$items[$i]};
 1862:             my ($name,$ipranges,%commblocks,%courses);
 1863:             if (ref($settings->{$item}) eq 'HASH') {
 1864:                 $name = $settings->{$item}->{'name'};
 1865:                 $ipranges = $settings->{$item}->{'ip'};
 1866:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1867:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1868:                 }
 1869:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1870:                     %courses = %{$settings->{$item}->{'courses'}};
 1871:                 }
 1872:             }
 1873:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1874:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1875:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1876:             for (my $k=0; $k<=$maxnum; $k++) {
 1877:                 my $vpos = $k+1;
 1878:                 my $selstr;
 1879:                 if ($k == $i) {
 1880:                     $selstr = ' selected="selected" ';
 1881:                 }
 1882:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1883:             }
 1884:             $datatable .= '</select>'.('&nbsp;'x2).
 1885:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1886:                 &mt('Delete?').'</label></span></td>'.
 1887:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1888:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1889:                 '</td></tr>';
 1890:             $itemcount ++;
 1891:         }
 1892:     }
 1893:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1894:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1895:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1896:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1897:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1898:     for (my $k=0; $k<$maxnum+1; $k++) {
 1899:         my $vpos = $k+1;
 1900:         my $selstr;
 1901:         if ($k == $maxnum) {
 1902:             $selstr = ' selected="selected" ';
 1903:         }
 1904:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1905:     }
 1906:     $datatable .= '</select>&nbsp;'."\n".
 1907:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1908:                   '<td colspan="2">'.
 1909:                   &ipaccess_options('add',$itemcount,$dom).
 1910:                   '</td>'."\n".
 1911:                   '</tr>'."\n";
 1912:     $$rowtotal ++;
 1913:     return $datatable;
 1914: }
 1915: 
 1916: sub ipaccess_options {
 1917:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1918:     my (%currblocks,%currcourses,$output);
 1919:     if (ref($blocksref) eq 'HASH') {
 1920:         %currblocks = %{$blocksref};
 1921:     }
 1922:     if (ref($coursesref) eq 'HASH') {
 1923:         %currcourses = %{$coursesref};
 1924:     }
 1925:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1926:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1927:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1928:               '</span></fieldset>'.
 1929:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1930:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1931:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1932:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1933:               $ipranges.'</textarea></fieldset>'.
 1934:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1935:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1936:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1937:               '<table>';
 1938:     foreach my $cid (sort(keys(%currcourses))) {
 1939:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1940:         $output .= '<tr><td><span class="LC_nobreak">'.
 1941:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1942:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1943:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1944:     }
 1945:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1946:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1947:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1948:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1949:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1950:                '</span></td></tr></table>'."\n".
 1951:                '</fieldset>';
 1952:     return $output;
 1953: }
 1954: 
 1955: sub blocker_checkboxes {
 1956:     my ($num,$blocks) = @_;
 1957:     my ($typeorder,$types) = &commblocktype_text();
 1958:     my $numinrow = 6;
 1959:     my $output = '<table>';
 1960:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1961:         my $block = $typeorder->[$i];
 1962:         my $blockstatus;
 1963:         if (ref($blocks) eq 'HASH') {
 1964:             if ($blocks->{$block} eq 'on') {
 1965:                 $blockstatus = 'checked="checked"';
 1966:             }
 1967:         }
 1968:         my $rem = $i%($numinrow);
 1969:         if ($rem == 0) {
 1970:             if ($i > 0) {
 1971:                 $output .= '</tr>';
 1972:             }
 1973:             $output .= '<tr>';
 1974:         }
 1975:         if ($i == scalar(@{$typeorder})-1) {
 1976:             my $colsleft = $numinrow-$rem;
 1977:             if ($colsleft > 1) {
 1978:                 $output .= '<td colspan="'.$colsleft.'">';
 1979:             } else {
 1980:                 $output .= '<td>';
 1981:             }
 1982:         } else {
 1983:             $output .= '<td>';
 1984:         }
 1985:         my $item = 'ipaccess_block_'.$num;
 1986:         if ($blockstatus) {
 1987:             $blockstatus = ' '.$blockstatus;
 1988:         }
 1989:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1990:                    '<input type="checkbox" name="'.$item.'"'.
 1991:                    $blockstatus.' value="'.$block.'"'.' />'.
 1992:                    $types->{$block}.'</label></span>'."\n".
 1993:                    '<br /></td>';
 1994:     }
 1995:     $output .= '</tr></table>';
 1996:     return $output;
 1997: }
 1998: 
 1999: sub commblocktype_text {
 2000:     my %types = &Apache::lonlocal::texthash(
 2001:         'com' => 'Messaging',
 2002:         'chat' => 'Chat Room',
 2003:         'boards' => 'Discussion',
 2004:         'port' => 'Portfolio',
 2005:         'groups' => 'Groups',
 2006:         'blogs' => 'Blogs',
 2007:         'about' => 'User Information',
 2008:         'printout' => 'Printouts',
 2009:         'passwd' => 'Change Password',
 2010:         'grades' => 'Gradebook',
 2011:         'search' => 'Course search',
 2012:         'index'  => 'Course content index',
 2013:         'wishlist' => 'Stored links',
 2014:         'annotate' => 'Annotations',
 2015:     );
 2016:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','index','annotate','passwd'];
 2017:     return ($typeorder,\%types);
 2018: }
 2019: 
 2020: sub print_rolecolors {
 2021:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 2022:     my %choices = &color_font_choices();
 2023:     my @bgs = ('pgbg','tabbg','sidebg');
 2024:     my @links = ('link','alink','vlink');
 2025:     my @images = ();
 2026:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 2027:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 2028:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2029:     my (%is_custom,%designs);
 2030:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 2031:     if (ref($settings) eq 'HASH') {
 2032:         if (ref($settings->{$role}) eq 'HASH') {
 2033:             if ($settings->{$role}->{'font'} ne '') {
 2034:                 $designs{'font'} = $settings->{$role}->{'font'};
 2035:                 $is_custom{'font'} = 1;
 2036:             }
 2037:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2038:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2039:                 $is_custom{'fontmenu'} = 1;
 2040:             }
 2041:             foreach my $item (@bgs) {
 2042:                 if ($settings->{$role}->{$item} ne '') {
 2043:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2044:                     $is_custom{$item} = 1;
 2045:                 }
 2046:             }
 2047:             foreach my $item (@links) {
 2048:                 if ($settings->{$role}->{$item} ne '') {
 2049:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2050:                     $is_custom{$item} = 1;
 2051:                 }
 2052:             }
 2053:         }
 2054:     } else {
 2055:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2056:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2057:             $is_custom{'fontmenu'} = 1; 
 2058:         }
 2059:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2060:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2061:             $is_custom{'font'} = 1;
 2062:         }
 2063:         foreach my $item (@bgs) {
 2064:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2065:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2066:                 $is_custom{$item} = 1;
 2067:             
 2068:             }
 2069:         }
 2070:         foreach my $item (@links) {
 2071:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2072:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2073:                 $is_custom{$item} = 1;
 2074:             }
 2075:         }
 2076:     }
 2077:     my $itemcount = 1;
 2078:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2079:     $datatable .= '</tr></table></td></tr>';
 2080:     return $datatable;
 2081: }
 2082: 
 2083: sub role_defaults {
 2084:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2085:     my %defaults;
 2086:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2087:         return %defaults;
 2088:     }
 2089:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2090:     if ($role eq 'login') {
 2091:         %defaults = (
 2092:                        font => $defaultdesign{$role.'.font'},
 2093:                     );
 2094:         if (ref($logintext) eq 'ARRAY') {
 2095:             foreach my $item (@{$logintext}) {
 2096:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2097:             }
 2098:         }
 2099:         foreach my $item (@{$images}) {
 2100:             $defaults{'showlogo'}{$item} = 1;
 2101:         }
 2102:     } else {
 2103:         %defaults = (
 2104:                        font => $defaultdesign{$role.'.font'},
 2105:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2106:                     );
 2107:     }
 2108:     foreach my $item (@{$bgs}) {
 2109:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2110:     }
 2111:     foreach my $item (@{$links}) {
 2112:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2113:     }
 2114:     foreach my $item (@{$images}) {
 2115:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2116:     }
 2117:     return %defaults;
 2118: }
 2119: 
 2120: sub display_color_options {
 2121:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2122:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2123:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2124:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2125:     my $datatable = '<tr'.$css_class.'>'.
 2126:         '<td>'.$choices->{'font'}.'</td>';
 2127:     if (!$is_custom->{'font'}) {
 2128:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2129:     } else {
 2130:         $datatable .= '<td>&nbsp;</td>';
 2131:     }
 2132:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2133: 
 2134:     $datatable .= '<td><span class="LC_nobreak">'.
 2135:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2136:                   ' value="'.$current_color.'" />&nbsp;'.
 2137:                   '&nbsp;</span></td></tr>';
 2138:     unless ($role eq 'login') { 
 2139:         $datatable .= '<tr'.$css_class.'>'.
 2140:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2141:         if (!$is_custom->{'fontmenu'}) {
 2142:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2143:         } else {
 2144:             $datatable .= '<td>&nbsp;</td>';
 2145:         }
 2146: 	$current_color = $designs->{'fontmenu'} ?
 2147: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2148:         $datatable .= '<td><span class="LC_nobreak">'.
 2149:                       '<input class="colorchooser" type="text" size="10" name="'
 2150: 		      .$role.'_fontmenu"'.
 2151:                       ' value="'.$current_color.'" />&nbsp;'.
 2152:                       '&nbsp;</span></td></tr>';
 2153:     }
 2154:     my $switchserver = &check_switchserver($dom,$confname);
 2155:     foreach my $img (@{$images}) {
 2156: 	$itemcount ++;
 2157:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2158:         $datatable .= '<tr'.$css_class.'>'.
 2159:                       '<td>'.$choices->{$img};
 2160:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2161:         if ($role eq 'login') {
 2162:             if ($img eq 'login') {
 2163:                 $login_hdr_pick =
 2164:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2165:                 $logincolors =
 2166:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2167:                                        $designs,$defaults);
 2168:             } else {
 2169:                 if ($img ne 'domlogo') {
 2170:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2171:                 }
 2172:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2173:                     $alttext = $designs->{'alttext'}{$img};
 2174:                 }
 2175:             }
 2176:         }
 2177:         $datatable .= '</td>';
 2178:         if ($designs->{$img} ne '') {
 2179:             $imgfile = $designs->{$img};
 2180: 	    $img_import = ($imgfile =~ m{^/adm/});
 2181:         } else {
 2182:             $imgfile = $defaults->{$img};
 2183:         }
 2184:         if ($imgfile) {
 2185:             my ($showfile,$fullsize);
 2186:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2187:                 my $urldir = $1;
 2188:                 my $filename = $2;
 2189:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2190:                 if (@info) {
 2191:                     my $thumbfile = 'tn-'.$filename;
 2192:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2193:                     if (@thumb) {
 2194:                         $showfile = $urldir.'/'.$thumbfile;
 2195:                     } else {
 2196:                         $showfile = $imgfile;
 2197:                     }
 2198:                 } else {
 2199:                     $showfile = '';
 2200:                 }
 2201:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2202:                 $showfile = $imgfile;
 2203:                 my $imgdir = $1;
 2204:                 my $filename = $2;
 2205:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2206:                     $showfile = "/$imgdir/tn-".$filename;
 2207:                 } else {
 2208:                     my $input = $londocroot.$imgfile;
 2209:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2210:                     if (!-e $output) {
 2211:                         my ($width,$height) = &thumb_dimensions();
 2212:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2213:                         if ($fullwidth ne '' && $fullheight ne '') {
 2214:                             if ($fullwidth > $width && $fullheight > $height) { 
 2215:                                 my $size = $width.'x'.$height;
 2216:                                 my @args = ('convert','-sample',$size,$input,$output);
 2217:                                 system({$args[0]} @args);
 2218:                                 $showfile = "/$imgdir/tn-".$filename;
 2219:                             }
 2220:                         }
 2221:                     }
 2222:                 }
 2223:             }
 2224:             if ($showfile) {
 2225:                 if ($showfile =~ m{^/(adm|res)/}) {
 2226:                     if ($showfile =~ m{^/res/}) {
 2227:                         my $local_showfile =
 2228:                             &Apache::lonnet::filelocation('',$showfile);
 2229:                         &Apache::lonnet::repcopy($local_showfile);
 2230:                     }
 2231:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2232:                 }
 2233:                 if ($imgfile) {
 2234:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2235:                         if ($imgfile =~ m{^/res/}) {
 2236:                             my $local_imgfile =
 2237:                                 &Apache::lonnet::filelocation('',$imgfile);
 2238:                             &Apache::lonnet::repcopy($local_imgfile);
 2239:                         }
 2240:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2241:                     } else {
 2242:                         $fullsize = $imgfile;
 2243:                     }
 2244:                 }
 2245:                 $datatable .= '<td>';
 2246:                 if ($img eq 'login') {
 2247:                     $datatable .= $login_hdr_pick;
 2248:                 } 
 2249:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2250:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2251:             } else {
 2252:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2253:                               &mt('Upload:').'<br />';
 2254:             }
 2255:         } else {
 2256:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2257:                           &mt('Upload:').'<br />';
 2258:         }
 2259:         if ($switchserver) {
 2260:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2261:         } else {
 2262:             if ($img ne 'login') { # suppress file selection for Log-in header
 2263:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2264:             }
 2265:         }
 2266:         if (($role eq 'login') && ($img ne 'login')) {
 2267:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2268:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2269:                           '</label></span>';
 2270:         }
 2271:         $datatable .= '</td></tr>';
 2272:     }
 2273:     $itemcount ++;
 2274:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2275:     $datatable .= '<tr'.$css_class.'>'.
 2276:                   '<td>'.$choices->{'bgs'}.'</td>';
 2277:     my $bgs_def;
 2278:     foreach my $item (@{$bgs}) {
 2279:         if (!$is_custom->{$item}) {
 2280:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 2281:         }
 2282:     }
 2283:     if ($bgs_def) {
 2284:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2285:     } else {
 2286:         $datatable .= '<td>&nbsp;</td>';
 2287:     }
 2288:     $datatable .= '<td class="LC_right_item">'.
 2289:                   '<table border="0"><tr>';
 2290: 
 2291:     foreach my $item (@{$bgs}) {
 2292:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2293: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2294:         if ($designs->{'bgs'}{$item}) {
 2295:             $datatable .= '&nbsp;';
 2296:         }
 2297:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2298:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2299:     }
 2300:     $datatable .= '</tr></table></td></tr>';
 2301:     $itemcount ++;
 2302:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2303:     $datatable .= '<tr'.$css_class.'>'.
 2304:                   '<td>'.$choices->{'links'}.'</td>';
 2305:     my $links_def;
 2306:     foreach my $item (@{$links}) {
 2307:         if (!$is_custom->{$item}) {
 2308:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2309:         }
 2310:     }
 2311:     if ($links_def) {
 2312:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2313:     } else {
 2314:         $datatable .= '<td>&nbsp;</td>';
 2315:     }
 2316:     $datatable .= '<td class="LC_right_item">'.
 2317:                   '<table border="0"><tr>';
 2318:     foreach my $item (@{$links}) {
 2319: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2320:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2321:         if ($designs->{'links'}{$item}) {
 2322:             $datatable.='&nbsp;';
 2323:         }
 2324:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2325:                       '" /></td>';
 2326:     }
 2327:     $$rowtotal += $itemcount;
 2328:     return $datatable;
 2329: }
 2330: 
 2331: sub logo_display_options {
 2332:     my ($img,$defaults,$designs) = @_;
 2333:     my $checkedon;
 2334:     if (ref($defaults) eq 'HASH') {
 2335:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2336:             if ($defaults->{'showlogo'}{$img}) {
 2337:                 $checkedon = 'checked="checked" ';     
 2338:             }
 2339:         } 
 2340:     }
 2341:     if (ref($designs) eq 'HASH') {
 2342:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2343:             if (defined($designs->{'showlogo'}{$img})) {
 2344:                 if ($designs->{'showlogo'}{$img} == 0) {
 2345:                     $checkedon = '';
 2346:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2347:                     $checkedon = 'checked="checked" ';
 2348:                 }
 2349:             }
 2350:         }
 2351:     }
 2352:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2353:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2354:            &mt('show').'</label>'."\n";
 2355: }
 2356: 
 2357: sub login_header_options  {
 2358:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2359:     my $output = '';
 2360:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2361:         $output .= &mt('Text default(s):').'<br />';
 2362:         if (!$is_custom->{'textcol'}) {
 2363:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2364:                        '&nbsp;&nbsp;&nbsp;';
 2365:         }
 2366:         if (!$is_custom->{'bgcol'}) {
 2367:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2368:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2369:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2370:         }
 2371:         $output .= '<br />';
 2372:     }
 2373:     $output .='<br />';
 2374:     return $output;
 2375: }
 2376: 
 2377: sub login_text_colors {
 2378:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2379:     my $color_menu = '<table border="0"><tr>';
 2380:     foreach my $item (@{$logintext}) {
 2381:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2382:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2383:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2384:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2385:     }
 2386:     $color_menu .= '</tr></table><br />';
 2387:     return $color_menu;
 2388: }
 2389: 
 2390: sub image_changes {
 2391:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2392:     my $output;
 2393:     if ($img eq 'login') {
 2394:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2395:     } elsif (!$is_custom) {
 2396:         if ($img ne 'domlogo') {
 2397:             $output = &mt('Default image:').'<br />';
 2398:         } else {
 2399:             $output = &mt('Default in use:').'<br />';
 2400:         }
 2401:     }
 2402:     if ($img ne 'login') {
 2403:         if ($img_import) {
 2404:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2405:         }
 2406:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2407:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2408:         if ($is_custom) {
 2409:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2410:                        '<input type="checkbox" name="'.
 2411:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2412:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2413:         } else {
 2414:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2415:         }
 2416:     }
 2417:     return $output;
 2418: }
 2419: 
 2420: sub print_quotas {
 2421:     my ($dom,$settings,$rowtotal,$action) = @_;
 2422:     my $context;
 2423:     if ($action eq 'quotas') {
 2424:         $context = 'tools';
 2425:     } else {
 2426:         $context = $action;
 2427:     }
 2428:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2429:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2430:     my $typecount = 0;
 2431:     my ($css_class,%titles);
 2432:     if ($context eq 'requestcourses') {
 2433:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2434:         @options =('norequest','approval','validate','autolimit');
 2435:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2436:         %titles = &courserequest_titles();
 2437:     } elsif ($context eq 'requestauthor') {
 2438:         @usertools = ('author');
 2439:         @options = ('norequest','approval','automatic');
 2440:         %titles = &authorrequest_titles();
 2441:     } else {
 2442:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
 2443:         %titles = &tool_titles();
 2444:     }
 2445:     if (ref($types) eq 'ARRAY') {
 2446:         foreach my $type (@{$types}) {
 2447:             my $currdefquota;
 2448:             unless (($context eq 'requestcourses') ||
 2449:                     ($context eq 'requestauthor')) {
 2450:                 if (ref($settings) eq 'HASH') {
 2451:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2452:                         $currdefquota = $settings->{defaultquota}->{$type};
 2453:                     } else {
 2454:                         $currdefquota = $settings->{$type};
 2455:                     }
 2456:                 }
 2457:             }
 2458:             if (defined($usertypes->{$type})) {
 2459:                 $typecount ++;
 2460:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2461:                 $datatable .= '<tr'.$css_class.'>'.
 2462:                               '<td>'.$usertypes->{$type}.'</td>'.
 2463:                               '<td class="LC_left_item">';
 2464:                 if ($context eq 'requestcourses') {
 2465:                     $datatable .= '<table><tr>';
 2466:                 }
 2467:                 my %cell;  
 2468:                 foreach my $item (@usertools) {
 2469:                     if ($context eq 'requestcourses') {
 2470:                         my ($curroption,$currlimit);
 2471:                         if (ref($settings) eq 'HASH') {
 2472:                             if (ref($settings->{$item}) eq 'HASH') {
 2473:                                 $curroption = $settings->{$item}->{$type};
 2474:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2475:                                     $currlimit = $1; 
 2476:                                 }
 2477:                             }
 2478:                         }
 2479:                         if (!$curroption) {
 2480:                             $curroption = 'norequest';
 2481:                         }
 2482:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2483:                         foreach my $option (@options) {
 2484:                             my $val = $option;
 2485:                             if ($option eq 'norequest') {
 2486:                                 $val = 0;  
 2487:                             }
 2488:                             if ($option eq 'validate') {
 2489:                                 my $canvalidate = 0;
 2490:                                 if (ref($validations{$item}) eq 'HASH') { 
 2491:                                     if ($validations{$item}{$type}) {
 2492:                                         $canvalidate = 1;
 2493:                                     }
 2494:                                 }
 2495:                                 next if (!$canvalidate);
 2496:                             }
 2497:                             my $checked = '';
 2498:                             if ($option eq $curroption) {
 2499:                                 $checked = ' checked="checked"';
 2500:                             } elsif ($option eq 'autolimit') {
 2501:                                 if ($curroption =~ /^autolimit/) {
 2502:                                     $checked = ' checked="checked"';
 2503:                                 }                       
 2504:                             } 
 2505:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2506:                                   '<input type="radio" name="crsreq_'.$item.
 2507:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2508:                                   $titles{$option}.'</label>';
 2509:                             if ($option eq 'autolimit') {
 2510:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2511:                                                 $item.'_limit_'.$type.'" size="1" '.
 2512:                                                 'value="'.$currlimit.'" />';
 2513:                             }
 2514:                             $cell{$item} .= '</span> ';
 2515:                             if ($option eq 'autolimit') {
 2516:                                 $cell{$item} .= $titles{'unlimited'};
 2517:                             }
 2518:                         }
 2519:                     } elsif ($context eq 'requestauthor') {
 2520:                         my $curroption;
 2521:                         if (ref($settings) eq 'HASH') {
 2522:                             $curroption = $settings->{$type};
 2523:                         }
 2524:                         if (!$curroption) {
 2525:                             $curroption = 'norequest';
 2526:                         }
 2527:                         foreach my $option (@options) {
 2528:                             my $val = $option;
 2529:                             if ($option eq 'norequest') {
 2530:                                 $val = 0;
 2531:                             }
 2532:                             my $checked = '';
 2533:                             if ($option eq $curroption) {
 2534:                                 $checked = ' checked="checked"';
 2535:                             }
 2536:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2537:                                   '<input type="radio" name="authorreq_'.$type.
 2538:                                   '" value="'.$val.'"'.$checked.' />'.
 2539:                                   $titles{$option}.'</label></span>&nbsp; ';
 2540:                         }
 2541:                     } else {
 2542:                         my $checked = 'checked="checked" ';
 2543:                         if ($item eq 'timezone') {
 2544:                             $checked = '';
 2545:                         }
 2546:                         if (ref($settings) eq 'HASH') {
 2547:                             if (ref($settings->{$item}) eq 'HASH') {
 2548:                                 if (!$settings->{$item}->{$type}) {
 2549:                                     $checked = '';
 2550:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2551:                                     $checked =  'checked="checked" ';
 2552:                                 }
 2553:                             }
 2554:                         }
 2555:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2556:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2557:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2558:                                       '</label></span>&nbsp; ';
 2559:                     }
 2560:                 }
 2561:                 if ($context eq 'requestcourses') {
 2562:                     $datatable .= '</tr><tr>';
 2563:                     foreach my $item (@usertools) {
 2564:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2565:                     }
 2566:                     $datatable .= '</tr></table>';
 2567:                 }
 2568:                 $datatable .= '</td>';
 2569:                 unless (($context eq 'requestcourses') ||
 2570:                         ($context eq 'requestauthor')) {
 2571:                     $datatable .= 
 2572:                               '<td class="LC_right_item">'.
 2573:                               '<span class="LC_nobreak">'.
 2574:                               '<input type="text" name="quota_'.$type.
 2575:                               '" value="'.$currdefquota.
 2576:                               '" size="5" /></span></td>';
 2577:                 }
 2578:                 $datatable .= '</tr>';
 2579:             }
 2580:         }
 2581:     }
 2582:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2583:         $defaultquota = '20';
 2584:         if (ref($settings) eq 'HASH') {
 2585:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2586:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2587:             } elsif (defined($settings->{'default'})) {
 2588:                 $defaultquota = $settings->{'default'};
 2589:             }
 2590:         }
 2591:     }
 2592:     $typecount ++;
 2593:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2594:     $datatable .= '<tr'.$css_class.'>'.
 2595:                   '<td>'.$othertitle.'</td>'.
 2596:                   '<td class="LC_left_item">';
 2597:     if ($context eq 'requestcourses') {
 2598:         $datatable .= '<table><tr>';
 2599:     }
 2600:     my %defcell;
 2601:     foreach my $item (@usertools) {
 2602:         if ($context eq 'requestcourses') {
 2603:             my ($curroption,$currlimit);
 2604:             if (ref($settings) eq 'HASH') {
 2605:                 if (ref($settings->{$item}) eq 'HASH') {
 2606:                     $curroption = $settings->{$item}->{'default'};
 2607:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2608:                         $currlimit = $1;
 2609:                     }
 2610:                 }
 2611:             }
 2612:             if (!$curroption) {
 2613:                 $curroption = 'norequest';
 2614:             }
 2615:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2616:             foreach my $option (@options) {
 2617:                 my $val = $option;
 2618:                 if ($option eq 'norequest') {
 2619:                     $val = 0;
 2620:                 }
 2621:                 if ($option eq 'validate') {
 2622:                     my $canvalidate = 0;
 2623:                     if (ref($validations{$item}) eq 'HASH') {
 2624:                         if ($validations{$item}{'default'}) {
 2625:                             $canvalidate = 1;
 2626:                         }
 2627:                     }
 2628:                     next if (!$canvalidate);
 2629:                 }
 2630:                 my $checked = '';
 2631:                 if ($option eq $curroption) {
 2632:                     $checked = ' checked="checked"';
 2633:                 } elsif ($option eq 'autolimit') {
 2634:                     if ($curroption =~ /^autolimit/) {
 2635:                         $checked = ' checked="checked"';
 2636:                     }
 2637:                 }
 2638:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2639:                                   '<input type="radio" name="crsreq_'.$item.
 2640:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2641:                                   $titles{$option}.'</label>';
 2642:                 if ($option eq 'autolimit') {
 2643:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2644:                                        $item.'_limit_default" size="1" '.
 2645:                                        'value="'.$currlimit.'" />';
 2646:                 }
 2647:                 $defcell{$item} .= '</span> ';
 2648:                 if ($option eq 'autolimit') {
 2649:                     $defcell{$item} .= $titles{'unlimited'};
 2650:                 }
 2651:             }
 2652:         } elsif ($context eq 'requestauthor') {
 2653:             my $curroption;
 2654:             if (ref($settings) eq 'HASH') {
 2655:                 $curroption = $settings->{'default'};
 2656:             }
 2657:             if (!$curroption) {
 2658:                 $curroption = 'norequest';
 2659:             }
 2660:             foreach my $option (@options) {
 2661:                 my $val = $option;
 2662:                 if ($option eq 'norequest') {
 2663:                     $val = 0;
 2664:                 }
 2665:                 my $checked = '';
 2666:                 if ($option eq $curroption) {
 2667:                     $checked = ' checked="checked"';
 2668:                 }
 2669:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2670:                               '<input type="radio" name="authorreq_default"'.
 2671:                               ' value="'.$val.'"'.$checked.' />'.
 2672:                               $titles{$option}.'</label></span>&nbsp; ';
 2673:             }
 2674:         } else {
 2675:             my $checked = 'checked="checked" ';
 2676:             if (ref($settings) eq 'HASH') {
 2677:                 if (ref($settings->{$item}) eq 'HASH') {
 2678:                     if ($settings->{$item}->{'default'} == 0) {
 2679:                         $checked = '';
 2680:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2681:                         $checked = 'checked="checked" ';
 2682:                     }
 2683:                 }
 2684:             }
 2685:             $datatable .= '<span class="LC_nobreak"><label>'.
 2686:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2687:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2688:                           '</label></span>&nbsp; ';
 2689:         }
 2690:     }
 2691:     if ($context eq 'requestcourses') {
 2692:         $datatable .= '</tr><tr>';
 2693:         foreach my $item (@usertools) {
 2694:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2695:         }
 2696:         $datatable .= '</tr></table>';
 2697:     }
 2698:     $datatable .= '</td>';
 2699:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2700:         $datatable .= '<td class="LC_right_item">'.
 2701:                       '<span class="LC_nobreak">'.
 2702:                       '<input type="text" name="defaultquota" value="'.
 2703:                       $defaultquota.'" size="5" /></span></td>';
 2704:     }
 2705:     $datatable .= '</tr>';
 2706:     $typecount ++;
 2707:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2708:     $datatable .= '<tr'.$css_class.'>'.
 2709:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2710:     if ($context eq 'requestcourses') {
 2711:         $datatable .= &mt('(overrides affiliation, if set)').
 2712:                       '</td>'.
 2713:                       '<td class="LC_left_item">'.
 2714:                       '<table><tr>';
 2715:     } else {
 2716:         $datatable .= &mt('(overrides affiliation, if checked)').
 2717:                       '</td>'.
 2718:                       '<td class="LC_left_item" colspan="2">'.
 2719:                       '<br />';
 2720:     }
 2721:     my %advcell;
 2722:     foreach my $item (@usertools) {
 2723:         if ($context eq 'requestcourses') {
 2724:             my ($curroption,$currlimit);
 2725:             if (ref($settings) eq 'HASH') {
 2726:                 if (ref($settings->{$item}) eq 'HASH') {
 2727:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2728:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2729:                         $currlimit = $1;
 2730:                     }
 2731:                 }
 2732:             }
 2733:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2734:             my $checked = '';
 2735:             if ($curroption eq '') {
 2736:                 $checked = ' checked="checked"';
 2737:             }
 2738:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2739:                                '<input type="radio" name="crsreq_'.$item.
 2740:                                '__LC_adv" value=""'.$checked.' />'.
 2741:                                &mt('No override set').'</label></span>&nbsp; ';
 2742:             foreach my $option (@options) {
 2743:                 my $val = $option;
 2744:                 if ($option eq 'norequest') {
 2745:                     $val = 0;
 2746:                 }
 2747:                 if ($option eq 'validate') {
 2748:                     my $canvalidate = 0;
 2749:                     if (ref($validations{$item}) eq 'HASH') {
 2750:                         if ($validations{$item}{'_LC_adv'}) {
 2751:                             $canvalidate = 1;
 2752:                         }
 2753:                     }
 2754:                     next if (!$canvalidate);
 2755:                 }
 2756:                 my $checked = '';
 2757:                 if ($val eq $curroption) {
 2758:                     $checked = ' checked="checked"';
 2759:                 } elsif ($option eq 'autolimit') {
 2760:                     if ($curroption =~ /^autolimit/) {
 2761:                         $checked = ' checked="checked"';
 2762:                     }
 2763:                 }
 2764:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2765:                                   '<input type="radio" name="crsreq_'.$item.
 2766:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2767:                                   $titles{$option}.'</label>';
 2768:                 if ($option eq 'autolimit') {
 2769:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2770:                                        $item.'_limit__LC_adv" size="1" '.
 2771:                                        'value="'.$currlimit.'" />';
 2772:                 }
 2773:                 $advcell{$item} .= '</span> ';
 2774:                 if ($option eq 'autolimit') {
 2775:                     $advcell{$item} .= $titles{'unlimited'};
 2776:                 }
 2777:             }
 2778:         } elsif ($context eq 'requestauthor') {
 2779:             my $curroption;
 2780:             if (ref($settings) eq 'HASH') {
 2781:                 $curroption = $settings->{'_LC_adv'};
 2782:             }
 2783:             my $checked = '';
 2784:             if ($curroption eq '') {
 2785:                 $checked = ' checked="checked"';
 2786:             }
 2787:             $datatable .= '<span class="LC_nobreak"><label>'.
 2788:                           '<input type="radio" name="authorreq__LC_adv"'.
 2789:                           ' value=""'.$checked.' />'.
 2790:                           &mt('No override set').'</label></span>&nbsp; ';
 2791:             foreach my $option (@options) {
 2792:                 my $val = $option;
 2793:                 if ($option eq 'norequest') {
 2794:                     $val = 0;
 2795:                 }
 2796:                 my $checked = '';
 2797:                 if ($val eq $curroption) {
 2798:                     $checked = ' checked="checked"';
 2799:                 }
 2800:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2801:                               '<input type="radio" name="authorreq__LC_adv"'.
 2802:                               ' value="'.$val.'"'.$checked.' />'.
 2803:                               $titles{$option}.'</label></span>&nbsp; ';
 2804:             }
 2805:         } else {
 2806:             my $checked = 'checked="checked" ';
 2807:             if (ref($settings) eq 'HASH') {
 2808:                 if (ref($settings->{$item}) eq 'HASH') {
 2809:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2810:                         $checked = '';
 2811:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2812:                         $checked = 'checked="checked" ';
 2813:                     }
 2814:                 }
 2815:             }
 2816:             $datatable .= '<span class="LC_nobreak"><label>'.
 2817:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2818:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2819:                           '</label></span>&nbsp; ';
 2820:         }
 2821:     }
 2822:     if ($context eq 'requestcourses') {
 2823:         $datatable .= '</tr><tr>';
 2824:         foreach my $item (@usertools) {
 2825:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2826:         }
 2827:         $datatable .= '</tr></table>';
 2828:     }
 2829:     $datatable .= '</td></tr>';
 2830:     $$rowtotal += $typecount;
 2831:     return $datatable;
 2832: }
 2833: 
 2834: sub print_requestmail {
 2835:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2836:     my ($now,$datatable,%currapp);
 2837:     $now = time;
 2838:     if (ref($settings) eq 'HASH') {
 2839:         if (ref($settings->{'notify'}) eq 'HASH') {
 2840:             if ($settings->{'notify'}{'approval'} ne '') {
 2841:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2842:             }
 2843:         }
 2844:     }
 2845:     my $numinrow = 2;
 2846:     my $css_class;
 2847:     if ($$rowtotal%2) {
 2848:         $css_class = 'LC_odd_row';
 2849:     }
 2850:     if ($customcss) {
 2851:         $css_class .= " $customcss";
 2852:     }
 2853:     $css_class =~ s/^\s+//;
 2854:     if ($css_class) {
 2855:         $css_class = ' class="'.$css_class.'"';
 2856:     }
 2857:     if ($rowstyle) {
 2858:         $css_class .= ' style="'.$rowstyle.'"';
 2859:     }
 2860:     my $text;
 2861:     if ($action eq 'requestcourses') {
 2862:         $text = &mt('Receive notification of course requests requiring approval');
 2863:     } elsif ($action eq 'requestauthor') {
 2864:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2865:     } else {
 2866:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2867:     }
 2868:     $datatable = '<tr'.$css_class.'>'.
 2869:                  ' <td>'.$text.'</td>'.
 2870:                  ' <td class="LC_left_item">';
 2871:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2872:                                                  $action.'notifyapproval',%currapp);
 2873:     if ($numdc > 0) {
 2874:         $datatable .= $table;
 2875:     } else {
 2876:         $datatable .= &mt('There are no active Domain Coordinators');
 2877:     }
 2878:     $datatable .='</td></tr>';
 2879:     return $datatable;
 2880: }
 2881: 
 2882: sub print_studentcode {
 2883:     my ($settings,$rowtotal) = @_;
 2884:     my $rownum = 0; 
 2885:     my ($output,%current);
 2886:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2887:     if (ref($settings) eq 'HASH') {
 2888:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2889:             foreach my $type (@crstypes) {
 2890:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2891:             }
 2892:         }
 2893:     }
 2894:     $output .= '<tr>'.
 2895:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2896:                '<td class="LC_left_item">';
 2897:     foreach my $type (@crstypes) {
 2898:         my $check = ' ';
 2899:         if ($current{$type}) {
 2900:             $check = ' checked="checked" ';
 2901:         }
 2902:         $output .= '<span class="LC_nobreak"><label>'.
 2903:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2904:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2905:     }
 2906:     $output .= '</td></tr>';
 2907:     $$rowtotal ++;
 2908:     return $output;
 2909: }
 2910: 
 2911: sub print_textbookcourses {
 2912:     my ($dom,$type,$settings,$rowtotal) = @_;
 2913:     my $rownum = 0;
 2914:     my $css_class;
 2915:     my $itemcount = 1;
 2916:     my $maxnum = 0;
 2917:     my $bookshash;
 2918:     if (ref($settings) eq 'HASH') {
 2919:         $bookshash = $settings->{$type};
 2920:     }
 2921:     my %ordered;
 2922:     if (ref($bookshash) eq 'HASH') {
 2923:         foreach my $item (keys(%{$bookshash})) {
 2924:             if (ref($bookshash->{$item}) eq 'HASH') {
 2925:                 my $num = $bookshash->{$item}{'order'};
 2926:                 $ordered{$num} = $item;
 2927:             }
 2928:         }
 2929:     }
 2930:     my $confname = $dom.'-domainconfig';
 2931:     my $switchserver = &check_switchserver($dom,$confname);
 2932:     my $maxnum = scalar(keys(%ordered));
 2933:     my $datatable;
 2934:     if (keys(%ordered)) {
 2935:         my @items = sort { $a <=> $b } keys(%ordered);
 2936:         for (my $i=0; $i<@items; $i++) {
 2937:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2938:             my $key = $ordered{$items[$i]};
 2939:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2940:             my $coursetitle = $coursehash{'description'};
 2941:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2942:             if (ref($bookshash->{$key}) eq 'HASH') {
 2943:                 $subject = $bookshash->{$key}->{'subject'};
 2944:                 $title = $bookshash->{$key}->{'title'};
 2945:                 if ($type eq 'textbooks') {
 2946:                     $publisher = $bookshash->{$key}->{'publisher'};
 2947:                     $author = $bookshash->{$key}->{'author'};
 2948:                     $image = $bookshash->{$key}->{'image'};
 2949:                     if ($image ne '') {
 2950:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2951:                         my $imagethumb = "$path/tn-".$imagefile;
 2952:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2953:                     }
 2954:                 }
 2955:             }
 2956:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2957:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2958:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2959:             for (my $k=0; $k<=$maxnum; $k++) {
 2960:                 my $vpos = $k+1;
 2961:                 my $selstr;
 2962:                 if ($k == $i) {
 2963:                     $selstr = ' selected="selected" ';
 2964:                 }
 2965:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2966:             }
 2967:             $datatable .= '</select>'.('&nbsp;'x2).
 2968:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2969:                 &mt('Delete?').'</label></span></td>'.
 2970:                 '<td colspan="2">'.
 2971:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2972:                 ('&nbsp;'x2).
 2973:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2974:             if ($type eq 'textbooks') {
 2975:                 $datatable .= ('&nbsp;'x2).
 2976:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2977:                               ('&nbsp;'x2).
 2978:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2979:                               ('&nbsp;'x2).
 2980:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2981:                 if ($image) {
 2982:                     $datatable .= $imgsrc.
 2983:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2984:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2985:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2986:                 }
 2987:                 if ($switchserver) {
 2988:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2989:                 } else {
 2990:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2991:                 }
 2992:             }
 2993:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2994:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2995:                           $coursetitle.'</span></td></tr>'."\n";
 2996:             $itemcount ++;
 2997:         }
 2998:     }
 2999:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3000:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 3001:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3002:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 3003:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 3004:     for (my $k=0; $k<$maxnum+1; $k++) {
 3005:         my $vpos = $k+1;
 3006:         my $selstr;
 3007:         if ($k == $maxnum) {
 3008:             $selstr = ' selected="selected" ';
 3009:         }
 3010:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3011:     }
 3012:     $datatable .= '</select>&nbsp;'."\n".
 3013:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3014:                   '<td colspan="2">'.
 3015:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3016:                   ('&nbsp;'x2).
 3017:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3018:                   ('&nbsp;'x2);
 3019:     if ($type eq 'textbooks') {
 3020:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3021:                       ('&nbsp;'x2).
 3022:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3023:                       ('&nbsp;'x2).
 3024:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3025:         if ($switchserver) {
 3026:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3027:         } else {
 3028:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3029:         }
 3030:         $datatable .= '</span>'."\n";
 3031:     }
 3032:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3033:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3034:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3035:                   &Apache::loncommon::selectcourse_link
 3036:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3037:                   '</span></td>'."\n".
 3038:                   '</tr>'."\n";
 3039:     $itemcount ++;
 3040:     return $datatable;
 3041: }
 3042: 
 3043: sub textbookcourses_javascript {
 3044:     my ($settings) = @_;
 3045:     return unless(ref($settings) eq 'HASH');
 3046:     my (%ordered,%total,%jstext);
 3047:     foreach my $type ('textbooks','templates') {
 3048:         $total{$type} = 0;
 3049:         if (ref($settings->{$type}) eq 'HASH') {
 3050:             foreach my $item (keys(%{$settings->{$type}})) {
 3051:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3052:                     my $num = $settings->{$type}->{$item}{'order'};
 3053:                     $ordered{$type}{$num} = $item;
 3054:                 }
 3055:             }
 3056:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3057:         }
 3058:         my @jsarray = ();
 3059:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3060:             push(@jsarray,$ordered{$type}{$item});
 3061:         }
 3062:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3063:     }
 3064:     return <<"ENDSCRIPT";
 3065: <script type="text/javascript">
 3066: // <![CDATA[
 3067: function reorderBooks(form,item,caller) {
 3068:     var changedVal;
 3069: $jstext{'textbooks'};
 3070: $jstext{'templates'};
 3071:     var newpos;
 3072:     var maxh;
 3073:     if (caller == 'textbooks') {  
 3074:         newpos = 'textbooks_addbook_pos';
 3075:         maxh = 1 + $total{'textbooks'};
 3076:     } else {
 3077:         newpos = 'templates_addbook_pos';
 3078:         maxh = 1 + $total{'templates'};
 3079:     }
 3080:     var current = new Array;
 3081:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3082:     if (item == newpos) {
 3083:         changedVal = newitemVal;
 3084:     } else {
 3085:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3086:         current[newitemVal] = newpos;
 3087:     }
 3088:     if (caller == 'textbooks') {
 3089:         for (var i=0; i<textbooks.length; i++) {
 3090:             var elementName = 'textbooks_'+textbooks[i];
 3091:             if (elementName != item) {
 3092:                 if (form.elements[elementName]) {
 3093:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3094:                     current[currVal] = elementName;
 3095:                 }
 3096:             }
 3097:         }
 3098:     }
 3099:     if (caller == 'templates') {
 3100:         for (var i=0; i<templates.length; i++) {
 3101:             var elementName = 'templates_'+templates[i];
 3102:             if (elementName != item) {
 3103:                 if (form.elements[elementName]) {
 3104:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3105:                     current[currVal] = elementName;
 3106:                 }
 3107:             }
 3108:         }
 3109:     }
 3110:     var oldVal;
 3111:     for (var j=0; j<maxh; j++) {
 3112:         if (current[j] == undefined) {
 3113:             oldVal = j;
 3114:         }
 3115:     }
 3116:     if (oldVal < changedVal) {
 3117:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3118:            var elementName = current[k];
 3119:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3120:         }
 3121:     } else {
 3122:         for (var k=changedVal; k<oldVal; k++) {
 3123:             var elementName = current[k];
 3124:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3125:         }
 3126:     }
 3127:     return;
 3128: }
 3129: 
 3130: // ]]>
 3131: </script>
 3132: 
 3133: ENDSCRIPT
 3134: }
 3135: 
 3136: sub ltitools_javascript {
 3137:     my ($settings) = @_;
 3138:     my $togglejs = &ltitools_toggle_js();
 3139:     unless (ref($settings) eq 'HASH') {
 3140:         return $togglejs;
 3141:     }
 3142:     my (%ordered,$total,%jstext);
 3143:     $total = 0;
 3144:     foreach my $item (keys(%{$settings})) {
 3145:         if (ref($settings->{$item}) eq 'HASH') {
 3146:             my $num = $settings->{$item}{'order'};
 3147:             $ordered{$num} = $item;
 3148:         }
 3149:     }
 3150:     $total = scalar(keys(%{$settings}));
 3151:     my @jsarray = ();
 3152:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3153:         push(@jsarray,$ordered{$item});
 3154:     }
 3155:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3156:     return <<"ENDSCRIPT";
 3157: <script type="text/javascript">
 3158: // <![CDATA[
 3159: function reorderLTITools(form,item) {
 3160:     var changedVal;
 3161: $jstext
 3162:     var newpos = 'ltitools_add_pos';
 3163:     var maxh = 1 + $total;
 3164:     var current = new Array;
 3165:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3166:     if (item == newpos) {
 3167:         changedVal = newitemVal;
 3168:     } else {
 3169:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3170:         current[newitemVal] = newpos;
 3171:     }
 3172:     for (var i=0; i<ltitools.length; i++) {
 3173:         var elementName = 'ltitools_'+ltitools[i];
 3174:         if (elementName != item) {
 3175:             if (form.elements[elementName]) {
 3176:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3177:                 current[currVal] = elementName;
 3178:             }
 3179:         }
 3180:     }
 3181:     var oldVal;
 3182:     for (var j=0; j<maxh; j++) {
 3183:         if (current[j] == undefined) {
 3184:             oldVal = j;
 3185:         }
 3186:     }
 3187:     if (oldVal < changedVal) {
 3188:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3189:            var elementName = current[k];
 3190:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3191:         }
 3192:     } else {
 3193:         for (var k=changedVal; k<oldVal; k++) {
 3194:             var elementName = current[k];
 3195:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3196:         }
 3197:     }
 3198:     return;
 3199: }
 3200: 
 3201: // ]]>
 3202: </script>
 3203: 
 3204: $togglejs
 3205: 
 3206: ENDSCRIPT
 3207: }
 3208: 
 3209: sub ltitools_toggle_js {
 3210:     return <<"ENDSCRIPT";
 3211: <script type="text/javascript">
 3212: // <![CDATA[
 3213: 
 3214: function toggleLTITools(form,setting,item) {
 3215:     var radioname = '';
 3216:     var divid = '';
 3217:     if ((setting == 'passback') || (setting == 'roster')) {
 3218:         radioname = 'ltitools_'+setting+'_'+item;
 3219:         divid = 'ltitools_'+setting+'time_'+item;
 3220:         var num = form.elements[radioname].length;
 3221:         if (num) {
 3222:             var setvis = '';
 3223:             for (var i=0; i<num; i++) {
 3224:                 if (form.elements[radioname][i].checked) {
 3225:                     if (form.elements[radioname][i].value == '1') {
 3226:                         if (document.getElementById(divid)) {
 3227:                             document.getElementById(divid).style.display = 'inline-block';
 3228:                         }
 3229:                         setvis = 1;
 3230:                     }
 3231:                     break;
 3232:                 }
 3233:             }
 3234:         }
 3235:         if (!setvis) {
 3236:             if (document.getElementById(divid)) {
 3237:                 document.getElementById(divid).style.display = 'none';
 3238:             }
 3239:         }
 3240:     }
 3241:     if (setting == 'user') {
 3242:         divid = 'ltitools_'+setting+'_div_'+item;
 3243:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3244:         if (document.getElementById(divid)) {
 3245:             if (document.getElementById(checkid)) {
 3246:                 if (document.getElementById(checkid).checked) {
 3247:                     document.getElementById(divid).style.display = 'inline-block';
 3248:                 } else {
 3249:                     document.getElementById(divid).style.display = 'none';
 3250:                 }
 3251:             }
 3252:         }
 3253:     }
 3254:     return;
 3255: }
 3256: // ]]>
 3257: </script>
 3258: 
 3259: ENDSCRIPT
 3260: }
 3261: 
 3262: sub wafproxy_javascript {
 3263:     my ($dom) = @_;
 3264:     return <<"ENDSCRIPT";
 3265: <script type="text/javascript">
 3266: // <![CDATA[
 3267: function updateWAF() {
 3268:     if (document.getElementById('wafproxy_remoteip')) {
 3269:         var wafremote = 0;
 3270:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3271:             wafremote = 1;
 3272:         }
 3273:         var fields = new Array('header','trust');
 3274:         for (var i=0; i<fields.length; i++) {
 3275:             if (document.getElementById('wafproxy_'+fields[i])) {
 3276:                 if (wafremote == 1) {
 3277:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3278:                 }
 3279:                 else {
 3280:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3281:                 }
 3282:             }
 3283:         }
 3284:         if (document.getElementById('wafproxyranges_$dom')) {
 3285:             if (wafremote == 1) {
 3286:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3287:             } else {
 3288:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3289:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3290:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3291:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3292:                         }
 3293:                     }
 3294:                 }
 3295:             }
 3296:         }
 3297:     }
 3298:     return;
 3299: }
 3300: 
 3301: function checkWAF() {
 3302:     if (document.getElementById('wafproxy_remoteip')) {
 3303:         var wafvpn = 0;
 3304:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3305:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3306:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3307:                     wafvpn = 1;
 3308:                 }
 3309:                 break;
 3310:             }
 3311:         }
 3312:         var vpn = new Array('vpnint','vpnext');
 3313:         for (var i=0; i<vpn.length; i++) {
 3314:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3315:                 if (wafvpn == 1) {
 3316:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3317:                 }
 3318:                 else {
 3319:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3320:                 }
 3321:             }
 3322:         }
 3323:         if (document.getElementById('wafproxyranges_$dom')) {
 3324:             if (wafvpn == 1) {
 3325:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3326:             }
 3327:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3328:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3329:             }
 3330:         }
 3331:     }
 3332:     return;
 3333: }
 3334: 
 3335: function toggleWAF() {
 3336:     if (document.getElementById('wafproxy_table')) {
 3337:         var wafproxy = 0;
 3338:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3339:              if (document.display.wafproxy_${dom}[i].checked) {
 3340:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3341:                      wafproxy = 1;
 3342:                      break;
 3343:                 }
 3344:             }
 3345:         }
 3346:         if (wafproxy == 1) {
 3347:             document.getElementById('wafproxy_table').style.display='inline';
 3348:         }
 3349:         else {
 3350:            document.getElementById('wafproxy_table').style.display='none';
 3351:         }
 3352:         if (document.getElementById('wafproxyrow_${dom}')) {
 3353:             if (wafproxy == 1) {
 3354:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3355:             }
 3356:             else {
 3357:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3358:             }
 3359:         }
 3360:         if (document.getElementById('nowafproxyrow_$dom')) {
 3361:             if (wafproxy == 1) {
 3362:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3363:             }
 3364:             else {
 3365:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3366:             }
 3367:         }
 3368:     }
 3369:     return;
 3370: }
 3371: // ]]>
 3372: </script>
 3373: 
 3374: ENDSCRIPT
 3375: }
 3376: 
 3377: sub proctoring_javascript {
 3378:     my ($settings) = @_;
 3379:     my (%ordered,$total,%jstext);
 3380:     $total = 0;
 3381:     if (ref($settings) eq 'HASH') {
 3382:         foreach my $item (keys(%{$settings})) {
 3383:             if (ref($settings->{$item}) eq 'HASH') {
 3384:                 my $num = $settings->{$item}{'order'};
 3385:                 $ordered{$num} = $item;
 3386:             }
 3387:         }
 3388:         $total = scalar(keys(%{$settings}));
 3389:     } else {
 3390:         %ordered = (
 3391:                        0 => 'proctorio',
 3392:                        1 => 'examity',
 3393:                    );
 3394:         $total = 2; 
 3395:     }
 3396:     my @jsarray = ();
 3397:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3398:         push(@jsarray,$ordered{$item});
 3399:     }
 3400:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3401:     return <<"ENDSCRIPT";
 3402: <script type="text/javascript">
 3403: // <![CDATA[
 3404: function reorderProctoring(form,item) {
 3405:     var changedVal;
 3406: $jstext
 3407:     var maxh = $total;
 3408:     var current = new Array;
 3409:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3410:     for (var i=0; i<proctors.length; i++) {
 3411:         var elementName = 'proctoring_pos_'+proctors[i];
 3412:         if (elementName != item) {
 3413:             if (form.elements[elementName]) {
 3414:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3415:                 current[currVal] = elementName;
 3416:             }
 3417:         }
 3418:     }
 3419:     var oldVal;
 3420:     for (var j=0; j<maxh; j++) {
 3421:         if (current[j] == undefined) {
 3422:             oldVal = j;
 3423:         }
 3424:     }
 3425:     if (oldVal < changedVal) {
 3426:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3427:            var elementName = current[k];
 3428:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3429:         }
 3430:     } else {
 3431:         for (var k=changedVal; k<oldVal; k++) {
 3432:             var elementName = current[k];
 3433:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3434:         }
 3435:     }
 3436:     return;
 3437: }
 3438: 
 3439: function toggleProctoring(form,item) {
 3440:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3441:     if (fieldsets.length) {
 3442:         var radioname = 'proctoring_available_'+item;
 3443:         var num = form.elements[radioname].length;
 3444:         if (num) {
 3445:             var setvis = '';
 3446:             for (var i=0; i<num; i++) {
 3447:                 if (form.elements[radioname][i].checked) {
 3448:                     if (form.elements[radioname][i].value == '1') {
 3449:                        setvis = 1;
 3450:                        break;
 3451:                     }
 3452:                 }
 3453:             }
 3454:             for (var j=0; j<fieldsets.length; j++) {
 3455:                 if (setvis) {
 3456:                     fieldsets[j].style.display = 'block';
 3457:                 } else {
 3458:                     fieldsets[j].style.display = 'none';
 3459:                 }
 3460:             }
 3461:         }
 3462:     }
 3463:     return;
 3464: }
 3465: 
 3466: // ]]>
 3467: </script>
 3468: 
 3469: ENDSCRIPT
 3470: }
 3471: 
 3472: 
 3473: sub lti_javascript {
 3474:     my ($dom,$settings) = @_;
 3475:     my $togglejs = &lti_toggle_js($dom);
 3476:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3477:     unless (ref($settings) eq 'HASH') {
 3478:         return $togglejs.'
 3479: <script type="text/javascript">
 3480: // <![CDATA[
 3481: 
 3482: '.$linkprot_js.'
 3483: 
 3484: // ]]>
 3485: </script>
 3486: ';
 3487:     }
 3488:     my (%ordered,$total,%jstext);
 3489:     $total = scalar(keys(%{$settings}));
 3490:     foreach my $item (keys(%{$settings})) {
 3491:         if (ref($settings->{$item}) eq 'HASH') {
 3492:             my $num = $settings->{$item}{'order'};
 3493:             if ($num eq '') {
 3494:                 $num = $total - 1;
 3495:             }
 3496:             $ordered{$num} = $item;
 3497:         }
 3498:     }
 3499:     my @jsarray = ();
 3500:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3501:         push(@jsarray,$ordered{$item});
 3502:     }
 3503:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3504:     return <<"ENDSCRIPT";
 3505: <script type="text/javascript">
 3506: // <![CDATA[
 3507: function reorderLTI(form,item) {
 3508:     var changedVal;
 3509: $jstext
 3510:     var newpos = 'lti_pos_add';
 3511:     var maxh = 1 + $total;
 3512:     var current = new Array;
 3513:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3514:     if (item == newpos) {
 3515:         changedVal = newitemVal;
 3516:     } else {
 3517:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3518:         current[newitemVal] = newpos;
 3519:     }
 3520:     for (var i=0; i<lti.length; i++) {
 3521:         var elementName = 'lti_pos_'+lti[i];
 3522:         if (elementName != item) {
 3523:             if (form.elements[elementName]) {
 3524:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3525:                 current[currVal] = elementName;
 3526:             }
 3527:         }
 3528:     }
 3529:     var oldVal;
 3530:     for (var j=0; j<maxh; j++) {
 3531:         if (current[j] == undefined) {
 3532:             oldVal = j;
 3533:         }
 3534:     }
 3535:     if (oldVal < changedVal) {
 3536:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3537:            var elementName = current[k];
 3538:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3539:         }
 3540:     } else {
 3541:         for (var k=changedVal; k<oldVal; k++) {
 3542:             var elementName = current[k];
 3543:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3544:         }
 3545:     }
 3546:     return;
 3547: }
 3548: 
 3549: $linkprot_js
 3550: 
 3551: // ]]>
 3552: </script>
 3553: 
 3554: $togglejs
 3555: 
 3556: ENDSCRIPT
 3557: }
 3558: 
 3559: sub lti_toggle_js {
 3560:     my ($dom) = @_;
 3561:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3562:                             localauth => 'Local auth argument',
 3563:                             krb       => 'Kerberos domain',
 3564:                          );
 3565:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3566:     &js_escape(\$crsincalert);
 3567:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3568:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3569:     my $course_servers = "'".join("','",keys(%servers))."'";
 3570:     return <<"ENDSCRIPT";
 3571: <script type="text/javascript">
 3572: // <![CDATA[
 3573: 
 3574: function toggleLTI(form,setting,item) {
 3575:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3576:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3577:         var setvis = '';
 3578:         var radioname = 'lti_requser_'+item;
 3579:         var num = form.elements[radioname].length;
 3580:         if (num) {
 3581:             for (var i=0; i<num; i++) {
 3582:                 if (form.elements[radioname][i].checked) {
 3583:                     if (form.elements[radioname][i].value == '1') {
 3584:                         setvis = 1;
 3585:                         break;
 3586:                     }
 3587:                 }
 3588:             }
 3589:         }
 3590:         if (usrfieldsets.length) {
 3591:             for (var j=0; j<usrfieldsets.length; j++) {
 3592:                 if (setvis) {
 3593:                     usrfieldsets[j].style.display = 'block';
 3594:                 } else {
 3595:                     usrfieldsets[j].style.display = 'none';
 3596:                 }
 3597:             }
 3598:         }
 3599:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3600:         if (crsfieldsets.length) {
 3601:             radioname = 'lti_crsinc_'+item;
 3602:             var num = form.elements[radioname].length;
 3603:             if (num) {
 3604:                 var crsvis = '';
 3605:                 for (var i=0; i<num; i++) {
 3606:                     if (form.elements[radioname][i].checked) {
 3607:                         if (form.elements[radioname][i].value == '1') {
 3608:                             if (setvis == '') {
 3609:                                 if (setting == 'crsinc'){
 3610:                                     alert("$crsincalert");
 3611:                                     form.elements[radioname][0].checked = true;
 3612:                                 }
 3613:                             } else {
 3614:                                 crsvis = 1;
 3615:                             }
 3616:                             break;
 3617:                         }
 3618:                     }
 3619:                 }
 3620:                 setvis = crsvis;
 3621:             }
 3622:             for (var j=0; j<crsfieldsets.length; j++) {
 3623:                 if (setvis) {
 3624:                     crsfieldsets[j].style.display = 'block';
 3625:                 } else {
 3626:                     crsfieldsets[j].style.display = 'none';
 3627:                 }
 3628:             }
 3629:         }
 3630:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3631:         var radioname = '';
 3632:         var divid = '';
 3633:         if (setting == 'user') {
 3634:             radioname = 'lti_mapuser_'+item;
 3635:             divid = 'lti_userfield_'+item;
 3636:         } else if (setting == 'crs') {
 3637:             radioname = 'lti_mapcrs_'+item;
 3638:             divid = 'lti_crsfield_'+item;
 3639:         } else if (setting == 'callback') {
 3640:             radioname = 'lti_callback_'+item;
 3641:             divid = 'lti_callbackfield_'+item;
 3642:         } else {
 3643:             radioname = 'lti_passback_'+item;
 3644:             divid =  'lti_passback_'+item;
 3645:         }
 3646:         var num = form.elements[radioname].length;
 3647:         if (num) {
 3648:             var setvis = '';
 3649:             for (var i=0; i<num; i++) {
 3650:                if (form.elements[radioname][i].checked) {
 3651:                    if ((setting == 'passback') || (setting == 'callback')) {
 3652:                        if (form.elements[radioname][i].value == '1') {
 3653:                            if (document.getElementById(divid)) {
 3654:                                document.getElementById(divid).style.display = 'inline-block';
 3655:                            }
 3656:                            setvis = 1;
 3657:                            break;
 3658:                        }
 3659:                    } else {
 3660:                        if (form.elements[radioname][i].value == 'other') {
 3661:                            if (document.getElementById(divid)) {
 3662:                                document.getElementById(divid).style.display = 'inline-block';
 3663:                            }
 3664:                            setvis = 1;
 3665:                            break;
 3666:                        }
 3667:                    }
 3668:                }
 3669:             }
 3670:             if (!setvis) {
 3671:                 if (document.getElementById(divid)) {
 3672:                     document.getElementById(divid).style.display = 'none';
 3673:                 }
 3674:             }
 3675:         }
 3676:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3677:         var numsec = form.elements['lti_crssec_'+item].length;
 3678:         if (numsec) {
 3679:             var setvis = '';
 3680:             for (var i=0; i<numsec; i++) {
 3681:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3682:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3683:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3684:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3685:                             setvis = 1;
 3686:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3687:                             if (numsrcsec) {
 3688:                                 var setsrcvis = '';
 3689:                                 for (var j=0; j<numsrcsec; j++) {
 3690:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3691:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3692:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3693:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3694:                                                 setsrcvis = 1;
 3695:                                             }
 3696:                                         }
 3697:                                     }
 3698:                                 }
 3699:                                 if (!setsrcvis) {
 3700:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3701:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3702:                                     }
 3703:                                 }
 3704:                             }
 3705:                         }
 3706:                     }
 3707:                 }
 3708:             }
 3709:             if (!setvis) {
 3710:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3711:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3712:                 }
 3713:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3714:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3715:                 }
 3716:             }
 3717:         }
 3718:     } else if (setting == 'lcauth') {
 3719:         var numauth = form.elements['lti_lcauth_'+item].length;
 3720:         if (numauth) {
 3721:             for (var i=0; i<numauth; i++) {
 3722:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3723:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3724:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3725:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3726:                         } else {
 3727:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3728:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3729:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3730:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3731:                                 } else {
 3732:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3733:                                 }
 3734:                             }
 3735:                         }
 3736:                     }
 3737:                 }
 3738:             }
 3739:         }
 3740:     } else if (setting == 'lcmenu') {
 3741:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3742:         var divid = 'lti_menufield_'+item;
 3743:         var setvis = '';
 3744:         for (var i=0; i<menus.length; i++) {
 3745:             var radioname = menus[i];
 3746:             var num = form.elements[radioname].length;
 3747:             if (num) {
 3748:                 for (var j=0; j<num; j++) {
 3749:                     if (form.elements[radioname][j].checked) {
 3750:                         if (form.elements[radioname][j].value == '1') {
 3751:                             if (document.getElementById(divid)) {
 3752:                                 document.getElementById(divid).style.display = 'inline-block';
 3753:                             }
 3754:                             setvis = 1;
 3755:                             break;
 3756:                         }
 3757:                     }
 3758:                 }
 3759:             }
 3760:             if (setvis == 1) {
 3761:                 break;
 3762:             }
 3763:         }
 3764:         if (!setvis) {
 3765:             if (document.getElementById(divid)) {
 3766:                 document.getElementById(divid).style.display = 'none';
 3767:             }
 3768:         }
 3769:     }
 3770:     return;
 3771: }
 3772: 
 3773: // ]]>
 3774: </script>
 3775: 
 3776: ENDSCRIPT
 3777: }
 3778: 
 3779: sub autoupdate_javascript {
 3780:     return <<"ENDSCRIPT";
 3781: <script type="text/javascript">
 3782: // <![CDATA[
 3783: function toggleLastActiveDays(form) {
 3784:     var radioname = 'lastactive';
 3785:     var divid = 'lastactive_div';
 3786:     var num = form.elements[radioname].length;
 3787:     if (num) {
 3788:         var setvis = '';
 3789:         for (var i=0; i<num; i++) {
 3790:             if (form.elements[radioname][i].checked) {
 3791:                 if (form.elements[radioname][i].value == '1') {
 3792:                     if (document.getElementById(divid)) {
 3793:                         document.getElementById(divid).style.display = 'inline-block';
 3794:                     }
 3795:                     setvis = 1;
 3796:                 }
 3797:                 break;
 3798:             }
 3799:         }
 3800:         if (!setvis) {
 3801:             if (document.getElementById(divid)) {
 3802:                 document.getElementById(divid).style.display = 'none';
 3803:             }
 3804:         }
 3805:     }
 3806:     return;
 3807: }
 3808: // ]]>
 3809: </script>
 3810: 
 3811: ENDSCRIPT
 3812: }
 3813: 
 3814: sub autoenroll_javascript {
 3815:     return <<"ENDSCRIPT";
 3816: <script type="text/javascript">
 3817: // <![CDATA[
 3818: function toggleFailsafe(form) {
 3819:     var radioname = 'autoenroll_failsafe';
 3820:     var divid = 'autoenroll_failsafe_div';
 3821:     var num = form.elements[radioname].length;
 3822:     if (num) {
 3823:         var setvis = '';
 3824:         for (var i=0; i<num; i++) {
 3825:             if (form.elements[radioname][i].checked) {
 3826:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3827:                     if (document.getElementById(divid)) {
 3828:                         document.getElementById(divid).style.display = 'inline-block';
 3829:                     }
 3830:                     setvis = 1;
 3831:                 }
 3832:                 break;
 3833:             }
 3834:         }
 3835:         if (!setvis) {
 3836:             if (document.getElementById(divid)) {
 3837:                 document.getElementById(divid).style.display = 'none';
 3838:             }
 3839:         }
 3840:     }
 3841:     return;
 3842: }
 3843: // ]]>
 3844: </script>
 3845: 
 3846: ENDSCRIPT
 3847: }
 3848: 
 3849: sub saml_javascript {
 3850:     return <<"ENDSCRIPT";
 3851: <script type="text/javascript">
 3852: // <![CDATA[
 3853: function toggleSamlOptions(form,hostid) {
 3854:     var radioname = 'saml_'+hostid;
 3855:     var tablecellon = 'samloptionson_'+hostid;
 3856:     var tablecelloff = 'samloptionsoff_'+hostid;
 3857:     var num = form.elements[radioname].length;
 3858:     if (num) {
 3859:         var setvis = '';
 3860:         for (var i=0; i<num; i++) {
 3861:             if (form.elements[radioname][i].checked) {
 3862:                 if (form.elements[radioname][i].value == '1') {
 3863:                     if (document.getElementById(tablecellon)) {
 3864:                         document.getElementById(tablecellon).style.display='';
 3865:                     }
 3866:                     if (document.getElementById(tablecelloff)) {
 3867:                         document.getElementById(tablecelloff).style.display='none';
 3868:                     }
 3869:                     setvis = 1;
 3870:                 }
 3871:                 break;
 3872:             }
 3873:         }
 3874:         if (!setvis) {
 3875:             if (document.getElementById(tablecellon)) {
 3876:                 document.getElementById(tablecellon).style.display='none';
 3877:             }
 3878:             if (document.getElementById(tablecelloff)) {
 3879:                 document.getElementById(tablecelloff).style.display='';
 3880:             }
 3881:         }
 3882:     }
 3883:     return;
 3884: }
 3885: // ]]>
 3886: </script>
 3887: 
 3888: ENDSCRIPT
 3889: }
 3890: 
 3891: sub ipaccess_javascript {
 3892:     my ($settings) = @_;
 3893:     my (%ordered,$total,%jstext);
 3894:     $total = 0;
 3895:     if (ref($settings) eq 'HASH') {
 3896:         foreach my $item (keys(%{$settings})) {
 3897:             if (ref($settings->{$item}) eq 'HASH') {
 3898:                 my $num = $settings->{$item}{'order'};
 3899:                 $ordered{$num} = $item;
 3900:             }
 3901:         }
 3902:         $total = scalar(keys(%{$settings}));
 3903:     }
 3904:     my @jsarray = ();
 3905:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3906:         push(@jsarray,$ordered{$item});
 3907:     }
 3908:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3909:     return <<"ENDSCRIPT";
 3910: <script type="text/javascript">
 3911: // <![CDATA[
 3912: function reorderIPaccess(form,item) {
 3913:     var changedVal;
 3914: $jstext
 3915:     var newpos = 'ipaccess_pos_add';
 3916:     var maxh = 1 + $total;
 3917:     var current = new Array;
 3918:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3919:     if (item == newpos) {
 3920:         changedVal = newitemVal;
 3921:     } else {
 3922:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3923:         current[newitemVal] = newpos;
 3924:     }
 3925:     for (var i=0; i<ipaccess.length; i++) {
 3926:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3927:         if (elementName != item) {
 3928:             if (form.elements[elementName]) {
 3929:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3930:                 current[currVal] = elementName;
 3931:             }
 3932:         }
 3933:     }
 3934:     var oldVal;
 3935:     for (var j=0; j<maxh; j++) {
 3936:         if (current[j] == undefined) {
 3937:             oldVal = j;
 3938:         }
 3939:     }
 3940:     if (oldVal < changedVal) {
 3941:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3942:            var elementName = current[k];
 3943:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3944:         }
 3945:     } else {
 3946:         for (var k=changedVal; k<oldVal; k++) {
 3947:             var elementName = current[k];
 3948:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3949:         }
 3950:     }
 3951:     return;
 3952: }
 3953: // ]]>
 3954: </script>
 3955: 
 3956: ENDSCRIPT
 3957: }
 3958: 
 3959: sub authordefaults_javascript {
 3960:     my %alert = &Apache::lonlocal::texthash (
 3961:                     reqd => 'Warning: at least one editor needs to be available.',
 3962:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3963:     );
 3964:     &js_escape(\%alert);
 3965:     return <<"ENDSCRIPT";
 3966: <script type="text/javascript">
 3967: // <![CDATA[
 3968: 
 3969: function checkEditors(form,checkbox,current) {
 3970:     if (form.elements[checkbox].length != undefined) {
 3971:         var count = 0;
 3972:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3973:             if (form.elements[checkbox][i].checked) {
 3974:                 count ++;
 3975:             }
 3976:         }
 3977:         if (count == 0) {
 3978:             if (current.type =='radio') {
 3979:                 current.checked = true;
 3980:                 alert('$alert{reqd}\\n$alert{rest}');
 3981:             }
 3982:         }
 3983:     }
 3984:     return;
 3985: }
 3986: // ]]>
 3987: </script>
 3988: 
 3989: ENDSCRIPT
 3990: }
 3991: 
 3992: sub print_autoenroll {
 3993:     my ($dom,$settings,$rowtotal) = @_;
 3994:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3995:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3996:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3997:     $failsafesty = 'none';
 3998:     %failsafechecked = (
 3999:         off => ' checked="checked"',
 4000:     );
 4001:     if (ref($settings) eq 'HASH') {
 4002:         if (exists($settings->{'run'})) {
 4003:             if ($settings->{'run'} eq '0') {
 4004:                 $runoff = ' checked="checked" ';
 4005:                 $runon = ' ';
 4006:             } else {
 4007:                 $runon = ' checked="checked" ';
 4008:                 $runoff = ' ';
 4009:             }
 4010:         } else {
 4011:             if ($autorun) {
 4012:                 $runon = ' checked="checked" ';
 4013:                 $runoff = ' ';
 4014:             } else {
 4015:                 $runoff = ' checked="checked" ';
 4016:                 $runon = ' ';
 4017:             }
 4018:         }
 4019:         if (exists($settings->{'co-owners'})) {
 4020:             if ($settings->{'co-owners'} eq '0') {
 4021:                 $coownersoff = ' checked="checked" ';
 4022:                 $coownerson = ' ';
 4023:             } else {
 4024:                 $coownerson = ' checked="checked" ';
 4025:                 $coownersoff = ' ';
 4026:             }
 4027:         } else {
 4028:             $coownersoff = ' checked="checked" ';
 4029:             $coownerson = ' ';
 4030:         }
 4031:         if (exists($settings->{'sender_domain'})) {
 4032:             $defdom = $settings->{'sender_domain'};
 4033:         }
 4034:         if (exists($settings->{'failsafe'})) {
 4035:             $failsafe = $settings->{'failsafe'};
 4036:             if ($failsafe eq 'zero') {
 4037:                 $failsafechecked{'zero'} = ' checked="checked"';
 4038:                 $failsafechecked{'off'} = '';
 4039:                 $failsafesty = 'inline-block';
 4040:             } elsif ($failsafe eq 'any') {
 4041:                 $failsafechecked{'any'} = ' checked="checked"';
 4042:                 $failsafechecked{'off'} = '';
 4043:             }
 4044:             $autofailsafe = $settings->{'autofailsafe'};
 4045:         } elsif (exists($settings->{'autofailsafe'})) {
 4046:             $autofailsafe = $settings->{'autofailsafe'};
 4047:             if ($autofailsafe ne '') {
 4048:                 $failsafechecked{'zero'} = ' checked="checked"';
 4049:                 $failsafe = 'zero';
 4050:                 $failsafechecked{'off'} = '';
 4051:             }
 4052:         }
 4053:     } else {
 4054:         if ($autorun) {
 4055:             $runon = ' checked="checked" ';
 4056:             $runoff = ' ';
 4057:         } else {
 4058:             $runoff = ' checked="checked" ';
 4059:             $runon = ' ';
 4060:         }
 4061:     }
 4062:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4063:     my $notif_sender;
 4064:     if (ref($settings) eq 'HASH') {
 4065:         $notif_sender = $settings->{'sender_uname'};
 4066:     }
 4067:     my $datatable='<tr class="LC_odd_row">'.
 4068:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4069:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4070:                   '<input type="radio" name="autoenroll_run"'.
 4071:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4072:                   '<label><input type="radio" name="autoenroll_run"'.
 4073:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4074:                   '</tr><tr>'.
 4075:                   '<td>'.&mt('Notification messages - sender').
 4076:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4077:                   &mt('username').':&nbsp;'.
 4078:                   '<input type="text" name="sender_uname" value="'.
 4079:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4080:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4081:                   '<tr class="LC_odd_row">'.
 4082:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4083:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4084:                   '<input type="radio" name="autoassign_coowners"'.
 4085:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4086:                   '<label><input type="radio" name="autoassign_coowners"'.
 4087:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4088:                   '</tr><tr>'.
 4089:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4090:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4091:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="off" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'off'}.' />'.&mt('Not in use').'</label></span>&nbsp;&nbsp;&nbsp; '.
 4092:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="zero" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'zero'}.' />'.&mt('Retrieved section enrollment is zero').'</label></span><br />'.
 4093:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="any" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'any'}.' />'.&mt('Retrieved section enrollment is zero or greater').'</label></span>'.
 4094:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4095:                   '<span class="LC_nobreak">'.
 4096:                   &mt('Threshold for number of students in section to drop: [_1]',
 4097:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4098:                   '</span></div></td></tr>';
 4099:     $$rowtotal += 4;
 4100:     return $datatable;
 4101: }
 4102: 
 4103: sub print_autoupdate {
 4104:     my ($position,$dom,$settings,$rowtotal) = @_;
 4105:     my ($enable,$datatable);
 4106:     if ($position eq 'top') {
 4107:         my %choices = &Apache::lonlocal::texthash (
 4108:                           run        => 'Auto-update active?',
 4109:                           classlists => 'Update information in classlists?',
 4110:                           unexpired  => 'Skip updates for users without active or future roles?',
 4111:                           lastactive => 'Skip updates for inactive users?',
 4112:         );
 4113:         my $itemcount = 0;
 4114:         my $updateon = ' ';
 4115:         my $updateoff = ' checked="checked" ';
 4116:         if (ref($settings) eq 'HASH') {
 4117:             if ($settings->{'run'} eq '1') {
 4118:                 $updateon = $updateoff;
 4119:                 $updateoff = ' ';
 4120:             }
 4121:         }
 4122:         $enable = '<tr class="LC_odd_row">'.
 4123:                   '<td>'.$choices{'run'}.'</td>'.
 4124:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4125:                   '<input type="radio" name="autoupdate_run"'.
 4126:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4127:                   '<label><input type="radio" name="autoupdate_run"'.
 4128:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4129:                   '</tr>';
 4130:         my @toggles = ('classlists','unexpired');
 4131:         my %defaultchecked = ('classlists' => 'off',
 4132:                               'unexpired'  => 'off'
 4133:                               );
 4134:         $$rowtotal ++;
 4135:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4136:                                                      \%choices,$itemcount,'','','left','no');
 4137:         $datatable = $enable.$datatable;
 4138:         $$rowtotal += $itemcount;
 4139:         my $lastactiveon = ' ';
 4140:         my $lastactiveoff = ' checked="checked" ';
 4141:         my $lastactivestyle = 'none';
 4142:         my $lastactivedays;
 4143:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4144:         if (ref($settings) eq 'HASH') {
 4145:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4146:                 $lastactiveon = $lastactiveoff;
 4147:                 $lastactiveoff = ' ';
 4148:                 $lastactivestyle = 'inline-block';
 4149:                 $lastactivedays = $settings->{'lastactive'};
 4150:             }
 4151:         }
 4152:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4153:         $datatable .= '<tr'.$css_class.'>'.
 4154:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4155:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4156:                       '<input type="radio" name="lastactive"'.
 4157:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4158:                       '&nbsp;<label>'.
 4159:                       '<input type="radio" name="lastactive"'.
 4160:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4161:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4162:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4163:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4164:                           $lastactivedays.'" />').
 4165:                       '</span></td>'.
 4166:                       '</tr>';
 4167:         $$rowtotal ++;
 4168:     } elsif ($position eq 'middle') {
 4169:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4170:         my $numinrow = 3;
 4171:         my $locknamesettings;
 4172:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4173:                                      $dom,$numinrow,$othertitle,
 4174:                                     'lockablenames',$rowtotal);
 4175:         $$rowtotal ++;
 4176:     } else {
 4177:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4178:         my @fields = ('lastname','firstname','middlename','generation',
 4179:                       'permanentemail','id');
 4180:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4181:         my $numrows = 0;
 4182:         if (ref($types) eq 'ARRAY') {
 4183:             if (@{$types} > 0) {
 4184:                 $datatable = 
 4185:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4186:                                          \@fields,$types,\$numrows);
 4187:                     $$rowtotal += @{$types}; 
 4188:             }
 4189:         }
 4190:         $datatable .= 
 4191:             &usertype_update_row($settings,{'default' => $othertitle},
 4192:                                  \%fieldtitles,\@fields,['default'],
 4193:                                  \$numrows);
 4194:         $$rowtotal ++;     
 4195:     }
 4196:     return $datatable;
 4197: }
 4198: 
 4199: sub print_autocreate {
 4200:     my ($dom,$settings,$rowtotal) = @_;
 4201:     my (%createon,%createoff,%currhash);
 4202:     my @types = ('xml','req');
 4203:     if (ref($settings) eq 'HASH') {
 4204:         foreach my $item (@types) {
 4205:             $createoff{$item} = ' checked="checked" ';
 4206:             $createon{$item} = ' ';
 4207:             if (exists($settings->{$item})) {
 4208:                 if ($settings->{$item}) {
 4209:                     $createon{$item} = ' checked="checked" ';
 4210:                     $createoff{$item} = ' ';
 4211:                 }
 4212:             }
 4213:         }
 4214:         if ($settings->{'xmldc'} ne '') {
 4215:             $currhash{$settings->{'xmldc'}} = 1;
 4216:         }
 4217:     } else {
 4218:         foreach my $item (@types) {
 4219:             $createoff{$item} = ' checked="checked" ';
 4220:             $createon{$item} = ' ';
 4221:         }
 4222:     }
 4223:     $$rowtotal += 2;
 4224:     my $numinrow = 2;
 4225:     my $datatable='<tr class="LC_odd_row">'.
 4226:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4227:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4228:                   '<input type="radio" name="autocreate_xml"'.
 4229:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4230:                   '<label><input type="radio" name="autocreate_xml"'.
 4231:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4232:                   '</td></tr><tr>'.
 4233:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4234:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4235:                   '<input type="radio" name="autocreate_req"'.
 4236:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4237:                   '<label><input type="radio" name="autocreate_req"'.
 4238:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4239:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4240:                                                    'autocreate_xmldc',%currhash);
 4241:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4242:     if ($numdc > 1) {
 4243:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4244:                       '</td><td class="LC_left_item">';
 4245:     } else {
 4246:         $datatable .= &mt('Course creation processed as:').
 4247:                       '</td><td class="LC_right_item">';
 4248:     }
 4249:     $datatable .= $dctable.'</td></tr>';
 4250:     $$rowtotal += $rows;
 4251:     return $datatable;
 4252: }
 4253: 
 4254: sub print_directorysrch {
 4255:     my ($position,$dom,$settings,$rowtotal) = @_;
 4256:     my $datatable;
 4257:     if ($position eq 'top') {
 4258:         my $instsrchon = ' ';
 4259:         my $instsrchoff = ' checked="checked" ';
 4260:         my ($exacton,$containson,$beginson);
 4261:         my $instlocalon = ' ';
 4262:         my $instlocaloff = ' checked="checked" ';
 4263:         if (ref($settings) eq 'HASH') {
 4264:             if ($settings->{'available'} eq '1') {
 4265:                 $instsrchon = $instsrchoff;
 4266:                 $instsrchoff = ' ';
 4267:             }
 4268:             if ($settings->{'localonly'} eq '1') {
 4269:                 $instlocalon = $instlocaloff;
 4270:                 $instlocaloff = ' ';
 4271:             }
 4272:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4273:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4274:                     if ($type eq 'exact') {
 4275:                         $exacton = ' checked="checked" ';
 4276:                     } elsif ($type eq 'contains') {
 4277:                         $containson = ' checked="checked" ';
 4278:                     } elsif ($type eq 'begins') {
 4279:                         $beginson = ' checked="checked" ';
 4280:                     }
 4281:                 }
 4282:             } else {
 4283:                 if ($settings->{'searchtypes'} eq 'exact') {
 4284:                     $exacton = ' checked="checked" ';
 4285:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4286:                     $containson = ' checked="checked" ';
 4287:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4288:                     $exacton = ' checked="checked" ';
 4289:                     $containson = ' checked="checked" ';
 4290:                 }
 4291:             }
 4292:         }
 4293:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4294:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4295: 
 4296:         my $numinrow = 4;
 4297:         my $cansrchrow = 0;
 4298:         $datatable='<tr class="LC_odd_row">'.
 4299:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4300:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4301:                    '<input type="radio" name="dirsrch_available"'.
 4302:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4303:                    '<label><input type="radio" name="dirsrch_available"'.
 4304:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4305:                    '</tr><tr>'.
 4306:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4307:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4308:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4309:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4310:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4311:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4312:                    '</tr>';
 4313:         $$rowtotal += 2;
 4314:         if (ref($usertypes) eq 'HASH') {
 4315:             if (keys(%{$usertypes}) > 0) {
 4316:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4317:                                              $numinrow,$othertitle,'cansearch',
 4318:                                              $rowtotal);
 4319:                 $cansrchrow = 1;
 4320:             }
 4321:         }
 4322:         if ($cansrchrow) {
 4323:             $$rowtotal ++;
 4324:             $datatable .= '<tr>';
 4325:         } else {
 4326:             $datatable .= '<tr class="LC_odd_row">';
 4327:         }
 4328:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4329:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4330:         foreach my $title (@{$titleorder}) {
 4331:             if (defined($searchtitles->{$title})) {
 4332:                 my $check = ' ';
 4333:                 if (ref($settings) eq 'HASH') {
 4334:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4335:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4336:                             $check = ' checked="checked" ';
 4337:                         }
 4338:                     }
 4339:                 }
 4340:                 $datatable .= '<td class="LC_left_item">'.
 4341:                               '<span class="LC_nobreak"><label>'.
 4342:                               '<input type="checkbox" name="searchby" '.
 4343:                               'value="'.$title.'"'.$check.'/>'.
 4344:                               $searchtitles->{$title}.'</label></span></td>';
 4345:             }
 4346:         }
 4347:         $datatable .= '</tr></table></td></tr>';
 4348:         $$rowtotal ++;
 4349:         if ($cansrchrow) {
 4350:             $datatable .= '<tr class="LC_odd_row">';
 4351:         } else {
 4352:             $datatable .= '<tr>';
 4353:         }
 4354:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4355:                       '<td class="LC_left_item" colspan="2">'.
 4356:                       '<span class="LC_nobreak"><label>'.
 4357:                       '<input type="checkbox" name="searchtypes" '.
 4358:                       $exacton.' value="exact" />'.&mt('Exact match').
 4359:                       '</label>&nbsp;'.
 4360:                       '<label><input type="checkbox" name="searchtypes" '.
 4361:                       $beginson.' value="begins" />'.&mt('Begins with').
 4362:                       '</label>&nbsp;'.
 4363:                       '<label><input type="checkbox" name="searchtypes" '.
 4364:                       $containson.' value="contains" />'.&mt('Contains').
 4365:                       '</label></span></td></tr>';
 4366:         $$rowtotal ++;
 4367:     } else {
 4368:         my $domsrchon = ' checked="checked" ';
 4369:         my $domsrchoff = ' ';
 4370:         my $domlocalon = ' ';
 4371:         my $domlocaloff = ' checked="checked" ';
 4372:         if (ref($settings) eq 'HASH') {
 4373:             if ($settings->{'lclocalonly'} eq '1') {
 4374:                 $domlocalon = $domlocaloff;
 4375:                 $domlocaloff = ' ';
 4376:             }
 4377:             if ($settings->{'lcavailable'} eq '0') {
 4378:                 $domsrchoff = $domsrchon;
 4379:                 $domsrchon = ' ';
 4380:             }
 4381:         }
 4382:         $datatable='<tr class="LC_odd_row">'.
 4383:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4384:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4385:                       '<input type="radio" name="dirsrch_domavailable"'.
 4386:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4387:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4388:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4389:                       '</tr><tr>'.
 4390:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4391:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4392:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4393:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4394:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4395:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4396:                       '</tr>';
 4397:         $$rowtotal += 2;
 4398:     }
 4399:     return $datatable;
 4400: }
 4401: 
 4402: sub print_contacts {
 4403:     my ($position,$dom,$settings,$rowtotal) = @_;
 4404:     my $datatable;
 4405:     my @contacts = ('adminemail','supportemail');
 4406:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4407:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4408:     if ($position eq 'top') {
 4409:         if (ref($settings) eq 'HASH') {
 4410:             foreach my $item (@contacts) {
 4411:                 if (exists($settings->{$item})) {
 4412:                     $to{$item} = $settings->{$item};
 4413:                 }
 4414:             }
 4415:         }
 4416:     } elsif ($position eq 'middle') {
 4417:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4418:                      'updatesmail','idconflictsmail','hostipmail');
 4419:         foreach my $type (@mailings) {
 4420:             $otheremails{$type} = '';
 4421:         }
 4422:     } elsif ($position eq 'lower') {
 4423:         if (ref($settings) eq 'HASH') {
 4424:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4425:                 %lonstatus = %{$settings->{'lonstatus'}};
 4426:             }
 4427:         }
 4428:     } else {
 4429:         @mailings = ('helpdeskmail','otherdomsmail');
 4430:         foreach my $type (@mailings) {
 4431:             $otheremails{$type} = '';
 4432:         }
 4433:         $bccemails{'helpdeskmail'} = '';
 4434:         $bccemails{'otherdomsmail'} = '';
 4435:         $includestr{'helpdeskmail'} = '';
 4436:         $includestr{'otherdomsmail'} = '';
 4437:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4438:     }
 4439:     if (ref($settings) eq 'HASH') {
 4440:         unless (($position eq 'top') || ($position eq 'lower')) {
 4441:             foreach my $type (@mailings) {
 4442:                 if (exists($settings->{$type})) {
 4443:                     if (ref($settings->{$type}) eq 'HASH') {
 4444:                         foreach my $item (@contacts) {
 4445:                             if ($settings->{$type}{$item}) {
 4446:                                 $checked{$type}{$item} = ' checked="checked" ';
 4447:                             }
 4448:                         }
 4449:                         $otheremails{$type} = $settings->{$type}{'others'};
 4450:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4451:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4452:                             if ($settings->{$type}{'include'} ne '') {
 4453:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4454:                                 $includestr{$type} = &unescape($includestr{$type});
 4455:                             }
 4456:                         }
 4457:                     }
 4458:                 } elsif ($type eq 'lonstatusmail') {
 4459:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4460:                 }
 4461:             }
 4462:         }
 4463:         if ($position eq 'bottom') {
 4464:             foreach my $type (@mailings) {
 4465:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4466:                 if ($settings->{$type}{'include'} ne '') {
 4467:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4468:                     $includestr{$type} = &unescape($includestr{$type});
 4469:                 }
 4470:             }
 4471:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4472:                 if (ref($fields) eq 'ARRAY') {
 4473:                     foreach my $field (@{$fields}) {
 4474:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4475:                     }
 4476:                 }
 4477:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4478:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4479:                 } else {
 4480:                     $maxsize = '1.0';
 4481:                 }
 4482:             } else {
 4483:                 if (ref($fields) eq 'ARRAY') {
 4484:                     foreach my $field (@{$fields}) {
 4485:                         $currfield{$field} = 'yes';
 4486:                     }
 4487:                 }
 4488:                 $maxsize = '1.0';
 4489:             }
 4490:         }
 4491:     } else {
 4492:         if ($position eq 'top') {
 4493:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4494:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4495:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4496:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4497:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4498:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4499:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4500:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4501:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4502:         } elsif ($position eq 'bottom') {
 4503:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4504:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4505:             if (ref($fields) eq 'ARRAY') {
 4506:                 foreach my $field (@{$fields}) {
 4507:                     $currfield{$field} = 'yes';
 4508:                 }
 4509:             }
 4510:             $maxsize = '1.0';
 4511:         }
 4512:     }
 4513:     my ($titles,$short_titles) = &contact_titles();
 4514:     my $rownum = 0;
 4515:     my $css_class;
 4516:     if ($position eq 'top') {
 4517:         foreach my $item (@contacts) {
 4518:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4519:             $datatable .= '<tr'.$css_class.'>'. 
 4520:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4521:                           '</span></td><td class="LC_right_item">'.
 4522:                           '<input type="text" name="'.$item.'" value="'.
 4523:                           $to{$item}.'" /></td></tr>';
 4524:             $rownum ++;
 4525:         }
 4526:     } elsif ($position eq 'bottom') {
 4527:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4528:         $datatable .= '<tr'.$css_class.'>'.
 4529:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4530:                       &mt('(e-mail, subject, and description always shown)').
 4531:                       '</td><td class="LC_left_item">';
 4532:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4533:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4534:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4535:             foreach my $field (@{$fields}) {
 4536:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4537:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4538:                     $datatable .= ' '.&mt('(logged-in users)');
 4539:                 }
 4540:                 $datatable .='</td><td>';
 4541:                 my $clickaction;
 4542:                 if ($field eq 'screenshot') {
 4543:                     $clickaction = ' onclick="screenshotSize(this);"';
 4544:                 }
 4545:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4546:                     foreach my $option (@{$possoptions->{$field}}) {
 4547:                         my $checked;
 4548:                         if ($currfield{$field} eq $option) {
 4549:                             $checked = ' checked="checked"';
 4550:                         }
 4551:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4552:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4553:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4554:                                       '</label></span>'.('&nbsp;'x2);
 4555:                     }
 4556:                 }
 4557:                 if ($field eq 'screenshot') {
 4558:                     my $display;
 4559:                     if ($currfield{$field} eq 'no') {
 4560:                         $display = ' style="display:none"';
 4561:                     }
 4562:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4563:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4564:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4565:                 }
 4566:                 $datatable .= '</td></tr>';
 4567:             }
 4568:             $datatable .= '</table>';
 4569:         }
 4570:         $datatable .= '</td></tr>'."\n";
 4571:         $rownum ++;
 4572:     }
 4573:     unless (($position eq 'top') || ($position eq 'lower')) {
 4574:         foreach my $type (@mailings) {
 4575:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4576:             $datatable .= '<tr'.$css_class.'>'.
 4577:                           '<td><span class="LC_nobreak">'.
 4578:                           $titles->{$type}.': </span></td>'.
 4579:                           '<td class="LC_left_item">';
 4580:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4581:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4582:             }
 4583:             $datatable .= '<span class="LC_nobreak">';
 4584:             foreach my $item (@contacts) {
 4585:                 $datatable .= '<label>'.
 4586:                               '<input type="checkbox" name="'.$type.'"'.
 4587:                               $checked{$type}{$item}.
 4588:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4589:                               '</label>&nbsp;';
 4590:             }
 4591:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4592:                           '<input type="text" name="'.$type.'_others" '.
 4593:                           'value="'.$otheremails{$type}.'"  />';
 4594:             my %locchecked;
 4595:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4596:                 foreach my $loc ('s','b') {
 4597:                     if ($includeloc{$type} eq $loc) {
 4598:                         $locchecked{$loc} = ' checked="checked"';
 4599:                         last;
 4600:                     }
 4601:                 }
 4602:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4603:                               '<input type="text" name="'.$type.'_bcc" '.
 4604:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4605:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4606:                               &mt('Text automatically added to e-mail:').' '.
 4607:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4608:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4609:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4610:                               ('&nbsp;'x2).
 4611:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4612:                               '</span></fieldset>';
 4613:             }
 4614:             $datatable .= '</td></tr>'."\n";
 4615:             $rownum ++;
 4616:         }
 4617:     }
 4618:     if ($position eq 'middle') {
 4619:         my %choices;
 4620:         my $corelink = &core_link_msu();
 4621:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4622:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4623:                                         $corelink);
 4624:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4625:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4626:         my %defaultchecked = ('reporterrors'  => 'on',
 4627:                               'reportupdates' => 'on',
 4628:                               'reportstatus'  => 'on');
 4629:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4630:                                                    \%choices,$rownum);
 4631:         $datatable .= $reports;
 4632:     } elsif ($position eq 'lower') {
 4633:         my (%current,%excluded,%weights);
 4634:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4635:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4636:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4637:         } else {
 4638:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4639:         }
 4640:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4641:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4642:         } else {
 4643:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4644:         }
 4645:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4646:             foreach my $type ('E','W','N','U') {
 4647:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4648:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4649:                 } else {
 4650:                     $weights{$type} = $defaults->{$type};
 4651:                 }
 4652:             }
 4653:         } else {
 4654:             foreach my $type ('E','W','N','U') {
 4655:                 $weights{$type} = $defaults->{$type};
 4656:             }
 4657:         }
 4658:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4659:             if (@{$lonstatus{'excluded'}} > 0) {
 4660:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4661:             }
 4662:         }
 4663:         foreach my $item ('errorthreshold','errorsysmail') {
 4664:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4665:             $datatable .= '<tr'.$css_class.'>'.
 4666:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4667:                           $titles->{$item}.
 4668:                           '</span></td><td class="LC_left_item">'.
 4669:                           '<input type="text" name="'.$item.'" value="'.
 4670:                           $current{$item}.'" size="5" /></td></tr>';
 4671:             $rownum ++;
 4672:         }
 4673:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4674:         $datatable .= '<tr'.$css_class.'>'.
 4675:                       '<td class="LC_left_item">'.
 4676:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4677:                       '</span></td><td class="LC_left_item"><table><tr>';
 4678:         foreach my $type ('E','W','N','U') {
 4679:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4680:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4681:                           $weights{$type}.'" size="5" /></td>';
 4682:         }
 4683:         $datatable .= '</tr></table></tr>';
 4684:         $rownum ++;
 4685:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4686:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4687:                       $titles->{'errorexcluded'}.'</td>'.
 4688:                       '<td class="LC_left_item"><table>';
 4689:         my $numinrow = 4;
 4690:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4691:         for (my $i=0; $i<@ids; $i++) {
 4692:             my $rem = $i%($numinrow);
 4693:             if ($rem == 0) {
 4694:                 if ($i > 0) {
 4695:                     $datatable .= '</tr>';
 4696:                 }
 4697:                 $datatable .= '<tr>';
 4698:             }
 4699:             my $check;
 4700:             if ($excluded{$ids[$i]}) {
 4701:                 $check = ' checked="checked" ';
 4702:             }
 4703:             $datatable .= '<td class="LC_left_item">'.
 4704:                           '<span class="LC_nobreak"><label>'.
 4705:                           '<input type="checkbox" name="errorexcluded" '.
 4706:                           'value="'.$ids[$i].'"'.$check.' />'.
 4707:                           $ids[$i].'</label></span></td>';
 4708:         }
 4709:         my $colsleft = $numinrow - @ids%($numinrow);
 4710:         if ($colsleft > 1 ) {
 4711:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4712:                           '&nbsp;</td>';
 4713:         } elsif ($colsleft == 1) {
 4714:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4715:         }
 4716:         $datatable .= '</tr></table></td></tr>';
 4717:         $rownum ++;
 4718:     } elsif ($position eq 'bottom') {
 4719:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4720:         my (@posstypes,%usertypeshash);
 4721:         if (ref($types) eq 'ARRAY') {
 4722:             @posstypes = @{$types};
 4723:         }
 4724:         if (@posstypes) {
 4725:             if (ref($usertypes) eq 'HASH') {
 4726:                 %usertypeshash = %{$usertypes};
 4727:             }
 4728:             my @overridden;
 4729:             my $numinrow = 4;
 4730:             if (ref($settings) eq 'HASH') {
 4731:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4732:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4733:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4734:                             push(@overridden,$key);
 4735:                             foreach my $item (@contacts) {
 4736:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4737:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4738:                                 }
 4739:                             }
 4740:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4741:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4742:                             $includeloc{'override_'.$key} = '';
 4743:                             $includestr{'override_'.$key} = '';
 4744:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4745:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4746:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4747:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4748:                             }
 4749:                         }
 4750:                     }
 4751:                 }
 4752:             }
 4753:             my $customclass = 'LC_helpdesk_override';
 4754:             my $optionsprefix = 'LC_options_helpdesk_';
 4755: 
 4756:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4757:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4758:                                          $numinrow,$othertitle,'overrides',
 4759:                                          \$rownum,$onclicktypes,$customclass);
 4760:             $rownum ++;
 4761:             $usertypeshash{'default'} = $othertitle;
 4762:             foreach my $status (@posstypes) {
 4763:                 my $css_class;
 4764:                 if ($rownum%2) {
 4765:                     $css_class = 'LC_odd_row ';
 4766:                 }
 4767:                 $css_class .= $customclass;
 4768:                 my $rowid = $optionsprefix.$status;
 4769:                 my $hidden = 1;
 4770:                 my $currstyle = 'display:none';
 4771:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4772:                     $currstyle = 'display:table-row';
 4773:                     $hidden = 0;
 4774:                 }
 4775:                 my $key = 'override_'.$status;
 4776:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4777:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4778:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4779:                                                   \@contacts,$short_titles);
 4780:                 unless ($hidden) {
 4781:                     $rownum ++;
 4782:                 }
 4783:             }
 4784:         }
 4785:     }
 4786:     $$rowtotal += $rownum;
 4787:     return $datatable;
 4788: }
 4789: 
 4790: sub core_link_msu {
 4791:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4792:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4793: }
 4794: 
 4795: sub overridden_helpdesk {
 4796:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4797:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4798:     my $class = 'LC_left_item';
 4799:     if ($css_class) {
 4800:         $css_class = ' class="'.$css_class.'"';
 4801:     }
 4802:     if ($rowid) {
 4803:         $rowid = ' id="'.$rowid.'"';
 4804:     }
 4805:     if ($rowstyle) {
 4806:         $rowstyle = ' style="'.$rowstyle.'"';
 4807:     }
 4808:     my ($output,$description);
 4809:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4810:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4811:               "<td>$description</td>\n".
 4812:               '<td class="'.$class.'" colspan="2">'.
 4813:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4814:               '<span class="LC_nobreak">';
 4815:     if (ref($contacts) eq 'ARRAY') {
 4816:         foreach my $item (@{$contacts}) {
 4817:             my $check;
 4818:             if (ref($checked) eq 'HASH') {
 4819:                $check = $checked->{$item};
 4820:             }
 4821:             my $title;
 4822:             if (ref($short_titles) eq 'HASH') {
 4823:                 $title = $short_titles->{$item};
 4824:             }
 4825:             $output .= '<label>'.
 4826:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4827:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4828:         }
 4829:     }
 4830:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4831:                '<input type="text" name="override_'.$type.'_others" '.
 4832:                'value="'.$otheremails.'"  />';
 4833:     my %locchecked;
 4834:     foreach my $loc ('s','b') {
 4835:         if ($includeloc eq $loc) {
 4836:             $locchecked{$loc} = ' checked="checked"';
 4837:             last;
 4838:         }
 4839:     }
 4840:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4841:                '<input type="text" name="override_'.$type.'_bcc" '.
 4842:                'value="'.$bccemails.'"  /></fieldset>'.
 4843:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4844:                &mt('Text automatically added to e-mail:').' '.
 4845:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4846:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4847:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4848:                ('&nbsp;'x2).
 4849:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4850:                '</span></fieldset>'.
 4851:                '</td></tr>'."\n";
 4852:     return $output;
 4853: }
 4854: 
 4855: sub contacts_javascript {
 4856:     return <<"ENDSCRIPT";
 4857: 
 4858: <script type="text/javascript">
 4859: // <![CDATA[
 4860: 
 4861: function screenshotSize(field) {
 4862:     if (document.getElementById('help_screenshotsize')) {
 4863:         if (field.value == 'no') {
 4864:             document.getElementById('help_screenshotsize').style.display="none";
 4865:         } else {
 4866:             document.getElementById('help_screenshotsize').style.display="";
 4867:         }
 4868:     }
 4869:     return;
 4870: }
 4871: 
 4872: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4873:     if (form.elements[checkbox].length != undefined) {
 4874:         var count = 0;
 4875:         if (docount) {
 4876:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4877:                 if (form.elements[checkbox][i].checked) {
 4878:                     count ++;
 4879:                 }
 4880:             }
 4881:         }
 4882:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4883:             var type = form.elements[checkbox][i].value;
 4884:             if (document.getElementById(prefix+type)) {
 4885:                 if (form.elements[checkbox][i].checked) {
 4886:                     document.getElementById(prefix+type).style.display = 'table-row';
 4887:                     if (count % 2 == 1) {
 4888:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4889:                     } else {
 4890:                         document.getElementById(prefix+type).className = target;
 4891:                     }
 4892:                     count ++;
 4893:                 } else {
 4894:                     document.getElementById(prefix+type).style.display = 'none';
 4895:                 }
 4896:             }
 4897:         }
 4898:     }
 4899:     return;
 4900: }
 4901: 
 4902: // ]]>
 4903: </script>
 4904: 
 4905: ENDSCRIPT
 4906: }
 4907: 
 4908: sub print_helpsettings {
 4909:     my ($position,$dom,$settings,$rowtotal) = @_;
 4910:     my $confname = $dom.'-domainconfig';
 4911:     my $formname = 'display';
 4912:     my ($datatable,$itemcount);
 4913:     if ($position eq 'top') {
 4914:         $itemcount = 1;
 4915:         my (%choices,%defaultchecked,@toggles);
 4916:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4917:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4918:                                      &mt('LON-CAPA bug tracker'),600,500));
 4919:         %defaultchecked = ('submitbugs' => 'on');
 4920:         @toggles = ('submitbugs');
 4921:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4922:                                                      \%choices,$itemcount);
 4923:         $$rowtotal ++;
 4924:     } else {
 4925:         my $css_class;
 4926:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4927:         my (%customroles,%ordered,%current);
 4928:         if (ref($settings) eq 'HASH') {
 4929:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4930:                 %current = %{$settings->{'adhoc'}};
 4931:             }
 4932:         }
 4933:         my $count = 0;
 4934:         foreach my $key (sort(keys(%existing))) {
 4935:             if ($key=~/^rolesdef\_(\w+)$/) {
 4936:                 my $rolename = $1;
 4937:                 my (%privs,$order);
 4938:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4939:                 $customroles{$rolename} = \%privs;
 4940:                 if (ref($current{$rolename}) eq 'HASH') {
 4941:                     $order = $current{$rolename}{'order'};
 4942:                 }
 4943:                 if ($order eq '') {
 4944:                     $order = $count;
 4945:                 }
 4946:                 $ordered{$order} = $rolename;
 4947:                 $count++;
 4948:             }
 4949:         }
 4950:         my $maxnum = scalar(keys(%ordered));
 4951:         my @roles_by_num = ();
 4952:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4953:             push(@roles_by_num,$item);
 4954:         }
 4955:         my $context = 'domprefs';
 4956:         my $crstype = 'Course';
 4957:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4958:         my @accesstypes = ('all','dh','da','none');
 4959:         my ($numstatustypes,@jsarray);
 4960:         if (ref($types) eq 'ARRAY') {
 4961:             if (@{$types} > 0) {
 4962:                 $numstatustypes = scalar(@{$types});
 4963:                 push(@accesstypes,'status');
 4964:                 @jsarray = ('bystatus');
 4965:             }
 4966:         }
 4967:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4968:         if (keys(%domhelpdesk)) {
 4969:             push(@accesstypes,('inc','exc'));
 4970:             push(@jsarray,('notinc','notexc'));
 4971:         }
 4972:         my $hiddenstr = join("','",@jsarray);
 4973:         my $context = 'domprefs';
 4974:         my $crstype = 'Course';
 4975:         my $prefix = 'helproles_';
 4976:         my $add_class = 'LC_hidden';
 4977:         foreach my $num (@roles_by_num) {
 4978:             my $role = $ordered{$num};
 4979:             my ($desc,$access,@statuses);
 4980:             if (ref($current{$role}) eq 'HASH') {
 4981:                 $desc = $current{$role}{'desc'};
 4982:                 $access = $current{$role}{'access'};
 4983:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4984:                     @statuses = @{$current{$role}{'insttypes'}};
 4985:                 }
 4986:             }
 4987:             if ($desc eq '') {
 4988:                 $desc = $role;
 4989:             }
 4990:             my $identifier = 'custhelp'.$num;
 4991:             my %full=();
 4992:             my %levels= (
 4993:                          course => {},
 4994:                          domain => {},
 4995:                          system => {},
 4996:                         );
 4997:             my %levelscurrent=(
 4998:                                course => {},
 4999:                                domain => {},
 5000:                                system => {},
 5001:                               );
 5002:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 5003:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5004:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5005:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 5006:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 5007:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 5008:             for (my $k=0; $k<=$maxnum; $k++) {
 5009:                 my $vpos = $k+1;
 5010:                 my $selstr;
 5011:                 if ($k == $num) {
 5012:                     $selstr = ' selected="selected" ';
 5013:                 }
 5014:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5015:             }
 5016:             $datatable .= '</select>'.('&nbsp;'x2).
 5017:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 5018:                           '</td>'.
 5019:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5020:                           &mt('Name shown to users:').
 5021:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 5022:                           '</fieldset>'.
 5023:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 5024:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 5025:                           '<fieldset>'.
 5026:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 5027:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 5028:                                                                    \%levelscurrent,$identifier,
 5029:                                                                    'LC_hidden',$prefix.$num.'_privs').
 5030:                           '</fieldset></td>';
 5031:             $itemcount ++;
 5032:         }
 5033:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5034:         my $newcust = 'custhelp'.$count;
 5035:         my (%privs,%levelscurrent);
 5036:         my %full=();
 5037:         my %levels= (
 5038:                      course => {},
 5039:                      domain => {},
 5040:                      system => {},
 5041:                     );
 5042:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 5043:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5044:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 5045:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 5046:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5047:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5048:         for (my $k=0; $k<$maxnum+1; $k++) {
 5049:             my $vpos = $k+1;
 5050:             my $selstr;
 5051:             if ($k == $maxnum) {
 5052:                 $selstr = ' selected="selected" ';
 5053:             }
 5054:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5055:         }
 5056:         $datatable .= '</select>&nbsp;'."\n".
 5057:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5058:                       '</label></span></td>'.
 5059:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5060:                       '<span class="LC_nobreak">'.
 5061:                       &mt('Internal name:').
 5062:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5063:                       '</span>'.('&nbsp;'x4).
 5064:                       '<span class="LC_nobreak">'.
 5065:                       &mt('Name shown to users:').
 5066:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5067:                       '</span></fieldset>'.
 5068:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5069:                                              $usertypes,$types,\%domhelpdesk).
 5070:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5071:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5072:                                                                 \@templateroles,$newcust).
 5073:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5074:                                                                \%levelscurrent,$newcust).
 5075:                       '</fieldset>'.
 5076:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5077:                       '</td></tr>';
 5078:         $count ++;
 5079:         $$rowtotal += $count;
 5080:     }
 5081:     return $datatable;
 5082: }
 5083: 
 5084: sub adhocbutton {
 5085:     my ($prefix,$num,$field,$visibility) = @_;
 5086:     my %lt = &Apache::lonlocal::texthash(
 5087:                                           show => 'Show details',
 5088:                                           hide => 'Hide details',
 5089:                                         );
 5090:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5091:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5092:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5093:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5094: }
 5095: 
 5096: sub helpsettings_javascript {
 5097:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5098:     return unless(ref($roles_by_num) eq 'ARRAY');
 5099:     my %html_js_lt = &Apache::lonlocal::texthash(
 5100:                                           show => 'Show details',
 5101:                                           hide => 'Hide details',
 5102:                                         );
 5103:     &html_escape(\%html_js_lt);
 5104:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5105:     return <<"ENDSCRIPT";
 5106: <script type="text/javascript">
 5107: // <![CDATA[
 5108: 
 5109: function reorderHelpRoles(form,item) {
 5110:     var changedVal;
 5111: $jstext
 5112:     var newpos = 'helproles_${total}_pos';
 5113:     var maxh = 1 + $total;
 5114:     var current = new Array();
 5115:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5116:     if (item == newpos) {
 5117:         changedVal = newitemVal;
 5118:     } else {
 5119:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5120:         current[newitemVal] = newpos;
 5121:     }
 5122:     for (var i=0; i<helproles.length; i++) {
 5123:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5124:         if (elementName != item) {
 5125:             if (form.elements[elementName]) {
 5126:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5127:                 current[currVal] = elementName;
 5128:             }
 5129:         }
 5130:     }
 5131:     var oldVal;
 5132:     for (var j=0; j<maxh; j++) {
 5133:         if (current[j] == undefined) {
 5134:             oldVal = j;
 5135:         }
 5136:     }
 5137:     if (oldVal < changedVal) {
 5138:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5139:            var elementName = current[k];
 5140:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5141:         }
 5142:     } else {
 5143:         for (var k=changedVal; k<oldVal; k++) {
 5144:             var elementName = current[k];
 5145:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5146:         }
 5147:     }
 5148:     return;
 5149: }
 5150: 
 5151: function helpdeskAccess(num) {
 5152:     var curraccess = null;
 5153:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5154:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5155:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5156:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5157:             }
 5158:         }
 5159:     }
 5160:     var shown = Array();
 5161:     var hidden = Array();
 5162:     if (curraccess == 'none') {
 5163:         hidden = Array('$hiddenstr');
 5164:     } else {
 5165:         if (curraccess == 'status') {
 5166:             shown = Array('bystatus');
 5167:             hidden = Array('notinc','notexc');
 5168:         } else {
 5169:             if (curraccess == 'exc') {
 5170:                 shown = Array('notexc');
 5171:                 hidden = Array('notinc','bystatus');
 5172:             }
 5173:             if (curraccess == 'inc') {
 5174:                 shown = Array('notinc');
 5175:                 hidden = Array('notexc','bystatus');
 5176:             }
 5177:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5178:                 hidden = Array('notinc','notexc','bystatus');
 5179:             }
 5180:         }
 5181:     }
 5182:     if (hidden.length > 0) {
 5183:         for (var i=0; i<hidden.length; i++) {
 5184:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5185:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5186:             }
 5187:         }
 5188:     }
 5189:     if (shown.length > 0) {
 5190:         for (var i=0; i<shown.length; i++) {
 5191:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5192:                 if (shown[i] == 'privs') {
 5193:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5194:                 } else {
 5195:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5196:                 }
 5197:             }
 5198:         }
 5199:     }
 5200:     return;
 5201: }
 5202: 
 5203: function toggleHelpdeskItem(num,field) {
 5204:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5205:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5206:             document.getElementById('helproles_'+num+'_'+field).className =
 5207:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5208:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5209:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5210:             }
 5211:         } else {
 5212:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5213:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5214:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5215:             }
 5216:         }
 5217:     }
 5218:     return;
 5219: }
 5220: 
 5221: // ]]>
 5222: </script>
 5223: 
 5224: ENDSCRIPT
 5225: }
 5226: 
 5227: sub helpdeskroles_access {
 5228:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5229:         $usertypes,$types,$domhelpdesk) = @_;
 5230:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5231:     my %lt = &Apache::lonlocal::texthash(
 5232:                     'rou'    => 'Role usage',
 5233:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5234:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5235:                     'dh'     => 'All with domain helpdesk role',
 5236:                     'da'     => 'All with domain helpdesk assistant role',
 5237:                     'none'   => 'None',
 5238:                     'status' => 'Determined based on institutional status',
 5239:                     'inc'    => 'Include all, but exclude specific personnel',
 5240:                     'exc'    => 'Exclude all, but include specific personnel',
 5241:                   );
 5242:     my %usecheck = (
 5243:                      all => ' checked="checked"',
 5244:                    );
 5245:     my %displaydiv = (
 5246:                       status => 'none',
 5247:                       inc    => 'none',
 5248:                       exc    => 'none',
 5249:                       priv   => 'block',
 5250:                      );
 5251:     my $output;
 5252:     if (ref($current) eq 'HASH') {
 5253:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5254:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5255:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5256:                 delete($usecheck{'all'});
 5257:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5258:                     my $access = $1;
 5259:                     $displaydiv{$access} = 'inline';
 5260:                 } elsif ($current->{access} eq 'none') {
 5261:                     $displaydiv{'priv'} = 'none';
 5262:                 }
 5263:             }
 5264:         }
 5265:     }
 5266:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5267:               '<p>'.$lt{'whi'}.'</p>';
 5268:     foreach my $access (@{$accesstypes}) {
 5269:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5270:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5271:                    $lt{$access}.'</label>';
 5272:         if ($access eq 'status') {
 5273:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5274:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5275:                                                                  $othertitle,$usertypes,$types).
 5276:                        '</div>';
 5277:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5278:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5279:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5280:                        '</div>';
 5281:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5282:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5283:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5284:                        '</div>';
 5285:         }
 5286:         $output .= '</p>';
 5287:     }
 5288:     $output .= '</fieldset>';
 5289:     return $output;
 5290: }
 5291: 
 5292: sub radiobutton_prefs {
 5293:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5294:         $additional,$align,$firstval) = @_;
 5295:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5296:                    (ref($choices) eq 'HASH'));
 5297: 
 5298:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5299: 
 5300:     foreach my $item (@{$toggles}) {
 5301:         if ($defaultchecked->{$item} eq 'on') {
 5302:             $checkedon{$item} = ' checked="checked" ';
 5303:             $checkedoff{$item} = ' ';
 5304:         } elsif ($defaultchecked->{$item} eq 'off') {
 5305:             $checkedoff{$item} = ' checked="checked" ';
 5306:             $checkedon{$item} = ' ';
 5307:         }
 5308:     }
 5309:     if (ref($settings) eq 'HASH') {
 5310:         foreach my $item (@{$toggles}) {
 5311:             if ($settings->{$item} eq '1') {
 5312:                 $checkedon{$item} =  ' checked="checked" ';
 5313:                 $checkedoff{$item} = ' ';
 5314:             } elsif ($settings->{$item} eq '0') {
 5315:                 $checkedoff{$item} =  ' checked="checked" ';
 5316:                 $checkedon{$item} = ' ';
 5317:             }
 5318:         }
 5319:     }
 5320:     if ($onclick) {
 5321:         $onclick = ' onclick="'.$onclick.'"';
 5322:     }
 5323:     foreach my $item (@{$toggles}) {
 5324:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5325:         $datatable .=
 5326:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5327:             '<span class="LC_nobreak">'.$choices->{$item}.
 5328:             '</span></td>';
 5329:         if ($align eq 'left') {
 5330:             $datatable .= '<td class="LC_left_item">';
 5331:         } else {
 5332:             $datatable .= '<td class="LC_right_item">';
 5333:         }
 5334:         $datatable .= '<span class="LC_nobreak">';
 5335:         if ($firstval eq 'no') {
 5336:             $datatable .=
 5337:                 '<label><input type="radio" name="'.
 5338:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5339:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5340:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5341:         } else {
 5342:             $datatable .=
 5343:                 '<label><input type="radio" name="'.
 5344:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5345:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5346:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5347:         }
 5348:         $datatable .= '</span>'.$additional.'</td></tr>';
 5349:         $itemcount ++;
 5350:     }
 5351:     return ($datatable,$itemcount);
 5352: }
 5353: 
 5354: sub print_ltitools {
 5355:     my ($position,$dom,$settings,$rowtotal) = @_;
 5356:     my (%rules,%encrypt,%privkeys,%linkprot);
 5357:     if (ref($settings) eq 'HASH') {
 5358:         if ($position eq 'top') {
 5359:             if (exists($settings->{'encrypt'})) {
 5360:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5361:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5362:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5363:                     }
 5364:                 }
 5365:             }
 5366:             if (exists($settings->{'private'})) {
 5367:                 if (ref($settings->{'private'}) eq 'HASH') {
 5368:                     if (ref($settings->{'private'}) eq 'HASH') {
 5369:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5370:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5371:                         }
 5372:                     }
 5373:                 }
 5374:             }
 5375:         } elsif ($position eq 'middle') {
 5376:             if (exists($settings->{'rules'})) {
 5377:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5378:                     %rules = %{$settings->{'rules'}};
 5379:                 }
 5380:             }
 5381:         } else {
 5382:             foreach my $key ('encrypt','private','rules') {
 5383:                 if (exists($settings->{$key})) {
 5384:                     delete($settings->{$key});
 5385:                 }
 5386:             }
 5387:         }
 5388:     }
 5389:     my $datatable;
 5390:     my $itemcount = 1;
 5391:     if ($position eq 'top') {
 5392:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5393:     } elsif ($position eq 'middle') {
 5394:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5395:         $$rowtotal += $itemcount;
 5396:     } else {
 5397:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5398:     }
 5399:     return $datatable;
 5400: }
 5401: 
 5402: sub secrets_form {
 5403:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5404:     my @ids=&Apache::lonnet::current_machine_ids();
 5405:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5406:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5407:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5408:     $itemcount = 0;
 5409:     foreach my $hostid (sort(keys(%servers))) {
 5410:         my ($showextra,$divsty,$switch);
 5411:         if ($hostid eq $primary) {
 5412:             if ($context eq 'ltisec') {
 5413:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5414:                     $showextra = 1;
 5415:                 }
 5416:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5417:                     $showextra = 1;
 5418:                 }
 5419:             } else {
 5420:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5421:                     $showextra = 1;
 5422:                 }
 5423:             }
 5424:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5425:                 $switch = 1;
 5426:             }
 5427:             if ($showextra) {
 5428:                 $numshown ++;
 5429:                 $divsty = 'display:inline-block';
 5430:             } else {
 5431:                 $divsty = 'display:none';
 5432:             }
 5433:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5434:                       '<legend>'.$hostid.'</legend>';
 5435:             if ($switch) {
 5436:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5437:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5438:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5439:                 if (exists($privkeys->{$hostid})) {
 5440:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5441:                               '<span class="LC_nobreak">'.
 5442:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5443:                               '<span class="LC_nobreak">'.&mt('Change?').
 5444:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5445:                               ('&nbsp;'x2).
 5446:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5447:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5448:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5449:                               '</span></div>';
 5450:                 } else {
 5451:                     $extra .= '<span class="LC_nobreak">'.
 5452:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5453:                               '</span>'."\n";
 5454:                 }
 5455:             } elsif (exists($privkeys->{$hostid})) {
 5456:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5457:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5458:                           '<span class="LC_nobreak">'.&mt('Change?').
 5459:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5460:                           ('&nbsp;'x2).
 5461:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5462:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5463:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5464:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5465:                           '<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>'.
 5466:                           '</span></div>';
 5467:             } else {
 5468:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5469:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5470:                           '<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>';
 5471:             }
 5472:             $extra .= '</fieldset>';
 5473:         }
 5474:     }
 5475:     my (%choices,@toggles,%defaultchecked);
 5476:     if ($context eq 'ltisec') {
 5477:         %choices = &Apache::lonlocal::texthash (
 5478:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5479:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5480:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5481:                                                );
 5482:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5483:         %defaultchecked = (
 5484:                            'ltisec_crslinkprot' => 'off',
 5485:                            'ltisec_domlinkprot' => 'off',
 5486:                            'ltisec_consumers'   => 'off',
 5487:                           );
 5488:     } else {
 5489:         %choices = &Apache::lonlocal::texthash (
 5490:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5491:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5492:                                                );
 5493:         @toggles = qw(toolsec_crs toolsec_dom);
 5494:         %defaultchecked = (
 5495:                            'toolsec_crs' => 'off',
 5496:                            'toolsec_dom' => 'off',
 5497:                           );
 5498:     }
 5499:     my ($onclick,$itemcount);
 5500:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5501:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5502:                                               \%choices,$itemcount,$onclick,'','left','no');
 5503: 
 5504:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5505:     my $noprivkeysty = 'display:inline-block';
 5506:     if ($numshown) {
 5507:         $noprivkeysty = 'display:none';
 5508:     }
 5509:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5510:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5511:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5512:                $extra.
 5513:                '</td></tr>';
 5514:     $itemcount ++;
 5515:     $$rowtotal += $itemcount;
 5516:     return $output;
 5517: }
 5518: 
 5519: sub print_proctoring {
 5520:     my ($dom,$settings,$rowtotal) = @_;
 5521:     my $itemcount = 1;
 5522:     my (%ordered,%providernames,%current,%currentdef);
 5523:     my $confname = $dom.'-domainconfig';
 5524:     my $switchserver = &check_switchserver($dom,$confname);
 5525:     if (ref($settings) eq 'HASH') {
 5526:         foreach my $item (keys(%{$settings})) {
 5527:             if (ref($settings->{$item}) eq 'HASH') {
 5528:                 my $num = $settings->{$item}{'order'};
 5529:                 $ordered{$num} = $item;
 5530:             }
 5531:         }
 5532:     } else {
 5533:         %ordered = (
 5534:                      1 => 'proctorio',
 5535:                      2 => 'examity',
 5536:                    );
 5537:     }
 5538:     %providernames = &proctoring_providernames();
 5539:     my $maxnum = scalar(keys(%ordered));
 5540:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5541:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5542:     if (ref($requref) eq 'HASH') {
 5543:         %requserfields = %{$requref};
 5544:     }
 5545:     if (ref($opturef) eq 'HASH') {
 5546:         %optuserfields = %{$opturef};
 5547:     }
 5548:     if (ref($defref) eq 'HASH') {
 5549:         %defaults = %{$defref};
 5550:     }
 5551:     if (ref($extref) eq 'HASH') {
 5552:         %extended = %{$extref};
 5553:     }
 5554:     if (ref($crsref) eq 'HASH') {
 5555:         %crsconf = %{$crsref};
 5556:     }
 5557:     if (ref($rolesref) eq 'ARRAY') {
 5558:         @courseroles = @{$rolesref};
 5559:     }
 5560:     if (ref($ltiref) eq 'ARRAY') {
 5561:         @ltiroles = @{$ltiref};
 5562:     }
 5563:     my $datatable;
 5564:     my $css_class;
 5565:     if (keys(%ordered)) {
 5566:         my @items = sort { $a <=> $b } keys(%ordered);
 5567:         for (my $i=0; $i<@items; $i++) {
 5568:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5569:             my $provider = $ordered{$items[$i]};
 5570:             my $optionsty = 'none';
 5571:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5572:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5573:             if (ref($settings) eq 'HASH') {
 5574:                 if (ref($settings->{$provider}) eq 'HASH') {
 5575:                     %current = %{$settings->{$provider}};
 5576:                     if ($current{'available'}) {
 5577:                         $optionsty = 'block';
 5578:                         $available = 1;
 5579:                     }
 5580:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5581:                         $lifetime = $current{'lifetime'};
 5582:                     }
 5583:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5584:                         $version = $current{'version'};
 5585:                     }
 5586:                     if ($current{'image'} ne '') {
 5587:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5588:                     }
 5589:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5590:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5591:                     }
 5592:                     $userincdom = $current{'incdom'};
 5593:                     if (ref($current{'roles'}) eq 'HASH') {
 5594:                         %rolemaps = %{$current{'roles'}};
 5595:                         $checkedfields{'roles'} = 1;
 5596:                     }
 5597:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5598:                         foreach my $val (@{$current{'defaults'}}) {
 5599:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5600:                                 $inuse{$val} = 1;
 5601:                             } else {
 5602:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5603:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5604:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5605:                                             $inuse{$poss} = $val;
 5606:                                             last;
 5607:                                         }
 5608:                                     }
 5609:                                 }
 5610:                             }
 5611:                         }
 5612:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5613:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5614:                             my $currval = $current{'defaults'}{$key}; 
 5615:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5616:                                 $inuse{$key} = 1;
 5617:                             } else {
 5618:                                 my $match;
 5619:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5620:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5621:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5622:                                             $inuse{$poss} = $key;
 5623:                                             last;
 5624:                                         }
 5625:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5626:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5627:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5628:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5629:                                                     $currentdef{$inner} = $currval;
 5630:                                                     $match = 1;
 5631:                                                     last;
 5632:                                                 }
 5633:                                             } elsif ($inner eq $key) {
 5634:                                                 $currentdef{$key} = $currval;
 5635:                                                 $match = 1;
 5636:                                                 last;
 5637:                                             }
 5638:                                         }
 5639:                                     }
 5640:                                     last if ($match);
 5641:                                 }
 5642:                             }
 5643:                         }
 5644:                     }
 5645:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5646:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5647:                     }
 5648:                 }
 5649:             }
 5650:             my %lt = &proctoring_titles($provider);
 5651:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5652:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5653:             my %checkedavailable = (
 5654:                                    yes => '',
 5655:                                    no  => ' checked="checked"',
 5656:                                  );
 5657:             if ($available) {
 5658:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5659:                 $checkedavailable{'no'} = '';
 5660:             }
 5661:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5662:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5663:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5664:             for (my $k=0; $k<$maxnum; $k++) {
 5665:                 my $vpos = $k+1;
 5666:                 my $selstr;
 5667:                 if ($k == $i) {
 5668:                     $selstr = ' selected="selected" ';
 5669:                 }
 5670:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5671:             }
 5672:             if ($version eq '') {
 5673:                 if ($provider eq 'proctorio') {
 5674:                     $version = '1.0';
 5675:                 } elsif ($provider eq 'examity') {
 5676:                     $version = '1.1';
 5677:                 }
 5678:             }
 5679:             if ($lifetime eq '') {
 5680:                 $lifetime = '300';
 5681:             }
 5682:             $datatable .=
 5683:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5684:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5685:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5686:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5687:                 '</td>'.
 5688:                 '<td colspan="2">'.
 5689:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5690:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5691:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5692:                 ('&nbsp;'x2).
 5693:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5694:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5695:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5696:                 ('&nbsp;'x2).
 5697:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5698:                 '<br />'.
 5699:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5700:                 '<br />'.
 5701:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5702:                 ('&nbsp;'x2).
 5703:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5704:                 '<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";
 5705:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5706:             if ($imgsrc) {
 5707:                 $datatable .= $imgsrc.
 5708:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5709:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5710:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5711:             }
 5712:             $datatable .= '&nbsp;';
 5713:             if ($switchserver) {
 5714:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5715:             } else {
 5716:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5717:             }
 5718:             unless ($imgsrc) {
 5719:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5720:             }
 5721:             $datatable .= '</fieldset>'."\n";
 5722:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5723:                 if (@{$requserfields{$provider}} > 0) {
 5724:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5725:                     foreach my $field (@{$requserfields{$provider}}) {
 5726:                         $datatable .= '<span class="LC_nobreak">'.
 5727:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5728:                                       $lt{$field}.'</label>';
 5729:                         if ($field eq 'user') {
 5730:                             my $seluserdom = '';
 5731:                             my $unseluserdom = ' selected="selected"';
 5732:                             if ($userincdom) {
 5733:                                 $seluserdom = $unseluserdom;
 5734:                                 $unseluserdom = '';
 5735:                             }
 5736:                             $datatable .= ':&nbsp;'.
 5737:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5738:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5739:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5740:                                 '</select>&nbsp;';
 5741:                         } else {
 5742:                             $datatable .= '&nbsp;';
 5743:                             if ($field eq 'roles') {
 5744:                                 $showroles = 1; 
 5745:                             } 
 5746:                         }
 5747:                         $datatable .= '</span> ';
 5748:                     }
 5749:                 }
 5750:                 $datatable .= '</fieldset>'."\n";
 5751:             }
 5752:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5753:                 if (@{$optuserfields{$provider}} > 0) {
 5754:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5755:                     foreach my $field (@{$optuserfields{$provider}}) {
 5756:                         my $checked;
 5757:                         if ($checkedfields{$field}) {
 5758:                             $checked = ' checked="checked"';
 5759:                         }
 5760:                         $datatable .= '<span class="LC_nobreak">'.
 5761:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5762:                     }
 5763:                     $datatable .= '</fieldset>'."\n";
 5764:                 }
 5765:             }
 5766:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5767:                 if (@{$defaults{$provider}}) {
 5768:                     my (%options,@selectboxes);
 5769:                     if (ref($extended{$provider}) eq 'HASH') {
 5770:                         %options = %{$extended{$provider}};
 5771:                     }
 5772:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5773:                     my ($rem,$numinrow,$dropdowns);
 5774:                     if ($provider eq 'proctorio') { 
 5775:                         $datatable .= '<table>';
 5776:                         $numinrow = 4;
 5777:                     }
 5778:                     my $i = 0;
 5779:                     foreach my $field (@{$defaults{$provider}}) {
 5780:                         my $checked;
 5781:                         if ($inuse{$field}) {
 5782:                             $checked = ' checked="checked"';
 5783:                         }
 5784:                         if ($provider eq 'examity') {
 5785:                             if ($field eq 'display') {
 5786:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5787:                                 foreach my $option ('iframe','tab','window') {
 5788:                                     my $checkdisp;
 5789:                                     if ($currentdef{'target'} eq $option) {
 5790:                                         $checkdisp = ' checked="checked"';
 5791:                                     }
 5792:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5793:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5794:                                 }
 5795:                                 $datatable .= ('&nbsp;'x4);
 5796:                                 foreach my $dimen ('width','height') {
 5797:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5798:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5799:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5800:                                                   ('&nbsp;'x2);
 5801:                                 }
 5802:                                 $datatable .= '</span><br />'.
 5803:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5804:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5805:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5806:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5807:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5808:                                               $currentdef{'explanation'}.
 5809:                                               '</textarea></div><div style=""></div><br />';
 5810:                             }
 5811:                         } else {
 5812:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5813:                                 my ($output,$selnone);
 5814:                                 unless ($checked) {
 5815:                                     $selnone = ' selected="selected"';
 5816:                                 }
 5817:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5818:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5819:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5820:                                 foreach my $option (@{$options{$field}}) {
 5821:                                     my $sel; 
 5822:                                     if ($inuse{$field} eq $option) {
 5823:                                         $sel = ' selected="selected"';
 5824:                                     }
 5825:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5826:                                 }
 5827:                                 $output .= '</select></span>';
 5828:                                 push(@selectboxes,$output);
 5829:                             } else {
 5830:                                 $rem = $i%($numinrow);
 5831:                                 if ($rem == 0) {
 5832:                                     if ($i > 0) {
 5833:                                         $datatable .= '</tr>';
 5834:                                     }
 5835:                                     $datatable .= '<tr>';
 5836:                                 }
 5837:                                 $datatable .= '<td class="LC_left_item">'.
 5838:                                               '<span class="LC_nobreak">'.
 5839:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5840:                                               $fieldtitles{$field}.'</label></span></td>';
 5841:                                 $i++;
 5842:                             }
 5843:                         }
 5844:                     }
 5845:                     if ($provider eq 'proctorio') {
 5846:                         if ($numinrow) {
 5847:                             $rem = $i%$numinrow;
 5848:                         }
 5849:                         my $colsleft = $numinrow - $rem;
 5850:                         if ($colsleft > 1) {
 5851:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5852:                         } else {
 5853:                             $datatable .= '<td class="LC_left_item">';
 5854:                         }
 5855:                         $datatable .= '&nbsp;'.
 5856:                                       '</td></tr></table>';
 5857:                         if (@selectboxes) {
 5858:                             $datatable .= '<hr /><table>';
 5859:                             $numinrow = 2;
 5860:                             for (my $i=0; $i<@selectboxes; $i++) {
 5861:                                 $rem = $i%($numinrow);
 5862:                                 if ($rem == 0) {
 5863:                                     if ($i > 0) {
 5864:                                         $datatable .= '</tr>';
 5865:                                     }
 5866:                                     $datatable .= '<tr>';
 5867:                                 }
 5868:                                 $datatable .= '<td class="LC_left_item">'.
 5869:                                               $selectboxes[$i].'</td>';
 5870:                             }
 5871:                             if ($numinrow) {
 5872:                                 $rem = $i%$numinrow;
 5873:                             }
 5874:                             $colsleft = $numinrow - $rem;
 5875:                             if ($colsleft > 1) {
 5876:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5877:                             } else {
 5878:                                 $datatable .= '<td class="LC_left_item">';
 5879:                             }
 5880:                             $datatable .= '&nbsp;'.
 5881:                                           '</td></tr></table>';
 5882:                         }
 5883:                     }
 5884:                     $datatable .= '</fieldset>';
 5885:                 }
 5886:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5887:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5888:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5889:                     my ($rem,$numinrow);
 5890:                     if ($provider eq 'proctorio') {
 5891:                         $datatable .= '<table>';
 5892:                         $numinrow = 4;
 5893:                     }
 5894:                     my $i = 0;
 5895:                     foreach my $item (@{$crsconf{$provider}}) {
 5896:                         my $name;
 5897:                         if ($provider eq 'examity') {
 5898:                             $name = $lt{'crs'.$item};
 5899:                         } elsif ($provider eq 'proctorio') {
 5900:                             $name = $fieldtitles{$item};
 5901:                             $rem = $i%($numinrow);
 5902:                             if ($rem == 0) {
 5903:                                 if ($i > 0) {
 5904:                                     $datatable .= '</tr>';
 5905:                                 }
 5906:                                 $datatable .= '<tr>';
 5907:                             }
 5908:                             $datatable .= '<td class="LC_left_item>';
 5909:                         }
 5910:                         my $checked;
 5911:                         if ($crsconfig{$item}) {
 5912:                             $checked = ' checked="checked"';
 5913:                         }
 5914:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5915:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5916:                                       $name.'</label></span>';
 5917:                         if ($provider eq 'examity') {
 5918:                             $datatable .= '&nbsp; ';
 5919:                         }
 5920:                         $datatable .= "\n";
 5921:                         $i++;
 5922:                     }
 5923:                     if ($provider eq 'proctorio') {
 5924:                         if ($numinrow) {
 5925:                             $rem = $i%$numinrow;
 5926:                         }
 5927:                         my $colsleft = $numinrow - $rem;
 5928:                         if ($colsleft > 1) {
 5929:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5930:                         } else {
 5931:                             $datatable .= '<td class="LC_left_item">';
 5932:                         }
 5933:                         $datatable .= '&nbsp;'.
 5934:                                       '</td></tr></table>';
 5935:                     }
 5936:                     $datatable .= '</fieldset>';
 5937:                 }
 5938:                 if ($showroles) {
 5939:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5940:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5941:                     foreach my $role (@courseroles) {
 5942:                         my ($selected,$selectnone);
 5943:                         if (!$rolemaps{$role}) {
 5944:                             $selectnone = ' selected="selected"';
 5945:                         }
 5946:                         $datatable .= '<td style="text-align: center">'.
 5947:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5948:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5949:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5950:                         foreach my $ltirole (@ltiroles) {
 5951:                             unless ($selectnone) {
 5952:                                 if ($rolemaps{$role} eq $ltirole) {
 5953:                                     $selected = ' selected="selected"';
 5954:                                 } else {
 5955:                                     $selected = '';
 5956:                                 }
 5957:                             }
 5958:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5959:                         }
 5960:                         $datatable .= '</select></td>';
 5961:                     }
 5962:                     $datatable .= '</tr></table></fieldset>'.
 5963:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5964:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5965:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5966:                                   '<tr><td></td><td>lms</td>'.
 5967:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5968:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5969:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5970:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5971:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5972:                         if (keys(%custom) > 0) {
 5973:                             foreach my $key (sort(keys(%custom))) {
 5974:                                 next if ($key eq 'lms');
 5975:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5976:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5977:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5978:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5979:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5980:                             }
 5981:                         }
 5982:                     }
 5983:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5984:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5985:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5986:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5987:                                   '</table></fieldset></td></tr>'."\n";
 5988:                 }
 5989:                 $datatable .= '</td></tr>';
 5990:             }
 5991:             $itemcount ++;
 5992:         }
 5993:     }
 5994:     return $datatable;
 5995: }
 5996: 
 5997: sub proctoring_data {
 5998:     my $requserfields = {
 5999:                       proctorio => ['user'],
 6000:                       examity   => ['roles','user'],
 6001:                      };
 6002:     my $optuserfields = {
 6003:                         proctorio => ['fullname'],
 6004:                         examity   => ['fullname','firstname','lastname','email'],
 6005:                      };
 6006:     my $defaults = {
 6007:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6008:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6009:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6010:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6011:                                 'reentry','calculator','whiteboard'],
 6012:                   examity   => ['display'],
 6013:                 };
 6014:     my $extended = { 
 6015:                   proctorio => {
 6016:                                  verifyid => ['verifyidauto','verifyidlive'],
 6017:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6018:                                  tabslinks => ['notabs','linksonly'],
 6019:                                  reentry => ['noreentry','agentreentry'],
 6020:                                  calculator => ['calculatorbasic','calculatorsci'],
 6021:                                },
 6022:                   examity => {
 6023:                                display => {
 6024:                                            target      => ['iframe','tab','window'],
 6025:                                            width       => '',
 6026:                                            height      => '',
 6027:                                            linktext    => '',
 6028:                                            explanation => '',
 6029:                                           },
 6030:                              },
 6031:                 };
 6032:     my $crsconf = {
 6033:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6034:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6035:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6036:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6037:                                'reentry','calculator','whiteboard'],
 6038:                  examity => ['label','title','target','linktext','explanation','append'],
 6039:                };
 6040:     my $courseroles = ['cc','in','ta','ep','st'];
 6041:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6042:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6043: }
 6044: 
 6045: sub proctoring_titles {
 6046:     my ($item) = @_;
 6047:     my (%common_lt,%custom_lt);
 6048:     %common_lt = &Apache::lonlocal::texthash (
 6049:                      'avai'      => 'Available?',
 6050:                      'base'      => 'Basic Settings',
 6051:                      'requ'      => 'User data required to be sent on launch',
 6052:                      'optu'      => 'User data optionally sent on launch',
 6053:                      'udsl'      => 'User data sent on launch',
 6054:                      'defa'      => 'Defaults for items configurable in course',
 6055:                      'sigmethod' => 'Signature Method',
 6056:                      'key'       => 'Key',
 6057:                      'lifetime'  => 'Nonce lifetime (s)',
 6058:                      'secret'    => 'Secret',
 6059:                      'icon'      => 'Icon',
 6060:                      'fullname'  => 'Full Name',
 6061:                      'visible'   => 'Visible input',
 6062:                      'username'  => 'username',
 6063:                      'user'      => 'User',
 6064:                  );
 6065:     if ($item eq 'proctorio') {
 6066:         %custom_lt = &Apache::lonlocal::texthash (
 6067:                          'version'        => 'OAuth version',
 6068:                          'url'            => 'API URL',
 6069:                          'uname:dom'      => 'username-domain',
 6070:         );
 6071:     } elsif ($item eq 'examity') {
 6072:         %custom_lt = &Apache::lonlocal::texthash (
 6073:                          'version'        => 'LTI Version',
 6074:                          'url'            => 'URL',
 6075:                          'uname:dom'      => 'username:domain',
 6076:                          'msgtype'        => 'Message Type',
 6077:                          'firstname'      => 'First Name',
 6078:                          'lastname'       => 'Last Name',
 6079:                          'email'          => 'E-mail',
 6080:                          'roles'          => 'Role',
 6081:                          'crstarget'      => 'Display target',
 6082:                          'crslabel'       => 'Course label',
 6083:                          'crstitle'       => 'Course title', 
 6084:                          'crslinktext'    => 'Link Text',
 6085:                          'crsexplanation' => 'Explanation',
 6086:                          'crsappend'      => 'Provider URL',
 6087:         );
 6088:     }
 6089:     my %lt = (%common_lt,%custom_lt);
 6090:     return %lt;
 6091: }
 6092: 
 6093: sub proctoring_fieldtitles {
 6094:     my ($item) = @_;
 6095:     if ($item eq 'proctorio') {
 6096:         return &Apache::lonlocal::texthash (
 6097:                   'recordvideo' => 'Record video',
 6098:                   'recordaudio' => 'Record audio',
 6099:                   'recordscreen' => 'Record screen',
 6100:                   'recordwebtraffic' => 'Record web traffic',
 6101:                   'recordroomstart' => 'Record room scan',
 6102:                   'verifyvideo' => 'Verify webcam',
 6103:                   'verifyaudio' => 'Verify microphone',
 6104:                   'verifydesktop' => 'Verify desktop recording',
 6105:                   'verifyid' => 'Photo ID verification',
 6106:                   'verifysignature' => 'Require signature',
 6107:                   'fullscreen' => 'Fullscreen',
 6108:                   'clipboard' => 'Disable copy/paste',
 6109:                   'tabslinks' => 'New tabs/windows',
 6110:                   'closetabs' => 'Close other tabs',
 6111:                   'onescreen' => 'Limit to single screen',
 6112:                   'print' => 'Disable Printing',
 6113:                   'downloads' => 'Disable Downloads',
 6114:                   'cache' => 'Empty cache after exam',
 6115:                   'rightclick' => 'Disable right click',
 6116:                   'reentry' => 'Re-entry to exam',
 6117:                   'calculator' => 'Onscreen calculator',
 6118:                   'whiteboard' => 'Onscreen whiteboard',
 6119:                   'verifyidauto' => 'Automated verification',
 6120:                   'verifyidlive' => 'Live agent verification',
 6121:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6122:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6123:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6124:                   'notabs' => 'Disaallowed',
 6125:                   'linksonly' => 'Allowed from links in exam',
 6126:                   'noreentry' => 'Disallowed',
 6127:                   'agentreentry' => 'Agent required for re-entry',
 6128:                   'calculatorbasic' => 'Basic',
 6129:                   'calculatorsci' => 'Scientific',
 6130:         );
 6131:     } elsif ($item eq 'examity') {
 6132:         return &Apache::lonlocal::texthash (
 6133:                 'target'         => 'Display target',   
 6134:                 'window'         => 'Window',
 6135:                 'tab'            => 'Tab',
 6136:                 'iframe'         => 'iFrame',
 6137:                 'height'         => 'Height (pixels)',
 6138:                 'width'          => 'Width (pixels)',
 6139:                 'linktext'       => 'Default Link Text',
 6140:                 'explanation'    => 'Default Explanation',
 6141:                 'append'         => 'Provider URL',
 6142:         );
 6143:     }
 6144: }
 6145: 
 6146: sub proctoring_providernames {
 6147:     return (
 6148:              proctorio => 'Proctorio',
 6149:              examity   => 'Examity',
 6150:            );
 6151: }
 6152: 
 6153: sub print_lti {
 6154:     my ($position,$dom,$settings,$rowtotal) = @_;
 6155:     my $itemcount = 1;
 6156:     my ($datatable,$css_class);
 6157:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 6158:     if (ref($settings) eq 'HASH') {
 6159:         if ($position eq 'top') {
 6160:             if (exists($settings->{'encrypt'})) {
 6161:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6162:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6163:                         if ($key eq 'consumers') {
 6164:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6165:                         } else {
 6166:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6167:                         }
 6168:                     }
 6169:                 }
 6170:             }
 6171:             if (exists($settings->{'private'})) {
 6172:                 if (ref($settings->{'private'}) eq 'HASH') {
 6173:                     if (ref($settings->{'private'}) eq 'HASH') {
 6174:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6175:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6176:                         }
 6177:                     }
 6178:                 }
 6179:             }
 6180:         } elsif ($position eq 'upper') {
 6181:             if (exists($settings->{'rules'})) {
 6182:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6183:                     %rules = %{$settings->{'rules'}};
 6184:                 }
 6185:             }
 6186:         } elsif ($position eq 'middle') {
 6187:             if (exists($settings->{'suggested'})) {
 6188:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 6189:                     %suggestions = %{$settings->{'suggested'}};
 6190:                 }
 6191:             }
 6192:         } elsif ($position eq 'lower') {
 6193:             if (exists($settings->{'linkprot'})) {
 6194:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6195:                     %linkprot = %{$settings->{'linkprot'}};
 6196:                     if ($linkprot{'lock'}) {
 6197:                         delete($linkprot{'lock'});
 6198:                     }
 6199:                 }
 6200:             }
 6201:         } else {
 6202:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 6203:                 if (exists($settings->{$key})) {
 6204:                     delete($settings->{$key});
 6205:                 }
 6206:             }
 6207:         }
 6208:     }
 6209:     if ($position eq 'top') {
 6210:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6211:     } elsif ($position eq 'upper') {
 6212:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6213:         $$rowtotal += $itemcount;
 6214:     } elsif ($position eq 'middle') {
 6215:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 6216:         $$rowtotal += $itemcount;
 6217:     } elsif ($position eq 'lower') {
 6218:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6219:     } else {
 6220:         my ($switchserver,$switchmessage);
 6221:         $switchserver = &check_switchserver($dom);
 6222:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6223:         my $maxnum = 0;
 6224:         my %ordered;
 6225:         if (ref($settings) eq 'HASH') {
 6226:             foreach my $item (keys(%{$settings})) {
 6227:                 if (ref($settings->{$item}) eq 'HASH') {
 6228:                     my $num = $settings->{$item}{'order'};
 6229:                     if ($num eq '') {
 6230:                         $num = scalar(keys(%{$settings}));
 6231:                     }
 6232:                     $ordered{$num} = $item;
 6233:                 }
 6234:             }
 6235:         }
 6236:         $maxnum = scalar(keys(%ordered));
 6237:         my %lt = &lti_names();
 6238:         if (keys(%ordered)) {
 6239:             my @items = sort { $a <=> $b } keys(%ordered);
 6240:             for (my $i=0; $i<@items; $i++) {
 6241:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6242:                 my $item = $ordered{$items[$i]};
 6243:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6244:                 if (ref($settings->{$item}) eq 'HASH') {
 6245:                     $key = $settings->{$item}->{'key'};
 6246:                     $usable = $settings->{$item}->{'usable'};
 6247:                     $lifetime = $settings->{$item}->{'lifetime'};
 6248:                     $consumer = $settings->{$item}->{'consumer'};
 6249:                     $requser = $settings->{$item}->{'requser'};
 6250:                     $crsinc = $settings->{$item}->{'crsinc'};
 6251:                     $current = $settings->{$item};
 6252:                 }
 6253:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6254:                 my %checkedrequser = (
 6255:                                        yes => ' checked="checked"',
 6256:                                        no  => '',
 6257:                                      );
 6258:                 if (!$requser) {
 6259:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6260:                     $checkedrequser{'yes'} = '';
 6261:                 }
 6262:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6263:                 my %checkedcrsinc = (
 6264:                                       yes => ' checked="checked"',
 6265:                                       no  => '',
 6266:                                     );
 6267:                 if (!$crsinc) {
 6268:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6269:                     $checkedcrsinc{'yes'} = '';
 6270:                 }
 6271:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6272:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6273:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6274:                 for (my $k=0; $k<=$maxnum; $k++) {
 6275:                     my $vpos = $k+1;
 6276:                     my $selstr;
 6277:                     if ($k == $i) {
 6278:                         $selstr = ' selected="selected" ';
 6279:                     }
 6280:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6281:                 }
 6282:                 $datatable .= '</select>'.('&nbsp;'x2).
 6283:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6284:                     &mt('Delete?').'</label></span></td>'.
 6285:                     '<td colspan="2">'.
 6286:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6287:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6288:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6289:                     ('&nbsp;'x2).
 6290:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6291:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6292:                     ('&nbsp;'x2).
 6293:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6294:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6295:                 if ($key ne '') {
 6296:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6297:                     if ($switchserver) {
 6298:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6299:                     } else {
 6300:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6301:                     }
 6302:                     $datatable .= '</span> '.('&nbsp;'x2);
 6303:                 } elsif (!$switchserver) {
 6304:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6305:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6306:                                   '</span> '.('&nbsp;'x2);
 6307:                 }
 6308:                 if ($switchserver) {
 6309:                     if ($usable ne '') {
 6310:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6311:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6312:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6313:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6314:                                       ('&nbsp;'x2).
 6315:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6316:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6317:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6318:                                       '</div>';
 6319:                     } elsif ($key eq '') {
 6320:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6321:                     } else {
 6322:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6323:                     }
 6324:                 } else {
 6325:                     if ($usable ne '') {
 6326:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6327:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6328:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6329:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6330:                                       ('&nbsp;'x2).
 6331:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6332:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6333:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6334:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6335:                                       '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label></span></div>';
 6336:                     } else {
 6337:                         $datatable .=
 6338:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6339:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6340:                             '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>';
 6341:                     }
 6342:                 }
 6343:                 $datatable .= '<br /><br />'.
 6344:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6345:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6346:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6347:                     '<br /><br />'.
 6348:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6349:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6350:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6351:                     ('&nbsp;'x4).
 6352:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6353:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6354:                 $itemcount ++;
 6355:             }
 6356:         }
 6357:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6358:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6359:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6360:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6361:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6362:         for (my $k=0; $k<$maxnum+1; $k++) {
 6363:             my $vpos = $k+1;
 6364:             my $selstr;
 6365:             if ($k == $maxnum) {
 6366:                 $selstr = ' selected="selected" ';
 6367:             }
 6368:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6369:         }
 6370:         $datatable .= '</select>&nbsp;'."\n".
 6371:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6372:                       '<td colspan="2">'.
 6373:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6374:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6375:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6376:                       ('&nbsp;'x2).
 6377:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6378:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6379:                       ('&nbsp;'x2).
 6380:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6381:         if ($switchserver) {
 6382:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6383:         } else {
 6384:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6385:                           ('&nbsp;'x2).
 6386:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6387:                           '<label><input type="checkbox" name="lti_add_visible" id="lti_add_visible" onclick="if (this.checked) { this.form.lti_secret_add.type='."'text'".' } else { this.form.lti_secret_add.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n";
 6388:         }
 6389:         $datatable .= '<br /><br />'.
 6390:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6391:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6392:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6393:                       '<br /><br />'.
 6394:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6395:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6396:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6397:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6398:                       '</td>'."\n".
 6399:                       '</tr>'."\n";
 6400:         $itemcount ++;
 6401:     }
 6402:     $$rowtotal += $itemcount;
 6403:     return $datatable;
 6404: }
 6405: 
 6406: sub lti_names {
 6407:     my %lt = &Apache::lonlocal::texthash(
 6408:                                           'version'   => 'LTI Version',
 6409:                                           'url'       => 'URL',
 6410:                                           'key'       => 'Key',
 6411:                                           'lifetime'  => 'Nonce lifetime (s)',
 6412:                                           'consumer'  => 'Consumer',
 6413:                                           'secret'    => 'Secret',
 6414:                                           'requser'   => "User's identity sent",
 6415:                                           'crsinc'    => "Course's identity sent",
 6416:                                           'email'     => 'Email address',
 6417:                                           'sourcedid' => 'User ID',
 6418:                                           'other'     => 'Other',
 6419:                                           'passback'  => 'Can return grades to Consumer:',
 6420:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6421:                                           'topmenu'   => 'Display LON-CAPA page header',
 6422:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6423:                                         );
 6424:     return %lt;
 6425: }
 6426: 
 6427: sub lti_options {
 6428:     my ($num,$current,$itemcount,%lt) = @_;
 6429:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6430:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6431:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6432:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6433:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6434:     $checked{'mapcrstype'} = {};
 6435:     $checked{'makeuser'} = {};
 6436:     $checked{'selfenroll'} = {};
 6437:     $checked{'crssec'} = {};
 6438:     $checked{'crssecsrc'} = {};
 6439:     $checked{'lcauth'} = {};
 6440:     $checked{'menuitem'} = {};
 6441:     if ($num eq 'add') {
 6442:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6443:     }
 6444:     my $userfieldsty = 'none';
 6445:     my $crsfieldsty = 'none';
 6446:     my $crssecfieldsty = 'none';
 6447:     my $secsrcfieldsty = 'none';
 6448:     my $callbacksty = 'none';
 6449:     my $passbacksty = 'none';
 6450:     my $optionsty = 'block';
 6451:     my $crssty = 'block';
 6452:     my $lcauthparm;
 6453:     my $lcauthparmstyle = 'display:none';
 6454:     my $lcauthparmtext;
 6455:     my $menusty;
 6456:     my $numinrow = 4;
 6457:     my %menutitles = &ltimenu_titles();
 6458: 
 6459:     if (ref($current) eq 'HASH') {
 6460:         if (!$current->{'requser'}) {
 6461:             $optionsty = 'none';
 6462:             $crssty = 'none';
 6463:         } elsif (!$current->{'crsinc'}) {
 6464:             $crssty = 'none';
 6465:         }
 6466:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6467:             $checked{'mapuser'}{'sourcedid'} = '';
 6468:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6469:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6470:             } else {
 6471:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6472:                 $userfield = $current->{'mapuser'};
 6473:                 $userfieldsty = 'inline-block';
 6474:             }
 6475:         }
 6476:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6477:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6478:             if ($current->{'mapcrs'} eq 'context_id') {
 6479:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6480:             } else {
 6481:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6482:                 $cidfield = $current->{'mapcrs'};
 6483:                 $crsfieldsty = 'inline-block';
 6484:             }
 6485:         }
 6486:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6487:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6488:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6489:             }
 6490:         }
 6491:         if (!$current->{'storecrs'}) {
 6492:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6493:             $checked{'storecrs'}{'Y'} = '';
 6494:         }
 6495:         if ($current->{'makecrs'}) {
 6496:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6497:         }
 6498:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6499:             foreach my $role (@{$current->{'makeuser'}}) {
 6500:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6501:             }
 6502:         }
 6503:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6504:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6505:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6506:                 $lcauthparm = $current->{'lcauthparm'};
 6507:                 $lcauthparmstyle = 'display:table-row';
 6508:                 if ($current->{'lcauth'} eq 'localauth') {
 6509:                     $lcauthparmtext = &mt('Local auth argument');
 6510:                 } else {
 6511:                     $lcauthparmtext = &mt('Kerberos domain');
 6512:                 }
 6513:             }
 6514:         }
 6515:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6516:             foreach my $role (@{$current->{'selfenroll'}}) {
 6517:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6518:             }
 6519:         }
 6520:         if (ref($current->{'maproles'}) eq 'HASH') {
 6521:             %rolemaps = %{$current->{'maproles'}};
 6522:         }
 6523:         if ($current->{'section'} ne '') {
 6524:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6525:             $crssecfieldsty = 'inline-block';
 6526:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6527:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6528:             } else {
 6529:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6530:                 $crssecsrc = $current->{'section'};
 6531:                 $secsrcfieldsty = 'inline-block';
 6532:             }
 6533:         } else {
 6534:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6535:         }
 6536:         if ($current->{'callback'} ne '') {
 6537:             $callback = $current->{'callback'};
 6538:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6539:             $callbacksty = 'inline-block';
 6540:         } else {
 6541:             $checked{'callback'}{'N'} = ' checked="checked"';
 6542:         }
 6543:         if ($current->{'topmenu'}) {
 6544:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6545:         } else {
 6546:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6547:         }
 6548:         if ($current->{'inlinemenu'}) {
 6549:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6550:         } else {
 6551:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6552:         }
 6553:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6554:             $menusty = 'inline-block';
 6555:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6556:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6557:                     if (exists($menutitles{$item})) {
 6558:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6559:                     }
 6560:                 }
 6561:             }
 6562:         } else {
 6563:             $menusty = 'none';
 6564:         }
 6565:     } else {
 6566:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6567:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6568:         $checked{'callback'}{'N'} = ' checked="checked"';
 6569:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6570:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6571:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6572:         $menusty = 'inline-block';
 6573:     }
 6574:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6575:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6576:                                official   => 'Official',
 6577:                                unofficial => 'Unofficial',
 6578:                                community  => 'Community',
 6579:                                textbook   => 'Textbook',
 6580:                                placement  => 'Placement Test',
 6581:                                lti        => 'LTI Provider',
 6582:     );
 6583:     my @authtypes = ('internal','krb4','krb5','localauth');
 6584:     my %shortauth = (
 6585:                      internal => 'int',
 6586:                      krb4 => 'krb4',
 6587:                      krb5 => 'krb5',
 6588:                      localauth  => 'loc'
 6589:                     );
 6590:     my %authnames = &authtype_names();
 6591:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6592:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6593:     my @courseroles = ('cc','in','ta','ep','st');
 6594:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6595:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6596:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6597:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6598:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6599:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6600:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6601:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6602:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6603:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6604:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6605:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6606:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6607:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6608:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6609:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6610:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6611:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6612:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6613:     foreach my $option ('sourcedid','email','other') {
 6614:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6615:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6616:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6617:     }
 6618:     $output .= '</span></div>'.
 6619:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6620:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6621:                'value="'.$userfield.'" /></div></fieldset>'.
 6622:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6623:     foreach my $ltirole (@ltiroles) {
 6624:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6625:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6626:     }
 6627:     $output .= '</fieldset>'.
 6628:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6629:                '<table>'.
 6630:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6631:                '</table>'.
 6632:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6633:                '<td class="LC_left_item">';
 6634:     foreach my $auth ('lti',@authtypes) {
 6635:         my $authtext;
 6636:         if ($auth eq 'lti') {
 6637:             $authtext = &mt('None');
 6638:         } else {
 6639:             $authtext = $authnames{$shortauth{$auth}};
 6640:         }
 6641:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6642:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6643:                    $authtext.'</label></span> &nbsp;';
 6644:     }
 6645:     $output .= '</td></tr>'.
 6646:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6647:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6648:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6649:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6650:                '</table></fieldset>'.
 6651:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6652:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6653:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6654:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6655:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6656:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6657:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6658:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6659:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6660:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6661:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6662:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6663:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6664:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6665:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6666:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6667:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6668:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6669:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6670:                    ('&nbsp;'x2);
 6671:     }
 6672:     $output .= '</span></div></fieldset>'.
 6673:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6674:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6675:                &mt('Unique course identifier').':&nbsp;';
 6676:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6677:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6678:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6679:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6680:     }
 6681:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6682:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6683:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6684:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6685:     foreach my $type (@coursetypes) {
 6686:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6687:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6688:                    ('&nbsp;'x2);
 6689:     }
 6690:     $output .= '</span><br /><br />'.
 6691:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6692:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6693:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6694:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6695:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6696:                '</fieldset>'.
 6697:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6698:     foreach my $ltirole (@lticourseroles) {
 6699:         my ($selected,$selectnone);
 6700:         if ($rolemaps{$ltirole} eq '') {
 6701:             $selectnone = ' selected="selected"';
 6702:         }
 6703:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6704:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6705:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6706:         foreach my $role (@courseroles) {
 6707:             unless ($selectnone) {
 6708:                 if ($rolemaps{$ltirole} eq $role) {
 6709:                     $selected = ' selected="selected"';
 6710:                 } else {
 6711:                     $selected = '';
 6712:                 }
 6713:             }
 6714:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6715:                        &Apache::lonnet::plaintext($role,'Course').
 6716:                        '</option>';
 6717:         }
 6718:         $output .= '</select></td>';
 6719:     }
 6720:     $output .= '</tr></table></fieldset>'.
 6721:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6722:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6723:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6724:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6725:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6726:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6727:                '</fieldset>'.
 6728:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6729:     foreach my $lticrsrole (@lticourseroles) {
 6730:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6731:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6732:     }
 6733:     $output .= '</fieldset>'.
 6734:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6735:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6736:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6737:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6738:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6739:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6740:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6741:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6742:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6743:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6744:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6745:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6746:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6747:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6748:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6749:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6750:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6751:     foreach my $extra ('roster','passback') {
 6752:         my $checkedon = '';
 6753:         my $checkedoff = ' checked="checked"';
 6754:         if ($extra eq 'passback') {
 6755:             $pb1p1chk = ' checked="checked"';
 6756:             $pb1p0chk = '';
 6757:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6758:         } else {
 6759:             $onclickpb = '';
 6760:         }
 6761:         if (ref($current) eq 'HASH') {
 6762:             if (($current->{$extra})) {
 6763:                 $checkedon = $checkedoff;
 6764:                 $checkedoff = '';
 6765:                 if ($extra eq 'passback') {
 6766:                     $passbacksty = 'inline-block';
 6767:                 }
 6768:                 if ($current->{'passbackformat'} eq '1.0') {
 6769:                     $pb1p0chk =  ' checked="checked"';
 6770:                     $pb1p1chk = '';
 6771:                 }
 6772:             }
 6773:         }
 6774:         $output .= $lt{$extra}.'&nbsp;'.
 6775:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6776:                    &mt('No').'</label>'.('&nbsp;'x2).
 6777:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6778:                    &mt('Yes').'</label><br />';
 6779:     }
 6780:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6781:                '<span class="LC_nobreak">'.&mt('Grade format').
 6782:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6783:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6784:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6785:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6786:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6787:     $output .= '</span></div></fieldset>';
 6788: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6789: #
 6790: #    $output .= '</fieldset>'.
 6791: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6792:     return $output;
 6793: }
 6794: 
 6795: sub ltimenu_titles {
 6796:     return &Apache::lonlocal::texthash(
 6797:                                         fullname    => 'Full name',
 6798:                                         coursetitle => 'Course title',
 6799:                                         role        => 'Role',
 6800:                                         logout      => 'Logout',
 6801:                                         grades      => 'Grades',
 6802:     );
 6803: }
 6804: 
 6805: sub linkprot_suggestions {
 6806:     my ($suggested,$itemcount) = @_;
 6807:     my $count = 0;
 6808:     my $next = 1;
 6809:     my %lt = &Apache::lonlocal::texthash(
 6810:                                           'name' => 'Suggested Launcher',
 6811:                                           'info' => 'Recommendations',
 6812:                                         );
 6813:     my ($datatable,$css_class,$dest);
 6814:     if (ref($suggested) eq 'HASH') {
 6815:         my @current = sort { $a <=> $b } keys(%{$suggested});
 6816:         $next += $current[-1];
 6817:         for (my $i=0; $i<@current; $i++) {
 6818:             my $num = $current[$i];
 6819:             my %values;
 6820:             if (ref($suggested->{$num}) eq 'HASH') {
 6821:                 %values = %{$suggested->{$num}};
 6822:             } else {
 6823:                 next;
 6824:             }
 6825:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6826:             $datatable .=
 6827:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6828:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 6829:                 &mt('Delete?').'</label></span></td><td>'."\n".
 6830:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6831:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 6832:                 '</fieldset></div>'.
 6833:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6834:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 6835:                 '</fieldset></div>'.
 6836:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6837:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 6838:             $$itemcount ++;
 6839:         }
 6840:     }
 6841:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6842:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6843:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 6844:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6845:                   '<td>'."\n".
 6846:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6847:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 6848:                   '</fieldset></div>'.
 6849:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6850:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 6851:                   '</fieldset></div>'.
 6852:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6853:                   '</td></tr>'."\n";
 6854:     return $datatable;
 6855: }
 6856: 
 6857: sub print_coursedefaults {
 6858:     my ($position,$dom,$settings,$rowtotal) = @_;
 6859:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6860:     my $itemcount = 1;
 6861:     my %choices =  &Apache::lonlocal::texthash (
 6862:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6863:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6864:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course (MB)',
 6865:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6866:         coursecredits        => 'Credits can be specified for courses',
 6867:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6868:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6869:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6870:         texengine            => 'Default method to display mathematics',
 6871:         postsubmit           => 'Disable submit button/keypress following student submission',
 6872:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6873:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6874:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6875:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6876:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6877:         crsauthor            => 'Standard LON-CAPA problems can be created within a course/community (by type)',
 6878:         crseditors           => 'Available editors for web pages and/or problems created in a course/community',
 6879:     );
 6880:     my %staticdefaults = (
 6881:                            anonsurvey_threshold => 10,
 6882:                            uploadquota          => 500,
 6883:                            coursequota          => 20,
 6884:                            postsubmit           => 60,
 6885:                            mysqltables          => 172800,
 6886:                            domexttool           => 1,
 6887:                            exttool              => 0,
 6888:                            crsauthor            => 1,
 6889:                            crseditors           => ['edit','xml'],  
 6890:                          );
 6891:     if ($position eq 'top') {
 6892:         %defaultchecked = (
 6893:                             'canuse_pdfforms' => 'off',
 6894:                             'uselcmath'       => 'on',
 6895:                             'usejsme'         => 'on',
 6896:                             'inline_chem'     => 'on',
 6897:                             'canclone'        => 'none',
 6898:                           );
 6899:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6900:         my $deftex = $Apache::lonnet::deftex;
 6901:         if (ref($settings) eq 'HASH') {
 6902:             if ($settings->{'texengine'}) {
 6903:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6904:                     $deftex = $settings->{'texengine'};
 6905:                 }
 6906:             }
 6907:         }
 6908:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6909:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6910:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6911:                        '</span></td><td class="LC_right_item">'.
 6912:                        '<select name="texengine">'."\n";
 6913:         my %texoptions = (
 6914:                             MathJax  => 'MathJax',
 6915:                             mimetex  => &mt('Convert to Images'),
 6916:                             tth      => &mt('TeX to HTML'),
 6917:                          );
 6918:         foreach my $renderer ('MathJax','mimetex','tth') {
 6919:             my $selected = '';
 6920:             if ($renderer eq $deftex) {
 6921:                 $selected = ' selected="selected"';
 6922:             }
 6923:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6924:         }
 6925:         $mathdisp .= '</select></td></tr>'."\n";
 6926:         $itemcount ++;
 6927:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6928:                                                      \%choices,$itemcount);
 6929:         $datatable = $mathdisp.$datatable;
 6930:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6931:         $datatable .=
 6932:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6933:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6934:             '</span></td><td class="LC_left_item">';
 6935:         my $currcanclone = 'none';
 6936:         my $onclick;
 6937:         my @cloneoptions = ('none','domain');
 6938:         my %clonetitles = &Apache::lonlocal::texthash (
 6939:                              none     => 'No additional course requesters',
 6940:                              domain   => "Any course requester in course's domain",
 6941:                              instcode => 'Course requests for official courses ...',
 6942:                           );
 6943:         my (%codedefaults,@code_order,@posscodes);
 6944:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6945:                                                     \@code_order) eq 'ok') {
 6946:             if (@code_order > 0) {
 6947:                 push(@cloneoptions,'instcode');
 6948:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6949:             }
 6950:         }
 6951:         if (ref($settings) eq 'HASH') {
 6952:             if ($settings->{'canclone'}) {
 6953:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6954:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6955:                         if (@code_order > 0) {
 6956:                             $currcanclone = 'instcode';
 6957:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6958:                         }
 6959:                     }
 6960:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6961:                     $currcanclone = $settings->{'canclone'};
 6962:                 }
 6963:             }
 6964:         }
 6965:         foreach my $option (@cloneoptions) {
 6966:             my ($checked,$additional);
 6967:             if ($currcanclone eq $option) {
 6968:                 $checked = ' checked="checked"';
 6969:             }
 6970:             if ($option eq 'instcode') {
 6971:                 if (@code_order) {
 6972:                     my $show = 'none';
 6973:                     if ($checked) {
 6974:                         $show = 'block';
 6975:                     }
 6976:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6977:                                   &mt('Institutional codes for new and cloned course have identical:').
 6978:                                   '<br />';
 6979:                     foreach my $item (@code_order) {
 6980:                         my $codechk;
 6981:                         if ($checked) {
 6982:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6983:                                 $codechk = ' checked="checked"';
 6984:                             }
 6985:                         }
 6986:                         $additional .= '<label>'.
 6987:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6988:                                        $item.'</label>';
 6989:                     }
 6990:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6991:                 }
 6992:             }
 6993:             $datatable .=
 6994:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6995:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6996:                 '</label>&nbsp;'.$additional.'</span><br />';
 6997:         }
 6998:         $datatable .= '</td>'.
 6999:                       '</tr>';
 7000:         $itemcount ++;
 7001:     } else {
 7002:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7003:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 7004:             %deftimeout,%currmysql);
 7005:         my $currusecredits = 0;
 7006:         my $postsubmitclient = 1;
 7007:         my $ltiauth = 0;
 7008:         my %domexttool;
 7009:         my %exttool;
 7010:         my %crsauthor;
 7011:         my %crseditors;
 7012:         my @types = ('official','unofficial','community','textbook','placement');
 7013:         if (ref($settings) eq 'HASH') {
 7014:             if ($settings->{'ltiauth'}) {
 7015:                 $ltiauth = 1;
 7016:             }
 7017:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 7018:                 foreach my $type (@types) {
 7019:                     if ($settings->{'domexttool'}->{$type}) {
 7020:                         $domexttool{$type} = ' checked="checked"';
 7021:                     }
 7022:                 }
 7023:             } else {
 7024:                 foreach my $type (@types) {
 7025:                     if ($staticdefaults{'domexttool'}) {
 7026:                         $domexttool{$type} = ' checked="checked"';
 7027:                     }
 7028:                 }
 7029:             }
 7030:             if (ref($settings->{'exttool'}) eq 'HASH') {
 7031:                 foreach my $type (@types) {
 7032:                     if ($settings->{'exttool'}->{$type}) {
 7033:                         $exttool{$type} = ' checked="checked"';
 7034:                     }
 7035:                 }
 7036:             }
 7037:             if (ref($settings->{'crsauthor'}) eq 'HASH') {
 7038:                 foreach my $type (@types) {
 7039:                     if ($settings->{'crsauthor'}->{$type}) {
 7040:                         $crsauthor{$type} = ' checked="checked"';
 7041:                     }
 7042:                 }
 7043:             } else {
 7044:                 foreach my $type (@types) {
 7045:                     if ($staticdefaults{'crsauthor'}) {
 7046:                         $crsauthor{$type} = ' checked="checked"';
 7047:                     }
 7048:                 }
 7049:             }
 7050:             if (ref($settings->{'crseditors'}) eq 'ARRAY') {
 7051:                 foreach my $editor (@{$settings->{'crseditors'}}) {
 7052:                     $crseditors{$editor} = ' checked="checked"';
 7053:                 }
 7054:             } else {
 7055:                 foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7056:                     $crseditors{$editor} = ' checked="checked"';
 7057:                 }
 7058:             }
 7059:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 7060:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 7061:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 7062:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 7063:                 }
 7064:             }
 7065:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 7066:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 7067:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 7068:                 }
 7069:             }
 7070:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 7071:                 foreach my $type (@types) {
 7072:                     next if ($type eq 'community');
 7073:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 7074:                     if ($defcredits{$type} ne '') {
 7075:                         $currusecredits = 1;
 7076:                     }
 7077:                 }
 7078:             }
 7079:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 7080:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 7081:                     $postsubmitclient = 0;
 7082:                     foreach my $type (@types) {
 7083:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7084:                     }
 7085:                 } else {
 7086:                     foreach my $type (@types) {
 7087:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 7088:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 7089:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 7090:                             } else {
 7091:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7092:                             }
 7093:                         } else {
 7094:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7095:                         }
 7096:                     }
 7097:                 }
 7098:             } else {
 7099:                 foreach my $type (@types) {
 7100:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7101:                 }
 7102:             }
 7103:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 7104:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 7105:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 7106:                 }
 7107:             } else {
 7108:                 foreach my $type (@types) {
 7109:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7110:                 }
 7111:             }
 7112:         } else {
 7113:             foreach my $type (@types) {
 7114:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7115:                 if ($staticdefaults{'domexttool'}) {
 7116:                     $domexttool{$type} = ' checked="checked"';
 7117:                 }
 7118:                 if ($staticdefaults{'crsauthor'}) {
 7119:                     $crsauthor{$type} = ' checked="checked"';
 7120:                 }
 7121:             }
 7122:             foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7123:                 $crseditors{$editor} = ' checked="checked"';
 7124:             }
 7125:         }
 7126:         if (!$currdefresponder) {
 7127:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7128:         } elsif ($currdefresponder < 1) {
 7129:             $currdefresponder = 1;
 7130:         }
 7131:         foreach my $type (@types) {
 7132:             if ($curruploadquota{$type} eq '') {
 7133:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7134:             }
 7135:             if ($currcoursequota{$type} eq '') {
 7136:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 7137:             }
 7138:         }
 7139:         $datatable .=
 7140:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7141:                 $choices{'anonsurvey_threshold'}.
 7142:                 '</span></td>'.
 7143:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7144:                 '<input type="text" name="anonsurvey_threshold"'.
 7145:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7146:                 '</td></tr>'."\n";
 7147:         $itemcount ++;
 7148:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7149:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7150:                       $choices{'uploadquota'}.
 7151:                       '</span></td>'.
 7152:                       '<td style="text-align: right" class="LC_right_item">'.
 7153:                       '<table><tr>';
 7154:         foreach my $type (@types) {
 7155:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7156:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7157:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7158:         }
 7159:         $datatable .= '</tr></table></td></tr>'."\n";
 7160:         $itemcount ++;
 7161:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7162:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7163:                       $choices{'coursequota'}.
 7164:                       '</span></td>'.
 7165:                       '<td style="text-align: right" class="LC_right_item">'.
 7166:                       '<table><tr>';
 7167:         foreach my $type (@types) {
 7168:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7169:                            '<input type="text" name="coursequota_'.$type.'"'.
 7170:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 7171:         }
 7172:         $datatable .= '</tr></table></td></tr>'."\n";
 7173:         $itemcount ++;
 7174:         my $onclick = "toggleDisplay(this.form,'credits');";
 7175:         my $display = 'none';
 7176:         if ($currusecredits) {
 7177:             $display = 'block';
 7178:         }
 7179:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7180:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7181:         foreach my $type (@types) {
 7182:             next if ($type eq 'community');
 7183:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7184:                            '<input type="text" name="'.$type.'_credits"'.
 7185:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7186:         }
 7187:         $additional .= '</tr></table></div>'."\n";
 7188:         %defaultchecked = ('coursecredits' => 'off');
 7189:         @toggles = ('coursecredits');
 7190:         my $current = {
 7191:                         'coursecredits' => $currusecredits,
 7192:                       };
 7193:         (my $table,$itemcount) =
 7194:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7195:                                \%choices,$itemcount,$onclick,$additional,'left');
 7196:         $datatable .= $table;
 7197:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7198:         my $display = 'none';
 7199:         if ($postsubmitclient) {
 7200:             $display = 'block';
 7201:         }
 7202:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7203:                       &mt('Number of seconds submit is disabled').'<br />'.
 7204:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7205:                       '<table><tr>';
 7206:         foreach my $type (@types) {
 7207:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7208:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7209:                            $deftimeout{$type}.'" size="5" /></td>';
 7210:         }
 7211:         $additional .= '</tr></table></div>'."\n";
 7212:         %defaultchecked = ('postsubmit' => 'on');
 7213:         @toggles = ('postsubmit');
 7214:         $current = {
 7215:                        'postsubmit' => $postsubmitclient,
 7216:                    };
 7217:         ($table,$itemcount) =
 7218:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7219:                                \%choices,$itemcount,$onclick,$additional,'left');
 7220:         $datatable .= $table;
 7221:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7222:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7223:                       $choices{'mysqltables'}.
 7224:                       '</span></td>'.
 7225:                       '<td style="text-align: right" class="LC_right_item">'.
 7226:                       '<table><tr>';
 7227:         foreach my $type (@types) {
 7228:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7229:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7230:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7231:         }
 7232:         $datatable .= '</tr></table></td></tr>'."\n";
 7233:         $itemcount ++;
 7234:         %defaultchecked = ('ltiauth' => 'off');
 7235:         @toggles = ('ltiauth');
 7236:         $current = {
 7237:                        'ltiauth' => $ltiauth,
 7238:                    };
 7239:         ($table,$itemcount) =
 7240:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7241:                                \%choices,$itemcount,undef,undef,'left');
 7242:         $datatable .= $table;
 7243:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7244:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7245:                       $choices{'domexttool'}.
 7246:                       '</span></td>'.
 7247:                       '<td style="text-align: right" class="LC_right_item">'.
 7248:                       '<table><tr>';
 7249:         foreach my $type (@types) {
 7250:             $datatable .= '<td style="text-align: left">'.
 7251:                           '<span class="LC_nobreak">'.
 7252:                           '<label><input type="checkbox" name="domexttool"'.
 7253:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7254:                           &mt($type).'</label></span></td>'."\n";
 7255:         }
 7256:         $datatable .= '</tr></table></td></tr>'."\n";
 7257:         $itemcount ++;
 7258:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7259:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7260:                       $choices{'exttool'}.
 7261:                       '</span></td>'.
 7262:                       '<td style="text-align: right" class="LC_right_item">'.
 7263:                       '<table><tr>';
 7264:         foreach my $type (@types) {
 7265:             $datatable .= '<td style="text-align: left">'.
 7266:                           '<span class="LC_nobreak">'.
 7267:                           '<label><input type="checkbox" name="exttool"'.
 7268:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7269:                           &mt($type).'</label></span></td>'."\n";
 7270:         }
 7271:         $datatable .= '</tr></table></td></tr>'."\n";
 7272:         $itemcount ++;
 7273:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7274:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7275:                       $choices{'crsauthor'}.
 7276:                       '</span></td>'.
 7277:                       '<td style="text-align: right" class="LC_right_item">'.
 7278:                       '<table><tr>';
 7279:         foreach my $type (@types) {
 7280:             $datatable .= '<td style="text-align: left">'.
 7281:                           '<span class="LC_nobreak">'.
 7282:                           '<label><input type="checkbox" name="crsauthor"'.
 7283:                           ' value="'.$type.'"'.$crsauthor{$type}.' />'.
 7284:                           &mt($type).'</label></span></td>'."\n";
 7285:         }
 7286:         $datatable .= '</tr></table></td></tr>'."\n";
 7287:         $itemcount ++;
 7288:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7289:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7290:                       $choices{'crseditors'}.
 7291:                       '</span></td>'.
 7292:                       '<td style="text-align: right" class="LC_right_item">'.
 7293:                       '<table><tr>';
 7294:         my @editors = ('edit','xml','daxe');
 7295:         my %editornames = &crseditor_titles();
 7296:         foreach my $editor (@editors) {
 7297:             $datatable .= '<td style="text-align: left">'.
 7298:                           '<span class="LC_nobreak">'.
 7299:                           '<label><input type="checkbox" name="crseditors"'.
 7300:                           ' value="'.$editor.'"'.$crseditors{$editor}.' />'.
 7301:                           $editornames{$editor}.'</label></span></td>'."\n";
 7302:         }
 7303:         $datatable .= '</tr></table></td></tr>'."\n";
 7304:     }
 7305:     $$rowtotal += $itemcount;
 7306:     return $datatable;
 7307: }
 7308: 
 7309: sub crseditor_titles {
 7310:     return &Apache::lonlocal::texthash(
 7311:                edit  => 'Standard editor (Edit)',
 7312:                xml   => 'Text editor (EditXML)',
 7313:                daxe  => 'Daxe editor (Daxe)',
 7314:            );
 7315: }
 7316: 
 7317: sub print_authordefaults {
 7318:     my ($position,$dom,$settings,$rowtotal) = @_;
 7319:     my ($css_class,$datatable,%checkedon,%checkedoff);
 7320:     my $itemcount = 1;
 7321:     my %titles = &authordefaults_titles();
 7322:     if ($position eq 'top') {
 7323:         my %defaultchecked = (
 7324:                             'nocodemirror' => 'off',
 7325:                             'daxecollapse' => 'off',
 7326:                             'domcoordacc'  => 'on',
 7327:                           );
 7328:         my @toggles = ('nocodemirror','daxecollapse','domcoordacc');
 7329:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 7330:                                                      \%titles,$itemcount);
 7331:         my %staticdefaults = (
 7332:                                 'copyright'    => 'default',
 7333:                                 'sourceavail'  => 'closed',
 7334:                              );
 7335:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7336:         my %currrights;
 7337:         foreach my $item ('copyright','sourceavail') {
 7338:             $currrights{$item} = $staticdefaults{$item};
 7339:             if (ref($settings) eq 'HASH') {
 7340:                 if (exists($settings->{$item})) {
 7341:                     $currrights{$item} = $settings->{$item};
 7342:                 }
 7343:             }
 7344:         }
 7345:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7346:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 7347:                       '</span></td><td class="LC_right_item">'.
 7348:                       &selectbox('copyright',$currrights{'copyright'},'',
 7349:                                  \&Apache::loncommon::copyrightdescription,
 7350:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 7351:                       '</td></tr>'."\n";
 7352:         $itemcount ++;
 7353:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7354:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7355:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 7356:                       '</span></td><td class="LC_right_item">'.
 7357:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 7358:                                  \&Apache::loncommon::source_copyrightdescription,
 7359:                                  (&Apache::loncommon::source_copyrightids)).
 7360:                       '</td></tr>'."\n";        
 7361:     } else {
 7362:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7363:         my $curreditors;
 7364:         my %staticdefaults = (
 7365:                                 editors => ['edit','xml'],
 7366:                                 authorquota => 500,
 7367:                                 webdav => 0,
 7368:                              );
 7369:         my $curreditors = $staticdefaults{'editors'};
 7370:         if ((ref($settings) eq 'HASH') &&
 7371:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 7372:             $curreditors = $settings->{'editors'};
 7373:         } else {
 7374:             $curreditors = $staticdefaults{'editors'};
 7375:         }
 7376:         my @editors = ('edit','xml','daxe');
 7377:         $datatable = '<tr'.$css_class.'>'."\n".
 7378:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 7379:                      '<td class="LC_left_item">'."\n".
 7380:                      '<span class="LC_nobreak">';
 7381:         foreach my $editor (@editors) {
 7382:             my $checked;
 7383:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 7384:                 $checked = ' checked="checked"';
 7385:             }
 7386:             $datatable .= '<label>'.
 7387:                           '<input type="checkbox" name="author_editors" '.
 7388:                           $checked.' value="'.$editor.'" '.
 7389:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 7390:                           $titles{$editor}.'</label>&nbsp;';
 7391:         }
 7392:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 7393:         $itemcount ++;
 7394:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7395:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7396:         my @insttypes;
 7397:         if (ref($types) eq 'ARRAY') {
 7398:             @insttypes = @{$types};
 7399:         }
 7400:         my $typecount = 0;
 7401:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 7402:         my @items = ('webdav','authorquota');
 7403:         my %quotas;
 7404:         if (ref($domconf{'quotas'}) eq 'HASH') {
 7405:             %quotas = %{$domconf{'quotas'}};
 7406:             foreach my $item (@items) {
 7407:                 if (ref($quotas{$item}) eq 'HASH') {
 7408:                     foreach my $type (@insttypes,'default') {
 7409:                         if ($item eq 'authorquota') {
 7410:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 7411:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7412:                             }
 7413:                         } elsif ($item eq 'webdav') {
 7414:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 7415:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7416:                             }
 7417:                         }
 7418:                     }
 7419:                 } else {
 7420:                     foreach my $type (@insttypes,'default') {
 7421:                         $quotas{$item}{$type} = $staticdefaults{$item};
 7422:                     }
 7423:                 }
 7424:             }
 7425:         } else {
 7426:             foreach my $item (@items) {
 7427:                 foreach my $type (@insttypes,'default') {
 7428:                     $quotas{$item}{$type} = $staticdefaults{$item};
 7429:                 }
 7430:             }
 7431:         }
 7432:         if (ref($usertypes) eq 'HASH') {
 7433:             my $numinrow = 4;
 7434:             my $onclick = '';
 7435:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7436:                                          $numinrow,$othertitle,'authorquota',
 7437:                                          \$itemcount,$onclick);
 7438:             $itemcount ++;
 7439:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7440:                                          $numinrow,$othertitle,'webdav',
 7441:                                          \$itemcount);
 7442:             $itemcount ++;
 7443:         }
 7444:         my $checkedno = ' checked="checked"';
 7445:         my ($checkedon,$checkedoff);
 7446:         if (ref($quotas{'webdav'}) eq 'HASH') {
 7447:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) {
 7448:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 7449:                     $checkedon = $checkedno;
 7450:                 } else {
 7451:                     $checkedoff = $checkedno;
 7452:                 }
 7453:                 undef($checkedno);
 7454:             }
 7455:         }
 7456:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7457:         $datatable .= '<tr'.$css_class.'>'.
 7458:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 7459:                              $titles{'webdav_LC_adv_over'}.
 7460:                       '</td>'.
 7461:                       '<td class="LC_left_item">';
 7462:         foreach my $option ('none','off','on') {
 7463:             my ($text,$val,$checked);
 7464:             if ($option eq 'none') {
 7465:                 $text = $titles{'none'};
 7466:                 $val = '';
 7467:                 $checked = $checkedno;
 7468:             } elsif ($option eq 'off') {
 7469:                 $text = $titles{'overoff'};
 7470:                 $val = 0;
 7471:                 $checked = $checkedoff;
 7472:             } elsif ($option eq 'on') {
 7473:                 $text = $titles{'overon'};
 7474:                 $val = 1;
 7475:                 $checked = $checkedon;
 7476:             }
 7477:             $datatable .= '<span class="LC_nobreak"><label>'.
 7478:                           '<input type="radio" name="webdav_LC_adv"'.
 7479:                           ' value="'.$val.'"'.$checked.' />'.
 7480:                           $text.'</label></span>&nbsp; ';
 7481:         }
 7482:         $datatable .= '</td></tr>';
 7483:         $itemcount ++;
 7484:         my %defchecked = (
 7485:                                 'archive' => 'off',
 7486:                              );
 7487:         my @toggles = ('archive');
 7488:         (my $archive,$itemcount) = &radiobutton_prefs($settings,['archive'],
 7489:                                                       {'archive' => 'off'},
 7490:                                                       \%titles,$itemcount);
 7491:         $datatable .= $archive."\n";
 7492:         $itemcount ++;
 7493:     }
 7494:     $$rowtotal += $itemcount;
 7495:     return $datatable;
 7496: }
 7497: 
 7498: sub authordefaults_titles {
 7499:     return &Apache::lonlocal::texthash(
 7500:                copyright => 'Copyright/Distribution',
 7501:                sourceavail => ' Source Available',
 7502:                editors => 'Available Editors',
 7503:                webdav => 'WebDAV',
 7504:                authorquota => 'Authoring Space quotas (MB)',
 7505:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 7506:                daxecollapse => 'Daxe editor: LON-CAPA standard menus start collapsed',
 7507:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 7508:                edit  => 'Standard editor (Edit)',
 7509:                xml   => 'Text editor (EditXML)',
 7510:                daxe  => 'Daxe editor (Daxe)',
 7511:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 7512:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 7513:                none => 'No override set',
 7514:                overon => 'Override -- webDAV on',
 7515:                overoff => 'Override -- webDAV off',
 7516:                archive => 'Authors can download tar.gz file of Authoring Space',
 7517:     );
 7518: }
 7519: 
 7520: sub selectbox {
 7521:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 7522:     my $selout = '<select name="'.$name.'">';
 7523:     foreach my $id (@idlist) {
 7524:         $selout.='<option value="'.$id.'"';
 7525:         if ($id eq $value) {
 7526:             $selout.=' selected="selected"';
 7527:         }
 7528:         if ($readonly) {
 7529:             $selout .= ' disabled="disabled"';
 7530:         }
 7531:         $selout.='>'.&{$functionref}($id).'</option>';
 7532:     }
 7533:     $selout.='</select>';
 7534:     return $selout;
 7535: }
 7536: 
 7537: sub print_selfenrollment {
 7538:     my ($position,$dom,$settings,$rowtotal) = @_;
 7539:     my ($css_class,$datatable);
 7540:     my $itemcount = 1;
 7541:     my @types = ('official','unofficial','community','textbook','placement');
 7542:     if (($position eq 'top') || ($position eq 'middle')) {
 7543:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7544:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7545:         my @rows;
 7546:         my $key;
 7547:         if ($position eq 'top') {
 7548:             $key = 'admin'; 
 7549:             if (ref($rowsref) eq 'ARRAY') {
 7550:                 @rows = @{$rowsref};
 7551:             }
 7552:         } elsif ($position eq 'middle') {
 7553:             $key = 'default';
 7554:             @rows = ('types','registered','approval','limit');
 7555:         }
 7556:         foreach my $row (@rows) {
 7557:             if (defined($titlesref->{$row})) {
 7558:                 $itemcount ++;
 7559:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7560:                 $datatable .= '<tr'.$css_class.'>'.
 7561:                               '<td>'.$titlesref->{$row}.'</td>'.
 7562:                               '<td class="LC_left_item">'.
 7563:                               '<table><tr>';
 7564:                 my (%current,%currentcap);
 7565:                 if (ref($settings) eq 'HASH') {
 7566:                     if (ref($settings->{$key}) eq 'HASH') {
 7567:                         foreach my $type (@types) {
 7568:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7569:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7570:                             }
 7571:                             if (($row eq 'limit') && ($key eq 'default')) {
 7572:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7573:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7574:                                 }
 7575:                             }
 7576:                         }
 7577:                     }
 7578:                 }
 7579:                 my %roles = (
 7580:                              '0' => &Apache::lonnet::plaintext('dc'),
 7581:                             ); 
 7582:             
 7583:                 foreach my $type (@types) {
 7584:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7585:                         $datatable .= '<th>'.&mt($type).'</th>';
 7586:                     }
 7587:                 }
 7588:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7589:                     $datatable .= '</tr><tr>';
 7590:                 }
 7591:                 foreach my $type (@types) {
 7592:                     if ($type eq 'community') {
 7593:                         $roles{'1'} = &mt('Community personnel');
 7594:                     } else {
 7595:                         $roles{'1'} = &mt('Course personnel');
 7596:                     }
 7597:                     $datatable .= '<td style="vertical-align: top">';
 7598:                     if ($position eq 'top') {
 7599:                         my %checked;
 7600:                         if ($current{$type} eq '0') {
 7601:                             $checked{'0'} = ' checked="checked"';
 7602:                         } else {
 7603:                             $checked{'1'} = ' checked="checked"';
 7604:                         }
 7605:                         foreach my $role ('1','0') {
 7606:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7607:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7608:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7609:                                           $roles{$role}.'</label></span> ';
 7610:                         }
 7611:                     } else {
 7612:                         if ($row eq 'types') {
 7613:                             my %checked;
 7614:                             if ($current{$type} =~ /^(all|dom)$/) {
 7615:                                 $checked{$1} = ' checked="checked"';
 7616:                             } else {
 7617:                                 $checked{''} = ' checked="checked"';
 7618:                             }
 7619:                             foreach my $val ('','dom','all') {
 7620:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7621:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7622:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7623:                             }
 7624:                         } elsif ($row eq 'registered') {
 7625:                             my %checked;
 7626:                             if ($current{$type} eq '1') {
 7627:                                 $checked{'1'} = ' checked="checked"';
 7628:                             } else {
 7629:                                 $checked{'0'} = ' checked="checked"';
 7630:                             }
 7631:                             foreach my $val ('0','1') {
 7632:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7633:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7634:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7635:                             }
 7636:                         } elsif ($row eq 'approval') {
 7637:                             my %checked;
 7638:                             if ($current{$type} =~ /^([12])$/) {
 7639:                                 $checked{$1} = ' checked="checked"';
 7640:                             } else {
 7641:                                 $checked{'0'} = ' checked="checked"';
 7642:                             }
 7643:                             for my $val (0..2) {
 7644:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7645:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7646:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7647:                             }
 7648:                         } elsif ($row eq 'limit') {
 7649:                             my %checked;
 7650:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7651:                                 $checked{$1} = ' checked="checked"';
 7652:                             } else {
 7653:                                 $checked{'none'} = ' checked="checked"';
 7654:                             }
 7655:                             my $cap;
 7656:                             if ($currentcap{$type} =~ /^\d+$/) {
 7657:                                 $cap = $currentcap{$type};
 7658:                             }
 7659:                             foreach my $val ('none','allstudents','selfenrolled') {
 7660:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7661:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7662:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7663:                             }
 7664:                             $datatable .= '<br />'.
 7665:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7666:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7667:                                           '</span>'; 
 7668:                         }
 7669:                     }
 7670:                     $datatable .= '</td>';
 7671:                 }
 7672:                 $datatable .= '</tr>';
 7673:             }
 7674:             $datatable .= '</table></td></tr>';
 7675:         }
 7676:     } elsif ($position eq 'bottom') {
 7677:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7678:     }
 7679:     $$rowtotal += $itemcount;
 7680:     return $datatable;
 7681: }
 7682: 
 7683: sub print_validation_rows {
 7684:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7685:     my ($itemsref,$namesref,$fieldsref);
 7686:     if ($caller eq 'selfenroll') { 
 7687:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7688:     } elsif ($caller eq 'requestcourses') {
 7689:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7690:     }
 7691:     my %currvalidation;
 7692:     if (ref($settings) eq 'HASH') {
 7693:         if (ref($settings->{'validation'}) eq 'HASH') {
 7694:             %currvalidation = %{$settings->{'validation'}};
 7695:         }
 7696:     }
 7697:     my $datatable;
 7698:     my $itemcount = 0;
 7699:     foreach my $item (@{$itemsref}) {
 7700:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7701:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7702:                       $namesref->{$item}.
 7703:                       '</span></td>'.
 7704:                       '<td class="LC_left_item">';
 7705:         if (($item eq 'url') || ($item eq 'button')) {
 7706:             $datatable .= '<span class="LC_nobreak">'.
 7707:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7708:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7709:         } elsif ($item eq 'fields') {
 7710:             my @currfields;
 7711:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7712:                 @currfields = @{$currvalidation{$item}};
 7713:             }
 7714:             foreach my $field (@{$fieldsref}) {
 7715:                 my $check = '';
 7716:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7717:                     $check = ' checked="checked"';
 7718:                 }
 7719:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7720:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7721:                               ' value="'.$field.'"'.$check.' />'.$field.
 7722:                               '</label></span> ';
 7723:             }
 7724:         } elsif ($item eq 'markup') {
 7725:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7726:                            $currvalidation{$item}.
 7727:                               '</textarea>';
 7728:         }
 7729:         $datatable .= '</td></tr>'."\n";
 7730:         if (ref($rowtotal)) {
 7731:             $itemcount ++;
 7732:         }
 7733:     }
 7734:     if ($caller eq 'requestcourses') {
 7735:         my %currhash;
 7736:         if (ref($settings) eq 'HASH') {
 7737:             if (ref($settings->{'validation'}) eq 'HASH') {
 7738:                 if ($settings->{'validation'}{'dc'} ne '') {
 7739:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7740:                 }
 7741:             }
 7742:         }
 7743:         my $numinrow = 2;
 7744:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7745:                                                        'validationdc',%currhash);
 7746:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7747:         $datatable .= '<tr'.$css_class.'><td>';
 7748:         if ($numdc > 1) {
 7749:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7750:         } else {
 7751:             $datatable .=  &mt('Course creation processed as: ');
 7752:         }
 7753:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7754:         $itemcount ++;
 7755:     }
 7756:     if (ref($rowtotal)) {
 7757:         $$rowtotal += $itemcount;
 7758:     }
 7759:     return $datatable;
 7760: }
 7761: 
 7762: sub print_privacy {
 7763:     my ($position,$dom,$settings,$rowtotal) = @_;
 7764:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7765:     my $itemcount = 0;
 7766:     if ($position eq 'top') {
 7767:         $numinrow = 2;
 7768:     } else {
 7769:         @items = ('domain','author','course','community');
 7770:         %names = &Apache::lonlocal::texthash (
 7771:                      domain => 'Assigned domain role(s)',
 7772:                      author => 'Assigned co-author role(s)',
 7773:                      course => 'Assigned course role(s)',
 7774:                      community => 'Assigned community role(s)',
 7775:                  );
 7776:         $numinrow = 4;
 7777:         ($othertitle,$usertypes,$types) =
 7778:             &Apache::loncommon::sorted_inst_types($dom);
 7779:     }
 7780:     if (($position eq 'top') || ($position eq 'middle')) {
 7781:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7782:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7783:         if ($position eq 'top') {
 7784:             my %curr;
 7785:             my @options = ('none','user','domain','auto');
 7786:             my %titles = &Apache::lonlocal::texthash (
 7787:                 none   => 'Not allowed',
 7788:                 user   => 'User authorizes',
 7789:                 domain => 'DC authorizes',
 7790:                 auto   => 'Unrestricted',
 7791:                 instdom => 'Other domain shares institution/provider',
 7792:                 extdom => 'Other domain has different institution/provider',
 7793:                 notify => 'Notify when role needs authorization',
 7794:             );
 7795:             my %names = &Apache::lonlocal::texthash (
 7796:                 domain => 'Domain role',
 7797:                 author => 'Co-author role',
 7798:                 course => 'Course role',
 7799:                 community => 'Community role',
 7800:             );
 7801:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7802:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7803:             foreach my $domtype ('instdom','extdom') {
 7804:                 my (%checked,$skip);
 7805:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7806:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7807:                               '<td class="LC_left_item">';
 7808:                 if ($domtype eq 'instdom') {
 7809:                     unless (@instdoms > 1) {
 7810:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7811:                         $skip = 1;
 7812:                     }
 7813:                 } elsif ($domtype eq 'extdom') {
 7814:                     if (keys(%by_location) == 0) {
 7815:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7816:                         $skip = 1;
 7817:                     }
 7818:                 }
 7819:                 unless ($skip) {
 7820:                     foreach my $roletype ('domain','author','course','community') {
 7821:                         $checked{'auto'} = ' checked="checked"';
 7822:                         if (ref($settings) eq 'HASH') {
 7823:                             if (ref($settings->{approval}) eq 'HASH') {
 7824:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7825:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7826:                                         $checked{$1} = ' checked="checked"';
 7827:                                         $checked{'auto'} = '';
 7828:                                     }
 7829:                                 }
 7830:                             }
 7831:                         }
 7832:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7833:                         foreach my $option (@options) {
 7834:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7835:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7836:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7837:                                           '</label></span>&nbsp; ';
 7838:                         }
 7839:                         $datatable .= '</fieldset>';
 7840:                     }
 7841:                 }
 7842:                 $datatable .= '</td></tr>';
 7843:                 $itemcount ++;
 7844:             }
 7845:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7846:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7847:                           '<td class="LC_left_item">';
 7848:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7849:                 my %curr;
 7850:                 if (ref($settings) eq 'HASH') {
 7851:                     if ($settings->{'notify'} ne '') {
 7852:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7853:                     }
 7854:                 }
 7855:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7856:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7857:                                                              'privacy_notify',%curr);
 7858:                 if ($numdc > 0) {
 7859:                     $datatable .= $table;
 7860:                 } else {
 7861:                     $datatable .= &mt('There are no active Domain Coordinators');
 7862:                 }
 7863:             } else {
 7864:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7865:             }
 7866:             $datatable .='</td></tr>';
 7867:         } elsif ($position eq 'middle') {
 7868:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7869:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7870:                     foreach my $item (@{$types}) {
 7871:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7872:                                                                $numinrow,$itemcount,'','','','','',
 7873:                                                                '',$usertypes->{$item});
 7874:                         $itemcount ++;
 7875:                     }
 7876:                 }
 7877:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7878:                                                        $numinrow,$itemcount,'','','','','',
 7879:                                                        '',$othertitle);
 7880:                 $itemcount ++;
 7881:             } else {
 7882:                 my (@insttypes,%insttitles);
 7883:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7884:                     @insttypes = @{$types};
 7885:                     %insttitles = %{$usertypes};
 7886:                 }
 7887:                 foreach my $item (@insttypes,'default') {
 7888:                     my $title;
 7889:                     if ($item eq 'default') {
 7890:                         $title = $othertitle;
 7891:                     } else {
 7892:                         $title = $insttitles{$item};
 7893:                     }
 7894:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7895:                     $datatable .= '<tr'.$css_class.'>'.
 7896:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7897:                                   '<td class="LC_left_item">'.
 7898:                                   &mt('Nothing to set here, as there are no other domains').
 7899:                                   '</td></tr>';
 7900:                     $itemcount ++;
 7901:                 }
 7902:             }
 7903:         }
 7904:     } else {
 7905:         my $prefix;
 7906:         if ($position eq 'lower') {
 7907:             $prefix = 'priv';
 7908:         } else {
 7909:             $prefix = 'unpriv';
 7910:         }
 7911:         foreach my $item (@items) {
 7912:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7913:                                                    $numinrow,$itemcount,'','','','','',
 7914:                                                    '',$names{$item});
 7915:             $itemcount ++;
 7916:         }
 7917:     }
 7918:     if (ref($rowtotal)) {
 7919:         $$rowtotal += $itemcount;
 7920:     }
 7921:     return $datatable;
 7922: }
 7923: 
 7924: sub print_passwords {
 7925:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7926:     my ($datatable,$css_class);
 7927:     my $itemcount = 0;
 7928:     my %titles = &Apache::lonlocal::texthash (
 7929:         captcha        => '"Forgot Password" CAPTCHA validation',
 7930:         link           => 'Reset link expiration (hours)',
 7931:         case           => 'Case-sensitive usernames/e-mail',
 7932:         prelink        => 'Information required (form 1)',
 7933:         postlink       => 'Information required (form 2)',
 7934:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7935:         customtext     => 'Domain specific text (HTML)',
 7936:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7937:         intauth_check  => 'Check bcrypt cost if authenticated',
 7938:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7939:         permanent      => 'Permanent e-mail address',
 7940:         critical       => 'Critical notification address',
 7941:         notify         => 'Notification address',
 7942:         min            => 'Minimum password length',
 7943:         max            => 'Maximum password length',
 7944:         chars          => 'Required characters',
 7945:         expire         => 'Password expiration (days)',
 7946:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7947:     );
 7948:     if ($position eq 'top') {
 7949:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7950:         my $shownlinklife = 2;
 7951:         my $prelink = 'both';
 7952:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7953:         if (ref($settings) eq 'HASH') {
 7954:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7955:                 $shownlinklife = $settings->{resetlink};
 7956:             }
 7957:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7958:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7959:             }
 7960:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7961:                 $prelink = $settings->{resetprelink};
 7962:             }
 7963:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7964:                 %postlink = %{$settings->{resetpostlink}};
 7965:             }
 7966:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7967:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7968:             }
 7969:             if ($settings->{resetremove}) {
 7970:                 $nostdtext = 1;
 7971:             }
 7972:             if ($settings->{resetcustom}) {
 7973:                 $customurl = $settings->{resetcustom};
 7974:             }
 7975:         } else {
 7976:             if (ref($types) eq 'ARRAY') {
 7977:                 foreach my $item (@{$types}) {
 7978:                     $casesens{$item} = 1;
 7979:                     $postlink{$item} = ['username','email'];
 7980:                 }
 7981:             }
 7982:             $casesens{'default'} = 1;
 7983:             $postlink{'default'} = ['username','email'];
 7984:             $prelink = 'both';
 7985:             %emailsrc = (
 7986:                           permanent => 1,
 7987:                           critical  => 1,
 7988:                           notify    => 1,
 7989:             );
 7990:         }
 7991:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7992:         $itemcount ++;
 7993:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7994:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7995:                       '<td class="LC_left_item">'.
 7996:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7997:                       'name="passwords_link" size="3" /></td></tr>';
 7998:         $itemcount ++;
 7999:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8000:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 8001:                       '<td class="LC_left_item">';
 8002:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8003:             foreach my $item (@{$types}) {
 8004:                 my $checkedcase;
 8005:                 if ($casesens{$item}) {
 8006:                     $checkedcase = ' checked="checked"';
 8007:                 }
 8008:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8009:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 8010:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 8011:                               '</span>&nbsp;&nbsp; ';
 8012:             }
 8013:         }
 8014:         my $checkedcase;
 8015:         if ($casesens{'default'}) {
 8016:             $checkedcase = ' checked="checked"';
 8017:         }
 8018:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8019:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 8020:                       $othertitle.'</label></span></td>';
 8021:         $itemcount ++;
 8022:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8023:         my %checkedpre = (
 8024:                              both => ' checked="checked"',
 8025:                              either => '',
 8026:                          );
 8027:         if ($prelink eq 'either') {
 8028:             $checkedpre{either} = ' checked="checked"';
 8029:             $checkedpre{both} = '';
 8030:         }
 8031:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 8032:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8033:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 8034:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 8035:                       '<span class="LC_nobreak"><label>'.
 8036:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 8037:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 8038:         $itemcount ++;
 8039:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8040:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 8041:                       '<td class="LC_left_item">';
 8042:         my %postlinked;
 8043:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8044:             foreach my $item (@{$types}) {
 8045:                 undef(%postlinked);
 8046:                 $datatable .= '<fieldset style="display: inline-block;">'.
 8047:                               '<legend>'.$usertypes->{$item}.'</legend>';
 8048:                 if (ref($postlink{$item}) eq 'ARRAY') {
 8049:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 8050:                 }
 8051:                 foreach my $field ('email','username') {
 8052:                     my $checked;
 8053:                     if ($postlinked{$field}) {
 8054:                         $checked = ' checked="checked"';
 8055:                     }
 8056:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8057:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 8058:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 8059:                                   '<span>&nbsp;&nbsp; ';
 8060:                 }
 8061:                 $datatable .= '</fieldset>';
 8062:             }
 8063:         }
 8064:         if (ref($postlink{'default'}) eq 'ARRAY') {
 8065:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 8066:         }
 8067:         $datatable .= '<fieldset style="display: inline-block;">'.
 8068:                       '<legend>'.$othertitle.'</legend>';
 8069:         foreach my $field ('email','username') {
 8070:             my $checked;
 8071:             if ($postlinked{$field}) {
 8072:                 $checked = ' checked="checked"';
 8073:             }
 8074:             $datatable .= '<span class="LC_nobreak"><label>'.
 8075:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 8076:                           $field.'"'.$checked.' />'.$field.'</label>'.
 8077:                           '<span>&nbsp;&nbsp; ';
 8078:         }
 8079:         $datatable .= '</fieldset></td></tr>';
 8080:         $itemcount ++;
 8081:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8082:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 8083:                       '<td class="LC_left_item">';
 8084:         foreach my $type ('permanent','critical','notify') {
 8085:             my $checkedemail;
 8086:             if ($emailsrc{$type}) {
 8087:                 $checkedemail = ' checked="checked"';
 8088:             }
 8089:             $datatable .= '<span class="LC_nobreak"><label>'.
 8090:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 8091:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 8092:                           '<span>&nbsp;&nbsp; ';
 8093:         }
 8094:         $datatable .= '</td></tr>';
 8095:         $itemcount ++;
 8096:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8097:         my $switchserver = &check_switchserver($dom,$confname);
 8098:         my ($showstd,$noshowstd);
 8099:         if ($nostdtext) {
 8100:             $noshowstd = ' checked="checked"';
 8101:         } else {
 8102:             $showstd = ' checked="checked"';
 8103:         }
 8104:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 8105:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8106:                       &mt('Retain standard text:').
 8107:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 8108:                       &mt('Yes').'</label>'.'&nbsp;'.
 8109:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 8110:                       &mt('No').'</label></span><br />'.
 8111:                       '<span class="LC_fontsize_small">'.
 8112:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 8113:                       &mt('Include custom text:');
 8114:         if ($customurl) {
 8115:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 8116:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8117:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 8118:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 8119:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 8120:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 8121:         }
 8122:         if ($switchserver) {
 8123:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 8124:         } else {
 8125:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8126:                          '<input type="file" name="passwords_customfile" /></span>';
 8127:         }
 8128:         $datatable .= '</td></tr>';
 8129:     } elsif ($position eq 'middle') {
 8130:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 8131:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 8132:         my %defaults;
 8133:         if (ref($domconf{'defaults'}) eq 'HASH') {
 8134:             %defaults = %{$domconf{'defaults'}};
 8135:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 8136:                 $defaults{'intauth_cost'} = 10;
 8137:             }
 8138:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 8139:                 $defaults{'intauth_check'} = 0;
 8140:             }
 8141:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 8142:                 $defaults{'intauth_switch'} = 0;
 8143:             }
 8144:         } else {
 8145:             %defaults = (
 8146:                           'intauth_cost'   => 10,
 8147:                           'intauth_check'  => 0,
 8148:                           'intauth_switch' => 0,
 8149:                         );
 8150:         }
 8151:         foreach my $item (@items) {
 8152:             if ($itemcount%2) {
 8153:                 $css_class = '';
 8154:             } else {
 8155:                 $css_class = ' class="LC_odd_row" ';
 8156:             }
 8157:             $datatable .= '<tr'.$css_class.'>'.
 8158:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 8159:                           '</span></td><td class="LC_left_item" colspan="3">';
 8160:             if ($item eq 'intauth_switch') {
 8161:                 my @options = (0,1,2);
 8162:                 my %optiondesc = &Apache::lonlocal::texthash (
 8163:                                    0 => 'No',
 8164:                                    1 => 'Yes',
 8165:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 8166:                                  );
 8167:                 $datatable .= '<table width="100%">';
 8168:                 foreach my $option (@options) {
 8169:                     my $checked = ' ';
 8170:                     if ($defaults{$item} eq $option) {
 8171:                         $checked = ' checked="checked"';
 8172:                     }
 8173:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8174:                                   '<label><input type="radio" name="'.$item.
 8175:                                   '" value="'.$option.'"'.$checked.' />'.
 8176:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8177:                 }
 8178:                 $datatable .= '</table>';
 8179:             } elsif ($item eq 'intauth_check') {
 8180:                 my @options = (0,1,2);
 8181:                 my %optiondesc = &Apache::lonlocal::texthash (
 8182:                                    0 => 'No',
 8183:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 8184:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 8185:                                  );
 8186:                 $datatable .= '<table width="100%">';
 8187:                 foreach my $option (@options) {
 8188:                     my $checked = ' ';
 8189:                     my $onclick;
 8190:                     if ($defaults{$item} eq $option) {
 8191:                         $checked = ' checked="checked"';
 8192:                     }
 8193:                     if ($option == 2) {
 8194:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 8195:                     }
 8196:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8197:                                   '<label><input type="radio" name="'.$item.
 8198:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 8199:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8200:                 }
 8201:                 $datatable .= '</table>';
 8202:             } else {
 8203:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8204:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 8205:             }
 8206:             $datatable .= '</td></tr>';
 8207:             $itemcount ++;
 8208:         }
 8209:     } elsif ($position eq 'lower') {
 8210:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 8211:     } else {
 8212:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8213:         my %ownerchg = (
 8214:                           by  => {},
 8215:                           for => {},
 8216:                        );
 8217:         my %ownertitles = &Apache::lonlocal::texthash (
 8218:                             by  => 'Course owner status(es) allowed',
 8219:                             for => 'Student status(es) allowed',
 8220:                           );
 8221:         if (ref($settings) eq 'HASH') {
 8222:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 8223:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 8224:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 8225:                 }
 8226:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 8227:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 8228:                 }
 8229:             }
 8230:         }
 8231:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8232:         $datatable .= '<tr '.$css_class.'>'.
 8233:                       '<td>'.
 8234:                       &mt('Requirements').'<ul>'.
 8235:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 8236:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 8237:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 8238:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 8239:                       '</ul>'.
 8240:                       '</td>'.
 8241:                       '<td class="LC_left_item">';
 8242:         foreach my $item ('by','for') {
 8243:             $datatable .= '<fieldset style="display: inline-block;">'.
 8244:                           '<legend>'.$ownertitles{$item}.'</legend>';
 8245:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8246:                 foreach my $type (@{$types}) {
 8247:                     my $checked;
 8248:                     if ($ownerchg{$item}{$type}) {
 8249:                         $checked = ' checked="checked"';
 8250:                     }
 8251:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8252:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 8253:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 8254:                                   '</span>&nbsp;&nbsp; ';
 8255:                 }
 8256:             }
 8257:             my $checked;
 8258:             if ($ownerchg{$item}{'default'}) {
 8259:                 $checked = ' checked="checked"';
 8260:             }
 8261:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8262:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 8263:                           $othertitle.'</label></span></fieldset>';
 8264:         }
 8265:         $datatable .= '</td></tr>';
 8266:     }
 8267:     return $datatable;
 8268: }
 8269: 
 8270: sub password_rules {
 8271:     my ($prefix,$itemcountref,$settings) = @_;
 8272:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 8273:     my %titles;
 8274:     if ($prefix eq 'passwords') {
 8275:         %titles = &Apache::lonlocal::texthash (
 8276:             min            => 'Minimum password length',
 8277:             max            => 'Maximum password length',
 8278:             chars          => 'Required characters',
 8279:         );
 8280:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 8281:         %titles = &Apache::lonlocal::texthash (
 8282:             min            => 'Minimum secret length',
 8283:             max            => 'Maximum secret length',
 8284:             chars          => 'Required characters',
 8285:         );
 8286:     }
 8287:     $min = $Apache::lonnet::passwdmin;
 8288:     my $datatable;
 8289:     my $itemcount;
 8290:     if (ref($itemcountref)) {
 8291:         $itemcount = $$itemcountref;
 8292:     }
 8293:     if (ref($settings) eq 'HASH') {
 8294:         if ($settings->{min}) {
 8295:             $min = $settings->{min};
 8296:         }
 8297:         if ($settings->{max}) {
 8298:             $max = $settings->{max};
 8299:         }
 8300:         if (ref($settings->{chars}) eq 'ARRAY') {
 8301:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 8302:         }
 8303:         if ($prefix eq 'passwords') {
 8304:             if ($settings->{expire}) {
 8305:                 $expire = $settings->{expire};
 8306:             }
 8307:             if ($settings->{numsaved}) {
 8308:                 $numsaved = $settings->{numsaved};
 8309:             }
 8310:         }
 8311:     }
 8312:     my %rulenames = &Apache::lonlocal::texthash(
 8313:                                                  uc => 'At least one upper case letter',
 8314:                                                  lc => 'At least one lower case letter',
 8315:                                                  num => 'At least one number',
 8316:                                                  spec => 'At least one non-alphanumeric',
 8317:                                                );
 8318:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8319:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 8320:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8321:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 8322:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8323:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 8324:                   '</span></td></tr>';
 8325:     $itemcount ++;
 8326:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8327:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 8328:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8329:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 8330:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8331:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 8332:                   '</span></td></tr>';
 8333:     $itemcount ++;
 8334:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8335:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 8336:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 8337:                   '</span></td>';
 8338:     my $numinrow = 2;
 8339:     my @possrules = ('uc','lc','num','spec');
 8340:     $datatable .= '<td class="LC_left_item"><table>';
 8341:     for (my $i=0; $i<@possrules; $i++) {
 8342:         my ($rem,$checked);
 8343:         if ($chars{$possrules[$i]}) {
 8344:             $checked = ' checked="checked"';
 8345:         }
 8346:         $rem = $i%($numinrow);
 8347:         if ($rem == 0) {
 8348:             if ($i > 0) {
 8349:                 $datatable .= '</tr>';
 8350:             }
 8351:             $datatable .= '<tr>';
 8352:         }
 8353:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 8354:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 8355:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 8356:     }
 8357:     my $rem = @possrules%($numinrow);
 8358:     my $colsleft = $numinrow - $rem;
 8359:     if ($colsleft > 1 ) {
 8360:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8361:                       '&nbsp;</td>';
 8362:     } elsif ($colsleft == 1) {
 8363:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8364:     }
 8365:     $datatable .='</table></td></tr>';
 8366:     $itemcount ++;
 8367:     if ($prefix eq 'passwords') {
 8368:         $titles{'expire'} = &mt('Password expiration (days)');
 8369:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 8370:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8371:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 8372:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8373:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 8374:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8375:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 8376:                       '</span></td></tr>';
 8377:         $itemcount ++;
 8378:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8379:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 8380:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8381:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 8382:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8383:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 8384:                       '</span></td></tr>';
 8385:         $itemcount ++;
 8386:     }
 8387:     if (ref($itemcountref)) {
 8388:         $$itemcountref += $itemcount;
 8389:     }
 8390:     return $datatable;
 8391: }
 8392: 
 8393: sub print_wafproxy {
 8394:     my ($position,$dom,$settings,$rowtotal) = @_;
 8395:     my $css_class;
 8396:     my $itemcount = 0;
 8397:     my $datatable;
 8398:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8399:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8400:     my %lt = &wafproxy_titles();
 8401:     foreach my $server (sort(keys(%servers))) {
 8402:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8403:         next if ($serverhome eq '');
 8404:         my $serverdom;
 8405:         if ($serverhome ne $server) {
 8406:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8407:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8408:                 $othercontrol{$server} = $serverdom;
 8409:             }
 8410:         } else {
 8411:             $serverdom = &Apache::lonnet::host_domain($server);
 8412:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8413:             if ($serverdom ne $dom) {
 8414:                 $othercontrol{$server} = $serverdom;
 8415:             } else {
 8416:                 $setdom = 1;
 8417:                 if (ref($settings) eq 'HASH') {
 8418:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8419:                         $aliases{$dom} = $settings->{'alias'};
 8420:                         if ($aliases{$dom} ne '') {
 8421:                             $showdom = 1;
 8422:                         }
 8423:                     }
 8424:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8425:                         $saml{$dom} = $settings->{'saml'};
 8426:                     }
 8427:                 }
 8428:             }
 8429:         }
 8430:     }
 8431:     if ($setdom) {
 8432:         %{$values{$dom}} = ();
 8433:         if (ref($settings) eq 'HASH') {
 8434:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8435:                 $values{$dom}{$item} = $settings->{$item};
 8436:             }
 8437:         }
 8438:     }
 8439:     if (keys(%othercontrol)) {
 8440:         %otherdoms = reverse(%othercontrol);
 8441:         foreach my $domain (keys(%otherdoms)) {
 8442:             %{$values{$domain}} = ();
 8443:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8444:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8445:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8446:                 if (exists($config{'wafproxy'}{'saml'})) {
 8447:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8448:                 }
 8449:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8450:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8451:                 }
 8452:             }
 8453:         }
 8454:     }
 8455:     if ($position eq 'top') {
 8456:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8457:         my %aliasinfo;
 8458:         foreach my $server (sort(keys(%servers))) {
 8459:             $itemcount ++;
 8460:             my $dom_in_effect;
 8461:             my $aliasrows = '<tr>'.
 8462:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8463:                             &mt('Hostname').': '.
 8464:                             '<span class="LC_nobreak LC_cusr_emph">'.
 8465:                             &Apache::lonnet::hostname($server).
 8466:                             '</span></td><td>&nbsp;</td>';
 8467:             if ($othercontrol{$server}) {
 8468:                 $dom_in_effect = $othercontrol{$server};
 8469:                 my ($current,$forsaml);
 8470:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8471:                     $current = $aliases{$dom_in_effect}{$server};
 8472:                 }
 8473:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8474:                     if ($saml{$dom_in_effect}{$server}) {
 8475:                         $forsaml = 1;
 8476:                     }
 8477:                 }
 8478:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8479:                               &mt('Alias').':&nbsp';
 8480:                 if ($current) {
 8481:                     $aliasrows .= $current;
 8482:                     if ($forsaml) {
 8483:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8484:                     }
 8485:                 } else {
 8486:                     $aliasrows .= &mt('None');
 8487:                 }
 8488:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8489:                               &mt('controlled by domain: [_1]',
 8490:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8491:             } else {
 8492:                 $dom_in_effect = $dom;
 8493:                 my ($current,$samlon,$samloff);
 8494:                 $samloff = ' checked="checked"';
 8495:                 if (ref($aliases{$dom}) eq 'HASH') {
 8496:                     if ($aliases{$dom}{$server}) {
 8497:                         $current = $aliases{$dom}{$server};
 8498:                     }
 8499:                 }
 8500:                 if (ref($saml{$dom}) eq 'HASH') {
 8501:                     if ($saml{$dom}{$server}) {
 8502:                         $samlon = $samloff;
 8503:                         undef($samloff);
 8504:                     }
 8505:                 }
 8506:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8507:                               &mt('Alias').':&nbsp;'.
 8508:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8509:                               'value="'.$current.'" size="30" />'.
 8510:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8511:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8512:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8513:                               &mt('No').'</label>&nbsp;<label>'.
 8514:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8515:                               &mt('Yes').'</label></span>'.
 8516:                               '</td>';
 8517:             }
 8518:             $aliasrows .= '</tr>';
 8519:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8520:         }
 8521:         if ($aliasinfo{$dom}) {
 8522:             my ($onclick,$wafon,$wafoff,$showtable);
 8523:             $onclick = ' onclick="javascript:toggleWAF();"';
 8524:             $wafoff = ' checked="checked"';
 8525:             $showtable = ' style="display:none";';
 8526:             if ($showdom) {
 8527:                 $wafon = $wafoff;
 8528:                 $wafoff = '';
 8529:                 $showtable = ' style="display:inline;"';
 8530:             }
 8531:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8532:             $datatable = '<tr'.$css_class.'>'.
 8533:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8534:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8535:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8536:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8537:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8538:                          &mt('No').'</label></span></td>'.
 8539:                          '<td class="LC_left_item">'.
 8540:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8541:                          '</table></td></tr>';
 8542:             $itemcount++;
 8543:         }
 8544:         if (keys(%otherdoms)) {
 8545:             foreach my $key (sort(keys(%otherdoms))) {
 8546:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8547:                 $datatable .= '<tr'.$css_class.'>'.
 8548:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8549:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8550:                               '</table></td></tr>';
 8551:                 $itemcount++;
 8552:             }
 8553:         }
 8554:     } else {
 8555:         my %ip_methods = &remoteip_methods();
 8556:         if ($setdom) {
 8557:             $itemcount ++;
 8558:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8559:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8560:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8561:             $wafstyle = ' style="display:none;"';
 8562:             $nowafstyle = ' style="display:table-row;"';
 8563:             $currwafdisplay = ' style="display: none"';
 8564:             $wafrangestyle = ' style="display: none"';
 8565:             $curr_remotip = 'n';
 8566:             $ssltossl = ' checked="checked"';
 8567:             if ($showdom) {
 8568:                 $wafstyle = ' style="display:table-row;"';
 8569:                 $nowafstyle =  ' style="display:none;"';
 8570:                 if (keys(%{$values{$dom}})) {
 8571:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8572:                         $curr_remotip = $values{$dom}{remoteip};
 8573:                     }
 8574:                     if ($curr_remotip eq 'h') {
 8575:                         $currwafdisplay = ' style="display:table-row"';
 8576:                         $wafrangestyle = ' style="display:inline-block;"';
 8577:                     }
 8578:                     if ($values{$dom}{'sslopt'}) {
 8579:                         $alltossl = ' checked="checked"';
 8580:                         $ssltossl = '';
 8581:                     }
 8582:                 }
 8583:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8584:                     $vpndircheck = ' checked="checked"';
 8585:                     $currwafvpn = ' style="display:table-row;"';
 8586:                     $wafrangestyle = ' style="display:inline-block;"';
 8587:                 } else {
 8588:                     $vpnaliascheck = ' checked="checked"';
 8589:                     $currwafvpn = ' style="display:none;"';
 8590:                 }
 8591:             }
 8592:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8593:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8594:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8595:                           '</tr>'.
 8596:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8597:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8598:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8599:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8600:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8601:                           '<td class="LC_left_item"><table>'.
 8602:                           '<tr>'.
 8603:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8604:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8605:             foreach my $option ('m','h','n') {
 8606:                 my $sel;
 8607:                 if ($option eq $curr_remotip) {
 8608:                    $sel = ' selected="selected"';
 8609:                 }
 8610:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8611:                               $ip_methods{$option}.'</option>';
 8612:             }
 8613:             $datatable .= '</select></td></tr>'."\n".
 8614:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8615:                           $lt{'ipheader'}.':&nbsp;'.
 8616:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8617:                           'name="wafproxy_ipheader" />'.
 8618:                           '</td></tr>'."\n".
 8619:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8620:                           $lt{'trusted'}.':<br />'.
 8621:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8622:                           $values{$dom}{'trusted'}.'</textarea>'.
 8623:                           '</td></tr>'."\n".
 8624:                           '<tr><td><hr /></td></tr>'."\n".
 8625:                           '<tr>'.
 8626:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8627:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8628:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8629:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8630:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8631:             foreach my $item ('vpnint','vpnext') {
 8632:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8633:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8634:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8635:                               $values{$dom}{$item}.'</textarea>'.
 8636:                               '</td></tr>'."\n";
 8637:             }
 8638:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8639:                           '<tr>'.
 8640:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8641:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8642:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8643:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8644:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8645:                           '</table></td></tr>';
 8646:         }
 8647:         if (keys(%otherdoms)) {
 8648:             foreach my $domain (sort(keys(%otherdoms))) {
 8649:                 $itemcount ++;
 8650:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8651:                 $datatable .= '<tr'.$css_class.'>'.
 8652:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8653:                               '<td class="LC_left_item"><table>';
 8654:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8655:                     my $showval = &mt('None');
 8656:                     if ($item eq 'ssl') {
 8657:                         $showval = $lt{'ssltossl'};
 8658:                     }
 8659:                     if ($values{$domain}{$item}) {
 8660:                         $showval = $values{$domain}{$item};
 8661:                         if ($item eq 'ssl') {
 8662:                             $showval = $lt{'alltossl'};
 8663:                         } elsif ($item eq 'remoteip') {
 8664:                             $showval = $ip_methods{$values{$domain}{$item}};
 8665:                         }
 8666:                     }
 8667:                     $datatable .= '<tr>'.
 8668:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8669:                 }
 8670:                 $datatable .= '</table></td></tr>';
 8671:             }
 8672:         }
 8673:     }
 8674:     $$rowtotal += $itemcount;
 8675:     return $datatable;
 8676: }
 8677: 
 8678: sub wafproxy_titles {
 8679:     return &Apache::lonlocal::texthash(
 8680:                remoteip   => "Method for determining user's IP",
 8681:                ipheader   => 'Request header containing remote IP',
 8682:                trusted    => 'Trusted IP range(s)',
 8683:                vpnaccess  => 'Access from institutional VPN',
 8684:                vpndirect  => 'via regular hostname (no WAF)',
 8685:                vpnaliased => 'via aliased hostname (WAF)',
 8686:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8687:                vpnext     => 'IP Range(s) for backend WAF connections',
 8688:                sslopt     => 'Forwarding http/https',
 8689:                alltossl   => 'WAF forwards both http and https requests to https',
 8690:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8691:            );
 8692: }
 8693: 
 8694: sub remoteip_methods {
 8695:     return &Apache::lonlocal::texthash(
 8696:               m => 'Use Apache mod_remoteip',
 8697:               h => 'Use headers parsed by LON-CAPA',
 8698:               n => 'Not in use',
 8699:            );
 8700: }
 8701: 
 8702: sub print_usersessions {
 8703:     my ($position,$dom,$settings,$rowtotal) = @_;
 8704:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8705:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8706:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8707: 
 8708:     my @alldoms = &Apache::lonnet::all_domains();
 8709:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8710:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8711:     my %altids = &id_for_thisdom(%servers);
 8712:     if ($position eq 'top') {
 8713:         if (keys(%serverhomes) > 1) {
 8714:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8715:             my ($curroffloadnow,$curroffloadoth);
 8716:             if (ref($settings) eq 'HASH') {
 8717:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8718:                     $curroffloadnow = $settings->{'offloadnow'};
 8719:                 }
 8720:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8721:                     $curroffloadoth = $settings->{'offloadoth'};
 8722:                 }
 8723:             }
 8724:             my $other_insts = scalar(keys(%by_location));
 8725:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8726:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8727:         } else {
 8728:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8729:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8730:                           '</td></tr>';
 8731:         }
 8732:     } else {
 8733:         my %titles = &usersession_titles();
 8734:         my ($prefix,@types);
 8735:         if ($position eq 'bottom') {
 8736:             $prefix = 'remote';
 8737:             @types = ('version','excludedomain','includedomain');
 8738:         } else {
 8739:             $prefix = 'hosted';
 8740:             @types = ('excludedomain','includedomain');
 8741:         }
 8742:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8743:     }
 8744:     $$rowtotal += $itemcount;
 8745:     return $datatable;
 8746: }
 8747: 
 8748: sub rules_by_location {
 8749:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8750:     my ($datatable,$itemcount,$css_class);
 8751:     if (keys(%{$by_location}) == 0) {
 8752:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8753:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8754:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8755:                      '</td></tr>';
 8756:         $itemcount = 1;
 8757:     } else {
 8758:         $itemcount = 0;
 8759:         my $numinrow = 5;
 8760:         my (%current,%checkedon,%checkedoff);
 8761:         my @locations = sort(keys(%{$by_location}));
 8762:         foreach my $type (@{$types}) {
 8763:             $checkedon{$type} = '';
 8764:             $checkedoff{$type} = ' checked="checked"';
 8765:         }
 8766:         if (ref($settings) eq 'HASH') {
 8767:             if (ref($settings->{$prefix}) eq 'HASH') {
 8768:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8769:                     $current{$key} = $settings->{$prefix}{$key};
 8770:                     if ($key eq 'version') {
 8771:                         if ($current{$key} ne '') {
 8772:                             $checkedon{$key} = ' checked="checked"';
 8773:                             $checkedoff{$key} = '';
 8774:                         }
 8775:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8776:                         $checkedon{$key} = ' checked="checked"';
 8777:                         $checkedoff{$key} = '';
 8778:                     }
 8779:                 }
 8780:             }
 8781:         }
 8782:         foreach my $type (@{$types}) {
 8783:             next if ($type ne 'version' && !@locations);
 8784:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8785:             $datatable .= '<tr'.$css_class.'>
 8786:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8787:                            <span class="LC_nobreak">&nbsp;
 8788:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8789:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8790:             if ($type eq 'version') {
 8791:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8792:                 my $selector = '<select name="'.$prefix.'_version">';
 8793:                 foreach my $version (@lcversions) {
 8794:                     my $selected = '';
 8795:                     if ($current{'version'} eq $version) {
 8796:                         $selected = ' selected="selected"';
 8797:                     }
 8798:                     $selector .= ' <option value="'.$version.'"'.
 8799:                                  $selected.'>'.$version.'</option>';
 8800:                 }
 8801:                 $selector .= '</select> ';
 8802:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8803:             } else {
 8804:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8805:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8806:                              ' />'.('&nbsp;'x2).
 8807:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8808:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8809:                              "\n".
 8810:                              '</div><div><table>';
 8811:                 my $rem;
 8812:                 for (my $i=0; $i<@locations; $i++) {
 8813:                     my ($showloc,$value,$checkedtype);
 8814:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8815:                         my $ip = $by_location->{$locations[$i]}->[0];
 8816:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8817:                             $value = join(':',@{$by_ip->{$ip}});
 8818:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8819:                             if (ref($current{$type}) eq 'ARRAY') {
 8820:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8821:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8822:                                         $checkedtype = ' checked="checked"';
 8823:                                         last;
 8824:                                     }
 8825:                                 }
 8826:                             }
 8827:                         }
 8828:                     }
 8829:                     $rem = $i%($numinrow);
 8830:                     if ($rem == 0) {
 8831:                         if ($i > 0) {
 8832:                             $datatable .= '</tr>';
 8833:                         }
 8834:                         $datatable .= '<tr>';
 8835:                     }
 8836:                     $datatable .= '<td class="LC_left_item">'.
 8837:                                   '<span class="LC_nobreak"><label>'.
 8838:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8839:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8840:                                   '</label></span></td>';
 8841:                 }
 8842:                 $rem = @locations%($numinrow);
 8843:                 my $colsleft = $numinrow - $rem;
 8844:                 if ($colsleft > 1 ) {
 8845:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8846:                                   '&nbsp;</td>';
 8847:                 } elsif ($colsleft == 1) {
 8848:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8849:                 }
 8850:                 $datatable .= '</tr></table>';
 8851:             }
 8852:             $datatable .= '</td></tr>';
 8853:             $itemcount ++;
 8854:         }
 8855:     }
 8856:     return ($datatable,$itemcount);
 8857: }
 8858: 
 8859: sub print_ssl {
 8860:     my ($position,$dom,$settings,$rowtotal) = @_;
 8861:     my ($css_class,$datatable);
 8862:     my $itemcount = 1;
 8863:     if ($position eq 'top') {
 8864:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8865:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8866:         my $same_institution;
 8867:         if ($intdom ne '') {
 8868:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8869:             if (ref($internet_names) eq 'ARRAY') {
 8870:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8871:                     $same_institution = 1;
 8872:                 }
 8873:             }
 8874:         }
 8875:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8876:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8877:         if ($same_institution) {
 8878:             my %domservers = &Apache::lonnet::get_servers($dom);
 8879:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8880:         } else {
 8881:             $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.");
 8882:         }
 8883:         $datatable .= '</td></tr>';
 8884:         $itemcount ++;
 8885:     } else {
 8886:         my %titles = &ssl_titles();
 8887:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8888:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8889:         my @alldoms = &Apache::lonnet::all_domains();
 8890:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8891:         my @domservers = &Apache::lonnet::get_servers($dom);
 8892:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8893:         my %altids = &id_for_thisdom(%servers);
 8894:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8895:             my $legacy;
 8896:             unless (ref($settings) eq 'HASH') {
 8897:                 my $name;
 8898:                 if ($position eq 'connto') {
 8899:                     $name = 'loncAllowInsecure';
 8900:                 } else {
 8901:                     $name = 'londAllowInsecure';
 8902:                 }
 8903:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8904:                 my @ids=&Apache::lonnet::current_machine_ids();
 8905:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8906:                     my %what = (
 8907:                                    $name => 1,
 8908:                                );
 8909:                     my ($result,$returnhash) =
 8910:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8911:                     if ($result eq 'ok') {
 8912:                         if (ref($returnhash) eq 'HASH') {
 8913:                             $legacy = $returnhash->{$name};
 8914:                         }
 8915:                     }
 8916:                 } else {
 8917:                     $legacy = $Apache::lonnet::perlvar{$name};
 8918:                 }
 8919:             }
 8920:             foreach my $type ('dom','intdom','other') {
 8921:                 my %checked;
 8922:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8923:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8924:                               '<td class="LC_right_item">';
 8925:                 my $skip; 
 8926:                 if ($type eq 'dom') {
 8927:                     unless (keys(%servers) > 1) {
 8928:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8929:                         $skip = 1;
 8930:                     }
 8931:                 }
 8932:                 if ($type eq 'intdom') {
 8933:                     unless (@instdoms > 1) {
 8934:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8935:                         $skip = 1;
 8936:                     } 
 8937:                 } elsif ($type eq 'other') {
 8938:                     if (keys(%by_location) == 0) {
 8939:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8940:                         $skip = 1;
 8941:                     }
 8942:                 }
 8943:                 unless ($skip) {
 8944:                     $checked{'yes'} = ' checked="checked"'; 
 8945:                     if (ref($settings) eq 'HASH') {
 8946:                         if (ref($settings->{$position}) eq 'HASH') {
 8947:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8948:                                 $checked{$1} = $checked{'yes'};
 8949:                                 delete($checked{'yes'}); 
 8950:                             }
 8951:                         }
 8952:                     } else {
 8953:                         if ($legacy == 0) {
 8954:                             $checked{'req'} = $checked{'yes'};
 8955:                             delete($checked{'yes'});    
 8956:                         }
 8957:                     }
 8958:                     foreach my $option ('no','yes','req') {
 8959:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8960:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8961:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8962:                                       '</label></span>'.('&nbsp;'x2);
 8963:                     }
 8964:                 }
 8965:                 $datatable .= '</td></tr>';
 8966:                 $itemcount ++; 
 8967:             }
 8968:         } else {
 8969:             my $prefix = 'replication';
 8970:             my @types = ('certreq','nocertreq');
 8971:             if (keys(%by_location) == 0) {
 8972:                 $datatable .= '<tr'.$css_class.'><td>'.
 8973:                               &mt('Nothing to set here, as there are no other institutions').
 8974:                               '</td></tr>';
 8975:                 $itemcount ++;
 8976:             } else {
 8977:                 ($datatable,$itemcount) = 
 8978:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8979:             }
 8980:         }
 8981:     }
 8982:     $$rowtotal += $itemcount;
 8983:     return $datatable;
 8984: }
 8985: 
 8986: sub ssl_titles {
 8987:     return &Apache::lonlocal::texthash (
 8988:                dom           => 'LON-CAPA servers/VMs from same domain',
 8989:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8990:                other         => 'External LON-CAPA servers/VMs',
 8991:                connto        => 'Connections to other servers',
 8992:                connfrom      => 'Connections from other servers',
 8993:                replication   => 'Replicating content to other institutions',
 8994:                certreq       => 'Client certificate required, but specific domains exempt',
 8995:                nocertreq     => 'No client certificate required, except for specific domains',
 8996:                no            => 'SSL not used',
 8997:                yes           => 'SSL Optional (used if available)',
 8998:                req           => 'SSL Required',
 8999:     );
 9000: }
 9001: 
 9002: sub print_trust {
 9003:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 9004:     my ($css_class,$datatable,%checked,%choices);
 9005:     my (%by_ip,%by_location,@intdoms,@instdoms);
 9006:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 9007:     my $itemcount = 1;
 9008:     my %titles = &trust_titles();
 9009:     my @types = ('exc','inc');
 9010:     if ($prefix eq 'top') {
 9011:         $prefix = 'content';
 9012:     } elsif ($prefix eq 'bottom') {
 9013:         $prefix = 'msg';
 9014:     }
 9015:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9016:     $$rowtotal += $itemcount;
 9017:     return $datatable;
 9018: }
 9019: 
 9020: sub trust_titles {
 9021:     return &Apache::lonlocal::texthash(
 9022:                content  => "Access to this domain's content by others",
 9023:                shared   => "Access to other domain's content by this domain",
 9024:                enroll   => "Enrollment in this domain's courses by others", 
 9025:                othcoau  => "Co-author roles in this domain for others",
 9026:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 9027:                domroles => "Domain roles in this domain assignable to others",
 9028:                catalog  => "Course Catalog for this domain displayed elsewhere",
 9029:                reqcrs   => "Requests for creation of courses in this domain by others",
 9030:                msg      => "Users in other domains can send messages to this domain",
 9031:                exc      => "Allow all, but exclude specific domains",
 9032:                inc      => "Deny all, but include specific domains",
 9033:            );
 9034: } 
 9035: 
 9036: sub build_location_hashes {
 9037:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 9038:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 9039:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 9040:     my %iphost = &Apache::lonnet::get_iphost();
 9041:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 9042:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 9043:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 9044:         foreach my $id (@{$iphost{$primary_ip}}) {
 9045:             my $intdom = &Apache::lonnet::internet_dom($id);
 9046:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 9047:                 push(@{$intdoms},$intdom);
 9048:             }
 9049:         }
 9050:     }
 9051:     foreach my $ip (keys(%iphost)) {
 9052:         if (ref($iphost{$ip}) eq 'ARRAY') {
 9053:             foreach my $id (@{$iphost{$ip}}) {
 9054:                 my $location = &Apache::lonnet::internet_dom($id);
 9055:                 if ($location) {
 9056:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 9057:                         my $dom = &Apache::lonnet::host_domain($id);
 9058:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 9059:                             push(@{$instdoms},$dom);
 9060:                         }
 9061:                         next;
 9062:                     }
 9063:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9064:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 9065:                             push(@{$by_ip->{$ip}},$location);
 9066:                         }
 9067:                     } else {
 9068:                         $by_ip->{$ip} = [$location];
 9069:                     }
 9070:                 }
 9071:             }
 9072:         }
 9073:     }
 9074:     foreach my $ip (sort(keys(%{$by_ip}))) {
 9075:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9076:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 9077:             my $first = $by_ip->{$ip}->[0];
 9078:             if (ref($by_location->{$first}) eq 'ARRAY') {
 9079:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 9080:                     push(@{$by_location->{$first}},$ip);
 9081:                 }
 9082:             } else {
 9083:                 $by_location->{$first} = [$ip];
 9084:             }
 9085:         }
 9086:     }
 9087:     return;
 9088: }
 9089: 
 9090: sub current_offloads_to {
 9091:     my ($dom,$settings,$servers) = @_;
 9092:     my (%spareid,%otherdomconfigs);
 9093:     if (ref($servers) eq 'HASH') {
 9094:         foreach my $lonhost (sort(keys(%{$servers}))) {
 9095:             my $gotspares;
 9096:             if (ref($settings) eq 'HASH') {
 9097:                 if (ref($settings->{'spares'}) eq 'HASH') {
 9098:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 9099:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 9100:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 9101:                         $gotspares = 1;
 9102:                     }
 9103:                 }
 9104:             }
 9105:             unless ($gotspares) {
 9106:                 my $gotspares;
 9107:                 my $serverhomeID =
 9108:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 9109:                 my $serverhomedom =
 9110:                     &Apache::lonnet::host_domain($serverhomeID);
 9111:                 if ($serverhomedom ne $dom) {
 9112:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 9113:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9114:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9115:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9116:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9117:                                 $gotspares = 1;
 9118:                             }
 9119:                         }
 9120:                     } else {
 9121:                         $otherdomconfigs{$serverhomedom} =
 9122:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 9123:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 9124:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9125:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9126:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 9127:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9128:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9129:                                         $gotspares = 1;
 9130:                                     }
 9131:                                 }
 9132:                             }
 9133:                         }
 9134:                     }
 9135:                 }
 9136:             }
 9137:             unless ($gotspares) {
 9138:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9139:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9140:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9141:                } else {
 9142:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 9143:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 9144:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9145:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9146:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9147:                     } else {
 9148:                         my %what = (
 9149:                              spareid => 1,
 9150:                         );
 9151:                         my ($result,$returnhash) = 
 9152:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 9153:                         if ($result eq 'ok') { 
 9154:                             if (ref($returnhash) eq 'HASH') {
 9155:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 9156:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 9157:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 9158:                                 }
 9159:                             }
 9160:                         }
 9161:                     }
 9162:                 }
 9163:             }
 9164:         }
 9165:     }
 9166:     return %spareid;
 9167: }
 9168: 
 9169: sub spares_row {
 9170:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 9171:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 9172:     my $css_class;
 9173:     my $numinrow = 4;
 9174:     my $itemcount = 1;
 9175:     my $datatable;
 9176:     my %typetitles = &sparestype_titles();
 9177:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9178:         foreach my $server (sort(keys(%{$servers}))) {
 9179:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 9180:             my ($othercontrol,$serverdom);
 9181:             if ($serverhome ne $server) {
 9182:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 9183:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9184:             } else {
 9185:                 $serverdom = &Apache::lonnet::host_domain($server);
 9186:                 if ($serverdom ne $dom) {
 9187:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9188:                 }
 9189:             }
 9190:             next unless (ref($spareid->{$server}) eq 'HASH');
 9191:             my ($checkednow,$checkedoth);
 9192:             if (ref($curroffloadnow) eq 'HASH') {
 9193:                 if ($curroffloadnow->{$server}) {
 9194:                     $checkednow = ' checked="checked"';
 9195:                 }
 9196:             }
 9197:             if (ref($curroffloadoth) eq 'HASH') {
 9198:                 if ($curroffloadoth->{$server}) {
 9199:                     $checkedoth = ' checked="checked"';
 9200:                 }
 9201:             }
 9202:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 9203:             $datatable .= '<tr'.$css_class.'>
 9204:                            <td rowspan="2">
 9205:                             <span class="LC_nobreak">'.
 9206:                           &mt('[_1] when busy, offloads to:'
 9207:                               ,'<b>'.$server.'</b>').'</span><br />'.
 9208:                           '<span class="LC_nobreak">'."\n".
 9209:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 9210:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 9211:                           "\n";
 9212:             if ($other_insts) {
 9213:                 $datatable .= '<br />'.
 9214:                               '<span class="LC_nobreak">'."\n".
 9215:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 9216:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 9217:                           "\n";
 9218:             }
 9219:             my (%current,%canselect);
 9220:             my @choices = 
 9221:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 9222:             foreach my $type ('primary','default') {
 9223:                 if (ref($spareid->{$server}) eq 'HASH') {
 9224:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 9225:                         my @spares = @{$spareid->{$server}{$type}};
 9226:                         if (@spares > 0) {
 9227:                             if ($othercontrol) {
 9228:                                 $current{$type} = join(', ',@spares);
 9229:                             } else {
 9230:                                 $current{$type} .= '<table>';
 9231:                                 my $numspares = scalar(@spares);
 9232:                                 for (my $i=0;  $i<@spares; $i++) {
 9233:                                     my $rem = $i%($numinrow);
 9234:                                     if ($rem == 0) {
 9235:                                         if ($i > 0) {
 9236:                                             $current{$type} .= '</tr>';
 9237:                                         }
 9238:                                         $current{$type} .= '<tr>';
 9239:                                     }
 9240:                                     $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;'.
 9241:                                                        $spareid->{$server}{$type}[$i].
 9242:                                                        '</label></td>'."\n";
 9243:                                 }
 9244:                                 my $rem = @spares%($numinrow);
 9245:                                 my $colsleft = $numinrow - $rem;
 9246:                                 if ($colsleft > 1 ) {
 9247:                                     $current{$type} .= '<td colspan="'.$colsleft.
 9248:                                                        '" class="LC_left_item">'.
 9249:                                                        '&nbsp;</td>';
 9250:                                 } elsif ($colsleft == 1) {
 9251:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 9252:                                 }
 9253:                                 $current{$type} .= '</tr></table>';
 9254:                             }
 9255:                         }
 9256:                     }
 9257:                     if ($current{$type} eq '') {
 9258:                         $current{$type} = &mt('None specified');
 9259:                     }
 9260:                     if ($othercontrol) {
 9261:                         if ($type eq 'primary') {
 9262:                             $canselect{$type} = $othercontrol;
 9263:                         }
 9264:                     } else {
 9265:                         $canselect{$type} = 
 9266:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 9267:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 9268:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 9269:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 9270:                         if (@choices > 0) {
 9271:                             foreach my $lonhost (@choices) {
 9272:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 9273:                             }
 9274:                         }
 9275:                         $canselect{$type} .= '</select>'."\n";
 9276:                     }
 9277:                 } else {
 9278:                     $current{$type} = &mt('Could not be determined');
 9279:                     if ($type eq 'primary') {
 9280:                         $canselect{$type} =  $othercontrol;
 9281:                     }
 9282:                 }
 9283:                 if ($type eq 'default') {
 9284:                     $datatable .= '<tr'.$css_class.'>';
 9285:                 }
 9286:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 9287:                               '<td>'.$current{$type}.'</td>'."\n".
 9288:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 9289:             }
 9290:             $itemcount ++;
 9291:         }
 9292:     }
 9293:     $$rowtotal += $itemcount;
 9294:     return $datatable;
 9295: }
 9296: 
 9297: sub possible_newspares {
 9298:     my ($server,$currspares,$serverhomes,$altids) = @_;
 9299:     my $serverhostname = &Apache::lonnet::hostname($server);
 9300:     my %excluded;
 9301:     if ($serverhostname ne '') {
 9302:         %excluded = (
 9303:                        $serverhostname => 1,
 9304:                     );
 9305:     }
 9306:     if (ref($currspares) eq 'HASH') {
 9307:         foreach my $type (keys(%{$currspares})) {
 9308:             if (ref($currspares->{$type}) eq 'ARRAY') {
 9309:                 if (@{$currspares->{$type}} > 0) {
 9310:                     foreach my $curr (@{$currspares->{$type}}) {
 9311:                         my $hostname = &Apache::lonnet::hostname($curr);
 9312:                         $excluded{$hostname} = 1;
 9313:                     }
 9314:                 }
 9315:             }
 9316:         }
 9317:     }
 9318:     my @choices;
 9319:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9320:         if (keys(%{$serverhomes}) > 1) {
 9321:             foreach my $name (sort(keys(%{$serverhomes}))) {
 9322:                 unless ($excluded{$name}) {
 9323:                     if (exists($altids->{$serverhomes->{$name}})) {
 9324:                         push(@choices,$altids->{$serverhomes->{$name}});
 9325:                     } else {
 9326:                         push(@choices,$serverhomes->{$name});
 9327:                     }
 9328:                 }
 9329:             }
 9330:         }
 9331:     }
 9332:     return sort(@choices);
 9333: }
 9334: 
 9335: sub print_loadbalancing {
 9336:     my ($dom,$settings,$rowtotal) = @_;
 9337:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9338:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9339:     my $numinrow = 1;
 9340:     my $datatable;
 9341:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9342:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 9343:     if (ref($settings) eq 'HASH') {
 9344:         %existing = %{$settings};
 9345:     }
 9346:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 9347:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9348:                                   \%currtargets,\%currrules,\%currcookies);
 9349:     } else {
 9350:         return;
 9351:     }
 9352:     my ($othertitle,$usertypes,$types) =
 9353:         &Apache::loncommon::sorted_inst_types($dom);
 9354:     my $rownum = 8;
 9355:     if (ref($types) eq 'ARRAY') {
 9356:         $rownum += scalar(@{$types});
 9357:     }
 9358:     my @css_class = ('LC_odd_row','LC_even_row');
 9359:     my $balnum = 0;
 9360:     my $islast;
 9361:     my (@toshow,$disabledtext);
 9362:     if (keys(%currbalancer) > 0) {
 9363:         @toshow = sort(keys(%currbalancer));
 9364:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 9365:             push(@toshow,'');
 9366:         }
 9367:     } else {
 9368:         @toshow = ('');
 9369:         $disabledtext = &mt('No existing load balancer');
 9370:     }
 9371:     foreach my $lonhost (@toshow) {
 9372:         if ($balnum == scalar(@toshow)-1) {
 9373:             $islast = 1;
 9374:         } else {
 9375:             $islast = 0;
 9376:         }
 9377:         my $cssidx = $balnum%2;
 9378:         my $targets_div_style = 'display: none';
 9379:         my $disabled_div_style = 'display: block';
 9380:         my $homedom_div_style = 'display: none';
 9381:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 9382:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9383:                       '<p>';
 9384:         if ($lonhost eq '') {
 9385:             $datatable .= '<span class="LC_nobreak">';
 9386:             if (keys(%currbalancer) > 0) {
 9387:                 $datatable .= &mt('Add balancer:');
 9388:             } else {
 9389:                 $datatable .= &mt('Enable balancer:');
 9390:             }
 9391:             $datatable .= '&nbsp;'.
 9392:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9393:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9394:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9395:                           '<option value="" selected="selected">'.&mt('None').
 9396:                           '</option>'."\n";
 9397:             foreach my $server (sort(keys(%servers))) {
 9398:                 next if ($currbalancer{$server});
 9399:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9400:             }
 9401:             $datatable .=
 9402:                 '</select>'."\n".
 9403:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9404:         } else {
 9405:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9406:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9407:                            &mt('Stop balancing').'</label>'.
 9408:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9409:             $targets_div_style = 'display: block';
 9410:             $disabled_div_style = 'display: none';
 9411:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9412:                 $homedom_div_style = 'display: block';
 9413:             }
 9414:         }
 9415:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9416:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9417:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9418:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9419:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9420:         my @sparestypes = ('primary','default');
 9421:         my %typetitles = &sparestype_titles();
 9422:         my %hostherechecked = (
 9423:                                   no => ' checked="checked"',
 9424:                               );
 9425:         my %balcookiechecked = (
 9426:                                   no => ' checked="checked"',
 9427:                                );
 9428:         foreach my $sparetype (@sparestypes) {
 9429:             my $targettable;
 9430:             for (my $i=0; $i<$numspares; $i++) {
 9431:                 my $checked;
 9432:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9433:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9434:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9435:                             $checked = ' checked="checked"';
 9436:                         }
 9437:                     }
 9438:                 }
 9439:                 my ($chkboxval,$disabled);
 9440:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9441:                     $chkboxval = $spares[$i];
 9442:                 }
 9443:                 if (exists($currbalancer{$spares[$i]})) {
 9444:                     $disabled = ' disabled="disabled"';
 9445:                 }
 9446:                 $targettable .=
 9447:                     '<td><span class="LC_nobreak"><label>'.
 9448:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9449:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9450:                     '</span></label></span></td>';
 9451:                 my $rem = $i%($numinrow);
 9452:                 if ($rem == 0) {
 9453:                     if (($i > 0) && ($i < $numspares-1)) {
 9454:                         $targettable .= '</tr>';
 9455:                     }
 9456:                     if ($i < $numspares-1) {
 9457:                         $targettable .= '<tr>';
 9458:                     }
 9459:                 }
 9460:             }
 9461:             if ($targettable ne '') {
 9462:                 my $rem = $numspares%($numinrow);
 9463:                 my $colsleft = $numinrow - $rem;
 9464:                 if ($colsleft > 1 ) {
 9465:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9466:                                     '&nbsp;</td>';
 9467:                 } elsif ($colsleft == 1) {
 9468:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9469:                 }
 9470:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9471:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9472:             }
 9473:             $hostherechecked{$sparetype} = '';
 9474:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9475:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9476:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9477:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9478:                         $hostherechecked{'no'} = '';
 9479:                     }
 9480:                 }
 9481:             }
 9482:         }
 9483:         if ($currcookies{$lonhost}) {
 9484:             %balcookiechecked = (
 9485:                                     yes => ' checked="checked"',
 9486:                                 );
 9487:         }
 9488:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9489:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9490:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9491:         foreach my $sparetype (@sparestypes) {
 9492:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9493:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9494:                           '</i></label><br />';
 9495:         }
 9496:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9497:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9498:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9499:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9500:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9501:                       '</div></td></tr>'.
 9502:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9503:                                            $othertitle,$usertypes,$types,\%servers,
 9504:                                            \%currbalancer,$lonhost,
 9505:                                            $targets_div_style,$homedom_div_style,
 9506:                                            $css_class[$cssidx],$balnum,$islast);
 9507:         $$rowtotal += $rownum;
 9508:         $balnum ++;
 9509:     }
 9510:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9511:     return $datatable;
 9512: }
 9513: 
 9514: sub get_loadbalancers_config {
 9515:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9516:     return unless ((ref($servers) eq 'HASH') &&
 9517:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9518:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9519:                    (ref($currcookies) eq 'HASH'));
 9520:     if (keys(%{$existing}) > 0) {
 9521:         my $oldlonhost;
 9522:         foreach my $key (sort(keys(%{$existing}))) {
 9523:             if ($key eq 'lonhost') {
 9524:                 $oldlonhost = $existing->{'lonhost'};
 9525:                 $currbalancer->{$oldlonhost} = 1;
 9526:             } elsif ($key eq 'targets') {
 9527:                 if ($oldlonhost) {
 9528:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9529:                 }
 9530:             } elsif ($key eq 'rules') {
 9531:                 if ($oldlonhost) {
 9532:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9533:                 }
 9534:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9535:                 $currbalancer->{$key} = 1;
 9536:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9537:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9538:                 if ($existing->{$key}{'cookie'}) {
 9539:                     $currcookies->{$key} = 1;
 9540:                 }
 9541:             }
 9542:         }
 9543:     } else {
 9544:         my ($balancerref,$targetsref) =
 9545:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9546:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9547:             foreach my $server (sort(keys(%{$balancerref}))) {
 9548:                 $currbalancer->{$server} = 1;
 9549:                 $currtargets->{$server} = $targetsref->{$server};
 9550:             }
 9551:         }
 9552:     }
 9553:     return;
 9554: }
 9555: 
 9556: sub loadbalancing_rules {
 9557:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9558:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9559:         $css_class,$balnum,$islast) = @_;
 9560:     my $output;
 9561:     my $num = 0;
 9562:     my ($alltypes,$othertypes,$titles) =
 9563:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9564:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9565:         foreach my $type (@{$alltypes}) {
 9566:             $num ++;
 9567:             my $current;
 9568:             if (ref($currrules) eq 'HASH') {
 9569:                 $current = $currrules->{$type};
 9570:             }
 9571:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9572:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9573:                     $current = '';
 9574:                 }
 9575:             }
 9576:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9577:                                              $servers,$currbalancer,$lonhost,$dom,
 9578:                                              $targets_div_style,$homedom_div_style,
 9579:                                              $css_class,$balnum,$num,$islast);
 9580:         }
 9581:     }
 9582:     return $output;
 9583: }
 9584: 
 9585: sub loadbalancing_titles {
 9586:     my ($dom,$intdom,$usertypes,$types) = @_;
 9587:     my %othertypes = (
 9588:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9589:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9590:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9591:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9592:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9593:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9594:                      );
 9595:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9596:     my @available;
 9597:     if (ref($types) eq 'ARRAY') {
 9598:         @available = @{$types};
 9599:     }
 9600:     unless (grep(/^default$/,@available)) {
 9601:         push(@available,'default');
 9602:     }
 9603:     unshift(@alltypes,@available);
 9604:     my %titles;
 9605:     foreach my $type (@alltypes) {
 9606:         if ($type =~ /^_LC_/) {
 9607:             $titles{$type} = $othertypes{$type};
 9608:         } elsif ($type eq 'default') {
 9609:             $titles{$type} = &mt('All users from [_1]',$dom);
 9610:             if (ref($types) eq 'ARRAY') {
 9611:                 if (@{$types} > 0) {
 9612:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9613:                 }
 9614:             }
 9615:         } elsif (ref($usertypes) eq 'HASH') {
 9616:             $titles{$type} = $usertypes->{$type};
 9617:         }
 9618:     }
 9619:     return (\@alltypes,\%othertypes,\%titles);
 9620: }
 9621: 
 9622: sub loadbalance_rule_row {
 9623:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9624:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9625:     my @rulenames;
 9626:     my %ruletitles = &offloadtype_text();
 9627:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9628:         @rulenames = ('balancer','offloadedto','specific');
 9629:     } else {
 9630:         @rulenames = ('default','homeserver');
 9631:         if ($type eq '_LC_external') {
 9632:             push(@rulenames,'externalbalancer');
 9633:         } else {
 9634:             push(@rulenames,'specific');
 9635:         }
 9636:         push(@rulenames,'none');
 9637:     }
 9638:     my $style = $targets_div_style;
 9639:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9640:         $style = $homedom_div_style;
 9641:     }
 9642:     my $space;
 9643:     if ($islast && $num == 1) {
 9644:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9645:     }
 9646:     my $output =
 9647:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9648:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9649:         '<td valaign="top">'.$space.
 9650:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9651:     for (my $i=0; $i<@rulenames; $i++) {
 9652:         my $rule = $rulenames[$i];
 9653:         my ($checked,$extra);
 9654:         if ($rulenames[$i] eq 'default') {
 9655:             $rule = '';
 9656:         }
 9657:         if ($rulenames[$i] eq 'specific') {
 9658:             if (ref($servers) eq 'HASH') {
 9659:                 my $default;
 9660:                 if (($current ne '') && (exists($servers->{$current}))) {
 9661:                     $checked = ' checked="checked"';
 9662:                 }
 9663:                 unless ($checked) {
 9664:                     $default = ' selected="selected"';
 9665:                 }
 9666:                 $extra =
 9667:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9668:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9669:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9670:                     '<option value=""'.$default.'></option>'."\n";
 9671:                 foreach my $server (sort(keys(%{$servers}))) {
 9672:                     if (ref($currbalancer) eq 'HASH') {
 9673:                         next if (exists($currbalancer->{$server}));
 9674:                     }
 9675:                     my $selected;
 9676:                     if ($server eq $current) {
 9677:                         $selected = ' selected="selected"';
 9678:                     }
 9679:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9680:                 }
 9681:                 $extra .= '</select>';
 9682:             }
 9683:         } elsif ($rule eq $current) {
 9684:             $checked = ' checked="checked"';
 9685:         }
 9686:         $output .= '<span class="LC_nobreak"><label>'.
 9687:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9688:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9689:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9690:                    ')"'.$checked.' />&nbsp;';
 9691:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9692:             $output .= $ruletitles{'particular'};
 9693:         } else {
 9694:             $output .= $ruletitles{$rulenames[$i]};
 9695:         }
 9696:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9697:     }
 9698:     $output .= '</div></td></tr>'."\n";
 9699:     return $output;
 9700: }
 9701: 
 9702: sub offloadtype_text {
 9703:     my %ruletitles = &Apache::lonlocal::texthash (
 9704:            'default'          => 'Offloads to default destinations',
 9705:            'homeserver'       => "Offloads to user's home server",
 9706:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9707:            'specific'         => 'Offloads to specific server',
 9708:            'none'             => 'No offload',
 9709:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9710:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9711:            'particular'       => 'Session hosted (after re-auth) on server:',
 9712:     );
 9713:     return %ruletitles;
 9714: }
 9715: 
 9716: sub sparestype_titles {
 9717:     my %typestitles = &Apache::lonlocal::texthash (
 9718:                           'primary' => 'primary',
 9719:                           'default' => 'default',
 9720:                       );
 9721:     return %typestitles;
 9722: }
 9723: 
 9724: sub contact_titles {
 9725:     my %titles = &Apache::lonlocal::texthash (
 9726:                    'supportemail'    => 'Support E-mail address',
 9727:                    'adminemail'      => 'Default Server Admin E-mail address',
 9728:                    'errormail'       => 'Error reports to be e-mailed to',
 9729:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9730:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9731:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9732:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9733:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9734:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9735:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9736:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9737:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9738:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9739:                    'errorweights'    => 'Weights used to compute error count',
 9740:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9741:                  );
 9742:     my %short_titles = &Apache::lonlocal::texthash (
 9743:                            adminemail   => 'Admin E-mail address',
 9744:                            supportemail => 'Support E-mail',
 9745:                        );   
 9746:     return (\%titles,\%short_titles);
 9747: }
 9748: 
 9749: sub helpform_fields {
 9750:     my %titles =  &Apache::lonlocal::texthash (
 9751:                        'username'   => 'Name',
 9752:                        'user'       => 'Username/domain',
 9753:                        'phone'      => 'Phone',
 9754:                        'cc'         => 'Cc e-mail',
 9755:                        'course'     => 'Course Details',
 9756:                        'section'    => 'Sections',
 9757:                        'screenshot' => 'File upload',
 9758:     );
 9759:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9760:     my %possoptions = (
 9761:                         username     => ['yes','no','req'],
 9762:                         phone        => ['yes','no','req'],
 9763:                         user         => ['yes','no'],
 9764:                         cc           => ['yes','no'],
 9765:                         course       => ['yes','no'],
 9766:                         section      => ['yes','no'],
 9767:                         screenshot   => ['yes','no'],
 9768:                       );
 9769:     my %fieldoptions = &Apache::lonlocal::texthash (
 9770:                          'yes'  => 'Optional',
 9771:                          'req'  => 'Required',
 9772:                          'no'   => "Not shown",
 9773:     );
 9774:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9775: }
 9776: 
 9777: sub tool_titles {
 9778:     my %titles = &Apache::lonlocal::texthash (
 9779:                      aboutme    => 'Personal web page',
 9780:                      blog       => 'Blog',
 9781:                      portfolio  => 'Portfolio',
 9782:                      portaccess => 'Share portfolio files',
 9783:                      timezone   => 'Can set time zone',
 9784:                      official   => 'Official courses (with institutional codes)',
 9785:                      unofficial => 'Unofficial courses',
 9786:                      community  => 'Communities',
 9787:                      textbook   => 'Textbook courses',
 9788:                      placement  => 'Placement tests',
 9789:                  );
 9790:     return %titles;
 9791: }
 9792: 
 9793: sub courserequest_titles {
 9794:     my %titles = &Apache::lonlocal::texthash (
 9795:                                    official   => 'Official',
 9796:                                    unofficial => 'Unofficial',
 9797:                                    community  => 'Communities',
 9798:                                    textbook   => 'Textbook',
 9799:                                    placement  => 'Placement tests',
 9800:                                    lti        => 'LTI Provider',
 9801:                                    norequest  => 'Not allowed',
 9802:                                    approval   => 'Approval by DC',
 9803:                                    validate   => 'With validation',
 9804:                                    autolimit  => 'Numerical limit',
 9805:                                    unlimited  => '(blank for unlimited)',
 9806:                  );
 9807:     return %titles;
 9808: }
 9809: 
 9810: sub authorrequest_titles {
 9811:     my %titles = &Apache::lonlocal::texthash (
 9812:                                    norequest  => 'Not allowed',
 9813:                                    approval   => 'Approval by Dom. Coord.',
 9814:                                    automatic  => 'Automatic approval',
 9815:                  );
 9816:     return %titles;
 9817: }
 9818: 
 9819: sub courserequest_conditions {
 9820:     my %conditions = &Apache::lonlocal::texthash (
 9821:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9822:        validate   => '(Processing of request subject to institutional validation).',
 9823:                  );
 9824:     return %conditions;
 9825: }
 9826: 
 9827: 
 9828: sub print_usercreation {
 9829:     my ($position,$dom,$settings,$rowtotal) = @_;
 9830:     my $numinrow = 4;
 9831:     my $datatable;
 9832:     if ($position eq 'top') {
 9833:         $$rowtotal ++;
 9834:         my $rowcount = 0;
 9835:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9836:         if (ref($rules) eq 'HASH') {
 9837:             if (keys(%{$rules}) > 0) {
 9838:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9839:                                                 $ruleorder,$numinrow,$rowcount);
 9840:                 $$rowtotal ++;
 9841:                 $rowcount ++;
 9842:             }
 9843:         }
 9844:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9845:         if (ref($idrules) eq 'HASH') {
 9846:             if (keys(%{$idrules}) > 0) {
 9847:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9848:                                                 $idruleorder,$numinrow,$rowcount);
 9849:                 $$rowtotal ++;
 9850:                 $rowcount ++;
 9851:             }
 9852:         }
 9853:         if ($rowcount == 0) {
 9854:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9855:             $$rowtotal ++;
 9856:             $rowcount ++;
 9857:         }
 9858:     } elsif ($position eq 'middle') {
 9859:         my @creators = ('author','course','requestcrs');
 9860:         my ($rules,$ruleorder) =
 9861:             &Apache::lonnet::inst_userrules($dom,'username');
 9862:         my %lt = &usercreation_types();
 9863:         my %checked;
 9864:         if (ref($settings) eq 'HASH') {
 9865:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9866:                 foreach my $item (@creators) {
 9867:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9868:                 }
 9869:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9870:                 foreach my $item (@creators) {
 9871:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9872:                         $checked{$item} = 'none';
 9873:                     }
 9874:                 }
 9875:             }
 9876:         }
 9877:         my $rownum = 0;
 9878:         foreach my $item (@creators) {
 9879:             $rownum ++;
 9880:             if ($checked{$item} eq '') {
 9881:                 $checked{$item} = 'any';
 9882:             }
 9883:             my $css_class;
 9884:             if ($rownum%2) {
 9885:                 $css_class = '';
 9886:             } else {
 9887:                 $css_class = ' class="LC_odd_row" ';
 9888:             }
 9889:             $datatable .= '<tr'.$css_class.'>'.
 9890:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9891:                          '</span></td><td style="text-align: right">';
 9892:             my @options = ('any');
 9893:             if (ref($rules) eq 'HASH') {
 9894:                 if (keys(%{$rules}) > 0) {
 9895:                     push(@options,('official','unofficial'));
 9896:                 }
 9897:             }
 9898:             push(@options,'none');
 9899:             foreach my $option (@options) {
 9900:                 my $type = 'radio';
 9901:                 my $check = ' ';
 9902:                 if ($checked{$item} eq $option) {
 9903:                     $check = ' checked="checked" ';
 9904:                 } 
 9905:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9906:                               '<input type="'.$type.'" name="can_createuser_'.
 9907:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9908:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9909:             }
 9910:             $datatable .= '</td></tr>';
 9911:         }
 9912:     } else {
 9913:         my @contexts = ('author','course','domain');
 9914:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9915:         my %checked;
 9916:         if (ref($settings) eq 'HASH') {
 9917:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9918:                 foreach my $item (@contexts) {
 9919:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9920:                         foreach my $auth (@authtypes) {
 9921:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9922:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9923:                             }
 9924:                         }
 9925:                     }
 9926:                 }
 9927:             }
 9928:         } else {
 9929:             foreach my $item (@contexts) {
 9930:                 foreach my $auth (@authtypes) {
 9931:                     $checked{$item}{$auth} = ' checked="checked" ';
 9932:                 }
 9933:             }
 9934:         }
 9935:         my %title = &context_names();
 9936:         my %authname = &authtype_names();
 9937:         my $rownum = 0;
 9938:         my $css_class; 
 9939:         foreach my $item (@contexts) {
 9940:             if ($rownum%2) {
 9941:                 $css_class = '';
 9942:             } else {
 9943:                 $css_class = ' class="LC_odd_row" ';
 9944:             }
 9945:             $datatable .=   '<tr'.$css_class.'>'.
 9946:                             '<td>'.$title{$item}.
 9947:                             '</td><td class="LC_left_item">'.
 9948:                             '<span class="LC_nobreak">';
 9949:             foreach my $auth (@authtypes) {
 9950:                 $datatable .= '<label>'. 
 9951:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9952:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9953:                               $authname{$auth}.'</label>&nbsp;';
 9954:             }
 9955:             $datatable .= '</span></td></tr>';
 9956:             $rownum ++;
 9957:         }
 9958:         $$rowtotal += $rownum;
 9959:     }
 9960:     return $datatable;
 9961: }
 9962: 
 9963: sub print_selfcreation {
 9964:     my ($position,$dom,$settings,$rowtotal) = @_;
 9965:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9966:         $emaildomain,$datatable);
 9967:     if (ref($settings) eq 'HASH') {
 9968:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9969:             $createsettings = $settings->{'cancreate'};
 9970:             if (ref($createsettings) eq 'HASH') {
 9971:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9972:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9973:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9974:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9975:                         @selfcreate = ('email','login','sso');
 9976:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9977:                         @selfcreate = ($createsettings->{'selfcreate'});
 9978:                     }
 9979:                 }
 9980:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9981:                     $processing = $createsettings->{'selfcreateprocessing'};
 9982:                 }
 9983:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9984:                     $emailoptions = $createsettings->{'emailoptions'};
 9985:                 }
 9986:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9987:                     $emailverified = $createsettings->{'emailverified'};
 9988:                 }
 9989:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9990:                     $emaildomain = $createsettings->{'emaildomain'};
 9991:                 }
 9992:             }
 9993:         }
 9994:     }
 9995:     my %radiohash;
 9996:     my $numinrow = 4;
 9997:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9998:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9999:     if ($position eq 'top') {
10000:         my %choices = &Apache::lonlocal::texthash (
10001:                                                       cancreate_login      => 'Institutional Login',
10002:                                                       cancreate_sso        => 'Institutional Single Sign On',
10003:                                                   );
10004:         my @toggles = sort(keys(%choices));
10005:         my %defaultchecked = (
10006:                                'cancreate_login' => 'off',
10007:                                'cancreate_sso'   => 'off',
10008:                              );
10009:         my ($onclick,$itemcount);
10010:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10011:                                                      \%choices,$itemcount,$onclick);
10012:         $$rowtotal += $itemcount;
10013: 
10014:         if (ref($usertypes) eq 'HASH') {
10015:             if (keys(%{$usertypes}) > 0) {
10016:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
10017:                                              $dom,$numinrow,$othertitle,
10018:                                              'statustocreate',$rowtotal);
10019:                 $$rowtotal ++;
10020:             }
10021:         }
10022:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
10023:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10024:         $fieldtitles{'inststatus'} = &mt('Institutional status');
10025:         my $rem;
10026:         my $numperrow = 2;
10027:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
10028:         $datatable .= '<tr'.$css_class.'>'.
10029:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
10030:                      '<td class="LC_left_item">'."\n".
10031:                      '<table>'."\n";
10032:         for (my $i=0; $i<@fields; $i++) {
10033:             $rem = $i%($numperrow);
10034:             if ($rem == 0) {
10035:                 if ($i > 0) {
10036:                     $datatable .= '</tr>';
10037:                 }
10038:                 $datatable .= '<tr>';
10039:             }
10040:             my $currval;
10041:             if (ref($createsettings) eq 'HASH') {
10042:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
10043:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
10044:                 }
10045:             }
10046:             $datatable .= '<td class="LC_left_item">'.
10047:                           '<span class="LC_nobreak">'.
10048:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
10049:                           'value="'.$currval.'" size="10" />&nbsp;'.
10050:                           $fieldtitles{$fields[$i]}.'</span></td>';
10051:         }
10052:         my $colsleft = $numperrow - $rem;
10053:         if ($colsleft > 1 ) {
10054:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10055:                          '&nbsp;</td>';
10056:         } elsif ($colsleft == 1) {
10057:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
10058:         }
10059:         $datatable .= '</tr></table></td></tr>';
10060:         $$rowtotal ++;
10061:     } elsif ($position eq 'middle') {
10062:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
10063:         my @posstypes;
10064:         if (ref($types) eq 'ARRAY') {
10065:             @posstypes = @{$types};
10066:         }
10067:         unless (grep(/^default$/,@posstypes)) {
10068:             push(@posstypes,'default');
10069:         }
10070:         my %usertypeshash;
10071:         if (ref($usertypes) eq 'HASH') {
10072:             %usertypeshash = %{$usertypes};
10073:         }
10074:         $usertypeshash{'default'} = $othertitle;
10075:         foreach my $status (@posstypes) {
10076:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
10077:                                                    $numinrow,$$rowtotal,\%usertypeshash);
10078:             $$rowtotal ++;
10079:         }
10080:     } else {
10081:         my %choices = &Apache::lonlocal::texthash (
10082:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
10083:                                                   );
10084:         my @toggles = sort(keys(%choices));
10085:         my %defaultchecked = (
10086:                                'cancreate_email' => 'off',
10087:                              );
10088:         my $customclass = 'LC_selfcreate_email';
10089:         my $classprefix = 'LC_canmodify_emailusername_';
10090:         my $optionsprefix = 'LC_options_emailusername_';
10091:         my $display = 'none';
10092:         my $rowstyle = 'display:none';
10093:         if (grep(/^\Qemail\E$/,@selfcreate)) {
10094:             $display = 'block';
10095:             $rowstyle = 'display:table-row';
10096:         }
10097:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
10098:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10099:                                                      \%choices,$$rowtotal,$onclick);
10100:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
10101:                                          $rowstyle);
10102:         $$rowtotal ++;
10103:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
10104:                                       $rowstyle);
10105:         $$rowtotal ++;
10106:         my (@ordered,@posstypes,%usertypeshash);
10107:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10108:         my ($emailrules,$emailruleorder) =
10109:             &Apache::lonnet::inst_userrules($dom,'email');
10110:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
10111:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
10112:         if (ref($types) eq 'ARRAY') {
10113:             @posstypes = @{$types};
10114:         }
10115:         if (@posstypes) {
10116:             unless (grep(/^default$/,@posstypes)) {
10117:                 push(@posstypes,'default');
10118:             }
10119:             if (ref($usertypes) eq 'HASH') {
10120:                 %usertypeshash = %{$usertypes};
10121:             }
10122:             my $currassign;
10123:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
10124:                 $currassign = {
10125:                                   selfassign => $domdefaults{'inststatusguest'},
10126:                               };
10127:                 @ordered = @{$domdefaults{'inststatusguest'}};
10128:             } else {
10129:                 $currassign = { selfassign => [] };
10130:             }
10131:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
10132:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
10133:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
10134:                                          $numinrow,$othertitle,'selfassign',
10135:                                          $rowtotal,$onclicktypes,$customclass,
10136:                                          $rowstyle);
10137:             $$rowtotal ++;
10138:             $usertypeshash{'default'} = $othertitle;
10139:             foreach my $status (@posstypes) {
10140:                 my $css_class;
10141:                 if ($$rowtotal%2) {
10142:                     $css_class = 'LC_odd_row ';
10143:                 }
10144:                 $css_class .= $customclass;
10145:                 my $rowid = $optionsprefix.$status;
10146:                 my $hidden = 1;
10147:                 my $currstyle = 'display:none';
10148:                 if (grep(/^\Q$status\E$/,@ordered)) {
10149:                     $currstyle = $rowstyle;
10150:                     $hidden = 0;
10151:                 }
10152:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10153:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
10154:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
10155:                 unless ($hidden) {
10156:                     $$rowtotal ++;
10157:                 }
10158:             }
10159:         } else {
10160:             my $css_class;
10161:             if ($$rowtotal%2) {
10162:                 $css_class = 'LC_odd_row ';
10163:             }
10164:             $css_class .= $customclass;
10165:             $usertypeshash{'default'} = $othertitle;
10166:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10167:                                          $emailrules,$emailruleorder,$settings,'default','',
10168:                                          $othertitle,$css_class,$rowstyle,$intdom);
10169:             $$rowtotal ++;
10170:         }
10171:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10172:         $numinrow = 1;
10173:         if (@posstypes) {
10174:             foreach my $status (@posstypes) {
10175:                 my $rowid = $classprefix.$status;
10176:                 my $datarowstyle = 'display:none';
10177:                 if (grep(/^\Q$status\E$/,@ordered)) {
10178:                     $datarowstyle = $rowstyle;
10179:                 }
10180:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
10181:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10182:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
10183:                 unless ($datarowstyle eq 'display:none') {
10184:                     $$rowtotal ++;
10185:                 }
10186:             }
10187:         } else {
10188:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
10189:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10190:                                                    $infotitles,'',$customclass,$rowstyle);
10191:         }
10192:     }
10193:     return $datatable;
10194: }
10195: 
10196: sub selfcreate_javascript {
10197:     return <<"ENDSCRIPT";
10198: 
10199: <script type="text/javascript">
10200: // <![CDATA[
10201: 
10202: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
10203:     var x = document.getElementsByClassName(target);
10204:     var insttypes = 0;
10205:     var insttypeRegExp = new RegExp(prefix);
10206:     if ((x.length != undefined) && (x.length > 0)) {
10207:         if (form.elements[radio].length != undefined) {
10208:             for (var i=0; i<form.elements[radio].length; i++) {
10209:                 if (form.elements[radio][i].checked) {
10210:                     if (form.elements[radio][i].value == 1) {
10211:                         for (var j=0; j<x.length; j++) {
10212:                             if (x[j].id == 'undefined') {
10213:                                 x[j].style.display = 'table-row';
10214:                             } else if (insttypeRegExp.test(x[j].id)) {
10215:                                 insttypes ++;
10216:                             } else {
10217:                                 x[j].style.display = 'table-row';
10218:                             }
10219:                         }
10220:                     } else {
10221:                         for (var j=0; j<x.length; j++) {
10222:                             x[j].style.display = 'none';
10223:                         }
10224:                     }
10225:                     break;
10226:                 }
10227:             }
10228:             if (insttypes > 0) {
10229:                 toggleDataRow(form,checkbox,target,altprefix);
10230:                 toggleDataRow(form,checkbox,target,prefix,1);
10231:             }
10232:         }
10233:     }
10234:     return;
10235: }
10236: 
10237: function toggleDataRow(form,checkbox,target,prefix,docount) {
10238:     if (form.elements[checkbox].length != undefined) {
10239:         var count = 0;
10240:         if (docount) {
10241:             for (var i=0; i<form.elements[checkbox].length; i++) {
10242:                 if (form.elements[checkbox][i].checked) {
10243:                     count ++;
10244:                 }
10245:             }
10246:         }
10247:         for (var i=0; i<form.elements[checkbox].length; i++) {
10248:             var type = form.elements[checkbox][i].value;
10249:             if (document.getElementById(prefix+type)) {
10250:                 if (form.elements[checkbox][i].checked) {
10251:                     document.getElementById(prefix+type).style.display = 'table-row';
10252:                     if (count % 2 == 1) {
10253:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
10254:                     } else {
10255:                         document.getElementById(prefix+type).className = target;
10256:                     }
10257:                     count ++;
10258:                 } else {
10259:                     document.getElementById(prefix+type).style.display = 'none';
10260:                 }
10261:             }
10262:         }
10263:     }
10264:     return;
10265: }
10266: 
10267: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
10268:     var caller = radio+'_'+status;
10269:     if (form.elements[caller].length != undefined) {
10270:         for (var i=0; i<form.elements[caller].length; i++) {
10271:             if (form.elements[caller][i].checked) {
10272:                 if (document.getElementById(altprefix+'_inst_'+status)) {
10273:                     var curr = form.elements[caller][i].value;
10274:                     if (prefix) {
10275:                         document.getElementById(prefix+'_'+status).style.display = 'none';
10276:                     }
10277:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
10278:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
10279:                     if (curr == 'custom') {
10280:                         if (prefix) {
10281:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
10282:                         }
10283:                     } else if (curr == 'inst') {
10284:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
10285:                     } else if (curr == 'noninst') {
10286:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
10287:                     }
10288:                     break;
10289:                 }
10290:             }
10291:         }
10292:     }
10293: }
10294: 
10295: // ]]>
10296: </script>
10297: 
10298: ENDSCRIPT
10299: }
10300: 
10301: sub noninst_users {
10302:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
10303:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
10304:     my $class = 'LC_left_item';
10305:     if ($css_class) {
10306:         $css_class = ' class="'.$css_class.'"';
10307:     }
10308:     if ($rowid) {
10309:         $rowid = ' id="'.$rowid.'"';
10310:     }
10311:     if ($rowstyle) {
10312:         $rowstyle = ' style="'.$rowstyle.'"';
10313:     }
10314:     my ($output,$description);
10315:     if ($type eq 'default') {
10316:         $description = &mt('Requests for: [_1]',$typetitle);
10317:     } else {
10318:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
10319:     }
10320:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
10321:               "<td>$description</td>\n".
10322:               '<td class="'.$class.'" colspan="2">'.
10323:               '<table><tr>';
10324:     my %headers = &Apache::lonlocal::texthash(
10325:               approve  => 'Processing',
10326:               email    => 'E-mail',
10327:               username => 'Username',
10328:     );
10329:     foreach my $item ('approve','email','username') {
10330:         $output .= '<th>'.$headers{$item}.'</th>';
10331:     }
10332:     $output .= '</tr><tr>';
10333:     foreach my $item ('approve','email','username') {
10334:         $output .= '<td style="vertical-align: top">';
10335:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
10336:         if ($item eq 'approve') {
10337:             %choices = &Apache::lonlocal::texthash (
10338:                                                      automatic => 'Automatically approved',
10339:                                                      approval  => 'Queued for approval',
10340:                                                    );
10341:             @options = ('automatic','approval');
10342:             $hashref = $processing;
10343:             $defoption = 'automatic';
10344:             $name = 'cancreate_emailprocess_'.$type;
10345:         } elsif ($item eq 'email') {
10346:             %choices = &Apache::lonlocal::texthash (
10347:                                                      any     => 'Any e-mail',
10348:                                                      inst    => 'Institutional only',
10349:                                                      noninst => 'Non-institutional only',
10350:                                                      custom  => 'Custom restrictions',
10351:                                                    );
10352:             @options = ('any','inst','noninst');
10353:             my $showcustom;
10354:             if (ref($emailrules) eq 'HASH') {
10355:                 if (keys(%{$emailrules}) > 0) {
10356:                     push(@options,'custom');
10357:                     $showcustom = 'cancreate_emailrule';
10358:                     if (ref($settings) eq 'HASH') {
10359:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
10360:                             foreach my $rule (@{$settings->{'email_rule'}}) {
10361:                                 if (exists($emailrules->{$rule})) {
10362:                                     $hascustom ++;
10363:                                 }
10364:                             }
10365:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
10366:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
10367:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
10368:                                     if (exists($emailrules->{$rule})) {
10369:                                         $hascustom ++;
10370:                                     }
10371:                                 }
10372:                             }
10373:                         }
10374:                     }
10375:                 }
10376:             }
10377:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
10378:                                                      "'cancreate_emaildomain','$type'".');"';
10379:             $hashref = $emailoptions;
10380:             $defoption = 'any';
10381:             $name = 'cancreate_emailoptions_'.$type;
10382:         } elsif ($item eq 'username') {
10383:             %choices = &Apache::lonlocal::texthash (
10384:                                                      all    => 'Same as e-mail',
10385:                                                      first  => 'Omit @domain',
10386:                                                      free   => 'Free to choose',
10387:                                                    );
10388:             @options = ('all','first','free');
10389:             $hashref = $emailverified;
10390:             $defoption = 'all';
10391:             $name = 'cancreate_usernameoptions_'.$type;
10392:         }
10393:         foreach my $option (@options) {
10394:             my $checked;
10395:             if (ref($hashref) eq 'HASH') {
10396:                 if ($type eq '') {
10397:                     if (!exists($hashref->{'default'})) {
10398:                         if ($option eq $defoption) {
10399:                             $checked = ' checked="checked"';
10400:                         }
10401:                     } else {
10402:                         if ($hashref->{'default'} eq $option) {
10403:                             $checked = ' checked="checked"';
10404:                         }
10405:                     }
10406:                 } else {
10407:                     if (!exists($hashref->{$type})) {
10408:                         if ($option eq $defoption) {
10409:                             $checked = ' checked="checked"';
10410:                         }
10411:                     } else {
10412:                         if ($hashref->{$type} eq $option) {
10413:                             $checked = ' checked="checked"';
10414:                         }
10415:                     }
10416:                 }
10417:             } elsif (($item eq 'email') && ($hascustom)) {
10418:                 if ($option eq 'custom') {
10419:                     $checked = ' checked="checked"';
10420:                 }
10421:             } elsif ($option eq $defoption) {
10422:                 $checked = ' checked="checked"';
10423:             }
10424:             $output .= '<span class="LC_nobreak"><label>'.
10425:                        '<input type="radio" name="'.$name.'"'.
10426:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10427:                        $choices{$option}.'</label></span><br />';
10428:             if ($item eq 'email') {
10429:                 if ($option eq 'custom') {
10430:                     my $id = 'cancreate_emailrule_'.$type;
10431:                     my $display = 'none';
10432:                     if ($checked) {
10433:                         $display = 'inline';
10434:                     }
10435:                     my $numinrow = 2;
10436:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10437:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10438:                                &user_formats_row('email',$settings,$emailrules,
10439:                                                  $emailruleorder,$numinrow,'',$type);
10440:                               '</table></fieldset>';
10441:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10442:                     my %text = &Apache::lonlocal::texthash (
10443:                                                              inst    => 'must end:',
10444:                                                              noninst => 'cannot end:',
10445:                                                            );
10446:                     my $value;
10447:                     if (ref($emaildomain) eq 'HASH') {
10448:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10449:                             $value = $emaildomain->{$type}->{$option};
10450:                         }
10451:                     }
10452:                     if ($value eq '') {
10453:                         $value = '@'.$intdom;
10454:                     }
10455:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10456:                     my $display = 'none';
10457:                     if ($checked) {
10458:                         $display = 'inline';
10459:                     }
10460:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10461:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10462:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10463:                                '</div>';
10464:                 }
10465:             }
10466:         }
10467:         $output .= '</td>'."\n";
10468:     }
10469:     $output .= "</tr></table></td></tr>\n";
10470:     return $output;
10471: }
10472: 
10473: sub captcha_choice {
10474:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10475:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10476:         $vertext,$currver);
10477:     my %lt = &captcha_phrases();
10478:     $keyentry = 'hidden';
10479:     my $colspan=2;
10480:     if ($context eq 'cancreate') {
10481:         $rowname = &mt('CAPTCHA validation');
10482:     } elsif ($context eq 'login') {
10483:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10484:     } elsif ($context eq 'passwords') {
10485:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10486:         $colspan=1;
10487:     }
10488:     if (ref($settings) eq 'HASH') {
10489:         if ($settings->{'captcha'}) {
10490:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10491:         } else {
10492:             $checked{'original'} = ' checked="checked"';
10493:         }
10494:         if ($settings->{'captcha'} eq 'recaptcha') {
10495:             $pubtext = $lt{'pub'};
10496:             $privtext = $lt{'priv'};
10497:             $keyentry = 'text';
10498:             $vertext = $lt{'ver'};
10499:             $currver = $settings->{'recaptchaversion'};
10500:             if ($currver ne '2') {
10501:                 $currver = 1;
10502:             }
10503:         }
10504:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10505:             $currpub = $settings->{'recaptchakeys'}{'public'};
10506:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10507:         }
10508:     } else {
10509:         $checked{'original'} = ' checked="checked"';
10510:     }
10511:     my $css_class;
10512:     if ($itemcount%2) {
10513:         $css_class = 'LC_odd_row';
10514:     }
10515:     if ($customcss) {
10516:         $css_class .= " $customcss";
10517:     }
10518:     $css_class =~ s/^\s+//;
10519:     if ($css_class) {
10520:         $css_class = ' class="'.$css_class.'"';
10521:     }
10522:     if ($rowstyle) {
10523:         $css_class .= ' style="'.$rowstyle.'"';
10524:     }
10525:     my $output = '<tr'.$css_class.'>'.
10526:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10527:                  '<table><tr><td>'."\n";
10528:     foreach my $option ('original','recaptcha','notused') {
10529:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10530:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10531:                    $lt{$option}.'</label></span>';
10532:         unless ($option eq 'notused') {
10533:             $output .= ('&nbsp;'x2)."\n";
10534:         }
10535:     }
10536: #
10537: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10538: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10539: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10540: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10541: #
10542:     $output .= '</td></tr>'."\n".
10543:                '<tr><td class="LC_zero_height">'."\n".
10544:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10545:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10546:                $currpub.'" size="40" /></span><br />'."\n".
10547:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10548:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10549:                $currpriv.'" size="40" /></span><br />'.
10550:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10551:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10552:                $currver.'" size="3" /></span><br />'.
10553:                '</td></tr></table>'."\n".
10554:                '</td></tr>';
10555:     return $output;
10556: }
10557: 
10558: sub user_formats_row {
10559:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10560:     my $output;
10561:     my %text = (
10562:                    'username' => 'new usernames',
10563:                    'id'       => 'IDs',
10564:                );
10565:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10566:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10567:         $output = '<tr '.$css_class.'>'.
10568:                   '<td><span class="LC_nobreak">'.
10569:                   &mt("Format rules to check for $text{$type}: ").
10570:                   '</td><td class="LC_left_item" colspan="2"><table>';
10571:     }
10572:     my $rem;
10573:     if (ref($ruleorder) eq 'ARRAY') {
10574:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10575:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10576:                 my $rem = $i%($numinrow);
10577:                 if ($rem == 0) {
10578:                     if ($i > 0) {
10579:                         $output .= '</tr>';
10580:                     }
10581:                     $output .= '<tr>';
10582:                 }
10583:                 my $check = ' ';
10584:                 if (ref($settings) eq 'HASH') {
10585:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10586:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10587:                             $check = ' checked="checked" ';
10588:                         }
10589:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10590:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10591:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10592:                                 $check = ' checked="checked" ';
10593:                             }
10594:                         }
10595:                     }
10596:                 }
10597:                 my $name = $type.'_rule';
10598:                 if ($type eq 'email') {
10599:                     $name .= '_'.$status;
10600:                 }
10601:                 $output .= '<td class="LC_left_item">'.
10602:                            '<span class="LC_nobreak"><label>'.
10603:                            '<input type="checkbox" name="'.$name.'" '.
10604:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10605:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10606:             }
10607:         }
10608:         $rem = @{$ruleorder}%($numinrow);
10609:     }
10610:     my $colsleft;
10611:     if ($rem) {
10612:         $colsleft = $numinrow - $rem;
10613:     }
10614:     if ($colsleft > 1 ) {
10615:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10616:                    '&nbsp;</td>';
10617:     } elsif ($colsleft == 1) {
10618:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10619:     }
10620:     $output .= '</tr>';
10621:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10622:         $output .= '</table></td></tr>';
10623:     }
10624:     return $output;
10625: }
10626: 
10627: sub usercreation_types {
10628:     my %lt = &Apache::lonlocal::texthash (
10629:                     author     => 'When adding a co-author',
10630:                     course     => 'When adding a user to a course',
10631:                     requestcrs => 'When requesting a course',
10632:                     any        => 'Any',
10633:                     official   => 'Institutional only ',
10634:                     unofficial => 'Non-institutional only',
10635:                     none       => 'None',
10636:     );
10637:     return %lt;
10638: }
10639: 
10640: sub selfcreation_types {
10641:     my %lt = &Apache::lonlocal::texthash (
10642:                     selfcreate => 'User creates own account',
10643:                     any        => 'Any',
10644:                     official   => 'Institutional only ',
10645:                     unofficial => 'Non-institutional only',
10646:                     email      => 'E-mail address',
10647:                     login      => 'Institutional Login',
10648:                     sso        => 'SSO',
10649:              );
10650: }
10651: 
10652: sub authtype_names {
10653:     my %lt = &Apache::lonlocal::texthash(
10654:                       int    => 'Internal',
10655:                       krb4   => 'Kerberos 4',
10656:                       krb5   => 'Kerberos 5',
10657:                       loc    => 'Local',
10658:                       lti    => 'LTI',
10659:                   );
10660:     return %lt;
10661: }
10662: 
10663: sub context_names {
10664:     my %context_title = &Apache::lonlocal::texthash(
10665:        author => 'Creating users when an Author',
10666:        course => 'Creating users when in a course',
10667:        domain => 'Creating users when a Domain Coordinator',
10668:     );
10669:     return %context_title;
10670: }
10671: 
10672: sub print_usermodification {
10673:     my ($position,$dom,$settings,$rowtotal) = @_;
10674:     my $numinrow = 4;
10675:     my ($context,$datatable,$rowcount);
10676:     if ($position eq 'top') {
10677:         $rowcount = 0;
10678:         $context = 'author'; 
10679:         foreach my $role ('ca','aa') {
10680:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10681:                                                    $numinrow,$rowcount);
10682:             $$rowtotal ++;
10683:             $rowcount ++;
10684:         }
10685:     } elsif ($position eq 'middle') {
10686:         $rowcount = 0;
10687:         $context = 'coauthor';
10688:         foreach my $role ('ca','aa') {
10689:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10690:                                                    $numinrow,$rowcount);
10691:             $$rowtotal ++;
10692:             $rowcount ++;
10693:         }
10694:     } elsif ($position eq 'bottom') {
10695:         $context = 'course';
10696:         $rowcount = 0;
10697:         foreach my $role ('st','ep','ta','in','cr') {
10698:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10699:                                                    $numinrow,$rowcount);
10700:             $$rowtotal ++;
10701:             $rowcount ++;
10702:         }
10703:     }
10704:     return $datatable;
10705: }
10706: 
10707: sub print_defaults {
10708:     my ($position,$dom,$settings,$rowtotal) = @_;
10709:     my $rownum = 0;
10710:     my ($datatable,$css_class,$titles);
10711:     unless ($position eq 'bottom') {
10712:         $titles = &defaults_titles($dom);
10713:     }
10714:     if ($position eq 'top') {
10715:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10716:                      'datelocale_def','portal_def');
10717:         my %defaults;
10718:         if (ref($settings) eq 'HASH') {
10719:             %defaults = %{$settings};
10720:         } else {
10721:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10722:             foreach my $item (@items) {
10723:                 $defaults{$item} = $domdefaults{$item};
10724:             }
10725:         }
10726:         foreach my $item (@items) {
10727:             if ($rownum%2) {
10728:                 $css_class = '';
10729:             } else {
10730:                 $css_class = ' class="LC_odd_row" ';
10731:             }
10732:             $datatable .= '<tr'.$css_class.'>'.
10733:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10734:                           '</span></td><td class="LC_right_item" colspan="3">';
10735:             if ($item eq 'auth_def') {
10736:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10737:                 my %shortauth = (
10738:                                  internal => 'int',
10739:                                  krb4 => 'krb4',
10740:                                  krb5 => 'krb5',
10741:                                  localauth  => 'loc',
10742:                                  lti => 'lti',
10743:                                 );
10744:                 my %authnames = &authtype_names();
10745:                 foreach my $auth (@authtypes) {
10746:                     my $checked = ' ';
10747:                     if ($defaults{$item} eq $auth) {
10748:                         $checked = ' checked="checked" ';
10749:                     }
10750:                     $datatable .= '<label><input type="radio" name="'.$item.
10751:                                   '" value="'.$auth.'"'.$checked.'/>'.
10752:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10753:                 }
10754:             } elsif ($item eq 'timezone_def') {
10755:                 my $includeempty = 1;
10756:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10757:             } elsif ($item eq 'datelocale_def') {
10758:                 my $includeempty = 1;
10759:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10760:             } elsif ($item eq 'lang_def') {
10761:                 my $includeempty = 1;
10762:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10763:             } elsif ($item eq 'portal_def') {
10764:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10765:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10766:                 my $portalsty = 'none';
10767:                 if ($defaults{$item}) {
10768:                     $portalsty = 'block';
10769:                 }
10770:                 foreach my $field ('email','web') {
10771:                     my $checkedoff = ' checked="checked"';
10772:                     my $checkedon;
10773:                     if ($defaults{$item.'_'.$field}) {
10774:                         $checkedon = $checkedoff;
10775:                         $checkedoff = '';
10776:                     }
10777:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10778:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10779:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10780:                               ('&nbsp;'x2).
10781:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10782:                               '</div>';
10783:                 }
10784:             } else {
10785:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10786:             }
10787:             $datatable .= '</td></tr>';
10788:             $rownum ++;
10789:         }
10790:     } elsif ($position eq 'middle') {
10791:         my %defaults;
10792:         if (ref($settings) eq 'HASH') {
10793:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10794:                 my $maxnum = @{$settings->{'inststatusorder'}};
10795:                 for (my $i=0; $i<$maxnum; $i++) {
10796:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10797:                     my $item = $settings->{'inststatusorder'}->[$i];
10798:                     my $title = $settings->{'inststatustypes'}->{$item};
10799:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10800:                     $datatable .= '<tr'.$css_class.'>'.
10801:                                   '<td><span class="LC_nobreak">'.
10802:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10803:                     for (my $k=0; $k<=$maxnum; $k++) {
10804:                         my $vpos = $k+1;
10805:                         my $selstr;
10806:                         if ($k == $i) {
10807:                             $selstr = ' selected="selected" ';
10808:                         }
10809:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10810:                     }
10811:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10812:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10813:                                   &mt('delete').'</span></td>'.
10814:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10815:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10816:                                   '</span></td></tr>';
10817:                 }
10818:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10819:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10820:                 $datatable .= '<tr '.$css_class.'>'.
10821:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10822:                 for (my $k=0; $k<=$maxnum; $k++) {
10823:                     my $vpos = $k+1;
10824:                     my $selstr;
10825:                     if ($k == $maxnum) {
10826:                         $selstr = ' selected="selected" ';
10827:                     }
10828:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10829:                 }
10830:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10831:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10832:                               '&nbsp;'.&mt('(new)').
10833:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10834:                               &mt('Name displayed').':'.
10835:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10836:                               '</tr>'."\n";
10837:                 $rownum ++;
10838:             }
10839:         }
10840:     } else {
10841:         my ($unamemaprules,$ruleorder) =
10842:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10843:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10844:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10845:             my $numinrow = 2;
10846:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10847:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10848:                                             $ruleorder,$numinrow).
10849:                           '</table></td></tr>';
10850:         }
10851:         if ($datatable eq '') {
10852:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10853:                           &mt('No rules set for domain in customized localenroll.pm').
10854:                           '</td></tr>';
10855:         }
10856:     }
10857:     $$rowtotal += $rownum;
10858:     return $datatable;
10859: }
10860: 
10861: sub get_languages_hash {
10862:     my %langchoices;
10863:     foreach my $id (&Apache::loncommon::languageids()) {
10864:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10865:         if ($code ne '') {
10866:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10867:         }
10868:     }
10869:     return %langchoices;
10870: }
10871: 
10872: sub defaults_titles {
10873:     my ($dom) = @_;
10874:     my %titles = &Apache::lonlocal::texthash (
10875:                    'auth_def'      => 'Default authentication type',
10876:                    'auth_arg_def'  => 'Default authentication argument',
10877:                    'lang_def'      => 'Default language',
10878:                    'timezone_def'  => 'Default timezone',
10879:                    'datelocale_def' => 'Default locale for dates',
10880:                    'portal_def'     => 'Portal/Default URL',
10881:                    'email'          => 'Email links use portal URL',
10882:                    'web'            => 'Public web links use portal URL',
10883:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10884:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10885:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10886:                  );
10887:     if ($dom) {
10888:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10889:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10890:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10891:         $protocol = 'http' if ($protocol ne 'https');
10892:         if ($uint_dom) {
10893:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10894:                                          $uint_dom);
10895:         }
10896:     }
10897:     return (\%titles);
10898: }
10899: 
10900: sub print_scantron {
10901:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10902:     if ($position eq 'top') {
10903:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10904:     } else {
10905:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10906:     }
10907: }
10908: 
10909: sub scantron_javascript {
10910:     return <<"ENDSCRIPT";
10911: 
10912: <script type="text/javascript">
10913: // <![CDATA[
10914: 
10915: function toggleScantron(form) {
10916:     var csvfieldset = new Array();
10917:     if (document.getElementById('scantroncsv_cols')) {
10918:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10919:     }
10920:     if (document.getElementById('scantroncsv_options')) {
10921:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10922:     }
10923:     if (csvfieldset.length) {
10924:         if (document.getElementById('scantronconfcsv')) {
10925:             var scantroncsv = document.getElementById('scantronconfcsv');
10926:             if (scantroncsv.checked) {
10927:                 for (var i=0; i<csvfieldset.length; i++) {
10928:                     csvfieldset[i].style.display = 'block';
10929:                 }
10930:             } else {
10931:                 for (var i=0; i<csvfieldset.length; i++) {
10932:                     csvfieldset[i].style.display = 'none';
10933:                 }
10934:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10935:                 if (csvselects.length) {
10936:                     for (var j=0; j<csvselects.length; j++) {
10937:                         csvselects[j].selectedIndex = 0;
10938:                     }
10939:                 }
10940:             }
10941:         }
10942:     }
10943:     return;
10944: }
10945: // ]]>
10946: </script>
10947: 
10948: ENDSCRIPT
10949: 
10950: }
10951: 
10952: sub print_scantronformat {
10953:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10954:     my $itemcount = 1;
10955:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10956:         %confhash);
10957:     my $switchserver = &check_switchserver($dom,$confname);
10958:     my %lt = &Apache::lonlocal::texthash (
10959:                 default => 'Default bubblesheet format file error',
10960:                 custom  => 'Custom bubblesheet format file error',
10961:              );
10962:     my %scantronfiles = (
10963:         default => 'default.tab',
10964:         custom => 'custom.tab',
10965:     );
10966:     foreach my $key (keys(%scantronfiles)) {
10967:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10968:                               .$scantronfiles{$key};
10969:     }
10970:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10971:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10972:         if (!$switchserver) {
10973:             my $servadm = $r->dir_config('lonAdmEMail');
10974:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10975:             if ($configuserok eq 'ok') {
10976:                 if ($author_ok eq 'ok') {
10977:                     my %legacyfile = (
10978:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10979:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10980:                     );
10981:                     my %md5chk;
10982:                     foreach my $type (keys(%legacyfile)) {
10983:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10984:                         chomp($md5chk{$type});
10985:                     }
10986:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10987:                         foreach my $type (keys(%legacyfile)) {
10988:                             ($scantronurls{$type},my $error) =
10989:                                 &legacy_scantronformat($r,$dom,$confname,
10990:                                                  $type,$legacyfile{$type},
10991:                                                  $scantronurls{$type},
10992:                                                  $scantronfiles{$type});
10993:                             if ($error ne '') {
10994:                                 $error{$type} = $error;
10995:                             }
10996:                         }
10997:                         if (keys(%error) == 0) {
10998:                             $is_custom = 1;
10999:                             $confhash{'scantron'}{'scantronformat'} =
11000:                                 $scantronurls{'custom'};
11001:                             my $putresult =
11002:                                 &Apache::lonnet::put_dom('configuration',
11003:                                                          \%confhash,$dom);
11004:                             if ($putresult ne 'ok') {
11005:                                 $error{'custom'} =
11006:                                     '<span class="LC_error">'.
11007:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11008:                             }
11009:                         }
11010:                     } else {
11011:                         ($scantronurls{'default'},my $error) =
11012:                             &legacy_scantronformat($r,$dom,$confname,
11013:                                           'default',$legacyfile{'default'},
11014:                                           $scantronurls{'default'},
11015:                                           $scantronfiles{'default'});
11016:                         if ($error eq '') {
11017:                             $confhash{'scantron'}{'scantronformat'} = ''; 
11018:                             my $putresult =
11019:                                 &Apache::lonnet::put_dom('configuration',
11020:                                                          \%confhash,$dom);
11021:                             if ($putresult ne 'ok') {
11022:                                 $error{'default'} =
11023:                                     '<span class="LC_error">'.
11024:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11025:                             }
11026:                         } else {
11027:                             $error{'default'} = $error;
11028:                         }
11029:                     }
11030:                 }
11031:             }
11032:         } else {
11033:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
11034:         }
11035:     }
11036:     if (ref($settings) eq 'HASH') {
11037:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
11038:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
11039:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
11040:                 $scantronurl = '';
11041:             } else {
11042:                 $scantronurl = $settings->{'scantronformat'};
11043:             }
11044:             $is_custom = 1;
11045:         } else {
11046:             $scantronurl = $scantronurls{'default'};
11047:         }
11048:     } else {
11049:         if ($is_custom) {
11050:             $scantronurl = $scantronurls{'custom'};
11051:         } else {
11052:             $scantronurl = $scantronurls{'default'};
11053:         }
11054:     }
11055:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11056:     $datatable .= '<tr'.$css_class.'>';
11057:     if (!$is_custom) {
11058:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
11059:                       '<span class="LC_nobreak">';
11060:         if ($scantronurl) {
11061:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
11062:                                                          undef,undef,undef,undef,'background-color:#ffffff');
11063:         } else {
11064:             $datatable = &mt('File unavailable for display');
11065:         }
11066:         $datatable .= '</span></td>';
11067:         if (keys(%error) == 0) { 
11068:             $datatable .= '<td style="vertical-align: bottom">';
11069:             if (!$switchserver) {
11070:                 $datatable .= &mt('Upload:').'<br />';
11071:             }
11072:         } else {
11073:             my $errorstr;
11074:             foreach my $key (sort(keys(%error))) {
11075:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11076:             }
11077:             $datatable .= '<td>'.$errorstr;
11078:         }
11079:     } else {
11080:         if (keys(%error) > 0) {
11081:             my $errorstr;
11082:             foreach my $key (sort(keys(%error))) {
11083:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11084:             } 
11085:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
11086:         } elsif ($scantronurl) {
11087:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
11088:                                                        undef,undef,undef,undef,'background-color:#ffffff');
11089:             $datatable .= '<td><span class="LC_nobreak">'.
11090:                           $link.
11091:                           '<label><input type="checkbox" name="scantronformat_del"'.
11092:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
11093:                           '<td><span class="LC_nobreak">&nbsp;'.
11094:                           &mt('Replace:').'</span><br />';
11095:         }
11096:     }
11097:     if (keys(%error) == 0) {
11098:         if ($switchserver) {
11099:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
11100:         } else {
11101:             $datatable .='<span class="LC_nobreak">&nbsp;'.
11102:                          '<input type="file" name="scantronformat" /></span>';
11103:         }
11104:     }
11105:     $datatable .= '</td></tr>';
11106:     $$rowtotal ++;
11107:     return $datatable;
11108: }
11109: 
11110: sub legacy_scantronformat {
11111:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
11112:     my ($url,$error);
11113:     my @statinfo = &Apache::lonnet::stat_file($newurl);
11114:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
11115:         my $modified = [];
11116:         (my $result,$url) =
11117:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
11118:                                                     'scantron','','',$newfile,$modified);
11119:         if ($result eq 'ok') {
11120:             &update_modify_urls($r,$modified);
11121:         } else {
11122:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
11123:         }
11124:     }
11125:     return ($url,$error);
11126: }
11127: 
11128: sub print_scantronconfig {
11129:     my ($dom,$settings,$rowtotal) = @_;
11130:     my $itemcount = 2;
11131:     my $is_checked = ' checked="checked"';
11132:     my %optionson = (
11133:                      hdr => ' checked="checked"',
11134:                      pad => ' checked="checked"',
11135:                      rem => ' checked="checked"',
11136:                     );
11137:     my %optionsoff = (
11138:                       hdr => '',
11139:                       pad => '',
11140:                       rem => '',
11141:                      );
11142:     my $currcsvsty = 'none';
11143:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
11144:     my @fields = &scantroncsv_fields();
11145:     my %titles = &scantronconfig_titles();
11146:     if (ref($settings) eq 'HASH') {
11147:         if (ref($settings->{config}) eq 'HASH') {
11148:             if ($settings->{config}->{dat}) {
11149:                 $checked{'dat'} = $is_checked;
11150:             }
11151:             if (ref($settings->{config}->{csv}) eq 'HASH') {
11152:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
11153:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
11154:                     if (keys(%csvfields) > 0) {
11155:                         $checked{'csv'} = $is_checked;
11156:                         $currcsvsty = 'block';
11157:                     }
11158:                 }
11159:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
11160:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
11161:                     foreach my $option (keys(%optionson)) {
11162:                         unless ($csvoptions{$option}) {
11163:                             $optionsoff{$option} = $optionson{$option};
11164:                             $optionson{$option} = '';
11165:                         }
11166:                     }
11167:                 }
11168:             }
11169:         } else {
11170:             $checked{'dat'} = $is_checked;
11171:         }
11172:     } else {
11173:         $checked{'dat'} = $is_checked;
11174:     }
11175:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
11176:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11177:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
11178:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
11179:     foreach my $item ('dat','csv') {
11180:         my $id;
11181:         if ($item eq 'csv') {
11182:             $id = 'id="scantronconfcsv" ';
11183:         }
11184:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
11185:                       $titles{$item}.'</label>'.('&nbsp;'x3);
11186:         if ($item eq 'csv') {
11187:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
11188:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
11189:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
11190:             foreach my $col (@fields) {
11191:                 my $selnone;
11192:                 if ($csvfields{$col} eq '') {
11193:                     $selnone = ' selected="selected"';
11194:                 }
11195:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
11196:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
11197:                               '<option value=""'.$selnone.'></option>';
11198:                 for (my $i=0; $i<20; $i++) {
11199:                     my $shown = $i+1;
11200:                     my $sel;
11201:                     unless ($selnone) {
11202:                         if (exists($csvfields{$col})) {
11203:                             if ($csvfields{$col} == $i) {
11204:                                 $sel = ' selected="selected"';
11205:                             }
11206:                         }
11207:                     }
11208:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
11209:                 }
11210:                 $datatable .= '</select></td></tr>';
11211:            }
11212:            $datatable .= '</table></fieldset>'.
11213:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
11214:                          '<legend>'.&mt('CSV Options').'</legend>';
11215:            foreach my $option ('hdr','pad','rem') {
11216:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
11217:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
11218:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
11219:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
11220:            }
11221:            $datatable .= '</fieldset>';
11222:            $itemcount ++;
11223:         }
11224:     }
11225:     $datatable .= '</td></tr>';
11226:     $$rowtotal ++;
11227:     return $datatable;
11228: }
11229: 
11230: sub scantronconfig_titles {
11231:     return &Apache::lonlocal::texthash(
11232:                                           dat => 'Standard format (.dat)',
11233:                                           csv => 'Comma separated values (.csv)',
11234:                                           hdr => 'Remove first line in file (contains column titles)',
11235:                                           pad => 'Prepend 0s to PaperID',
11236:                                           rem => 'Remove leading spaces (except Question Response columns)',
11237:                                           CODE => 'CODE',
11238:                                           ID   => 'Student ID',
11239:                                           PaperID => 'Paper ID',
11240:                                           FirstName => 'First Name',
11241:                                           LastName => 'Last Name',
11242:                                           FirstQuestion => 'First Question Response',
11243:                                           Section => 'Section',
11244:     );
11245: }
11246: 
11247: sub scantroncsv_fields {
11248:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
11249: }
11250: 
11251: sub print_coursecategories {
11252:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
11253:     my $datatable;
11254:     if ($position eq 'top') {
11255:         my (%checked);
11256:         my @catitems = ('unauth','auth');
11257:         my @cattypes = ('std','domonly','codesrch','none');
11258:         $checked{'unauth'} = 'std';
11259:         $checked{'auth'} = 'std';
11260:         if (ref($settings) eq 'HASH') {
11261:             foreach my $type (@cattypes) {
11262:                 if ($type eq $settings->{'unauth'}) {
11263:                     $checked{'unauth'} = $type;
11264:                 }
11265:                 if ($type eq $settings->{'auth'}) {
11266:                     $checked{'auth'} = $type;
11267:                 }
11268:             }
11269:         }
11270:         my %lt = &Apache::lonlocal::texthash (
11271:                                                unauth   => 'Catalog type for unauthenticated users',
11272:                                                auth     => 'Catalog type for authenticated users',
11273:                                                none     => 'No catalog',
11274:                                                std      => 'Standard catalog',
11275:                                                domonly  => 'Domain-only catalog',
11276:                                                codesrch => "Code search form",
11277:                                              );
11278:        my $itemcount = 0;
11279:        foreach my $item (@catitems) {
11280:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11281:            $datatable .= '<tr '.$css_class.'>'.
11282:                          '<td>'.$lt{$item}.'</td>'.
11283:                          '<td class="LC_right_item"><span class="LC_nobreak">';
11284:            foreach my $type (@cattypes) {
11285:                my $ischecked;
11286:                if ($checked{$item} eq $type) {
11287:                    $ischecked=' checked="checked"';
11288:                }
11289:                $datatable .= '<label>'.
11290:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
11291:                              ' />'.$lt{$type}.'</label>&nbsp;';
11292:            }
11293:            $datatable .= '</span></td></tr>';
11294:            $itemcount ++;
11295:         }
11296:         $$rowtotal += $itemcount;
11297:     } elsif ($position eq 'middle') {
11298:         my $toggle_cats_crs = ' ';
11299:         my $toggle_cats_dom = ' checked="checked" ';
11300:         my $can_cat_crs = ' ';
11301:         my $can_cat_dom = ' checked="checked" ';
11302:         my $toggle_catscomm_comm = ' ';
11303:         my $toggle_catscomm_dom = ' checked="checked" ';
11304:         my $can_catcomm_comm = ' ';
11305:         my $can_catcomm_dom = ' checked="checked" ';
11306:         my $toggle_catsplace_place = ' ';
11307:         my $toggle_catsplace_dom = ' checked="checked" ';
11308:         my $can_catplace_place = ' ';
11309:         my $can_catplace_dom = ' checked="checked" ';
11310: 
11311:         if (ref($settings) eq 'HASH') {
11312:             if ($settings->{'togglecats'} eq 'crs') {
11313:                 $toggle_cats_crs = $toggle_cats_dom;
11314:                 $toggle_cats_dom = ' ';
11315:             }
11316:             if ($settings->{'categorize'} eq 'crs') {
11317:                 $can_cat_crs = $can_cat_dom;
11318:                 $can_cat_dom = ' ';
11319:             }
11320:             if ($settings->{'togglecatscomm'} eq 'comm') {
11321:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
11322:                 $toggle_catscomm_dom = ' ';
11323:             }
11324:             if ($settings->{'categorizecomm'} eq 'comm') {
11325:                 $can_catcomm_comm = $can_catcomm_dom;
11326:                 $can_catcomm_dom = ' ';
11327:             }
11328:             if ($settings->{'togglecatsplace'} eq 'place') {
11329:                 $toggle_catsplace_place = $toggle_catsplace_dom;
11330:                 $toggle_catsplace_dom = ' ';
11331:             }
11332:             if ($settings->{'categorizeplace'} eq 'place') {
11333:                 $can_catplace_place = $can_catplace_dom;
11334:                 $can_catplace_dom = ' ';
11335:             }
11336:         }
11337:         my %title = &Apache::lonlocal::texthash (
11338:                      togglecats      => 'Show/Hide a course in catalog',
11339:                      togglecatscomm  => 'Show/Hide a community in catalog',
11340:                      togglecatsplace => 'Show/Hide a placement test in catalog',
11341:                      categorize      => 'Assign a category to a course',
11342:                      categorizecomm  => 'Assign a category to a community',
11343:                      categorizeplace => 'Assign a category to a placement test',
11344:                     );
11345:         my %level = &Apache::lonlocal::texthash (
11346:                      dom   => 'Set in Domain',
11347:                      crs   => 'Set in Course',
11348:                      comm  => 'Set in Community',
11349:                      place => 'Set in Placement Test',
11350:                     );
11351:         $datatable = '<tr class="LC_odd_row">'.
11352:                   '<td>'.$title{'togglecats'}.'</td>'.
11353:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11354:                   '<input type="radio" name="togglecats"'.
11355:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11356:                   '<label><input type="radio" name="togglecats"'.
11357:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
11358:                   '</tr><tr>'.
11359:                   '<td>'.$title{'categorize'}.'</td>'.
11360:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11361:                   '<label><input type="radio" name="categorize"'.
11362:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11363:                   '<label><input type="radio" name="categorize"'.
11364:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
11365:                   '</tr><tr class="LC_odd_row">'.
11366:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
11367:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11368:                   '<input type="radio" name="togglecatscomm"'.
11369:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11370:                   '<label><input type="radio" name="togglecatscomm"'.
11371:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
11372:                   '</tr><tr>'.
11373:                   '<td>'.$title{'categorizecomm'}.'</td>'.
11374:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11375:                   '<label><input type="radio" name="categorizecomm"'.
11376:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11377:                   '<label><input type="radio" name="categorizecomm"'.
11378:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
11379:                   '</tr><tr class="LC_odd_row">'.
11380:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
11381:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11382:                   '<input type="radio" name="togglecatsplace"'.
11383:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11384:                   '<label><input type="radio" name="togglecatscomm"'.
11385:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
11386:                   '</tr><tr>'.
11387:                   '<td>'.$title{'categorizeplace'}.'</td>'.
11388:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11389:                   '<label><input type="radio" name="categorizeplace"'.
11390:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11391:                   '<label><input type="radio" name="categorizeplace"'.
11392:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
11393:                   '</tr>';
11394:         $$rowtotal += 6;
11395:     } else {
11396:         my $css_class;
11397:         my $itemcount = 1;
11398:         my $cathash; 
11399:         if (ref($settings) eq 'HASH') {
11400:             $cathash = $settings->{'cats'};
11401:         }
11402:         if (ref($cathash) eq 'HASH') {
11403:             my (@cats,@trails,%allitems,%idx,@jsarray);
11404:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11405:                                                    \%allitems,\%idx,\@jsarray);
11406:             my $maxdepth = scalar(@cats);
11407:             my $colattrib = '';
11408:             if ($maxdepth > 2) {
11409:                 $colattrib = ' colspan="2" ';
11410:             }
11411:             my @path;
11412:             if (@cats > 0) {
11413:                 if (ref($cats[0]) eq 'ARRAY') {
11414:                     my $numtop = @{$cats[0]};
11415:                     my $maxnum = $numtop;
11416:                     my %default_names = (
11417:                           instcode    => &mt('Official courses'),
11418:                           communities => &mt('Communities'),
11419:                           placement   => &mt('Placement Tests'),
11420:                     );
11421: 
11422:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11423:                         ($cathash->{'instcode::0'} eq '') ||
11424:                         (!grep(/^communities$/,@{$cats[0]})) || 
11425:                         ($cathash->{'communities::0'} eq '') ||
11426:                         (!grep(/^placement$/,@{$cats[0]})) ||
11427:                         ($cathash->{'placement::0'} eq '')) {
11428:                         $maxnum ++;
11429:                     }
11430:                     my $lastidx;
11431:                     for (my $i=0; $i<$numtop; $i++) {
11432:                         my $parent = $cats[0][$i];
11433:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11434:                         my $item = &escape($parent).'::0';
11435:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11436:                         $lastidx = $idx{$item};
11437:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11438:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11439:                         for (my $k=0; $k<=$maxnum; $k++) {
11440:                             my $vpos = $k+1;
11441:                             my $selstr;
11442:                             if ($k == $i) {
11443:                                 $selstr = ' selected="selected" ';
11444:                             }
11445:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11446:                         }
11447:                         $datatable .= '</select></span></td><td>';
11448:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11449:                             $datatable .=  '<span class="LC_nobreak">'
11450:                                            .$default_names{$parent}.'</span>';
11451:                             if ($parent eq 'instcode') {
11452:                                 $datatable .= '<br /><span class="LC_nobreak">('
11453:                                               .&mt('with institutional codes')
11454:                                               .')</span></td><td'.$colattrib.'>';
11455:                             } else {
11456:                                 $datatable .= '<table><tr><td>';
11457:                             }
11458:                             $datatable .= '<span class="LC_nobreak">'
11459:                                           .'<label><input type="radio" name="'
11460:                                           .$parent.'" value="1" checked="checked" />'
11461:                                           .&mt('Display').'</label>';
11462:                             if ($parent eq 'instcode') {
11463:                                 $datatable .= '&nbsp;';
11464:                             } else {
11465:                                 $datatable .= '</span></td></tr><tr><td>'
11466:                                               .'<span class="LC_nobreak">';
11467:                             }
11468:                             $datatable .= '<label><input type="radio" name="'
11469:                                           .$parent.'" value="0" />'
11470:                                           .&mt('Do not display').'</label></span>';
11471:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11472:                                 $datatable .= '</td></tr></table>';
11473:                             }
11474:                             $datatable .= '</td>';
11475:                         } else {
11476:                             $datatable .= $parent
11477:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11478:                                           .'<input type="checkbox" name="deletecategory" '
11479:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11480:                         }
11481:                         my $depth = 1;
11482:                         push(@path,$parent);
11483:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11484:                         pop(@path);
11485:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11486:                         $itemcount ++;
11487:                     }
11488:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11489:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11490:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11491:                     for (my $k=0; $k<=$maxnum; $k++) {
11492:                         my $vpos = $k+1;
11493:                         my $selstr;
11494:                         if ($k == $numtop) {
11495:                             $selstr = ' selected="selected" ';
11496:                         }
11497:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11498:                     }
11499:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11500:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11501:                                   .'</tr>'."\n";
11502:                     $itemcount ++;
11503:                     foreach my $default ('instcode','communities','placement') {
11504:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11505:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11506:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11507:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11508:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11509:                             for (my $k=0; $k<=$maxnum; $k++) {
11510:                                 my $vpos = $k+1;
11511:                                 my $selstr;
11512:                                 if ($k == $maxnum) {
11513:                                     $selstr = ' selected="selected" ';
11514:                                 }
11515:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11516:                             }
11517:                             $datatable .= '</select></span></td>'.
11518:                                           '<td><span class="LC_nobreak">'.
11519:                                           $default_names{$default}.'</span>';
11520:                             if ($default eq 'instcode') {
11521:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11522:                                               .&mt('with institutional codes').')</span>';
11523:                             }
11524:                             $datatable .= '</td>'
11525:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11526:                                           .&mt('Display').'</label>&nbsp;'
11527:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11528:                                           .&mt('Do not display').'</label></span></td></tr>';
11529:                         }
11530:                     }
11531:                 }
11532:             } else {
11533:                 $datatable .= &initialize_categories($itemcount);
11534:             }
11535:         } else {
11536:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11537:                           .&initialize_categories($itemcount);
11538:         }
11539:         $$rowtotal += $itemcount;
11540:     }
11541:     return $datatable;
11542: }
11543: 
11544: sub print_serverstatuses {
11545:     my ($dom,$settings,$rowtotal) = @_;
11546:     my $datatable;
11547:     my @pages = &serverstatus_pages();
11548:     my (%namedaccess,%machineaccess);
11549:     foreach my $type (@pages) {
11550:         $namedaccess{$type} = '';
11551:         $machineaccess{$type}= '';
11552:     }
11553:     if (ref($settings) eq 'HASH') {
11554:         foreach my $type (@pages) {
11555:             if (exists($settings->{$type})) {
11556:                 if (ref($settings->{$type}) eq 'HASH') {
11557:                     foreach my $key (keys(%{$settings->{$type}})) {
11558:                         if ($key eq 'namedusers') {
11559:                             $namedaccess{$type} = $settings->{$type}->{$key};
11560:                         } elsif ($key eq 'machines') {
11561:                             $machineaccess{$type} = $settings->{$type}->{$key};
11562:                         }
11563:                     }
11564:                 }
11565:             }
11566:         }
11567:     }
11568:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11569:     my $rownum = 0;
11570:     my $css_class;
11571:     foreach my $type (@pages) {
11572:         $rownum ++;
11573:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11574:         $datatable .= '<tr'.$css_class.'>'.
11575:                       '<td><span class="LC_nobreak">'.
11576:                       $titles->{$type}.'</span></td>'.
11577:                       '<td class="LC_left_item">'.
11578:                       '<input type="text" name="'.$type.'_namedusers" '.
11579:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11580:                       '<td class="LC_right_item">'.
11581:                       '<span class="LC_nobreak">'.
11582:                       '<input type="text" name="'.$type.'_machines" '.
11583:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11584:                       '</span></td></tr>'."\n";
11585:     }
11586:     $$rowtotal += $rownum;
11587:     return $datatable;
11588: }
11589: 
11590: sub serverstatus_pages {
11591:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11592:             'checksums','clusterstatus','certstatus','metadata_keywords',
11593:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11594:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11595: }
11596: 
11597: sub defaults_javascript {
11598:     my ($settings) = @_;
11599:     return unless (ref($settings) eq 'HASH');
11600:     my $portal_js = <<"ENDPORTAL";
11601: 
11602: function portalExtras(caller) {
11603:     var x = caller.value;
11604:     var y = new Array('email','web');
11605:     for (var i=0; i<y.length; i++) {
11606:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11607:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11608:             if (x.length > 0) {
11609:                 z.style.display = 'block';
11610:             } else {
11611:                 z.style.display = 'none';
11612:             }
11613:         }
11614:     }
11615: }
11616: ENDPORTAL
11617:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11618:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11619:         if ($maxnum eq '') {
11620:             $maxnum = 0;
11621:         }
11622:         $maxnum ++;
11623:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11624:         return <<"ENDSCRIPT";
11625: <script type="text/javascript">
11626: // <![CDATA[
11627: function reorderTypes(form,caller) {
11628:     var changedVal;
11629: $jstext 
11630:     var newpos = 'addinststatus_pos';
11631:     var current = new Array;
11632:     var maxh = $maxnum;
11633:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11634:     var oldVal;
11635:     if (caller == newpos) {
11636:         changedVal = newitemVal;
11637:     } else {
11638:         var curritem = 'inststatus_pos_'+caller;
11639:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11640:         current[newitemVal] = newpos;
11641:     }
11642:     for (var i=0; i<inststatuses.length; i++) {
11643:         if (inststatuses[i] != caller) {
11644:             var elementName = 'inststatus_pos_'+inststatuses[i];
11645:             if (form.elements[elementName]) {
11646:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11647:                 current[currVal] = elementName;
11648:             }
11649:         }
11650:     }
11651:     for (var j=0; j<maxh; j++) {
11652:         if (current[j] == undefined) {
11653:             oldVal = j;
11654:         }
11655:     }
11656:     if (oldVal < changedVal) {
11657:         for (var k=oldVal+1; k<=changedVal ; k++) {
11658:            var elementName = current[k];
11659:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11660:         }
11661:     } else {
11662:         for (var k=changedVal; k<oldVal; k++) {
11663:             var elementName = current[k];
11664:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11665:         }
11666:     }
11667:     return;
11668: }
11669: 
11670: $portal_js
11671: 
11672: // ]]>
11673: </script>
11674: 
11675: ENDSCRIPT
11676:     } else {
11677: return <<"ENDSCRIPT";
11678: <script type="text/javascript">
11679: // <![CDATA[
11680: $portal_js
11681: // ]]>
11682: </script>
11683: 
11684: ENDSCRIPT
11685:     }
11686:     return;
11687: }
11688: 
11689: sub passwords_javascript {
11690:     my ($prefix) = @_;
11691:     my %intalert;
11692:     if ($prefix eq 'passwords') {
11693:         %intalert = &Apache::lonlocal::texthash (
11694:             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.',
11695:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11696:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11697:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11698:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11699:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11700:         );
11701:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11702:         %intalert = &Apache::lonlocal::texthash (
11703:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11704:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11705:         );
11706:     }
11707:     &js_escape(\%intalert);
11708:     my $defmin = $Apache::lonnet::passwdmin;
11709:     my $intauthjs;
11710:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11711: 
11712: function warnIntAuth(field) {
11713:     if (field.name == 'intauth_check') {
11714:         if (field.value == '2') {
11715:             alert('$intalert{authcheck}');
11716:         }
11717:     }
11718:     if (field.name == 'intauth_cost') {
11719:         field.value.replace(/\s/g,'');
11720:         if (field.value != '') {
11721:             var regexdigit=/^\\d+\$/;
11722:             if (!regexdigit.test(field.value)) {
11723:                 alert('$intalert{authcost}');
11724:             }
11725:         }
11726:     }
11727:     return;
11728: }
11729: 
11730: ENDSCRIPT
11731: 
11732:      }
11733: 
11734:      $intauthjs .= <<"ENDSCRIPT";
11735: 
11736: function warnInt$prefix(field) {
11737:     field.value.replace(/^\s+/,'');
11738:     field.value.replace(/\s+\$/,'');
11739:     var regexdigit=/^\\d+\$/;
11740:     if (field.name == '${prefix}_min') {
11741:         if (field.value == '') {
11742:             alert('$intalert{passmin}');
11743:             field.value = '$defmin';
11744:         } else {
11745:             if (!regexdigit.test(field.value)) {
11746:                 alert('$intalert{passmin}');
11747:                 field.value = '$defmin';
11748:             }
11749:             var minval = parseInt(field.value,10);
11750:             if (minval < $defmin) {
11751:                 alert('$intalert{passmin}');
11752:                 field.value = '$defmin';
11753:             }
11754:         }
11755:     } else {
11756:         if (field.value == '0') {
11757:             field.value = '';
11758:         }
11759:         if (field.value != '') {
11760:             if (field.name == '${prefix}_expire') {
11761:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11762:                 if (!regexpposnum.test(field.value)) {
11763:                     alert('$intalert{passexp}');
11764:                     field.value = '';
11765:                 } else {
11766:                     var expval = parseFloat(field.value);
11767:                     if (expval == 0) {
11768:                         alert('$intalert{passexp}');
11769:                         field.value = '';
11770:                     }
11771:                 }
11772:             } else {
11773:                 if (!regexdigit.test(field.value)) {
11774:                     if (field.name == '${prefix}_max') {
11775:                         alert('$intalert{passmax}');
11776:                     } else {
11777:                         if (field.name == '${prefix}_numsaved') {
11778:                             alert('$intalert{passnum}');
11779:                         }
11780:                     }
11781:                     field.value = '';
11782:                 }
11783:             }
11784:         }
11785:     }
11786:     return;
11787: }
11788: 
11789: ENDSCRIPT
11790:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11791: }
11792: 
11793: sub coursecategories_javascript {
11794:     my ($settings) = @_;
11795:     my ($output,$jstext,$cathash);
11796:     if (ref($settings) eq 'HASH') {
11797:         $cathash = $settings->{'cats'};
11798:     }
11799:     if (ref($cathash) eq 'HASH') {
11800:         my (@cats,@jsarray,%idx);
11801:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11802:         if (@jsarray > 0) {
11803:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11804:             for (my $i=0; $i<@jsarray; $i++) {
11805:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11806:                     my $catstr = join('","',@{$jsarray[$i]});
11807:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11808:                 }
11809:             }
11810:         }
11811:     } else {
11812:         $jstext  = '    var categories = Array(1);'."\n".
11813:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11814:     }
11815:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11816:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11817:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11818:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11819:     &js_escape(\$instcode_reserved);
11820:     &js_escape(\$communities_reserved);
11821:     &js_escape(\$placement_reserved);
11822:     &js_escape(\$choose_again);
11823:     $output = <<"ENDSCRIPT";
11824: <script type="text/javascript">
11825: // <![CDATA[
11826: function reorderCats(form,parent,item,idx) {
11827:     var changedVal;
11828: $jstext
11829:     var newpos = 'addcategory_pos';
11830:     if (parent == '') {
11831:         var has_instcode = 0;
11832:         var maxtop = categories[idx].length;
11833:         for (var j=0; j<maxtop; j++) {
11834:             if (categories[idx][j] == 'instcode::0') {
11835:                 has_instcode == 1;
11836:             }
11837:         }
11838:         if (has_instcode == 0) {
11839:             categories[idx][maxtop] = 'instcode_pos';
11840:         }
11841:     } else {
11842:         newpos += '_'+parent;
11843:     }
11844:     var maxh = 1 + categories[idx].length;
11845:     var current = new Array;
11846:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11847:     if (item == newpos) {
11848:         changedVal = newitemVal;
11849:     } else {
11850:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11851:         current[newitemVal] = newpos;
11852:     }
11853:     for (var i=0; i<categories[idx].length; i++) {
11854:         var elementName = categories[idx][i];
11855:         if (elementName != item) {
11856:             if (form.elements[elementName]) {
11857:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11858:                 current[currVal] = elementName;
11859:             }
11860:         }
11861:     }
11862:     var oldVal;
11863:     for (var j=0; j<maxh; j++) {
11864:         if (current[j] == undefined) {
11865:             oldVal = j;
11866:         }
11867:     }
11868:     if (oldVal < changedVal) {
11869:         for (var k=oldVal+1; k<=changedVal ; k++) {
11870:            var elementName = current[k];
11871:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11872:         }
11873:     } else {
11874:         for (var k=changedVal; k<oldVal; k++) {
11875:             var elementName = current[k];
11876:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11877:         }
11878:     }
11879:     return;
11880: }
11881: 
11882: function categoryCheck(form) {
11883:     if (form.elements['addcategory_name'].value == 'instcode') {
11884:         alert('$instcode_reserved\\n$choose_again');
11885:         return false;
11886:     }
11887:     if (form.elements['addcategory_name'].value == 'communities') {
11888:         alert('$communities_reserved\\n$choose_again');
11889:         return false;
11890:     }
11891:     if (form.elements['addcategory_name'].value == 'placement') {
11892:         alert('$placement_reserved\\n$choose_again');
11893:         return false;
11894:     }
11895:     return true;
11896: }
11897: 
11898: // ]]>
11899: </script>
11900: 
11901: ENDSCRIPT
11902:     return $output;
11903: }
11904: 
11905: sub initialize_categories {
11906:     my ($itemcount) = @_;
11907:     my ($datatable,$css_class,$chgstr);
11908:     my %default_names = &Apache::lonlocal::texthash (
11909:                       instcode    => 'Official courses (with institutional codes)',
11910:                       communities => 'Communities',
11911:                       placement   => 'Placement Tests',
11912:                         );
11913:     my %selnum = (
11914:                    instcode    => '0',
11915:                    communities => '1',
11916:                    placement   => '2',
11917:                  );
11918:     my %selected;
11919:     foreach my $default ('instcode','communities','placement') {
11920:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11921:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11922:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11923:         $selected{$selnum{$default}} = ' selected="selected"';
11924:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11925:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11926:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11927:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11928:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11929:                      .'<option value="3">4</option></select>&nbsp;'
11930:                      .$default_names{$default}
11931:                      .'</span></td><td><span class="LC_nobreak">'
11932:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11933:                      .&mt('Display').'</label>&nbsp;<label>'
11934:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11935:                  .'</label></span></td></tr>';
11936:         $itemcount ++;
11937:     }
11938:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11939:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11940:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11941:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11942:                   .'<option value="0">1</option>'
11943:                   .'<option value="1">2</option>'
11944:                   .'<option value="2">3</option>'
11945:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11946:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11947:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11948:                   .'</td></tr>';
11949:     return $datatable;
11950: }
11951: 
11952: sub build_category_rows {
11953:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11954:     my ($text,$name,$item,$chgstr);
11955:     if (ref($cats) eq 'ARRAY') {
11956:         my $maxdepth = scalar(@{$cats});
11957:         if (ref($cats->[$depth]) eq 'HASH') {
11958:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11959:                 my $numchildren = @{$cats->[$depth]{$parent}};
11960:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11961:                 $text .= '<td><table class="LC_data_table">';
11962:                 my ($idxnum,$parent_name,$parent_item);
11963:                 my $higher = $depth - 1;
11964:                 if ($higher == 0) {
11965:                     $parent_name = &escape($parent).'::'.$higher;
11966:                 } else {
11967:                     if (ref($path) eq 'ARRAY') {
11968:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11969:                     }
11970:                 }
11971:                 $parent_item = 'addcategory_pos_'.$parent_name;
11972:                 for (my $j=0; $j<=$numchildren; $j++) {
11973:                     if ($j < $numchildren) {
11974:                         $name = $cats->[$depth]{$parent}[$j];
11975:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11976:                         $idxnum = $idx->{$item};
11977:                     } else {
11978:                         $name = $parent_name;
11979:                         $item = $parent_item;
11980:                     }
11981:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11982:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11983:                     for (my $i=0; $i<=$numchildren; $i++) {
11984:                         my $vpos = $i+1;
11985:                         my $selstr;
11986:                         if ($j == $i) {
11987:                             $selstr = ' selected="selected" ';
11988:                         }
11989:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11990:                     }
11991:                     $text .= '</select>&nbsp;';
11992:                     if ($j < $numchildren) {
11993:                         my $deeper = $depth+1;
11994:                         $text .= $name.'&nbsp;'
11995:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11996:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11997:                         if(ref($path) eq 'ARRAY') {
11998:                             push(@{$path},$name);
11999:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
12000:                             pop(@{$path});
12001:                         }
12002:                     } else {
12003:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
12004:                         if ($j == $numchildren) {
12005:                             $text .= $name;
12006:                         } else {
12007:                             $text .= $item;
12008:                         }
12009:                         $text .= '" value="" />';
12010:                     }
12011:                     $text .= '</td></tr>';
12012:                 }
12013:                 $text .= '</table></td>';
12014:             } else {
12015:                 my $higher = $depth-1;
12016:                 if ($higher == 0) {
12017:                     $name = &escape($parent).'::'.$higher;
12018:                 } else {
12019:                     if (ref($path) eq 'ARRAY') {
12020:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12021:                     }
12022:                 }
12023:                 my $colspan;
12024:                 if ($parent ne 'instcode') {
12025:                     $colspan = $maxdepth - $depth - 1;
12026:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
12027:                 }
12028:             }
12029:         }
12030:     }
12031:     return $text;
12032: }
12033: 
12034: sub modifiable_userdata_row {
12035:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
12036:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
12037:     my ($role,$rolename,$statustype);
12038:     $role = $item;
12039:     if ($context eq 'cancreate') {
12040:         if ($item =~ /^(emailusername)_(.+)$/) {
12041:             $role = $1;
12042:             $statustype = $2;
12043:             if (ref($usertypes) eq 'HASH') {
12044:                 if ($usertypes->{$statustype}) {
12045:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
12046:                 } else {
12047:                     $rolename = &mt('Data provided by user');
12048:                 }
12049:             }
12050:         }
12051:     } elsif ($context eq 'selfcreate') {
12052:         if (ref($usertypes) eq 'HASH') {
12053:             $rolename = $usertypes->{$role};
12054:         } else {
12055:             $rolename = $role;
12056:         }
12057:     } elsif ($context eq 'lti') {
12058:         $rolename = &mt('Institutional data used (if available)');
12059:     } elsif ($context eq 'privacy') {
12060:         $rolename = $itemdesc;
12061:     } else {
12062:         if ($role eq 'cr') {
12063:             $rolename = &mt('Custom role');
12064:         } else {
12065:             $rolename = &Apache::lonnet::plaintext($role);
12066:         }
12067:     }
12068:     my (@fields,%fieldtitles);
12069:     if (ref($fieldsref) eq 'ARRAY') {
12070:         @fields = @{$fieldsref};
12071:     } else {
12072:         @fields = ('lastname','firstname','middlename','generation',
12073:                    'permanentemail','id');
12074:     }
12075:     if ((ref($titlesref) eq 'HASH')) {
12076:         %fieldtitles = %{$titlesref};
12077:     } else {
12078:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12079:     }
12080:     my $output;
12081:     my $css_class;
12082:     if ($rowcount%2) {
12083:         $css_class = 'LC_odd_row';
12084:     }
12085:     if ($customcss) {
12086:         $css_class .= " $customcss";
12087:     }
12088:     $css_class =~ s/^\s+//;
12089:     if ($css_class) {
12090:         $css_class = ' class="'.$css_class.'"';
12091:     }
12092:     if ($rowstyle) {
12093:         $css_class .= ' style="'.$rowstyle.'"';
12094:     }
12095:     if ($rowid) {
12096:         $rowid = ' id="'.$rowid.'"';
12097:     }
12098:     $output = '<tr '.$css_class.$rowid.'>'.
12099:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
12100:               '<td class="LC_left_item" colspan="2"><table>';
12101:     my $rem;
12102:     my %checks;
12103:     if (ref($settings) eq 'HASH') {
12104:         my $hashref;
12105:         if ($context eq 'lti') {
12106:             if (ref($settings) eq 'HASH') {
12107:                 my %instdata;
12108:                 if (ref($settings->{'instdata'}) eq 'ARRAY') {
12109:                     map { $instdata{$_} = 1; } @{$settings->{'instdata'}};
12110:                 }
12111:                 $hashref = \%instdata;
12112:             }
12113:         } elsif ($context eq 'privacy') {
12114:             my ($key,$inner) = split(/_/,$role);
12115:             if (ref($settings) eq 'HASH') {
12116:                 if (ref($settings->{$key}) eq 'HASH') {
12117:                     $hashref = $settings->{$key}->{$inner};
12118:                 }
12119:             }
12120:         } elsif (ref($settings->{$context}) eq 'HASH') {
12121:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
12122:                 $hashref = $settings->{$context}->{$role};
12123:             }
12124:             if ($role eq 'emailusername') {
12125:                 if ($statustype) {
12126:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
12127:                         $hashref = $settings->{$context}->{$role}->{$statustype};
12128:                     }
12129:                 }
12130:             }
12131:         }
12132:         if (ref($hashref) eq 'HASH') {
12133:             foreach my $field (@fields) {
12134:                 if ($hashref->{$field}) {
12135:                     if ($role eq 'emailusername') {
12136:                         $checks{$field} = $hashref->{$field};
12137:                     } else {
12138:                         $checks{$field} = ' checked="checked" ';
12139:                     }
12140:                 }
12141:             }
12142:         }
12143:     }
12144:     my $total = scalar(@fields);
12145:     for (my $i=0; $i<$total; $i++) {
12146:         $rem = $i%($numinrow);
12147:         if ($rem == 0) {
12148:             if ($i > 0) {
12149:                 $output .= '</tr>';
12150:             }
12151:             $output .= '<tr>';
12152:         }
12153:         my $check = ' ';
12154:         unless ($role eq 'emailusername') {
12155:             if (exists($checks{$fields[$i]})) {
12156:                 $check = $checks{$fields[$i]};
12157:             } elsif ($context eq 'privacy') {
12158:                 if ($role =~ /^priv_(domain|course)$/) {
12159:                     if (ref($settings) ne 'HASH') {
12160:                         $check = ' checked="checked" ';
12161:                     }
12162:                 } elsif ($role =~ /^priv_(author|community)$/) {
12163:                     if (ref($settings) ne 'HASH') {
12164:                         unless ($fields[$i] eq 'id') {
12165:                             $check = ' checked="checked" ';
12166:                         }
12167:                     }
12168:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
12169:                     if (ref($settings) ne 'HASH') {
12170:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
12171:                             $check = ' checked="checked" ';
12172:                         }
12173:                     }
12174:                 }
12175:             } elsif ($context ne 'lti') {
12176:                 if ($role eq 'st') {
12177:                     if (ref($settings) ne 'HASH') {
12178:                         $check = ' checked="checked" '; 
12179:                     }
12180:                 }
12181:             }
12182:         }
12183:         $output .= '<td class="LC_left_item">'.
12184:                    '<span class="LC_nobreak">';
12185:         my $prefix = 'canmodify';
12186:         if ($role eq 'emailusername') {
12187:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
12188:                 $checks{$fields[$i]} = 'omit';
12189:             }
12190:             foreach my $option ('required','optional','omit') {
12191:                 my $checked='';
12192:                 if ($checks{$fields[$i]} eq $option) {
12193:                     $checked='checked="checked" ';
12194:                 }
12195:                 $output .= '<label>'.
12196:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
12197:                            &mt($option).'</label>'.('&nbsp;' x2);
12198:             }
12199:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
12200:         } else {
12201:             if ($context eq 'lti') {
12202:                 $prefix = 'lti';
12203:             } elsif ($context eq 'coauthor') {
12204:                 $prefix = 'cacanmodify';
12205:             } elsif ($context eq 'privacy') {
12206:                 $prefix = 'privacy';
12207:             }
12208:             $output .= '<label>'.
12209:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
12210:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
12211:                        '</label>';
12212:         }
12213:         $output .= '</span></td>';
12214:     }
12215:     $rem = $total%$numinrow;
12216:     my $colsleft;
12217:     if ($rem) {
12218:         $colsleft = $numinrow - $rem;
12219:     }
12220:     if ($colsleft > 1) {
12221:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
12222:                    '&nbsp;</td>';
12223:     } elsif ($colsleft == 1) {
12224:         $output .= '<td class="LC_left_item">&nbsp;</td>';
12225:     }
12226:     $output .= '</tr></table></td></tr>';
12227:     return $output;
12228: }
12229: 
12230: sub insttypes_row {
12231:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
12232:         $customcss,$rowstyle) = @_;
12233:     my %lt = &Apache::lonlocal::texthash (
12234:                       cansearch      => 'Users allowed to search',
12235:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
12236:                       lockablenames  => 'User preference to lock name',
12237:                       selfassign     => 'Self-reportable affiliations',
12238:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
12239:                       webdav         => 'WebDAV access available',
12240:                       authorquota    => 'Authoring Space quota (MB)',
12241:              );
12242:     my ($showdom,$defaultquota);
12243:     if ($context eq 'cansearch') {
12244:         $showdom = ' ('.$dom.')';
12245:     } elsif ($context eq 'authorquota') {
12246:         $defaultquota = 500;
12247:     }
12248:     my $class = 'LC_left_item';
12249:     if ($context eq 'statustocreate') {
12250:         $class = 'LC_right_item';
12251:     }
12252:     my $css_class;
12253:     if ($$rowtotal%2) {
12254:         $css_class = 'LC_odd_row';
12255:     }
12256:     if ($customcss) {
12257:         $css_class .= ' '.$customcss;
12258:     }
12259:     $css_class =~ s/^\s+//;
12260:     if ($css_class) {
12261:         $css_class = ' class="'.$css_class.'"';
12262:     }
12263:     if ($rowstyle) {
12264:         $css_class .= ' style="'.$rowstyle.'"';
12265:     }
12266:     if ($onclick) {
12267:         $onclick = 'onclick="'.$onclick.'" ';
12268:     }
12269:     my $output = '<tr'.$css_class.'>'.
12270:                  '<td>'.$lt{$context}.$showdom.
12271:                  '</td><td class="'.$class.'" colspan="2"><table>';
12272:     my $rem;
12273:     if (ref($types) eq 'ARRAY') {
12274:         for (my $i=0; $i<@{$types}; $i++) {
12275:             if (defined($usertypes->{$types->[$i]})) {
12276:                 my $rem = $i%($numinrow);
12277:                 if ($rem == 0) {
12278:                     if ($i > 0) {
12279:                         $output .= '</tr>';
12280:                     }
12281:                     $output .= '<tr>';
12282:                 }
12283:                 if ($context eq 'authorquota') {
12284:                     my $currquota;
12285:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
12286:                         $currquota = $settings->{$context}->{$types->[$i]};
12287:                     } else {
12288:                         $currquota = $defaultquota;
12289:                     }
12290:                     $output .= '<td class="LC_left_item">'."\n".
12291:                                '<label><span class="LC_nobreak">'."\n".
12292:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
12293:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
12294:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12295:                                '</label></td>';
12296:                 } else {
12297:                     my $check = ' ';
12298:                     if (ref($settings) eq 'HASH') {
12299:                         if (ref($settings->{$context}) eq 'ARRAY') {
12300:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
12301:                                 $check = ' checked="checked" ';
12302:                             }
12303:                         } elsif (ref($settings->{$context}) eq 'HASH') {
12304:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
12305:                                 $check = ' checked="checked" ';
12306:                             } elsif ($context eq 'webdav') {
12307:                                 if ($settings->{$context}->{$types->[$i]}) {
12308:                                     $check = ' checked="checked" ';
12309:                                 }
12310:                             }
12311:                         } elsif ($context eq 'statustocreate') {
12312:                             $check = ' checked="checked" ';
12313:                         }
12314:                     }
12315:                     $output .= '<td class="LC_left_item">'.
12316:                                '<span class="LC_nobreak"><label>'.
12317:                                '<input type="checkbox" name="'.$context.'" '.
12318:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
12319:                                $usertypes->{$types->[$i]}.'</label></span></td>';
12320:                 }
12321:             }
12322:         }
12323:         $rem = @{$types}%($numinrow);
12324:     }
12325:     my $colsleft = $numinrow - $rem;
12326:     if ($context eq 'overrides') {
12327:         if ($colsleft > 1) {
12328:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12329:         } else {
12330:             $output .= '<td class="LC_left_item">';
12331:         }
12332:         $output .= '&nbsp;';
12333:     } else {
12334:         if ($rem == 0) {
12335:             $output .= '<tr>';
12336:         }
12337:         if ($colsleft > 1) {
12338:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12339:         } else {
12340:             $output .= '<td class="LC_left_item">';
12341:         }
12342:         if ($context eq 'authorquota') {
12343:             my $currquota = 500;
12344:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
12345:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
12346:                     $currquota = $settings->{$context}{'default'};
12347:                 }
12348:             }
12349:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
12350:                        '<input type="text" name="'.$context.'_default" '.
12351:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12352:                        '</label>';
12353:         } else {
12354:             my $defcheck = ' ';
12355:             if (ref($settings) eq 'HASH') {
12356:                 if (ref($settings->{$context}) eq 'ARRAY') {
12357:                     if (grep(/^default$/,@{$settings->{$context}})) {
12358:                         $defcheck = ' checked="checked" ';
12359:                     }
12360:                 } elsif (ref($settings->{$context}) eq 'HASH') {
12361:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
12362:                         $defcheck = ' checked="checked" ';
12363:                     } elsif ($context eq 'webdav') {
12364:                         if ($settings->{$context}->{'default'}) {
12365:                             $defcheck = ' checked="checked" ';
12366:                         }
12367:                     }
12368:                 } elsif ($context eq 'statustocreate') {
12369:                     $defcheck = ' checked="checked" ';
12370:                 }
12371:             }
12372:             $output .= '<span class="LC_nobreak"><label>'.
12373:                        '<input type="checkbox" name="'.$context.'" '.
12374:                        'value="default"'.$defcheck.$onclick.'/>'.
12375:                        $othertitle.'</label></span>';
12376:         }
12377:     }
12378:     $output .= '</td></tr></table></td></tr>';
12379:     return $output;
12380: }
12381: 
12382: sub sorted_searchtitles {
12383:     my %searchtitles = &Apache::lonlocal::texthash(
12384:                          'uname' => 'username',
12385:                          'lastname' => 'last name',
12386:                          'lastfirst' => 'last name, first name',
12387:                      );
12388:     my @titleorder = ('uname','lastname','lastfirst');
12389:     return (\%searchtitles,\@titleorder);
12390: }
12391: 
12392: sub sorted_searchtypes {
12393:     my %srchtypes_desc = (
12394:                            exact    => 'is exact match',
12395:                            contains => 'contains ..',
12396:                            begins   => 'begins with ..',
12397:                          );
12398:     my @srchtypeorder = ('exact','begins','contains');
12399:     return (\%srchtypes_desc,\@srchtypeorder);
12400: }
12401: 
12402: sub usertype_update_row {
12403:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
12404:     my $datatable;
12405:     my $numinrow = 4;
12406:     foreach my $type (@{$types}) {
12407:         if (defined($usertypes->{$type})) {
12408:             $$rownums ++;
12409:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
12410:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
12411:                           '</td><td class="LC_left_item"><table>';
12412:             for (my $i=0; $i<@{$fields}; $i++) {
12413:                 my $rem = $i%($numinrow);
12414:                 if ($rem == 0) {
12415:                     if ($i > 0) {
12416:                         $datatable .= '</tr>';
12417:                     }
12418:                     $datatable .= '<tr>';
12419:                 }
12420:                 my $check = ' ';
12421:                 if (ref($settings) eq 'HASH') {
12422:                     if (ref($settings->{'fields'}) eq 'HASH') {
12423:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
12424:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
12425:                                 $check = ' checked="checked" ';
12426:                             }
12427:                         }
12428:                     }
12429:                 }
12430: 
12431:                 if ($i == @{$fields}-1) {
12432:                     my $colsleft = $numinrow - $rem;
12433:                     if ($colsleft > 1) {
12434:                         $datatable .= '<td colspan="'.$colsleft.'">';
12435:                     } else {
12436:                         $datatable .= '<td>';
12437:                     }
12438:                 } else {
12439:                     $datatable .= '<td>';
12440:                 }
12441:                 $datatable .= '<span class="LC_nobreak"><label>'.
12442:                               '<input type="checkbox" name="updateable_'.$type.
12443:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
12444:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
12445:             }
12446:             $datatable .= '</tr></table></td></tr>';
12447:         }
12448:     }
12449:     return $datatable;
12450: }
12451: 
12452: sub modify_login {
12453:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12454:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12455:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12456:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12457:     %title = ( coursecatalog => 'Display course catalog',
12458:                adminmail => 'Display administrator E-mail address',
12459:                helpdesk  => 'Display "Contact Helpdesk" link',
12460:                newuser => 'Link for visitors to create a user account',
12461:                loginheader => 'Log-in box header',
12462:                saml => 'Dual SSO and non-SSO login');
12463:     @offon = ('off','on');
12464:     if (ref($domconfig{login}) eq 'HASH') {
12465:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12466:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12467:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12468:             }
12469:         }
12470:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12471:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12472:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12473:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12474:                     $saml{$lonhost} = 1;
12475:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12476:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12477:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12478:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12479:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12480:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12481:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12482:                 }
12483:             }
12484:         }
12485:     }
12486:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12487:                                            \%domconfig,\%loginhash);
12488:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12489:     foreach my $item (@toggles) {
12490:         $loginhash{login}{$item} = $env{'form.'.$item};
12491:     }
12492:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12493:     if (ref($colchanges{'login'}) eq 'HASH') {  
12494:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12495:                                          \%loginhash);
12496:     }
12497: 
12498:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12499:     my %domservers = &Apache::lonnet::get_servers($dom);
12500:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12501:     if (keys(%servers) > 1) {
12502:         foreach my $lonhost (keys(%servers)) {
12503:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12504:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12505:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12506:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12507:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12508:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12509:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12510:                         $changes{'loginvia'}{$lonhost} = 1;
12511:                     } else {
12512:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12513:                         $changes{'loginvia'}{$lonhost} = 1;
12514:                     }
12515:                 } else {
12516:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12517:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12518:                         $changes{'loginvia'}{$lonhost} = 1;
12519:                     }
12520:                 }
12521:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12522:                     foreach my $item (@loginvia_attribs) {
12523:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12524:                     }
12525:                 } else {
12526:                     foreach my $item (@loginvia_attribs) {
12527:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12528:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12529:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12530:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12531:                                 $new = '/';
12532:                             }
12533:                         }
12534:                         if (($item eq 'custompath') && 
12535:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12536:                             $new = '';
12537:                         }
12538:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12539:                             $changes{'loginvia'}{$lonhost} = 1;
12540:                         }
12541:                         if ($item eq 'exempt') {
12542:                             $new = &check_exempt_addresses($new);
12543:                         }
12544:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12545:                     }
12546:                 }
12547:             } else {
12548:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12549:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12550:                     $changes{'loginvia'}{$lonhost} = 1;
12551:                     foreach my $item (@loginvia_attribs) {
12552:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12553:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12554:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12555:                                 $new = '/';
12556:                             }
12557:                         }
12558:                         if (($item eq 'custompath') && 
12559:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12560:                             $new = '';
12561:                         }
12562:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12563:                     }
12564:                 }
12565:             }
12566:         }
12567:     }
12568: 
12569:     my $servadm = $r->dir_config('lonAdmEMail');
12570:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12571:     if (ref($domconfig{'login'}) eq 'HASH') {
12572:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12573:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12574:                 if ($lang eq 'nolang') {
12575:                     push(@currlangs,$lang);
12576:                 } elsif (defined($langchoices{$lang})) {
12577:                     push(@currlangs,$lang);
12578:                 } else {
12579:                     next;
12580:                 }
12581:             }
12582:         }
12583:     }
12584:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12585:     if (@currlangs > 0) {
12586:         foreach my $lang (@currlangs) {
12587:             if (grep(/^\Q$lang\E$/,@delurls)) {
12588:                 $changes{'helpurl'}{$lang} = 1;
12589:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12590:                 $changes{'helpurl'}{$lang} = 1;
12591:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12592:                 push(@newlangs,$lang);
12593:             } else {
12594:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12595:             }
12596:         }
12597:     }
12598:     unless (grep(/^nolang$/,@currlangs)) {
12599:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12600:             $changes{'helpurl'}{'nolang'} = 1;
12601:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12602:             push(@newlangs,'nolang');
12603:         }
12604:     }
12605:     if ($env{'form.loginhelpurl_add_lang'}) {
12606:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12607:             ($env{'form.loginhelpurl_add_file.filename'})) {
12608:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12609:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12610:         }
12611:     }
12612:     if ((@newlangs > 0) || ($addedfile)) {
12613:         my $error;
12614:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12615:         if ($configuserok eq 'ok') {
12616:             if ($switchserver) {
12617:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12618:             } elsif ($author_ok eq 'ok') {
12619:                 my @allnew = @newlangs;
12620:                 if ($addedfile ne '') {
12621:                     push(@allnew,$addedfile);
12622:                 }
12623:                 my $modified = [];
12624:                 foreach my $lang (@allnew) {
12625:                     my $formelem = 'loginhelpurl_'.$lang;
12626:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12627:                         $formelem = 'loginhelpurl_add_file';
12628:                     }
12629:                     (my $result,$newurl{$lang}) =
12630:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12631:                                                                 "help/$lang",'','',$newfile{$lang},
12632:                                                                 $modified);
12633:                     if ($result eq 'ok') {
12634:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12635:                         $changes{'helpurl'}{$lang} = 1;
12636:                     } else {
12637:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12638:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12639:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12640:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12641:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12642:                         }
12643:                     }
12644:                 }
12645:                 &update_modify_urls($r,$modified);
12646:             } else {
12647:                 $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);
12648:             }
12649:         } else {
12650:             $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);
12651:         }
12652:         if ($error) {
12653:             &Apache::lonnet::logthis($error);
12654:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12655:         }
12656:     }
12657: 
12658:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12659:     if (ref($domconfig{'login'}) eq 'HASH') {
12660:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12661:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12662:                 if ($domservers{$lonhost}) {
12663:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12664:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12665:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12666:                     }
12667:                 }
12668:             }
12669:         }
12670:     }
12671:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12672:     foreach my $lonhost (sort(keys(%domservers))) {
12673:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12674:             $changes{'headtag'}{$lonhost} = 1;
12675:         } else {
12676:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12677:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12678:             }
12679:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12680:                 push(@newhosts,$lonhost);
12681:             } elsif ($currheadtagurls{$lonhost}) {
12682:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12683:                 if ($currexempt{$lonhost}) {
12684:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12685:                         $changes{'headtag'}{$lonhost} = 1;
12686:                     }
12687:                 } elsif ($possexempt{$lonhost}) {
12688:                     $changes{'headtag'}{$lonhost} = 1;
12689:                 }
12690:                 if ($possexempt{$lonhost}) {
12691:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12692:                 }
12693:             }
12694:         }
12695:     }
12696:     if (@newhosts) {
12697:         my $error;
12698:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12699:         if ($configuserok eq 'ok') {
12700:             if ($switchserver) {
12701:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12702:             } elsif ($author_ok eq 'ok') {
12703:                 my $modified = [];
12704:                 foreach my $lonhost (@newhosts) {
12705:                     my $formelem = 'loginheadtag_'.$lonhost;
12706:                     (my $result,$newheadtagurls{$lonhost}) =
12707:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12708:                                                                 "login/headtag/$lonhost",'','',
12709:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12710:                                                                 $modified);
12711:                     if ($result eq 'ok') {
12712:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12713:                         $changes{'headtag'}{$lonhost} = 1;
12714:                         if ($possexempt{$lonhost}) {
12715:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12716:                         }
12717:                     } else {
12718:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12719:                                            $newheadtagurls{$lonhost},$result);
12720:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12721:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12722:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12723:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12724:                         }
12725:                     }
12726:                 }
12727:                 &update_modify_urls($r,$modified);
12728:             } else {
12729:                 $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);
12730:             }
12731:         } else {
12732:             $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);
12733:         }
12734:         if ($error) {
12735:             &Apache::lonnet::logthis($error);
12736:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12737:         }
12738:     }
12739:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12740:     my @newsamlimgs;
12741:     foreach my $lonhost (keys(%domservers)) {
12742:         if ($env{'form.saml_'.$lonhost}) {
12743:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12744:                 push(@newsamlimgs,$lonhost);
12745:             }
12746:             foreach my $item ('text','alt','url','title','window','notsso') {
12747:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12748:             }
12749:             if ($saml{$lonhost}) {
12750:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12751:                     $env{'form.saml_window_'.$lonhost} = '';
12752:                 }
12753:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12754: #FIXME Need to obsolete published image
12755:                     delete($currsaml{$lonhost}{'img'});
12756:                     $changes{'saml'}{$lonhost} = 1;
12757:                 }
12758:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12759:                     $changes{'saml'}{$lonhost} = 1;
12760:                 }
12761:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12762:                     $changes{'saml'}{$lonhost} = 1;
12763:                 }
12764:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12765:                     $changes{'saml'}{$lonhost} = 1;
12766:                 }
12767:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12768:                     $changes{'saml'}{$lonhost} = 1;
12769:                 }
12770:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12771:                     $changes{'saml'}{$lonhost} = 1;
12772:                 }
12773:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12774:                     $changes{'saml'}{$lonhost} = 1;
12775:                 }
12776:             } else {
12777:                 $changes{'saml'}{$lonhost} = 1;
12778:             }
12779:             foreach my $item ('text','alt','url','title','window','notsso') {
12780:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12781:             }
12782:         } else {
12783:             if ($saml{$lonhost}) {
12784:                 $changes{'saml'}{$lonhost} = 1;
12785:                 delete($currsaml{$lonhost});
12786:             }
12787:         }
12788:     }
12789:     foreach my $posshost (keys(%currsaml)) {
12790:         unless (exists($domservers{$posshost})) {
12791:             delete($currsaml{$posshost});
12792:         }
12793:     }
12794:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12795:     if (@newsamlimgs) {
12796:         my $error;
12797:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12798:         if ($configuserok eq 'ok') {
12799:             if ($switchserver) {
12800:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12801:             } elsif ($author_ok eq 'ok') {
12802:                 my $modified = [];
12803:                 foreach my $lonhost (@newsamlimgs) {
12804:                     my $formelem = 'saml_img_'.$lonhost;
12805:                     my ($result,$imgurl) =
12806:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12807:                                                                 "login/saml/$lonhost",'','',
12808:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12809:                                                                 $modified);
12810:                     if ($result eq 'ok') {
12811:                         $currsaml{$lonhost}{'img'} = $imgurl;
12812:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12813:                         $changes{'saml'}{$lonhost} = 1;
12814:                     } else {
12815:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12816:                                            $lonhost,$result);
12817:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12818:                     }
12819:                 }
12820:                 &update_modify_urls($r,$modified);
12821:             } else {
12822:                 $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);
12823:             }
12824:         } else {
12825:             $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);
12826:         }
12827:         if ($error) {
12828:             &Apache::lonnet::logthis($error);
12829:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12830:         }
12831:     }
12832:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12833: 
12834:     my $defaulthelpfile = '/adm/loginproblems.html';
12835:     my $defaulttext = &mt('Default in use');
12836: 
12837:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12838:                                              $dom);
12839:     if ($putresult eq 'ok') {
12840:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12841:         my %defaultchecked = (
12842:                     'coursecatalog' => 'on',
12843:                     'helpdesk'      => 'on',
12844:                     'adminmail'     => 'off',
12845:                     'newuser'       => 'off',
12846:         );
12847:         if (ref($domconfig{'login'}) eq 'HASH') {
12848:             foreach my $item (@toggles) {
12849:                 if ($defaultchecked{$item} eq 'on') { 
12850:                     if (($domconfig{'login'}{$item} eq '0') &&
12851:                         ($env{'form.'.$item} eq '1')) {
12852:                         $changes{$item} = 1;
12853:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12854:                               $domconfig{'login'}{$item} eq '1') &&
12855:                              ($env{'form.'.$item} eq '0')) {
12856:                         $changes{$item} = 1;
12857:                     }
12858:                 } elsif ($defaultchecked{$item} eq 'off') {
12859:                     if (($domconfig{'login'}{$item} eq '1') &&
12860:                         ($env{'form.'.$item} eq '0')) {
12861:                         $changes{$item} = 1;
12862:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12863:                               $domconfig{'login'}{$item} eq '0') &&
12864:                              ($env{'form.'.$item} eq '1')) {
12865:                         $changes{$item} = 1;
12866:                     }
12867:                 }
12868:             }
12869:         }
12870:         if (keys(%changes) > 0 || $colchgtext) {
12871:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12872:             if (exists($changes{'saml'})) {
12873:                 my $hostid_in_use;
12874:                 my @hosts = &Apache::lonnet::current_machine_ids();
12875:                 if (@hosts > 1) {
12876:                     foreach my $hostid (@hosts) {
12877:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12878:                             $hostid_in_use = $hostid;
12879:                             last;
12880:                         }
12881:                     }
12882:                 } else {
12883:                     $hostid_in_use = $r->dir_config('lonHostID');
12884:                 }
12885:                 if (($hostid_in_use) &&
12886:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12887:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12888:                 }
12889:                 if (ref($lastactref) eq 'HASH') {
12890:                     if (ref($changes{'saml'}) eq 'HASH') {
12891:                         my %updates;
12892:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12893:                         $lastactref->{'samllanding'} = \%updates;
12894:                     }
12895:                 }
12896:             }
12897:             if (ref($lastactref) eq 'HASH') {
12898:                 $lastactref->{'domainconfig'} = 1;
12899:             }
12900:             $resulttext = &mt('Changes made:').'<ul>';
12901:             foreach my $item (sort(keys(%changes))) {
12902:                 if ($item eq 'loginvia') {
12903:                     if (ref($changes{$item}) eq 'HASH') {
12904:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12905:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12906:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12907:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12908:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12909:                                     $protocol = 'http' if ($protocol ne 'https');
12910:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12911: 
12912:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12913:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12914:                                     } else {
12915:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12916:                                     }
12917:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12918:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12919:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12920:                                     }
12921:                                     $resulttext .= '</li>';
12922:                                 } else {
12923:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12924:                                 }
12925:                             } else {
12926:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12927:                             }
12928:                         }
12929:                         $resulttext .= '</ul></li>';
12930:                     }
12931:                 } elsif ($item eq 'helpurl') {
12932:                     if (ref($changes{$item}) eq 'HASH') {
12933:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12934:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12935:                                 my ($chg,$link);
12936:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12937:                                 if ($lang eq 'nolang') {
12938:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12939:                                 } else {
12940:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12941:                                 }
12942:                                 $resulttext .= '<li>'.$chg.'</li>';
12943:                             } else {
12944:                                 my $chg;
12945:                                 if ($lang eq 'nolang') {
12946:                                     $chg = &mt('custom log-in help file for no preferred language');
12947:                                 } else {
12948:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12949:                                 }
12950:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12951:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12952:                                                       '?inhibitmenu=yes',$chg,600,500).
12953:                                                '</li>';
12954:                             }
12955:                         }
12956:                     }
12957:                 } elsif ($item eq 'headtag') {
12958:                     if (ref($changes{$item}) eq 'HASH') {
12959:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12960:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12961:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12962:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12963:                                 $resulttext .= '<li><a href="'.
12964:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12965:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12966:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12967:                                 if ($possexempt{$lonhost}) {
12968:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12969:                                 } else {
12970:                                     $resulttext .= &mt('included for any client IP');
12971:                                 }
12972:                                 $resulttext .= '</li>';
12973:                             }
12974:                         }
12975:                     }
12976:                 } elsif ($item eq 'saml') {
12977:                     if (ref($changes{$item}) eq 'HASH') {
12978:                         my %notlt = (
12979:                                        text   => 'Text for log-in by SSO',
12980:                                        img    => 'SSO button image',
12981:                                        alt    => 'Alt text for button image',
12982:                                        url    => 'SSO URL',
12983:                                        title  => 'Tooltip for SSO link',
12984:                                        window => 'Pop-up window if iframe',
12985:                                        notsso => 'Text for non-SSO log-in',
12986:                                     );
12987:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12988:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12989:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12990:                                                '<ul>';
12991:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
12992:                                     if ($currsaml{$lonhost}{$key} eq '') {
12993:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12994:                                     } else {
12995:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12996:                                         if ($key eq 'img') {
12997:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12998:                                         } elsif ($key eq 'window') {
12999:                                             $value = 'On';
13000:                                         }
13001:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
13002:                                                                   $value).'</li>';
13003:                                     }
13004:                                 }
13005:                                 $resulttext .= '</ul></li>';
13006:                             } else {
13007:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
13008:                             }
13009:                         }
13010:                     }
13011:                 } elsif ($item eq 'captcha') {
13012:                     if (ref($loginhash{'login'}) eq 'HASH') {
13013:                         my $chgtxt;
13014:                         if ($loginhash{'login'}{$item} eq 'notused') {
13015:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
13016:                         } else {
13017:                             my %captchas = &captcha_phrases();
13018:                             if ($captchas{$loginhash{'login'}{$item}}) {
13019:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
13020:                             } else {
13021:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
13022:                             }
13023:                         }
13024:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13025:                     }
13026:                 } elsif ($item eq 'recaptchakeys') {
13027:                     if (ref($loginhash{'login'}) eq 'HASH') {
13028:                         my ($privkey,$pubkey);
13029:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
13030:                             $pubkey = $loginhash{'login'}{$item}{'public'};
13031:                             $privkey = $loginhash{'login'}{$item}{'private'};
13032:                         }
13033:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
13034:                         if (!$pubkey) {
13035:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
13036:                         } else {
13037:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13038:                         }
13039:                         if (!$privkey) {
13040:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
13041:                         } else {
13042:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
13043:                         }
13044:                         $chgtxt .= '</ul>';
13045:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13046:                     }
13047:                 } elsif ($item eq 'recaptchaversion') {
13048:                     if (ref($loginhash{'login'}) eq 'HASH') {
13049:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
13050:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
13051:                                            '</li>';
13052:                         }
13053:                     }
13054:                 } else {
13055:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
13056:                 }
13057:             }
13058:             $resulttext .= $colchgtext.'</ul>';
13059:         } else {
13060:             $resulttext = &mt('No changes made to log-in page settings');
13061:         }
13062:     } else {
13063:         $resulttext = '<span class="LC_error">'.
13064: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13065:     }
13066:     if ($errors) {
13067:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13068:                        $errors.'</ul>';
13069:     }
13070:     return $resulttext;
13071: }
13072: 
13073: sub check_exempt_addresses {
13074:     my ($iplist) = @_;
13075:     $iplist =~ s/^\s+//;
13076:     $iplist =~ s/\s+$//;
13077:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
13078:     my (@okips,$new);
13079:     foreach my $ip (@poss_ips) {
13080:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
13081:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
13082:                 push(@okips,$ip);
13083:             }
13084:         }
13085:     }
13086:     if (@okips > 0) {
13087:         $new = join(',',@okips);
13088:     } else {
13089:         $new = '';
13090:     }
13091:     return $new;
13092: }
13093: 
13094: sub color_font_choices {
13095:     my %choices =
13096:         &Apache::lonlocal::texthash (
13097:             bgs => "Background colors",
13098:             links => "Link colors",
13099:             images => "Images",
13100:             font => "Font color",
13101:             fontmenu => "Font menu",
13102:             pgbg => "Page",
13103:             tabbg => "Header",
13104:             sidebg => "Border",
13105:             link => "Link",
13106:             alink => "Active link",
13107:             vlink => "Visited link",
13108:         );
13109:     return %choices;
13110: }
13111: 
13112: sub modify_ipaccess {
13113:     my ($dom,$lastactref,%domconfig) = @_;
13114:     my (@allpos,%changes,%confhash,$errors,$resulttext);
13115:     my (@items,%deletions,%itemids,@warnings);
13116:     my ($typeorder,$types) = &commblocktype_text();
13117:     if ($env{'form.ipaccess_add'}) {
13118:         my $name = $env{'form.ipaccess_name_add'};
13119:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
13120:         if ($newid) {
13121:             $itemids{'add'} = $newid;
13122:             push(@items,'add');
13123:             $changes{$newid} = 1;
13124:         } else {
13125:             $error = &mt('Failed to acquire unique ID for new IP access control item');
13126:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13127:         }
13128:     }
13129:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13130:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
13131:         if (@todelete) {
13132:             map { $deletions{$_} = 1; } @todelete;
13133:         }
13134:         my $maxnum = $env{'form.ipaccess_maxnum'};
13135:         for (my $i=0; $i<$maxnum; $i++) {
13136:             my $itemid = $env{'form.ipaccess_id_'.$i};
13137:             $itemid =~ s/\D+//g;
13138:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13139:                 if ($deletions{$itemid}) {
13140:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
13141:                 } else {
13142:                     push(@items,$i);
13143:                     $itemids{$i} = $itemid;
13144:                 }
13145:             }
13146:         }
13147:     }
13148:     foreach my $idx (@items) {
13149:         my $itemid = $itemids{$idx};
13150:         next unless ($itemid);
13151:         my ($position,%current);
13152:         if ($idx eq 'add') {
13153:             $position = $env{'form.ipaccess_pos_add'};
13154:         } else {
13155:             $position = $env{'form.ipaccess_pos_'.$itemid};
13156:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13157:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
13158:             }
13159:         }
13160:         $position =~ s/\D+//g;
13161:         if ($position ne '') {
13162:             $allpos[$position] = $itemid;
13163:         }
13164:         my $name = $env{'form.ipaccess_name_'.$idx};
13165:         $name =~ s/^\s+|\s+$//g;
13166:         $confhash{$itemid}{'name'} = $name;
13167:         my $possrange = $env{'form.ipaccess_range_'.$idx};
13168:         $possrange =~ s/^\s+|\s+$//g;
13169:         unless ($possrange eq '') {
13170:             $possrange =~ s/[\r\n]+/\s/g;
13171:             $possrange =~ s/\s*-\s*/-/g;
13172:             $possrange =~ s/\s+/,/g;
13173:             $possrange =~ s/,+/,/g;
13174:             if ($possrange ne '') {
13175:                 my (@ok,$count);
13176:                 $count = 0;
13177:                 foreach my $poss (split(/\,/,$possrange)) {
13178:                     $count ++;
13179:                     $poss = &validate_ip_pattern($poss);
13180:                     if ($poss ne '') {
13181:                         push(@ok,$poss);
13182:                     }
13183:                 }
13184:                 my $diff = $count - scalar(@ok);
13185:                 if ($diff) {
13186:                     $errors .= '<li><span class="LC_error">'.
13187:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
13188:                                    $diff,$name).
13189:                                '</span></li>';
13190:                 }
13191:                 if (@ok) {
13192:                     my @cidr_list;
13193:                     foreach my $item (@ok) {
13194:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
13195:                     }
13196:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
13197:                 }
13198:             }
13199:         }
13200:         foreach my $field ('name','ip') {
13201:             unless (($idx eq 'add') || ($changes{$itemid})) {
13202:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
13203:                     $changes{$itemid} = 1;
13204:                     last;
13205:                 }
13206:             }
13207:         }
13208:         $confhash{$itemid}{'commblocks'} = {};
13209: 
13210:         my %commblocks;
13211:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
13212:         foreach my $type (@{$typeorder}) {
13213:             if ($commblocks{$type}) {
13214:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
13215:             }
13216:             unless (($idx eq 'add') || ($changes{$itemid})) {
13217:                 if (ref($current{'commblocks'}) eq 'HASH') {
13218:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
13219:                         $changes{$itemid} = 1;
13220:                     }
13221:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
13222:                     $changes{$itemid} = 1;
13223:                 }
13224:             }
13225:         }
13226:         $confhash{$itemid}{'courses'} = {};
13227:         my %crsdeletions;
13228:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
13229:         if (@delcrs) {
13230:             map { $crsdeletions{$_} = 1; } @delcrs;
13231:         }
13232:         if (ref($current{'courses'}) eq 'HASH') {
13233:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
13234:                 if ($crsdeletions{$cid}) {
13235:                     $changes{$itemid} = 1;
13236:                 } else {
13237:                     $confhash{$itemid}{'courses'}{$cid} = 1;
13238:                 }
13239:             }
13240:         }
13241:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
13242:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
13243:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
13244:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
13245:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
13246:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
13247:                 $errors .= '<li><span class="LC_error">'.
13248:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
13249:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
13250:                            '</span></li>';
13251:             } else {
13252:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
13253:                 $changes{$itemid} = 1;
13254:             }
13255:         }
13256:     }
13257:     if (@allpos > 0) {
13258:         my $idx = 0;
13259:         foreach my $itemid (@allpos) {
13260:             if ($itemid ne '') {
13261:                 $confhash{$itemid}{'order'} = $idx;
13262:                 unless ($changes{$itemid}) {
13263:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13264:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13265:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
13266:                                 $changes{$itemid} = 1;
13267:                             }
13268:                         }
13269:                     }
13270:                 }
13271:                 $idx ++;
13272:             }
13273:         }
13274:     }
13275:     if (keys(%changes)) {
13276:         my %defaultshash = (
13277:                               ipaccess => \%confhash,
13278:                            );
13279:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13280:                                                  $dom);
13281:         if ($putresult eq 'ok') {
13282:             my $cachetime = 1800;
13283:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
13284:             if (ref($lastactref) eq 'HASH') {
13285:                 $lastactref->{'ipaccess'} = 1;
13286:             }
13287:             $resulttext = &mt('Changes made:').'<ul>';
13288:             my %bynum;
13289:             foreach my $itemid (sort(keys(%changes))) {
13290:                 if (ref($confhash{$itemid}) eq 'HASH') {
13291:                     my $position = $confhash{$itemid}{'order'};
13292:                     if ($position =~ /^\d+$/) {
13293:                         $bynum{$position} = $itemid;
13294:                     }
13295:                 }
13296:             }
13297:             if (keys(%deletions)) {
13298:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
13299:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13300:                 }
13301:             }
13302:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13303:                 my $itemid = $bynum{$pos};
13304:                 if (ref($confhash{$itemid}) eq 'HASH') {
13305:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
13306:                     my $position = $pos + 1;
13307:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13308:                     if ($confhash{$itemid}{'ip'} eq '') {
13309:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
13310:                     } else {
13311:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
13312:                     }
13313:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
13314:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
13315:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
13316:                                        '</li>';
13317:                     } else {
13318:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
13319:                     }
13320:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
13321:                         my @courses;
13322:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
13323:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
13324:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
13325:                         }
13326:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
13327:                                              join('</li><li>',@courses).'</li></ul>';
13328:                     } else {
13329:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
13330:                     }
13331:                     $resulttext .= '</ul></li>';
13332:                 }
13333:             }
13334:             $resulttext .= '</ul>';
13335:         } else {
13336:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13337:         }
13338:     } else {
13339:         $resulttext = &mt('No changes made');
13340:     }
13341:     if ($errors) {
13342:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13343:                        $errors.'</ul></p>';
13344:     }
13345:     return $resulttext;
13346: }
13347: 
13348: sub get_ipaccess_id {
13349:     my ($domain,$location) = @_;
13350:     # get lock on ipaccess db
13351:     my $lockhash = {
13352:                       lock => $env{'user.name'}.
13353:                               ':'.$env{'user.domain'},
13354:                    };
13355:     my $tries = 0;
13356:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13357:     my ($id,$error);
13358: 
13359:     while (($gotlock ne 'ok') && ($tries<10)) {
13360:         $tries ++;
13361:         sleep (0.1);
13362:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13363:     }
13364:     if ($gotlock eq 'ok') {
13365:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
13366:         if ($currids{'lock'}) {
13367:             delete($currids{'lock'});
13368:             if (keys(%currids)) {
13369:                 my @curr = sort { $a <=> $b } keys(%currids);
13370:                 if ($curr[-1] =~ /^\d+$/) {
13371:                     $id = 1 + $curr[-1];
13372:                 }
13373:             } else {
13374:                 $id = 1;
13375:             }
13376:             if ($id) {
13377:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
13378:                     $error = 'nostore';
13379:                 }
13380:             } else {
13381:                 $error = 'nonumber';
13382:             }
13383:         }
13384:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
13385:     } else {
13386:         $error = 'nolock';
13387:     }
13388:     return ($id,$error);
13389: }
13390: 
13391: sub modify_authordefaults {
13392:     my ($dom,$lastactref,%domconfig) = @_;
13393: #
13394: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
13395: #
13396:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
13397:     if (ref($domconfig{'quotas'}) eq 'HASH') {
13398:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
13399:             if ($key =~ /^webdav|authorquota$/) {
13400:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
13401:             } else {
13402:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
13403:             }
13404:         }
13405:     }
13406:     my %staticdefaults = (
13407:                            'copyright'    => 'default',
13408:                            'sourceavail'  => 'closed',
13409:                            'nocodemirror' => 'off',
13410:                            'daxecollapse' => 'off',
13411:                            'domcoordacc'  => 'on',
13412:                            'editors'      => ['edit','xml'],
13413:                            'authorquota'  => 500,
13414:                            'webdav'       => 0,
13415:                            'archive'      => 'off',
13416:                          );
13417:     my %titles = &authordefaults_titles();
13418:     foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13419:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13420:             $confhash{$item} = $env{'form.'.$item};
13421:         }
13422:     }
13423:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
13424:         $confhash{'copyright'} = $1;
13425:     }
13426:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
13427:         $confhash{'sourceavail'} = $1;
13428:     }
13429:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
13430:     my @okeditors = ('edit','xml','daxe');
13431:     my @editors;
13432:     foreach my $item (@posseditors) {
13433:         if (grep(/^\Q$item\E$/,@okeditors)) {
13434:             push(@editors,$item);
13435:         }
13436:     }
13437:     $confhash{'editors'} = \@editors;
13438: 
13439:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13440:     my @insttypes;
13441:     if (ref($types) eq 'ARRAY') {
13442:         @insttypes = @{$types};
13443:     }
13444:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
13445:     my %webdav;
13446:     map { $webdav{$_} = 1; } @webdavon;
13447:     foreach my $type (@insttypes,'default') {
13448:         my $possquota = $env{'form.authorquota_'.$type};
13449:         if ($possquota =~ /^\d+$/) {
13450:             $save_quotas{'authorquota'}{$type} = $possquota;
13451:         }
13452:         if ($webdav{$type}) {
13453:             $save_quotas{'webdav'}{$type} = 1;
13454:         } else {
13455:             $save_quotas{'webdav'}{$type} = 0;
13456:         }
13457:     }
13458:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
13459:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
13460:     }
13461:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
13462:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail','archive') {
13463:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
13464:                 $changes{$item} = 1;
13465:              }
13466:         }
13467:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
13468:             my @diffs =
13469:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13470:                                                    $domconfig{'authordefaults'}{'editors'});
13471:             unless (@diffs == 0) {
13472:                 $changes{'editors'} = 1;
13473:             }
13474:         } else {
13475:             my @diffs =
13476:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13477:                                                    $staticdefaults{'editors'});
13478:             unless (@diffs == 0) {
13479:                 $changes{'editors'} = 1;
13480:             }
13481:         }
13482:     } else {
13483:         my @offon = ('off','on');
13484:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13485:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) {
13486:                 $changes{$item} = 1; 
13487:             }
13488:         }
13489:         foreach my $item ('copyright','sourceavail') {
13490:             if ($confhash{$item} ne $staticdefaults{$item}) {
13491:                 $changes{$item} = 1;
13492:             }
13493:         }
13494:         my @diffs =
13495:             &Apache::loncommon::compare_arrays($confhash{'editors'},
13496:                                                $staticdefaults{'editors'});
13497:         unless (@diffs == 0) {
13498:             $changes{'editors'} = 1;
13499:         }
13500:     }
13501:     foreach my $key ('authorquota','webdav') {
13502:         if (ref($curr_quotas{$key}) eq 'HASH') {
13503:             foreach my $type (@insttypes,'default') {
13504:                 if (exists($save_quotas{$key}{$type})) {
13505:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
13506:                         $changes{$key}{$type} = 1;
13507:                     }
13508:                 } elsif (exists($curr_quotas{$key}{$type})) {
13509:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
13510:                 } else {
13511:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13512:                 }
13513:             }
13514:         } else {
13515:             foreach my $type (@insttypes,'default') {
13516:                 if (exists($save_quotas{$key}{$type})) {
13517:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
13518:                         $changes{$key}{$type} = 1;
13519:                     }
13520:                 } else {
13521:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13522:                 }
13523:             }
13524:         }
13525:     }
13526:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
13527:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13528:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) {
13529:                 $changes{'webdav_LC_adv'} = 1;
13530:             }
13531:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
13532:             $changes{'webdav_LC_adv'} = 1;
13533:         }
13534:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13535:         $changes{'webdav_LC_adv'} = 1;
13536:     }
13537:     my %confighash = (
13538:                         quotas  => \%save_quotas,
13539:                         authordefaults => \%confhash,
13540:                      );
13541:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
13542:                                              $dom);
13543:     my $resulttext;
13544:     if ($putresult eq 'ok') {
13545:         if (keys(%changes)) {
13546:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13547:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
13548:                 ($changes{'webdav_LC_adv'})) {
13549:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
13550:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
13551:                 }
13552:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
13553:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
13554:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
13555:                 }
13556:             }
13557:             $resulttext = &mt('Changes made:').'<ul>';
13558:             my $authoroverride;
13559:             foreach my $key ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail') {
13560:                 if (exists($changes{$key})) {
13561:                     $domdefaults{$key} = $confhash{$key};
13562:                     my $shown;
13563:                     unless ($authoroverride) {
13564:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
13565:                         $authoroverride = 1;
13566:                     }
13567:                     if (($key eq 'nocodemirror') || ($key eq 'daxecollapse') || ($key eq 'domcoordacc')) {
13568:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13569:                     } elsif ($key eq 'copyright') {
13570:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
13571:                     } elsif ($key eq 'sourceavail') {
13572:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
13573:                     }
13574:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13575:                 }
13576:             }
13577:             if ($authoroverride) {
13578:                 $resulttext .= '</ul></li>';
13579:             }
13580:             my $domcoordoverride;
13581:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv','archive') {
13582:                 if (exists($changes{$key})) {
13583:                     my $shown;
13584:                     unless ($domcoordoverride) {
13585:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
13586:                         $domcoordoverride = 1;
13587:                     }
13588:                     if ($key eq 'editors') {
13589:                         if (ref($confhash{'editors'}) eq 'ARRAY') {
13590:                             $domdefaults{'editors'} = join(',',@{$confhash{'editors'}});
13591:                             if (@{$confhash{'editors'}}) {
13592:                                 $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
13593:                             } else {
13594:                                 $shown = &mt('None');
13595:                             }
13596:                         }
13597:                     } elsif ($key eq 'authorquota') {
13598:                         foreach my $type (@insttypes) {
13599:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
13600:                         }
13601:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
13602:                     } elsif ($key eq 'webdav') {
13603:                         foreach my $type (@insttypes) {
13604:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
13605:                         }
13606:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
13607:                     } elsif ($key eq 'webdav_LC_adv') {
13608:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13609:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
13610:                         } else {
13611:                             $shown = $titles{'none'};
13612:                         }
13613:                     } elsif ($key eq 'archive') {
13614:                         $domdefaults{$key} = $confhash{$key};
13615:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13616:                     }
13617:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13618:                 }
13619:             }
13620:             if ($domcoordoverride) {
13621:                 $resulttext .= '</ul></li>';
13622:             }
13623:             $resulttext .= '</ul>';
13624:             my $cachetime = 24*60*60;
13625:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13626:             if (ref($lastactref) eq 'HASH') {
13627:                 $lastactref->{'domdefaults'} = 1;
13628:             }
13629:         } else {
13630:             $resulttext = &mt('No changes made to Authoring Space defaults');
13631:         }
13632:     }
13633:     return $resulttext;
13634: }
13635: 
13636: sub modify_rolecolors {
13637:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
13638:     my ($resulttext,%rolehash);
13639:     $rolehash{'rolecolors'} = {};
13640:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
13641:         if ($domconfig{'rolecolors'} eq '') {
13642:             $domconfig{'rolecolors'} = {};
13643:         }
13644:     }
13645:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
13646:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
13647:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
13648:                                              $dom);
13649:     if ($putresult eq 'ok') {
13650:         if (keys(%changes) > 0) {
13651:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13652:             if (ref($lastactref) eq 'HASH') {
13653:                 $lastactref->{'domainconfig'} = 1;
13654:             }
13655:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
13656:                                              $rolehash{'rolecolors'});
13657:         } else {
13658:             $resulttext = &mt('No changes made to default color schemes');
13659:         }
13660:     } else {
13661:         $resulttext = '<span class="LC_error">'.
13662: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13663:     }
13664:     if ($errors) {
13665:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13666:                        $errors.'</ul>';
13667:     }
13668:     return $resulttext;
13669: }
13670: 
13671: sub modify_colors {
13672:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
13673:     my (%changes,%choices);
13674:     my @bgs;
13675:     my @links = ('link','alink','vlink');
13676:     my @logintext;
13677:     my @images;
13678:     my $servadm = $r->dir_config('lonAdmEMail');
13679:     my $errors;
13680:     my %defaults;
13681:     foreach my $role (@{$roles}) {
13682:         if ($role eq 'login') {
13683:             %choices = &login_choices();
13684:             @logintext = ('textcol','bgcol');
13685:         } else {
13686:             %choices = &color_font_choices();
13687:         }
13688:         if ($role eq 'login') {
13689:             @images = ('img','logo','domlogo','login');
13690:             @bgs = ('pgbg','mainbg','sidebg');
13691:         } else {
13692:             @images = ();
13693:             @bgs = ('pgbg','tabbg','sidebg');
13694:         }
13695:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
13696:         $env{'form.'.$role.'_font'} = lc($env{'form.'.$role.'_font'});
13697:         if ($env{'form.'.$role.'_font'} =~ /^\w+/) {
13698:             $env{'form.'.$role.'_font'} = '#'.$env{'form.'.$role.'_font'};
13699:         }
13700:         unless ($env{'form.'.$role.'_font'} eq lc($defaults{'font'})) {
13701:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13702:         }
13703:         if ($role eq 'login') {
13704:             foreach my $item (@logintext) {
13705:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13706:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13707:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13708:                 }
13709:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13710:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13711:                 }
13712:             }
13713:         } else {
13714:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13715:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13716:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13717:             }
13718:             unless ($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13719:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13720:             }
13721:         }
13722:         foreach my $item (@bgs) {
13723:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13724:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13725:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13726:             }
13727:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13728:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13729:             }
13730:         }
13731:         foreach my $item (@links) {
13732:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13733:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13734:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13735:             }
13736:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13737:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13738:             }
13739:         }
13740:         my ($configuserok,$author_ok,$switchserver) = 
13741:             &config_check($dom,$confname,$servadm);
13742:         my ($width,$height) = &thumb_dimensions();
13743:         if (ref($domconfig->{$role}) ne 'HASH') {
13744:             $domconfig->{$role} = {};
13745:         }
13746:         foreach my $img (@images) {
13747:             if ($role eq 'login') {
13748:                 if (($img eq 'img') || ($img eq 'logo')) {  
13749:                     if (defined($env{'form.login_showlogo_'.$img})) {
13750:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13751:                     } else { 
13752:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13753:                     }
13754:                 }
13755:                 if ($env{'form.login_alt_'.$img} ne '') {
13756:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13757:                 }
13758:             }
13759: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13760: 		 && !defined($domconfig->{$role}{$img})
13761: 		 && !$env{'form.'.$role.'_del_'.$img}
13762: 		 && $env{'form.'.$role.'_import_'.$img}) {
13763: 		# import the old configured image from the .tab setting
13764: 		# if they haven't provided a new one 
13765: 		$domconfig->{$role}{$img} = 
13766: 		    $env{'form.'.$role.'_import_'.$img};
13767: 	    }
13768:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13769:                 my $error;
13770:                 if ($configuserok eq 'ok') {
13771:                     if ($switchserver) {
13772:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13773:                     } else {
13774:                         if ($author_ok eq 'ok') {
13775:                             my $modified = [];
13776:                             my ($result,$logourl) = 
13777:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13778:                                                                         $dom,$confname,$img,$width,$height,
13779:                                                                         '',$modified);
13780:                             if ($result eq 'ok') {
13781:                                 $confhash->{$role}{$img} = $logourl;
13782:                                 $changes{$role}{'images'}{$img} = 1;
13783:                                 &update_modify_urls($r,$modified);
13784:                             } else {
13785:                                 $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);
13786:                             }
13787:                         } else {
13788:                             $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);
13789:                         }
13790:                     }
13791:                 } else {
13792:                     $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);
13793:                 }
13794:                 if ($error) {
13795:                     &Apache::lonnet::logthis($error);
13796:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13797:                 }
13798:             } elsif ($domconfig->{$role}{$img} ne '') {
13799:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13800:                     my $error;
13801:                     if ($configuserok eq 'ok') {
13802: # is confname an author?
13803:                         if ($switchserver eq '') {
13804:                             if ($author_ok eq 'ok') {
13805:                                 my $modified = [];
13806:                                 my ($result,$logourl) = 
13807:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13808:                                                                             $dom,$confname,$img,$width,$height,
13809:                                                                             '',$modified);
13810:                                 if ($result eq 'ok') {
13811:                                     $confhash->{$role}{$img} = $logourl;
13812: 				    $changes{$role}{'images'}{$img} = 1;
13813:                                     &update_modify_urls($r,$modified);
13814:                                 }
13815:                             }
13816:                         }
13817:                     }
13818:                 }
13819:             }
13820:         }
13821:         if (ref($domconfig) eq 'HASH') {
13822:             if (ref($domconfig->{$role}) eq 'HASH') {
13823:                 foreach my $img (@images) {
13824:                     if ($domconfig->{$role}{$img} ne '') {
13825:                         if ($env{'form.'.$role.'_del_'.$img}) {
13826:                             $confhash->{$role}{$img} = '';
13827:                             $changes{$role}{'images'}{$img} = 1;
13828:                         } else {
13829:                             if ($confhash->{$role}{$img} eq '') {
13830:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13831:                             }
13832:                         }
13833:                     } else {
13834:                         if ($env{'form.'.$role.'_del_'.$img}) {
13835:                             $confhash->{$role}{$img} = '';
13836:                             $changes{$role}{'images'}{$img} = 1;
13837:                         } 
13838:                     }
13839:                     if ($role eq 'login') {
13840:                         if (($img eq 'logo') || ($img eq 'img')) {
13841:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13842:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13843:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13844:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13845:                                 }
13846:                             } else {
13847:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13848:                                     $changes{$role}{'showlogo'}{$img} = 1;
13849:                                 }
13850:                             }
13851:                         }
13852:                         if ($img ne 'login') {
13853:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13854:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13855:                                     $domconfig->{$role}{'alttext'}{$img}) {
13856:                                     $changes{$role}{'alttext'}{$img} = 1;
13857:                                 }
13858:                             } else {
13859:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13860:                                     $changes{$role}{'alttext'}{$img} = 1;
13861:                                 }
13862:                             }
13863:                         }
13864:                     }
13865:                 }
13866:                 if ($domconfig->{$role}{'font'} ne '') {
13867:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13868:                         $changes{$role}{'font'} = 1;
13869:                     }
13870:                 } else {
13871:                     if ($confhash->{$role}{'font'}) {
13872:                         $changes{$role}{'font'} = 1;
13873:                     }
13874:                 }
13875:                 if ($role ne 'login') {
13876:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13877:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13878:                             $changes{$role}{'fontmenu'} = 1;
13879:                         }
13880:                     } else {
13881:                         if ($confhash->{$role}{'fontmenu'}) {
13882:                             $changes{$role}{'fontmenu'} = 1;
13883:                         }
13884:                     }
13885:                 }
13886:                 foreach my $item (@bgs) {
13887:                     if ($domconfig->{$role}{$item} ne '') {
13888:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13889:                             $changes{$role}{'bgs'}{$item} = 1;
13890:                         } 
13891:                     } else {
13892:                         if ($confhash->{$role}{$item}) {
13893:                             $changes{$role}{'bgs'}{$item} = 1;
13894:                         }
13895:                     }
13896:                 }
13897:                 foreach my $item (@links) {
13898:                     if ($domconfig->{$role}{$item} ne '') {
13899:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13900:                             $changes{$role}{'links'}{$item} = 1;
13901:                         }
13902:                     } else {
13903:                         if ($confhash->{$role}{$item}) {
13904:                             $changes{$role}{'links'}{$item} = 1;
13905:                         }
13906:                     }
13907:                 }
13908:                 foreach my $item (@logintext) {
13909:                     if ($domconfig->{$role}{$item} ne '') {
13910:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13911:                             $changes{$role}{'logintext'}{$item} = 1;
13912:                         }
13913:                     } else {
13914:                         if ($confhash->{$role}{$item}) {
13915:                             $changes{$role}{'logintext'}{$item} = 1;
13916:                         }
13917:                     }
13918:                 }
13919:             } else {
13920:                 &default_change_checker($role,\@images,\@links,\@bgs,
13921:                                         \@logintext,$confhash,\%changes); 
13922:             }
13923:         } else {
13924:             &default_change_checker($role,\@images,\@links,\@bgs,
13925:                                     \@logintext,$confhash,\%changes); 
13926:         }
13927:     }
13928:     return ($errors,%changes);
13929: }
13930: 
13931: sub config_check {
13932:     my ($dom,$confname,$servadm) = @_;
13933:     my ($configuserok,$author_ok,$switchserver,%currroles);
13934:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13935:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13936:                                                    $confname,$servadm);
13937:     if ($configuserok eq 'ok') {
13938:         $switchserver = &check_switchserver($dom,$confname);
13939:         if ($switchserver eq '') {
13940:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13941:         }
13942:     }
13943:     return ($configuserok,$author_ok,$switchserver);
13944: }
13945: 
13946: sub default_change_checker {
13947:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13948:     foreach my $item (@{$links}) {
13949:         if ($confhash->{$role}{$item}) {
13950:             $changes->{$role}{'links'}{$item} = 1;
13951:         }
13952:     }
13953:     foreach my $item (@{$bgs}) {
13954:         if ($confhash->{$role}{$item}) {
13955:             $changes->{$role}{'bgs'}{$item} = 1;
13956:         }
13957:     }
13958:     foreach my $item (@{$logintext}) {
13959:         if ($confhash->{$role}{$item}) {
13960:             $changes->{$role}{'logintext'}{$item} = 1;
13961:         }
13962:     }
13963:     foreach my $img (@{$images}) {
13964:         if ($env{'form.'.$role.'_del_'.$img}) {
13965:             $confhash->{$role}{$img} = '';
13966:             $changes->{$role}{'images'}{$img} = 1;
13967:         }
13968:         if ($role eq 'login') {
13969:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13970:                 $changes->{$role}{'showlogo'}{$img} = 1;
13971:             }
13972:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13973:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13974:                     $changes->{$role}{'alttext'}{$img} = 1;
13975:                 }
13976:             }
13977:         }
13978:     }
13979:     if ($confhash->{$role}{'font'}) {
13980:         $changes->{$role}{'font'} = 1;
13981:     }
13982: }
13983: 
13984: sub display_colorchgs {
13985:     my ($dom,$changes,$roles,$confhash) = @_;
13986:     my (%choices,$resulttext);
13987:     if (!grep(/^login$/,@{$roles})) {
13988:         $resulttext = &mt('Changes made:').'<br />';
13989:     }
13990:     foreach my $role (@{$roles}) {
13991:         if ($role eq 'login') {
13992:             %choices = &login_choices();
13993:         } else {
13994:             %choices = &color_font_choices();
13995:         }
13996:         if (ref($changes->{$role}) eq 'HASH') {
13997:             if ($role ne 'login') {
13998:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13999:             }
14000:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
14001:                 if ($role ne 'login') {
14002:                     $resulttext .= '<ul>';
14003:                 }
14004:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
14005:                     if ($role ne 'login') {
14006:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
14007:                     }
14008:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
14009:                         if (($role eq 'login') && ($key eq 'showlogo')) {
14010:                             if ($confhash->{$role}{$key}{$item}) {
14011:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
14012:                             } else {
14013:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
14014:                             }
14015:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
14016:                             if ($confhash->{$role}{$key}{$item} ne '') {
14017:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
14018:                                                $confhash->{$role}{$key}{$item}).'</li>';
14019:                             } else {
14020:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
14021:                             }
14022:                         } elsif ($confhash->{$role}{$item} eq '') {
14023:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
14024:                         } else {
14025:                             my $newitem = $confhash->{$role}{$item};
14026:                             if ($key eq 'images') {
14027:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
14028:                             }
14029:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
14030:                         }
14031:                     }
14032:                     if ($role ne 'login') {
14033:                         $resulttext .= '</ul></li>';
14034:                     }
14035:                 } else {
14036:                     if ($confhash->{$role}{$key} eq '') {
14037:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
14038:                     } else {
14039:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
14040:                     }
14041:                 }
14042:                 if ($role ne 'login') {
14043:                     $resulttext .= '</ul>';
14044:                 }
14045:             }
14046:         }
14047:     }
14048:     return $resulttext;
14049: }
14050: 
14051: sub thumb_dimensions {
14052:     return ('200','50');
14053: }
14054: 
14055: sub check_dimensions {
14056:     my ($inputfile) = @_;
14057:     my ($fullwidth,$fullheight);
14058:     if ($inputfile =~ m|^[/\w.\-]+$|) {
14059:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
14060:             my $imageinfo = <PIPE>;
14061:             if (!close(PIPE)) {
14062:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
14063:             }
14064:             chomp($imageinfo);
14065:             my ($fullsize) = 
14066:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
14067:             if ($fullsize) {
14068:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
14069:             }
14070:         }
14071:     }
14072:     return ($fullwidth,$fullheight);
14073: }
14074: 
14075: sub check_configuser {
14076:     my ($uhome,$dom,$confname,$servadm) = @_;
14077:     my ($configuserok,%currroles);
14078:     if ($uhome eq 'no_host') {
14079:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
14080:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
14081:         $configuserok = 
14082:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
14083:                              $configpass,'','','','','',undef,$servadm);
14084:     } else {
14085:         $configuserok = 'ok';
14086:         %currroles = 
14087:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
14088:     }
14089:     return ($configuserok,%currroles);
14090: }
14091: 
14092: sub check_authorstatus {
14093:     my ($dom,$confname,%currroles) = @_;
14094:     my $author_ok;
14095:     if (!$currroles{':'.$dom.':au'}) {
14096:         my $start = time;
14097:         my $end = 0;
14098:         $author_ok = 
14099:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
14100:                                         'au',$end,$start,'','','domconfig');
14101:     } else {
14102:         $author_ok = 'ok';
14103:     }
14104:     return $author_ok;
14105: }
14106: 
14107: sub update_modify_urls {
14108:     my ($r,$modified) = @_;
14109:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
14110:         push(@{$modified_urls},$modified);
14111:         unless ($registered_cleanup) {
14112:             my $handlers = $r->get_handlers('PerlCleanupHandler');
14113:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
14114:             $registered_cleanup=1;
14115:         }
14116:     }
14117: }
14118: 
14119: sub notifysubscribed {
14120:     foreach my $targetsource (@{$modified_urls}){
14121:         next unless (ref($targetsource) eq 'ARRAY');
14122:         my ($target,$source)=@{$targetsource};
14123:         if ($source ne '') {
14124:             if (open(my $logfh,">>",$source.'.log')) {
14125:                 print $logfh "\nCleanup phase: Notifications\n";
14126:                 my @subscribed=&subscribed_hosts($target);
14127:                 foreach my $subhost (@subscribed) {
14128:                     print $logfh "\nNotifying host ".$subhost.':';
14129:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
14130:                     print $logfh $reply;
14131:                 }
14132:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
14133:                 foreach my $subhost (@subscribedmeta) {
14134:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
14135:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
14136:                                                         $subhost);
14137:                     print $logfh $reply;
14138:                 }
14139:                 print $logfh "\n============ Done ============\n";
14140:                 close($logfh);
14141:             }
14142:         }
14143:     }
14144:     return OK;
14145: }
14146: 
14147: sub subscribed_hosts {
14148:     my ($target) = @_;
14149:     my @subscribed;
14150:     if (open(my $fh,"<","$target.subscription")) {
14151:         while (my $subline=<$fh>) {
14152:             if ($subline =~ /^($match_lonid):/) {
14153:                 my $host = $1;
14154:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
14155:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
14156:                         push(@subscribed,$host);
14157:                     }
14158:                 }
14159:             }
14160:         }
14161:     }
14162:     return @subscribed;
14163: }
14164: 
14165: sub check_switchserver {
14166:     my ($dom,$confname) = @_;
14167:     my ($allowed,$switchserver,$home);
14168:     if ($confname eq '') {
14169:         $home = &Apache::lonnet::domain($dom,'primary');
14170:     } else {
14171:         $home = &Apache::lonnet::homeserver($confname,$dom);
14172:         if ($home eq 'no_host') {
14173:             $home = &Apache::lonnet::domain($dom,'primary');
14174:         }
14175:     }
14176:     my @ids=&Apache::lonnet::current_machine_ids();
14177:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
14178:     if (!$allowed) {
14179: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
14180:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
14181:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
14182:     }
14183:     return $switchserver;
14184: }
14185: 
14186: sub modify_quotas {
14187:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14188:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
14189:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
14190:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
14191:         $validationfieldsref);
14192:     if ($action eq 'quotas') {
14193:         $context = 'tools';
14194:     } else {
14195:         $context = $action;
14196:     }
14197:     if ($context eq 'requestcourses') {
14198:         @usertools = ('official','unofficial','community','textbook','placement','lti');
14199:         @options =('norequest','approval','validate','autolimit');
14200:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
14201:         %titles = &courserequest_titles();
14202:         $toolregexp = join('|',@usertools);
14203:         %conditions = &courserequest_conditions();
14204:         $confname = $dom.'-domainconfig';
14205:         my $servadm = $r->dir_config('lonAdmEMail');
14206:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14207:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
14208:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
14209:     } elsif ($context eq 'requestauthor') {
14210:         @usertools = ('author');
14211:         %titles = &authorrequest_titles();
14212:     } else {
14213:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
14214:         %titles = &tool_titles();
14215:     }
14216:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14217:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14218:     foreach my $key (keys(%env)) {
14219:         if ($context eq 'requestcourses') {
14220:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
14221:                 my $item = $1;
14222:                 my $type = $2;
14223:                 if ($type =~ /^limit_(.+)/) {
14224:                     $limithash{$item}{$1} = $env{$key};
14225:                 } else {
14226:                     $confhash{$item}{$type} = $env{$key};
14227:                 }
14228:             }
14229:         } elsif ($context eq 'requestauthor') {
14230:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
14231:                 $confhash{$1} = $env{$key};
14232:             }
14233:         } else {
14234:             if ($key =~ /^form\.quota_(.+)$/) {
14235:                 $confhash{'defaultquota'}{$1} = $env{$key};
14236:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
14237:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
14238:             }
14239:         }
14240:     }
14241:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14242:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
14243:         @approvalnotify = sort(@approvalnotify);
14244:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
14245:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
14246:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
14247:         foreach my $type (@hasuniquecode) {
14248:             if (grep(/^\Q$type\E$/,@crstypes)) {
14249:                 $confhash{'uniquecode'}{$type} = 1;
14250:             }
14251:         }
14252:         my (%newbook,%allpos);
14253:         if ($context eq 'requestcourses') {
14254:             foreach my $type ('textbooks','templates') {
14255:                 @{$allpos{$type}} = (); 
14256:                 my $invalid;
14257:                 if ($type eq 'textbooks') {
14258:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
14259:                 } else {
14260:                     $invalid = &mt('Invalid LON-CAPA course for template');
14261:                 }
14262:                 if ($env{'form.'.$type.'_addbook'}) {
14263:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
14264:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
14265:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
14266:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
14267:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14268:                         } else {
14269:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
14270:                             my $position = $env{'form.'.$type.'_addbook_pos'};
14271:                             $position =~ s/\D+//g;
14272:                             if ($position ne '') {
14273:                                 $allpos{$type}[$position] = $newbook{$type};
14274:                             }
14275:                         }
14276:                     } else {
14277:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14278:                     }
14279:                 }
14280:             } 
14281:         }
14282:         if (ref($domconfig{$action}) eq 'HASH') {
14283:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
14284:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
14285:                     $changes{'notify'}{'approval'} = 1;
14286:                 }
14287:             } else {
14288:                 if ($confhash{'notify'}{'approval'}) {
14289:                     $changes{'notify'}{'approval'} = 1;
14290:                 }
14291:             }
14292:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
14293:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
14294:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
14295:                         unless ($confhash{'uniquecode'}{$crstype}) {
14296:                             $changes{'uniquecode'} = 1;
14297:                         }
14298:                     }
14299:                     unless ($changes{'uniquecode'}) {
14300:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
14301:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
14302:                                 $changes{'uniquecode'} = 1;
14303:                             }
14304:                         }
14305:                     }
14306:                } else {
14307:                    $changes{'uniquecode'} = 1;
14308:                }
14309:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
14310:                 $changes{'uniquecode'} = 1;
14311:             }
14312:             if ($context eq 'requestcourses') {
14313:                 foreach my $type ('textbooks','templates') {
14314:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
14315:                         my %deletions;
14316:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
14317:                         if (@todelete) {
14318:                             map { $deletions{$_} = 1; } @todelete;
14319:                         }
14320:                         my %imgdeletions;
14321:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
14322:                         if (@todeleteimages) {
14323:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
14324:                         }
14325:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
14326:                         for (my $i=0; $i<=$maxnum; $i++) {
14327:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
14328:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
14329:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
14330:                                 if ($deletions{$key}) {
14331:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
14332:                                         #FIXME need to obsolete item in RES space
14333:                                     }
14334:                                     next;
14335:                                 } else {
14336:                                     my $newpos = $env{'form.'.$itemid};
14337:                                     $newpos =~ s/\D+//g;
14338:                                     foreach my $item ('subject','title','publisher','author') {
14339:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14340:                                                  ($type eq 'templates'));
14341:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
14342:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
14343:                                             $changes{$type}{$key} = 1;
14344:                                         }
14345:                                     }
14346:                                     $allpos{$type}[$newpos] = $key;
14347:                                 }
14348:                                 if ($imgdeletions{$key}) {
14349:                                     $changes{$type}{$key} = 1;
14350:                                     #FIXME need to obsolete item in RES space
14351:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
14352:                                     my ($cdom,$cnum) = split(/_/,$key);
14353:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14354:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14355:                                     } else {
14356:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
14357:                                                                                       $cdom,$cnum,$type,$configuserok,
14358:                                                                                       $switchserver,$author_ok);
14359:                                         if ($imgurl) {
14360:                                             $confhash{$type}{$key}{'image'} = $imgurl;
14361:                                             $changes{$type}{$key} = 1; 
14362:                                         }
14363:                                         if ($error) {
14364:                                             &Apache::lonnet::logthis($error);
14365:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14366:                                         }
14367:                                     }
14368:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
14369:                                     $confhash{$type}{$key}{'image'} = 
14370:                                         $domconfig{$action}{$type}{$key}{'image'};
14371:                                 }
14372:                             }
14373:                         }
14374:                     }
14375:                 }
14376:             }
14377:         } else {
14378:             if ($confhash{'notify'}{'approval'}) {
14379:                 $changes{'notify'}{'approval'} = 1;
14380:             }
14381:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
14382:                 $changes{'uniquecode'} = 1;
14383:             }
14384:         }
14385:         if ($context eq 'requestcourses') {
14386:             foreach my $type ('textbooks','templates') {
14387:                 if ($newbook{$type}) {
14388:                     $changes{$type}{$newbook{$type}} = 1;
14389:                     foreach my $item ('subject','title','publisher','author') {
14390:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14391:                                  ($type eq 'template'));
14392:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
14393:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
14394:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
14395:                         }
14396:                     }
14397:                     if ($type eq 'textbooks') {
14398:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
14399:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
14400:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14401:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14402:                             } else {
14403:                                 my ($imageurl,$error) =
14404:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
14405:                                                             $configuserok,$switchserver,$author_ok);
14406:                                 if ($imageurl) {
14407:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
14408:                                 }
14409:                                 if ($error) {
14410:                                     &Apache::lonnet::logthis($error);
14411:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14412:                                 }
14413:                             }
14414:                         }
14415:                     }
14416:                 }
14417:                 if (@{$allpos{$type}} > 0) {
14418:                     my $idx = 0;
14419:                     foreach my $item (@{$allpos{$type}}) {
14420:                         if ($item ne '') {
14421:                             $confhash{$type}{$item}{'order'} = $idx;
14422:                             if (ref($domconfig{$action}) eq 'HASH') {
14423:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
14424:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
14425:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
14426:                                             $changes{$type}{$item} = 1;
14427:                                         }
14428:                                     }
14429:                                 }
14430:                             }
14431:                             $idx ++;
14432:                         }
14433:                     }
14434:                 }
14435:             }
14436:             if (ref($validationitemsref) eq 'ARRAY') {
14437:                 foreach my $item (@{$validationitemsref}) {
14438:                     if ($item eq 'fields') {
14439:                         my @changed;
14440:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
14441:                         if (@{$confhash{'validation'}{$item}} > 0) {
14442:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
14443:                         }
14444:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14445:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14446:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
14447:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
14448:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
14449:                                 } else {
14450:                                     @changed = @{$confhash{'validation'}{$item}};
14451:                                 }
14452:                             } else {
14453:                                 @changed = @{$confhash{'validation'}{$item}};
14454:                             }
14455:                         } else {
14456:                             @changed = @{$confhash{'validation'}{$item}};
14457:                         }
14458:                         if (@changed) {
14459:                             if ($confhash{'validation'}{$item}) {
14460:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
14461:                             } else {
14462:                                 $changes{'validation'}{$item} = &mt('None');
14463:                             }
14464:                         }
14465:                     } else {
14466:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
14467:                         if ($item eq 'markup') {
14468:                             if ($env{'form.requestcourses_validation_'.$item}) {
14469:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14470:                             }
14471:                         }
14472:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14473:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14474:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
14475:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14476:                                 }
14477:                             } else {
14478:                                 if ($confhash{'validation'}{$item} ne '') {
14479:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14480:                                 }
14481:                             }
14482:                         } else {
14483:                             if ($confhash{'validation'}{$item} ne '') {
14484:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14485:                             }
14486:                         }
14487:                     }
14488:                 }
14489:             }
14490:             if ($env{'form.validationdc'}) {
14491:                 my $newval = $env{'form.validationdc'};
14492:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14493:                 if (exists($domcoords{$newval})) {
14494:                     $confhash{'validation'}{'dc'} = $newval;
14495:                 }
14496:             }
14497:             if (ref($confhash{'validation'}) eq 'HASH') {
14498:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14499:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14500:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14501:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
14502:                                 if ($confhash{'validation'}{'dc'} eq '') {
14503:                                     $changes{'validation'}{'dc'} = &mt('None');
14504:                                 } else {
14505:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14506:                                 }
14507:                             }
14508:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
14509:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14510:                         }
14511:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
14512:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14513:                     }
14514:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
14515:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14516:                 }
14517:             } else {
14518:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14519:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14520:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14521:                             $changes{'validation'}{'dc'} = &mt('None');
14522:                         }
14523:                     }
14524:                 }
14525:             }
14526:         }
14527:     } else {
14528:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
14529:     }
14530:     foreach my $item (@usertools) {
14531:         foreach my $type (@{$types},'default','_LC_adv') {
14532:             my $unset; 
14533:             if ($context eq 'requestcourses') {
14534:                 $unset = '0';
14535:                 if ($type eq '_LC_adv') {
14536:                     $unset = '';
14537:                 }
14538:                 if ($confhash{$item}{$type} eq 'autolimit') {
14539:                     $confhash{$item}{$type} .= '=';
14540:                     unless ($limithash{$item}{$type} =~ /\D/) {
14541:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
14542:                     }
14543:                 }
14544:             } elsif ($context eq 'requestauthor') {
14545:                 $unset = '0';
14546:                 if ($type eq '_LC_adv') {
14547:                     $unset = '';
14548:                 }
14549:             } else {
14550:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
14551:                     $confhash{$item}{$type} = 1;
14552:                 } else {
14553:                     $confhash{$item}{$type} = 0;
14554:                 }
14555:             }
14556:             if (ref($domconfig{$action}) eq 'HASH') {
14557:                 if ($action eq 'requestauthor') {
14558:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
14559:                         $changes{$type} = 1;
14560:                     }
14561:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
14562:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
14563:                         $changes{$item}{$type} = 1;
14564:                     }
14565:                 } else {
14566:                     if ($context eq 'requestcourses') {
14567:                         if ($confhash{$item}{$type} ne $unset) {
14568:                             $changes{$item}{$type} = 1;
14569:                         }
14570:                     } else {
14571:                         if (!$confhash{$item}{$type}) {
14572:                             $changes{$item}{$type} = 1;
14573:                         }
14574:                     }
14575:                 }
14576:             } else {
14577:                 if ($context eq 'requestcourses') {
14578:                     if ($confhash{$item}{$type} ne $unset) {
14579:                         $changes{$item}{$type} = 1;
14580:                     }
14581:                 } elsif ($context eq 'requestauthor') {
14582:                     if ($confhash{$type} ne $unset) {
14583:                         $changes{$type} = 1;
14584:                     }
14585:                 } else {
14586:                     if (!$confhash{$item}{$type}) {
14587:                         $changes{$item}{$type} = 1;
14588:                     }
14589:                 }
14590:             }
14591:         }
14592:     }
14593:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14594:         if (ref($domconfig{'quotas'}) eq 'HASH') {
14595:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14596:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
14597:                     if (exists($confhash{'defaultquota'}{$key})) {
14598:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
14599:                             $changes{'defaultquota'}{$key} = 1;
14600:                         }
14601:                     } else {
14602:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
14603:                     }
14604:                 }
14605:             } else {
14606:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
14607:                     if (exists($confhash{'defaultquota'}{$key})) {
14608:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
14609:                             $changes{'defaultquota'}{$key} = 1;
14610:                         }
14611:                     } else {
14612:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
14613:                     }
14614:                 }
14615:             }
14616:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
14617:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
14618:             }
14619:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
14620:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
14621:             }
14622:         }
14623:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
14624:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
14625:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
14626:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14627:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
14628:                             $changes{'defaultquota'}{$key} = 1;
14629:                         }
14630:                     } else {
14631:                         if (!exists($domconfig{'quotas'}{$key})) {
14632:                             $changes{'defaultquota'}{$key} = 1;
14633:                         }
14634:                     }
14635:                 } else {
14636:                     $changes{'defaultquota'}{$key} = 1;
14637:                 }
14638:             }
14639:         }
14640:     }
14641: 
14642:     if ($context eq 'requestauthor') {
14643:         $domdefaults{'requestauthor'} = \%confhash;
14644:     } else {
14645:         foreach my $key (keys(%confhash)) {
14646:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
14647:                 $domdefaults{$key} = $confhash{$key};
14648:             }
14649:         }
14650:     }
14651: 
14652:     my %quotahash = (
14653:                       $action => { %confhash }
14654:                     );
14655:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
14656:                                              $dom);
14657:     if ($putresult eq 'ok') {
14658:         if (keys(%changes) > 0) {
14659:             my $cachetime = 24*60*60;
14660:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14661:             if (ref($lastactref) eq 'HASH') {
14662:                 $lastactref->{'domdefaults'} = 1;
14663:             }
14664:             $resulttext = &mt('Changes made:').'<ul>';
14665:             unless (($context eq 'requestcourses') ||
14666:                     ($context eq 'requestauthor')) {
14667:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
14668:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
14669:                     foreach my $type (@{$types},'default') {
14670:                         if (defined($changes{'defaultquota'}{$type})) {
14671:                             my $typetitle = $usertypes->{$type};
14672:                             if ($type eq 'default') {
14673:                                 $typetitle = $othertitle;
14674:                             }
14675:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
14676:                         }
14677:                     }
14678:                     $resulttext .= '</ul></li>';
14679:                 }
14680:             }
14681:             my %newenv;
14682:             foreach my $item (@usertools) {
14683:                 my (%haschgs,%inconf);
14684:                 if ($context eq 'requestauthor') {
14685:                     %haschgs = %changes;
14686:                     %inconf = %confhash;
14687:                 } else {
14688:                     if (ref($changes{$item}) eq 'HASH') {
14689:                         %haschgs = %{$changes{$item}};
14690:                     }
14691:                     if (ref($confhash{$item}) eq 'HASH') {
14692:                         %inconf = %{$confhash{$item}};
14693:                     }
14694:                 }
14695:                 if (keys(%haschgs) > 0) {
14696:                     my $newacc = 
14697:                         &Apache::lonnet::usertools_access($env{'user.name'},
14698:                                                           $env{'user.domain'},
14699:                                                           $item,'reload',$context);
14700:                     if (($context eq 'requestcourses') ||
14701:                         ($context eq 'requestauthor')) {
14702:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14703:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14704:                         }
14705:                     } else {
14706:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14707:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14708:                         }
14709:                     }
14710:                     unless ($context eq 'requestauthor') {
14711:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14712:                     }
14713:                     foreach my $type (@{$types},'default','_LC_adv') {
14714:                         if ($haschgs{$type}) {
14715:                             my $typetitle = $usertypes->{$type};
14716:                             if ($type eq 'default') {
14717:                                 $typetitle = $othertitle;
14718:                             } elsif ($type eq '_LC_adv') {
14719:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14720:                             }
14721:                             if ($inconf{$type}) {
14722:                                 if ($context eq 'requestcourses') {
14723:                                     my $cond;
14724:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14725:                                         if ($1 eq '') {
14726:                                             $cond = &mt('(Automatic processing of any request).');
14727:                                         } else {
14728:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14729:                                         }
14730:                                     } else { 
14731:                                         $cond = $conditions{$inconf{$type}};
14732:                                     }
14733:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14734:                                 } elsif ($context eq 'requestauthor') {
14735:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14736:                                                              $titles{$inconf{$type}},$typetitle);
14737: 
14738:                                 } else {
14739:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14740:                                 }
14741:                             } else {
14742:                                 if ($type eq '_LC_adv') {
14743:                                     if ($inconf{$type} eq '0') {
14744:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14745:                                     } else { 
14746:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14747:                                     }
14748:                                 } else {
14749:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14750:                                 }
14751:                             }
14752:                         }
14753:                     }
14754:                     unless ($context eq 'requestauthor') {
14755:                         $resulttext .= '</ul></li>';
14756:                     }
14757:                 }
14758:             }
14759:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14760:                 if (ref($changes{'notify'}) eq 'HASH') {
14761:                     if ($changes{'notify'}{'approval'}) {
14762:                         if (ref($confhash{'notify'}) eq 'HASH') {
14763:                             if ($confhash{'notify'}{'approval'}) {
14764:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14765:                             } else {
14766:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14767:                             }
14768:                         }
14769:                     }
14770:                 }
14771:             }
14772:             if ($action eq 'requestcourses') {
14773:                 my @offon = ('off','on');
14774:                 if ($changes{'uniquecode'}) {
14775:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14776:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14777:                         $resulttext .= '<li>'.
14778:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14779:                                        '</li>';
14780:                     } else {
14781:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14782:                                        '</li>';
14783:                     }
14784:                 }
14785:                 foreach my $type ('textbooks','templates') {
14786:                     if (ref($changes{$type}) eq 'HASH') {
14787:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14788:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14789:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14790:                             my $coursetitle = $coursehash{'description'};
14791:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14792:                             $resulttext .= '<li>';
14793:                             foreach my $item ('subject','title','publisher','author') {
14794:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14795:                                          ($type eq 'templates'));
14796:                                 my $name = $item.':';
14797:                                 $name =~ s/^(\w)/\U$1/;
14798:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14799:                             }
14800:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14801:                             if ($type eq 'textbooks') {
14802:                                 if ($confhash{$type}{$key}{'image'}) {
14803:                                     $resulttext .= ' '.&mt('Image: [_1]',
14804:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14805:                                                    ' alt="Textbook cover" />').'<br />';
14806:                                 }
14807:                             }
14808:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14809:                         }
14810:                         $resulttext .= '</ul></li>';
14811:                     }
14812:                 }
14813:                 if (ref($changes{'validation'}) eq 'HASH') {
14814:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14815:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14816:                         foreach my $item (@{$validationitemsref}) {
14817:                             if (exists($changes{'validation'}{$item})) {
14818:                                 if ($item eq 'markup') {
14819:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14820:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14821:                                 } else {
14822:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14823:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14824:                                 }
14825:                             }
14826:                         }
14827:                         if (exists($changes{'validation'}{'dc'})) {
14828:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14829:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14830:                         }
14831:                         $resulttext .= '</ul></li>';
14832:                     }
14833:                 }
14834:             }
14835:             $resulttext .= '</ul>';
14836:             if (keys(%newenv)) {
14837:                 &Apache::lonnet::appenv(\%newenv);
14838:             }
14839:         } else {
14840:             if ($context eq 'requestcourses') {
14841:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14842:             } elsif ($context eq 'requestauthor') {
14843:                 $resulttext = &mt('No changes made to rights to request author space.');
14844:             } else {
14845:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14846:             }
14847:         }
14848:     } else {
14849:         $resulttext = '<span class="LC_error">'.
14850: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14851:     }
14852:     if ($errors) {
14853:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14854:                        '<ul>'.$errors.'</ul></p>';
14855:     }
14856:     return $resulttext;
14857: }
14858: 
14859: sub process_textbook_image {
14860:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14861:     my $filename = $env{'form.'.$caller.'.filename'};
14862:     my ($error,$url);
14863:     my ($width,$height) = (50,50);
14864:     if ($configuserok eq 'ok') {
14865:         if ($switchserver) {
14866:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14867:                          $switchserver);
14868:         } elsif ($author_ok eq 'ok') {
14869:             my $modified = [];
14870:             my ($result,$imageurl) =
14871:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14872:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14873:                                                         '',$modified);
14874:             if ($result eq 'ok') {
14875:                 $url = $imageurl;
14876:                 &update_modify_urls($r,$modified);
14877:             } else {
14878:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14879:             }
14880:         } else {
14881:             $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);
14882:         }
14883:     } else {
14884:         $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);
14885:     }
14886:     return ($url,$error);
14887: }
14888: 
14889: sub modify_ltitools {
14890:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14891:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14892:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14893: 
14894:     my $confname = $dom.'-domainconfig';
14895:     my $servadm = $r->dir_config('lonAdmEMail');
14896:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14897: 
14898:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14899:     my $toolserror =
14900:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14901:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14902: 
14903:     my $home = &Apache::lonnet::domain($dom,'primary');
14904:     unless (($home eq 'no_host') || ($home eq '')) {
14905:         my @ids=&Apache::lonnet::current_machine_ids();
14906:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14907:     }
14908: 
14909:     if (keys(%ltitoolschg)) {
14910:         foreach my $id (keys(%ltitoolschg)) {
14911:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14912:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14913:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14914:                         if ($is_home) {
14915:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14916:                         }
14917:                     }
14918:                 }
14919:             }
14920:         }
14921:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14922:         if (keys(%ltitoolschg)) {
14923:             %newltitools = %ltitoolschg;
14924:         }
14925:     }
14926:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14927:         foreach my $id (%{$domconfig{'ltitools'}}) {
14928:             next if ($id !~ /^\d+$/);
14929:             unless (exists($ltitoolschg{$id})) {
14930:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14931:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14932:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14933:                             if ($is_home) {
14934:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14935:                             }
14936:                         } else {
14937:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14938:                         }
14939:                     }
14940:                 } else {
14941:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14942:                 }
14943:             }
14944:         }
14945:     }
14946:     if ($toolserror) {
14947:         $errors = '<li>'.$toolserror.'</li>';
14948:     }
14949:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14950:         $resulttext = &mt('No changes made.');
14951:         if ($errors) {
14952:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14953:                                  $errors.'</ul>';
14954:         }
14955:         return $resulttext;
14956:     }
14957:     my %ltitoolshash = (
14958:                           $action => { %newltitools }
14959:                        );
14960:     if (keys(%secchanges)) {
14961:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14962:     }
14963:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14964:     if ($putresult eq 'ok') {
14965:         my %keystore;
14966:         if ($is_home) {
14967:             my %toolsenchash = (
14968:                                    $action => { %newtoolsenc }
14969:                                );
14970:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14971:             my $cachetime = 24*60*60;
14972:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
14973:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14974:         }
14975:         $resulttext = &mt('Changes made:').'<ul>';
14976:         if (keys(%secchanges) > 0) {
14977:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14978:         }
14979:         if (keys(%ltitoolschg) > 0) {
14980:             $resulttext .= $ltitoolsoutput;
14981:         }
14982:         my $cachetime = 24*60*60;
14983:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
14984:         if (ref($lastactref) eq 'HASH') {
14985:             $lastactref->{'ltitools'} = 1;
14986:         }
14987:     } else {
14988:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14989:     }
14990:     if ($errors) {
14991:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14992:                        $errors.'</ul></p>';
14993:     }
14994:     return $resulttext;
14995: }
14996: 
14997: sub fetch_secrets {
14998:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
14999:     my %keyset;
15000:     %{$currsec} = ();
15001:     $newsec->{'private'}{'keys'} = [];
15002:     $newsec->{'encrypt'} = {};
15003:     $newsec->{'rules'} = {};
15004:     if ($context eq 'ltisec') {
15005:         $newsec->{'linkprot'} = {};
15006:     }
15007:     if (ref($domconfig->{$context}) eq 'HASH') {
15008:         %{$currsec} = %{$domconfig->{$context}};
15009:         if ($context eq 'ltisec') {
15010:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
15011:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
15012:                     unless ($id =~ /^\d+$/) {
15013:                         delete($currsec->{'linkprot'}{$id});
15014:                     }
15015:                 }
15016:             }
15017:         }
15018:         if (ref($currsec->{'private'}) eq 'HASH') {
15019:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
15020:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
15021:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
15022:             }
15023:         }
15024:     }
15025:     my @items= ('crs','dom');
15026:     if ($context eq 'ltisec') {
15027:         push(@items,'consumers');
15028:     }
15029:     foreach my $item (@items) {
15030:         my $formelement;
15031:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
15032:             $formelement = 'form.'.$context.'_'.$item;
15033:         } else {
15034:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
15035:         }
15036:         if ($env{$formelement}) {
15037:             $newsec->{'encrypt'}{$item} = 1;
15038:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
15039:                 unless ($currsec->{'encrypt'}{$item}) {
15040:                     $secchanges->{'encrypt'} = 1;
15041:                 }
15042:             } else {
15043:                 $secchanges->{'encrypt'} = 1;
15044:             }
15045:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
15046:             if ($currsec->{'encrypt'}{$item}) {
15047:                 $secchanges->{'encrypt'} = 1;
15048:             }
15049:         }
15050:     }
15051:     my $secrets;
15052:     if ($context eq 'ltisec') {
15053:         $secrets = 'ltisecrets';
15054:     } else {
15055:         $secrets = 'toolsecrets';
15056:     }
15057:     unless (exists($currsec->{'rules'})) {
15058:         $currsec->{'rules'} = {};
15059:     }
15060:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
15061: 
15062:     my @ids=&Apache::lonnet::current_machine_ids();
15063:     my %servers = &Apache::lonnet::get_servers($dom,'library');
15064: 
15065:     foreach my $hostid (keys(%servers)) {
15066:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
15067:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
15068:             if (exists($env{$keyitem})) {
15069:                 $env{$keyitem} =~ s/(`)/'/g;
15070:                 if ($keyset{$hostid}) {
15071:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
15072:                         if ($env{$keyitem} ne '') {
15073:                             $secchanges->{'private'} = 1;
15074:                             $newkeyset->{$hostid} = $env{$keyitem};
15075:                         }
15076:                     }
15077:                 } elsif ($env{$keyitem} ne '') {
15078:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
15079:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
15080:                     }
15081:                     $secchanges->{'private'} = 1;
15082:                     $newkeyset->{$hostid} = $env{$keyitem};
15083:                 }
15084:             }
15085:         }
15086:     }
15087: }
15088: 
15089: sub store_security {
15090:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
15091:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
15092:                    (ref($keystore) eq 'HASH'));
15093:     if (keys(%{$secchanges})) {
15094:         if ($secchanges->{'private'}) {
15095:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
15096:             foreach my $hostid (keys(%{$newkeyset})) {
15097:                 my $storehash = {
15098:                                    key => $newkeyset->{$hostid},
15099:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
15100:                                 };
15101:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
15102:                                                                   $dom,$hostid);
15103:             }
15104:         }
15105:     }
15106: }
15107: 
15108: sub lti_security_results {
15109:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
15110:     my $output;
15111:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15112:     my $needs_update;
15113:     foreach my $item (keys(%{$secchanges})) {
15114:         if ($item eq 'encrypt') {
15115:             $needs_update = 1;
15116:             my %encrypted;
15117:             if ($context eq 'lti') {
15118:                 %encrypted = (
15119:                               crs  => {
15120:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
15121:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
15122:                                       },
15123:                               dom => {
15124:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
15125:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
15126:                                      },
15127:                               consumers => {
15128:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
15129:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
15130:                                            },
15131:                              );
15132:             } else {
15133:                 %encrypted = (
15134:                               crs  => {
15135:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
15136:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
15137:                                       },
15138:                               dom => {
15139:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
15140:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
15141:                                      },
15142:                              );
15143:             }
15144:             my @types= ('crs','dom');
15145:             if ($context eq 'lti') {
15146:                 foreach my $type (@types) {
15147:                     undef($domdefaults{'linkprotenc_'.$type});
15148:                 }
15149:                 push(@types,'consumers');
15150:                 undef($domdefaults{'ltienc_consumers'});
15151:             } elsif ($context eq 'ltitools') {
15152:                 foreach my $type (@types) {
15153:                     undef($domdefaults{'toolenc_'.$type});
15154:                 }
15155:             }
15156:             foreach my $type (@types) {
15157:                 my $shown = $encrypted{$type}{'off'};
15158:                 if (ref($newsec->{$item}) eq 'HASH') {
15159:                     if ($newsec->{$item}{$type}) {
15160:                         if ($context eq 'lti') {
15161:                             if ($type eq 'consumers') {
15162:                                 $domdefaults{'ltienc_consumers'} = 1;
15163:                             } else {
15164:                                 $domdefaults{'linkprotenc_'.$type} = 1;
15165:                             }
15166:                         } elsif ($context eq 'ltitools') {
15167:                             $domdefaults{'toolenc_'.$type} = 1;
15168:                         }
15169:                         $shown = $encrypted{$type}{'on'};
15170:                     }
15171:                 }
15172:                 $output .= '<li>'.$shown.'</li>';
15173:             }
15174:         } elsif ($item eq 'rules') {
15175:             my %titles = &Apache::lonlocal::texthash(
15176:                                       min   => 'Minimum password length',
15177:                                       max   => 'Maximum password length',
15178:                                       chars => 'Required characters',
15179:                          );
15180:             foreach my $rule ('min','max') {
15181:                 if ($newsec->{rules}{$rule} eq '') {
15182:                     if ($rule eq 'min') {
15183:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15184:                                    ' '.&mt('Default of [_1] will be used',
15185:                                            $Apache::lonnet::passwdmin).'</li>';
15186:                     } else {
15187:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15188:                     }
15189:                 } else {
15190:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
15191:                 }
15192:             }
15193:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
15194:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
15195:                     my %rulenames = &Apache::lonlocal::texthash(
15196:                                              uc => 'At least one upper case letter',
15197:                                              lc => 'At least one lower case letter',
15198:                                              num => 'At least one number',
15199:                                              spec => 'At least one non-alphanumeric',
15200:                                     );
15201:                     my $needed = '<ul><li>'.
15202:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
15203:                                  '</li></ul>';
15204:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15205:                 } else {
15206:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15207:                 }
15208:             } else {
15209:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15210:             }
15211:         } elsif ($item eq 'private') {
15212:             $needs_update = 1;
15213:             if ($context eq 'lti') {
15214:                 undef($domdefaults{'ltiprivhosts'});
15215:             } elsif ($context eq 'ltitools') {
15216:                 undef($domdefaults{'toolprivhosts'});
15217:             }
15218:             if (keys(%{$newkeyset})) {
15219:                 my @privhosts;
15220:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
15221:                     if ($keystore->{$hostid} eq 'ok') {
15222:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
15223:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
15224:                             push(@privhosts,$hostid);
15225:                         }
15226:                     }
15227:                 }
15228:                 if (@privhosts) {
15229:                     if ($context eq 'lti') {
15230:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
15231:                     } elsif ($context eq 'ltitools') {
15232:                         $domdefaults{'toolprivhosts'} = \@privhosts;
15233:                     }
15234:                 }
15235:             }
15236:         } elsif ($item eq 'linkprot') {
15237:             next;
15238:         } elsif ($item eq 'suggested') {
15239:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
15240:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
15241:                 my $suggestions;
15242:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
15243:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
15244:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
15245:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
15246:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
15247:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
15248:                                         '</li>';
15249:                     } else {
15250:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
15251:                                                    $newsec->{'suggested'}->{$id}).'</li>';
15252:                     }
15253:                 }
15254:                 if ($suggestions) {
15255:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
15256:                                '<ul>'.$suggestions.'</ul>'.
15257:                                '</li>';
15258:                 }
15259:             }
15260:         }
15261:     }
15262:     if ($needs_update) {
15263:         my $cachetime = 24*60*60;
15264:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15265:     }
15266:     return $output;
15267: }
15268: 
15269: sub modify_proctoring {
15270:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15271:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15272:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
15273:     my $confname = $dom.'-domainconfig';
15274:     my $servadm = $r->dir_config('lonAdmEMail');
15275:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
15276:     my %providernames = &proctoring_providernames();
15277:     my $maxnum = scalar(keys(%providernames));
15278: 
15279:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
15280:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
15281:     if (ref($requref) eq 'HASH') {
15282:         %requserfields = %{$requref};
15283:     }
15284:     if (ref($opturef) eq 'HASH') {
15285:         %optuserfields = %{$opturef};
15286:     }
15287:     if (ref($defref) eq 'HASH') {
15288:         %defaults = %{$defref};
15289:     }
15290:     if (ref($extref) eq 'HASH') {
15291:         %extended = %{$extref};
15292:     }
15293:     if (ref($crsref) eq 'HASH') {
15294:         %crsconf = %{$crsref};
15295:     }
15296:     if (ref($rolesref) eq 'ARRAY') {
15297:         @courseroles = @{$rolesref};
15298:     }
15299:     if (ref($ltiref) eq 'ARRAY') {
15300:         @ltiroles = @{$ltiref};
15301:     }
15302: 
15303:     if (ref($domconfig{$action}) eq 'HASH') {
15304:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
15305:         if (@todeleteimages) {
15306:             map { $imgdeletions{$_} = 1; } @todeleteimages;
15307:         }
15308:     }
15309:     my %customadds;
15310:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
15311:     if (@newcustom) {
15312:         map { $customadds{$_} = 1; } @newcustom;
15313:     }
15314:     foreach my $provider (sort(keys(%providernames))) {
15315:         $confhash{$provider} = {};
15316:         my $pos = $env{'form.proctoring_pos_'.$provider};
15317:         $pos =~ s/\D+//g;
15318:         $allpos[$pos] = $provider;
15319:         my (%current,%currentenc);
15320:         my $showroles = 0;
15321:         if (ref($domconfig{$action}) eq 'HASH') {
15322:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15323:                 %current = %{$domconfig{$action}{$provider}};
15324:                 foreach my $item ('key','secret') { 
15325:                     $currentenc{$item} = $current{$item};
15326:                     delete($current{$item});
15327:                 }
15328:             }
15329:         }
15330:         if ($env{'form.proctoring_available_'.$provider}) {
15331:             $confhash{$provider}{'available'} = 1;
15332:             unless ($current{'available'}) {
15333:                 $changes{$provider} = 1;
15334:             }
15335:         } else {
15336:             %{$confhash{$provider}} = %current;
15337:             %{$encconfhash{$provider}} = %currentenc;
15338:             $confhash{$provider}{'available'} = 0;
15339:             if ($current{'available'}) {
15340:                 $changes{$provider} = 1;
15341:             }
15342:         }
15343:         if ($confhash{$provider}{'available'}) {
15344:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
15345:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
15346:                 if ($field eq 'lifetime') {
15347:                     if ($possval =~ /^\d+$/) {
15348:                         $confhash{$provider}{$field} = $possval;
15349:                     }
15350:                 } elsif ($field eq 'version') {
15351:                     if ($possval =~ /^\d+\.\d+$/) {
15352:                         $confhash{$provider}{$field} = $possval;
15353:                     }
15354:                 } elsif ($field eq 'sigmethod') {
15355:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
15356:                         $confhash{$provider}{$field} = $possval;
15357:                     }
15358:                 } elsif ($field eq 'url') {
15359:                     $confhash{$provider}{$field} = $possval;
15360:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
15361:                     $encconfhash{$provider}{$field} = $possval;
15362:                     unless ($currentenc{$field} eq $possval) {
15363:                         $changes{$provider} = 1;
15364:                     }
15365:                 }
15366:                 unless (($field eq 'key') || ($field eq 'secret')) {
15367:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
15368:                         $changes{$provider} = 1;
15369:                     }
15370:                 }
15371:             }
15372:             if ($imgdeletions{$provider}) {
15373:                 $changes{$provider} = 1;
15374:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
15375:                 my ($imageurl,$error) =
15376:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
15377:                                               $configuserok,$switchserver,$author_ok);
15378:                 if ($imageurl) {
15379:                     $confhash{$provider}{'image'} = $imageurl;
15380:                     $changes{$provider} = 1; 
15381:                 }
15382:                 if ($error) {
15383:                     &Apache::lonnet::logthis($error);
15384:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15385:                 }
15386:             } elsif (exists($current{'image'})) {
15387:                 $confhash{$provider}{'image'} = $current{'image'};
15388:             }
15389:             if (ref($requserfields{$provider}) eq 'ARRAY') {
15390:                 if (@{$requserfields{$provider}} > 0) {
15391:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
15392:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
15393:                             $confhash{$provider}{'incdom'} = 1;
15394:                         }
15395:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
15396:                             $changes{$provider} = 1;
15397:                         }
15398:                     }
15399:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
15400:                         $showroles = 1;
15401:                     }
15402:                 }
15403:             }
15404:             $confhash{$provider}{'fields'} = [];
15405:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
15406:                 if (@{$optuserfields{$provider}} > 0) {
15407:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
15408:                     foreach my $field (@{$optuserfields{$provider}}) {
15409:                         if (grep(/^\Q$field\E$/,@optfields)) {
15410:                             push(@{$confhash{$provider}{'fields'}},$field);
15411:                         }
15412:                     }
15413:                 }
15414:                 if (ref($current{'fields'}) eq 'ARRAY') {
15415:                     unless ($changes{$provider}) {
15416:                         my @new = sort(@{$confhash{$provider}{'fields'}});
15417:                         my @old = sort(@{$current{'fields'}}); 
15418:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15419:                         if (@diffs) {
15420:                             $changes{$provider} = 1;
15421:                         }
15422:                     }
15423:                 } elsif (@{$confhash{$provider}{'fields'}}) {
15424:                     $changes{$provider} = 1;
15425:                 }
15426:             }
15427:             if (ref($defaults{$provider}) eq 'ARRAY') {  
15428:                 if (@{$defaults{$provider}} > 0) {
15429:                     my %options;
15430:                     if (ref($extended{$provider}) eq 'HASH') {
15431:                         %options = %{$extended{$provider}};
15432:                     }
15433:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
15434:                     foreach my $field (@{$defaults{$provider}}) {
15435:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
15436:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
15437:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
15438:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
15439:                             }
15440:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
15441:                             foreach my $inner (keys(%{$options{$field}})) {
15442:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
15443:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
15444:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
15445:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
15446:                                     }
15447:                                 } else {
15448:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
15449:                                 }
15450:                             }
15451:                         } else {
15452:                             if (grep(/^\Q$field\E$/,@checked)) {
15453:                                 push(@{$confhash{$provider}{'defaults'}},$field);
15454:                             }
15455:                         }
15456:                     }
15457:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15458:                         if (ref($current{'defaults'}) eq 'ARRAY') {
15459:                             unless ($changes{$provider}) {
15460:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
15461:                                 my @old = sort(@{$current{'defaults'}});
15462:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15463:                                 if (@diffs) {
15464:                                     $changes{$provider} = 1; 
15465:                                 }
15466:                             }
15467:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
15468:                             if (@{$current{'defaults'}}) {
15469:                                 $changes{$provider} = 1;
15470:                             }
15471:                         }
15472:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15473:                         if (ref($current{'defaults'}) eq 'HASH') {
15474:                             unless ($changes{$provider}) {
15475:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15476:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15477:                                         $changes{$provider} = 1;
15478:                                         last;
15479:                                     }
15480:                                 }
15481:                             }
15482:                             unless ($changes{$provider}) {
15483:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15484:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15485:                                         $changes{$provider} = 1;
15486:                                         last;
15487:                                     }
15488:                                 }
15489:                             }
15490:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15491:                             $changes{$provider} = 1;
15492:                         }
15493:                     }
15494:                 }
15495:             }
15496:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15497:                 if (@{$crsconf{$provider}} > 0) {
15498:                     $confhash{$provider}{'crsconf'} = [];
15499:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15500:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15501:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15502:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15503:                         }
15504:                     }
15505:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15506:                         unless ($changes{$provider}) {  
15507:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15508:                             my @old = sort(@{$current{'crsconf'}});
15509:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15510:                             if (@diffs) {
15511:                                 $changes{$provider} = 1;
15512:                             }
15513:                         }
15514:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15515:                         $changes{$provider} = 1;
15516:                     }
15517:                 }
15518:             }
15519:             if ($showroles) {
15520:                 $confhash{$provider}{'roles'} = {};
15521:                 foreach my $role (@courseroles) {
15522:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15523:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15524:                         $confhash{$provider}{'roles'}{$role} = $poss;
15525:                     }
15526:                 }
15527:                 unless ($changes{$provider}) {
15528:                     if (ref($current{'roles'}) eq 'HASH') {
15529:                         foreach my $role (keys(%{$current{'roles'}})) {
15530:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15531:                                 $changes{$provider} = 1;
15532:                                 last
15533:                             }
15534:                         }
15535:                         unless ($changes{$provider}) {
15536:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15537:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15538:                                     $changes{$provider} = 1;
15539:                                     last;
15540:                                 }
15541:                             }
15542:                         }
15543:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15544:                         $changes{$provider} = 1;
15545:                     }
15546:                 }
15547:             }
15548:             if (ref($current{'custom'}) eq 'HASH') {
15549:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15550:                 foreach my $key (keys(%{$current{'custom'}})) {
15551:                     if (grep(/^\Q$key\E$/,@customdels)) {
15552:                         $changes{$provider} = 1;
15553:                     } else {
15554:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15555:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15556:                             $changes{$provider} = 1;
15557:                         }
15558:                     }
15559:                 }
15560:             }
15561:             if ($customadds{$provider}) {
15562:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15563:                 $name =~ s/(`)/'/g;
15564:                 $name =~ s/^\s+//;
15565:                 $name =~ s/\s+$//;
15566:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15567:                 $value =~ s/(`)/'/g;
15568:                 $value =~ s/^\s+//;
15569:                 $value =~ s/\s+$//;
15570:                 if ($name ne '') {
15571:                     $confhash{$provider}{'custom'}{$name} = $value;
15572:                     $changes{$provider} = 1;
15573:                 }
15574:             }
15575:         }
15576:     }
15577:     if (@allpos > 0) {
15578:         my $idx = 0;
15579:         foreach my $provider (@allpos) {
15580:             if ($provider ne '') {
15581:                 $confhash{$provider}{'order'} = $idx;
15582:                 unless ($changes{$provider}) {
15583:                     if (ref($domconfig{$action}) eq 'HASH') {
15584:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15585:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15586:                                 $changes{$provider} = 1;
15587:                             }
15588:                         }
15589:                     }
15590:                 }
15591:                 $idx ++;
15592:             }
15593:         }
15594:     }
15595:     my %proc_hash = (
15596:                           $action => { %confhash }
15597:                        );
15598:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15599:                                              $dom);
15600:     if ($putresult eq 'ok') {
15601:         my %proc_enchash = (
15602:                              $action => { %encconfhash }
15603:                          );
15604:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15605:         if (keys(%changes) > 0) {
15606:             my $cachetime = 24*60*60;
15607:             my %procall = %confhash;
15608:             foreach my $provider (keys(%procall)) {
15609:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15610:                     foreach my $key ('key','secret') {
15611:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15612:                     }
15613:                 }
15614:             }
15615:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15616:             if (ref($lastactref) eq 'HASH') {
15617:                 $lastactref->{'proctoring'} = 1;
15618:             }
15619:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15620:             my %bynum;
15621:             foreach my $provider (sort(keys(%changes))) {
15622:                 my $position = $confhash{$provider}{'order'};
15623:                 $bynum{$position} = $provider;
15624:             }
15625:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15626:                 my $provider = $bynum{$pos};
15627:                 my %lt = &proctoring_titles($provider);
15628:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15629:                 if (!$confhash{$provider}{'available'}) {
15630:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15631:                 } else {
15632:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15633:                     if ($confhash{$provider}{'image'}) {
15634:                         $resulttext .= '&nbsp;'.
15635:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15636:                                        ' alt="'.&mt('Proctoring icon').'" />';
15637:                     }
15638:                     $resulttext .= '<ul>';
15639:                     my $position = $pos + 1;
15640:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15641:                     foreach my $key ('version','sigmethod','url','lifetime') {
15642:                         if ($confhash{$provider}{$key} ne '') {
15643:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15644:                         }
15645:                     }
15646:                     if ($encconfhash{$provider}{'key'} ne '') {
15647:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15648:                     }
15649:                     if ($encconfhash{$provider}{'secret'} ne '') {
15650:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15651:                         my $num = length($encconfhash{$provider}{'secret'});
15652:                         $resulttext .= ('*'x$num).'</li>';
15653:                     }
15654:                     my (@fields,$showroles);
15655:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15656:                         push(@fields,@{$requserfields{$provider}});
15657:                     }
15658:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15659:                         push(@fields,@{$confhash{$provider}{'fields'}});
15660:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15661:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15662:                     }
15663:                     if (@fields) {
15664:                         if (grep(/^roles$/,@fields)) {
15665:                             $showroles = 1;
15666:                         }
15667:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15668:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15669:                     }
15670:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15671:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15672:                             if ($confhash{$provider}{'incdom'}) {
15673:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15674:                             } else { 
15675:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15676:                             }
15677:                         }
15678:                     }
15679:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15680:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15681:                             $resulttext .= '<li>'.$lt{'defa'};
15682:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15683:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15684:                             }
15685:                             $resulttext =~ s/,$//;
15686:                             $resulttext .= '</li>';
15687:                         }
15688:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15689:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15690:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15691:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15692:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15693:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15694:                                 }
15695:                             }
15696:                             $resulttext .= '</ul></li>';
15697:                         }
15698:                     }
15699:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15700:                         if (@{$crsconf{$provider}} > 0) {
15701:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15702:                             my $numconfig = 0;
15703:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15704:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15705:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15706:                                         $numconfig ++;
15707:                                         if ($provider eq 'examity') {
15708:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15709:                                         } else {
15710:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15711:                                         }
15712:                                     }
15713:                                     $resulttext =~ s/,$//;
15714:                                 }
15715:                             }
15716:                             if (!$numconfig) {
15717:                                 $resulttext .= '&nbsp;'.&mt('None');
15718:                             }
15719:                             $resulttext .= '</li>';
15720:                         }
15721:                     }
15722:                     if ($showroles) {
15723:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15724:                             my $rolemaps;
15725:                             foreach my $role (@courseroles) {
15726:                                 if ($confhash{$provider}{'roles'}{$role}) {
15727:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15728:                                                  $confhash{$provider}{'roles'}{$role}.',';
15729:                                 }
15730:                             }
15731:                             if ($rolemaps) {
15732:                                 $rolemaps =~ s/,$//;
15733:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15734:                             }
15735:                         }
15736:                     }
15737:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15738:                         my $customlist;
15739:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15740:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15741:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15742:                             }
15743:                             $customlist =~ s/,$//;
15744:                         }
15745:                         if ($customlist) {
15746:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15747:                         }
15748:                     } 
15749:                     $resulttext .= '</ul></li>';
15750:                 }
15751:             }
15752:             $resulttext .= '</ul>';
15753:         } else {
15754:             $resulttext = &mt('No changes made.');
15755:         }
15756:     } else {
15757:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15758:     }
15759:     if ($errors) {
15760:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15761:                        $errors.'</ul>';
15762:     }
15763:     return $resulttext;
15764: }
15765: 
15766: sub process_proctoring_image {
15767:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15768:     my $filename = $env{'form.'.$caller.'.filename'};
15769:     my ($error,$url);
15770:     my ($width,$height) = (21,21);
15771:     if ($configuserok eq 'ok') {
15772:         if ($switchserver) {
15773:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15774:                          $switchserver);
15775:         } elsif ($author_ok eq 'ok') {
15776:             my $modified = [];
15777:             my ($result,$imageurl,$madethumb) =
15778:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15779:                                                         "proctoring/$provider/icon",$width,$height,
15780:                                                         '',$modified);
15781:             if ($result eq 'ok') {
15782:                 if ($madethumb) {
15783:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15784:                     my $imagethumb = "$path/tn-".$imagefile;
15785:                     $url = $imagethumb;
15786:                 } else {
15787:                     $url = $imageurl;
15788:                 }
15789:                 &update_modify_urls($r,$modified);
15790:             } else {
15791:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15792:             }
15793:         } else {
15794:             $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);
15795:         }
15796:     } else {
15797:         $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);
15798:     }
15799:     return ($url,$error);
15800: }
15801: 
15802: sub modify_lti {
15803:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15804:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15805:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15806:     my (%posslti,%posslticrs,%posscrstype);
15807:     my @courseroles = ('cc','in','ta','ep','st');
15808:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15809:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15810:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15811:     my %coursetypetitles = &Apache::lonlocal::texthash (
15812:                                official   => 'Official',
15813:                                unofficial => 'Unofficial',
15814:                                community  => 'Community',
15815:                                textbook   => 'Textbook',
15816:                                placement  => 'Placement Test',
15817:                                lti        => 'LTI Provider',
15818:     );
15819:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15820:     my %lt = &lti_names();
15821:     map { $posslti{$_} = 1; } @ltiroles;
15822:     map { $posslticrs{$_} = 1; } @lticourseroles;
15823:     map { $posscrstype{$_} = 1; } @coursetypes;
15824: 
15825:     my %menutitles = &ltimenu_titles();
15826:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15827: 
15828:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15829: 
15830:     my (%linkprotchg,$linkprotoutput,$is_home);
15831:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15832:                                                            \%linkprotchg,'domain');
15833:     my $home = &Apache::lonnet::domain($dom,'primary');
15834:     unless (($home eq 'no_host') || ($home eq '')) {
15835:         my @ids=&Apache::lonnet::current_machine_ids();
15836:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15837:     }
15838: 
15839:     if (keys(%linkprotchg)) {
15840:         $secchanges{'linkprot'} = 1;
15841:         my %oldlinkprot;
15842:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15843:             %oldlinkprot = %{$currltisec{'linkprot'}};
15844:         }
15845:         foreach my $id (keys(%linkprotchg)) {
15846:             if (ref($linkprotchg{$id}) eq 'HASH') {
15847:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15848:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15849:                         if ($is_home) {
15850:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15851:                         }
15852:                     }
15853:                 }
15854:             } else {
15855:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15856:             }
15857:         }
15858:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15859:         if (keys(%linkprotchg)) {
15860:             %{$newltisec{'linkprot'}} = %linkprotchg;
15861:         }
15862:     }
15863:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15864:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
15865:             next if ($id !~ /^\d+$/);
15866:             unless (exists($linkprotchg{$id})) {
15867:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15868:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15869:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15870:                             if ($is_home) {
15871:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15872:                             }
15873:                         } else {
15874:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15875:                         }
15876:                     }
15877:                 } else {
15878:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15879:                 }
15880:             }
15881:         }
15882:     }
15883:     if ($proterror) {
15884:         $errors .= '<li>'.$proterror.'</li>';
15885:     }
15886: 
15887:     my (%delsuggested,%suggids,@suggested);;
15888:     if (ref($currltisec{'suggested'}) eq 'HASH') {
15889:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
15890:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
15891:         for (my $i=0; $i<$maxnum; $i++) {
15892:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
15893:             $itemid =~ s/\D+//g;
15894:             if ($itemid) {
15895:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
15896:                     push(@suggested,$i);
15897:                     $suggids{$i} = $itemid;
15898:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
15899:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
15900:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15901:                         }
15902:                     } else {
15903:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
15904:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15905:                         } else {
15906:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
15907:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
15908:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
15909:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
15910:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
15911:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
15912:                                 $secchanges{'suggested'}{$itemid} = 1;
15913:                             }
15914:                         }
15915:                     }
15916:                 }
15917:             }
15918:         }
15919:     }
15920:     foreach my $key (keys(%delsuggested)) {
15921:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
15922:         $secchanges{'suggested'}{$key} = 1;
15923:     }
15924:     if (($env{'form.linkprot_suggested_add'}) &&
15925:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
15926:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
15927:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
15928:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
15929:         if ($newsuggid) {
15930:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
15931:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
15932:             $secchanges{'suggested'}{$newsuggid} = 1;
15933:         } else {
15934:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
15935:             if ($errormsg) {
15936:                 $error .= ' ('.$errormsg.')';
15937:             }
15938:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15939:         }
15940:     }
15941:     my (@items,%deletions,%itemids);
15942:     if ($env{'form.lti_add'}) {
15943:         my $consumer = $env{'form.lti_consumer_add'};
15944:         $consumer =~ s/(`)/'/g;
15945:         ($newid,my $errormsg) = &get_lti_id($dom,$consumer,'lti');
15946:         if ($newid) {
15947:             $itemids{'add'} = $newid;
15948:             push(@items,'add');
15949:             $changes{$newid} = 1;
15950:         } else {
15951:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15952:             if ($errormsg) {
15953:                 $error .= ' ('.$errormsg.')';
15954:             }
15955:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15956:         }
15957:     }
15958:     if (ref($domconfig{$action}) eq 'HASH') {
15959:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15960:         if (@todelete) {
15961:             map { $deletions{$_} = 1; } @todelete;
15962:         }
15963:         my $maxnum = $env{'form.lti_maxnum'};
15964:         for (my $i=0; $i<$maxnum; $i++) {
15965:             my $itemid = $env{'form.lti_id_'.$i};
15966:             $itemid =~ s/\D+//g;
15967:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15968:                 if ($deletions{$itemid}) {
15969:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15970:                 } else {
15971:                     push(@items,$i);
15972:                     $itemids{$i} = $itemid;
15973:                 }
15974:             }
15975:         }
15976:     }
15977:     my (%keystore,$secstored);
15978:     if ($is_home) {
15979:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
15980:     }
15981: 
15982:     my ($cipher,$privnum);
15983:     if ((@items > 0) && ($is_home)) {
15984:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
15985:                                              $newltisec{'encrypt'},$keystore{$home});
15986:     }
15987:     foreach my $idx (@items) {
15988:         my $itemid = $itemids{$idx};
15989:         next unless ($itemid);
15990:         my %currlti;
15991:         unless ($idx eq 'add') {
15992:             if (ref($domconfig{$action}) eq 'HASH') {
15993:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15994:                     %currlti = %{$domconfig{$action}{$itemid}};
15995:                 }
15996:             }
15997:         }
15998:         my $position = $env{'form.lti_pos_'.$itemid};
15999:         $position =~ s/\D+//g;
16000:         if ($position ne '') {
16001:             $allpos[$position] = $itemid;
16002:         }
16003:         foreach my $item ('consumer','lifetime','requser','crsinc') {
16004:             my $formitem = 'form.lti_'.$item.'_'.$idx;
16005:             $env{$formitem} =~ s/(`)/'/g;
16006:             if ($item eq 'lifetime') {
16007:                 $env{$formitem} =~ s/[^\d.]//g;
16008:             }
16009:             if ($env{$formitem} ne '') {
16010:                 $confhash{$itemid}{$item} = $env{$formitem};
16011:                 unless (($idx eq 'add') || ($changes{$itemid})) {
16012:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
16013:                         $changes{$itemid} = 1;
16014:                     }
16015:                 }
16016:             }
16017:         }
16018:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
16019:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
16020:         }
16021:         if ($confhash{$itemid}{'requser'}) {
16022:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
16023:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
16024:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
16025:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
16026:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
16027:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
16028:                 $mapuser =~ s/(`)/'/g;
16029:                 $mapuser =~ s/^\s+|\s+$//g;
16030:                 $confhash{$itemid}{'mapuser'} = $mapuser;
16031:             }
16032:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
16033:             my @makeuser;
16034:             foreach my $ltirole (sort(@possmakeuser)) {
16035:                 if ($posslti{$ltirole}) {
16036:                     push(@makeuser,$ltirole);
16037:                 }
16038:             }
16039:             $confhash{$itemid}{'makeuser'} = \@makeuser;
16040:             if (@makeuser) {
16041:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
16042:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
16043:                     $confhash{$itemid}{'lcauth'} = $lcauth;
16044:                     if ($lcauth ne 'internal') {
16045:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
16046:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
16047:                         $lcauthparm =~ s/`//g;
16048:                         if ($lcauthparm ne '') {
16049:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
16050:                         }
16051:                     }
16052:                 } else {
16053:                     $confhash{$itemid}{'lcauth'} = 'lti';
16054:                 }
16055:             }
16056:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
16057:             if (@possinstdata) {
16058:                 foreach my $field (sort(@possinstdata)) {
16059:                     if (exists($fieldtitles{$field})) {
16060:                         push(@{$confhash{$itemid}{'instdata'}},$field);
16061:                     }
16062:                 }
16063:             }
16064:             if ($env{'form.lti_callback_'.$idx}) {
16065:                 if ($env{'form.lti_callbackparam_'.$idx}) {
16066:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
16067:                     $callback =~ s/^\s+|\s+$//g;
16068:                     $confhash{$itemid}{'callback'} = $callback;
16069:                 }
16070:             }
16071:             foreach my $field ('topmenu','inlinemenu') {
16072:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
16073:                     $confhash{$itemid}{$field} = 1;
16074:                 }
16075:             }
16076:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
16077:                 $confhash{$itemid}{lcmenu} = [];
16078:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
16079:                 foreach my $field (@possmenu) {
16080:                     if (exists($menutitles{$field})) {
16081:                         if ($field eq 'grades') {
16082:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
16083:                         }
16084:                         push(@{$confhash{$itemid}{lcmenu}},$field);
16085:                     }
16086:                 }
16087:             }
16088:             if ($confhash{$itemid}{'crsinc'}) {
16089:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
16090:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
16091:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
16092:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
16093:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
16094:                     $mapcrs =~ s/(`)/'/g;
16095:                     $mapcrs =~ s/^\s+|\s+$//g;
16096:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
16097:                 }
16098:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
16099:                 my @crstypes;
16100:                 foreach my $type (sort(@posstypes)) {
16101:                     if ($posscrstype{$type}) {
16102:                         push(@crstypes,$type);
16103:                     }
16104:                 }
16105:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
16106:                 if ($env{'form.lti_storecrs_'.$idx}) {
16107:                     $confhash{$itemid}{'storecrs'} = 1;
16108:                 }       
16109:                 if ($env{'form.lti_makecrs_'.$idx}) {
16110:                     $confhash{$itemid}{'makecrs'} = 1;
16111:                 }
16112:                 foreach my $ltirole (@lticourseroles) {
16113:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
16114:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
16115:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
16116:                     }
16117:                 }
16118:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
16119:                 my @selfenroll;
16120:                 foreach my $type (sort(@possenroll)) {
16121:                     if ($posslticrs{$type}) {
16122:                         push(@selfenroll,$type);
16123:                     }
16124:                 }
16125:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
16126:                 if ($env{'form.lti_crssec_'.$idx}) {
16127:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
16128:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
16129:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
16130:                         my $section = $env{'form.lti_customsection_'.$idx};
16131:                         $section =~ s/(`)/'/g;
16132:                         $section =~ s/^\s+|\s+$//g;
16133:                         if ($section ne '') {
16134:                             $confhash{$itemid}{'section'} = $section;
16135:                         }
16136:                     }
16137:                 }
16138:                 foreach my $field ('passback','roster') {
16139:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
16140:                         $confhash{$itemid}{$field} = 1;
16141:                     }
16142:                 }
16143:                 if ($env{'form.lti_passback_'.$idx}) {
16144:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
16145:                         $confhash{$itemid}{'passbackformat'} = '1.0';
16146:                     } else {
16147:                         $confhash{$itemid}{'passbackformat'} = '1.1';
16148:                     }
16149:                 }
16150:             }
16151:             unless (($idx eq 'add') || ($changes{$itemid})) {
16152:                 if ($confhash{$itemid}{'crsinc'}) {
16153:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
16154:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16155:                             $changes{$itemid} = 1;
16156:                         }
16157:                     }
16158:                     unless ($changes{$itemid}) {
16159:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
16160:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
16161:                                 $changes{$itemid} = 1;
16162:                             }
16163:                         }
16164:                     }
16165:                     foreach my $field ('mapcrstype','selfenroll') {
16166:                         unless ($changes{$itemid}) {
16167:                             if (ref($currlti{$field}) eq 'ARRAY') {
16168:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16169:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16170:                                                                                    $confhash{$itemid}{$field});
16171:                                     if (@diffs) {
16172:                                         $changes{$itemid} = 1;
16173:                                     }
16174:                                 } elsif (@{$currlti{$field}} > 0) {
16175:                                     $changes{$itemid} = 1;
16176:                                 }
16177:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16178:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16179:                                     $changes{$itemid} = 1;
16180:                                 }
16181:                             }
16182:                         }
16183:                     }
16184:                     unless ($changes{$itemid}) {
16185:                         if (ref($currlti{'maproles'}) eq 'HASH') {
16186:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16187:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
16188:                                     if ($currlti{'maproles'}{$ltirole} ne 
16189:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
16190:                                         $changes{$itemid} = 1;
16191:                                         last;
16192:                                     }
16193:                                 }
16194:                                 unless ($changes{$itemid}) {
16195:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
16196:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
16197:                                             $currlti{'maproles'}{$ltirole}) {
16198:                                             $changes{$itemid} = 1;
16199:                                             last;
16200:                                         }
16201:                                     }
16202:                                 }
16203:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
16204:                                 $changes{$itemid} = 1;
16205:                             }
16206:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16207:                             unless ($changes{$itemid}) {
16208:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
16209:                                     $changes{$itemid} = 1;
16210:                                 }
16211:                             }
16212:                         }
16213:                     }
16214:                 }
16215:                 unless ($changes{$itemid}) {
16216:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
16217:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16218:                             $changes{$itemid} = 1;
16219:                         }
16220:                     }
16221:                     unless ($changes{$itemid}) {
16222:                         foreach my $field ('makeuser','lcmenu','instdata') {
16223:                             if (ref($currlti{$field}) eq 'ARRAY') {
16224:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16225:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16226:                                                                                    $confhash{$itemid}{$field});
16227:                                     if (@diffs) {
16228:                                         $changes{$itemid} = 1;
16229:                                     }
16230:                                 } elsif (@{$currlti{$field}} > 0) {
16231:                                     $changes{$itemid} = 1;
16232:                                 }
16233:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16234:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16235:                                     $changes{$itemid} = 1;
16236:                                 }
16237:                             }
16238:                         }
16239:                     }
16240:                 }
16241:             }
16242:         }
16243:         if ($is_home) {
16244:             my $keyitem = 'form.lti_key_'.$idx;
16245:             $env{$keyitem} =~ s/(`)/'/g;
16246:             if ($env{$keyitem} ne '') {
16247:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
16248:                 unless ($changes{$itemid}) {
16249:                     if ($currlti{'key'} ne $env{$keyitem}) {
16250:                         $changes{$itemid} = 1;
16251:                     }
16252:                 }
16253:             }
16254:             my $secretitem = 'form.lti_secret_'.$idx;
16255:             $env{$secretitem} =~ s/(`)/'/g;
16256:             if ($currlti{'usable'}) {
16257:                 if ($env{'form.lti_changesecret_'.$idx}) {
16258:                     if ($env{$secretitem} ne '') {
16259:                         if ($privnum && $cipher) {
16260:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16261:                             $confhash{$itemid}{'cipher'} = $privnum;
16262:                         } else {
16263:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
16264:                         }
16265:                         $changes{$itemid} = 1;
16266:                     }
16267:                 } else {
16268:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
16269:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
16270:                 }
16271:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16272:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
16273:                         $confhash{$itemid}{'usable'} = 1;
16274:                     }
16275:                 }
16276:             } elsif ($env{$secretitem} ne '') {
16277:                 if ($privnum && $cipher) {
16278:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16279:                     $confhash{$itemid}{'cipher'} = $privnum;
16280:                 } else {
16281:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
16282:                 }
16283:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16284:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
16285:                         $confhash{$itemid}{'usable'} = 1;
16286:                     }
16287:                 }
16288:                 $changes{$itemid} = 1;
16289:             }
16290:         }
16291:         unless ($changes{$itemid}) {
16292:             foreach my $key (keys(%currlti)) {
16293:                 if (ref($currlti{$key}) eq 'HASH') {
16294:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
16295:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
16296:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
16297:                                 $changes{$itemid} = 1;
16298:                                 last;
16299:                             }
16300:                         }
16301:                     } elsif (keys(%{$currlti{$key}}) > 0) {
16302:                         $changes{$itemid} = 1;
16303:                     }
16304:                 }
16305:                 last if ($changes{$itemid});
16306:             }
16307:         }
16308:     }
16309:     if (@allpos > 0) {
16310:         my $idx = 0;
16311:         foreach my $itemid (@allpos) {
16312:             if ($itemid ne '') {
16313:                 $confhash{$itemid}{'order'} = $idx;
16314:                 if (ref($domconfig{$action}) eq 'HASH') {
16315:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16316:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
16317:                             $changes{$itemid} = 1;
16318:                         }
16319:                     }
16320:                 }
16321:                 $idx ++;
16322:             }
16323:         }
16324:     }
16325: 
16326:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
16327:         return &mt('No changes made.');
16328:     }
16329: 
16330:     my %ltihash = (
16331:                       $action => { %confhash }
16332:                   );
16333:     my %ltienchash;
16334: 
16335:     if ($is_home) {
16336:         %ltienchash = (
16337:                          $action => { %ltienc }
16338:                       );
16339:     }
16340:     if (keys(%secchanges)) {
16341:         $ltihash{'ltisec'} = \%newltisec;
16342:         if ($secchanges{'linkprot'}) {
16343:             if ($is_home) {
16344:                 $ltienchash{'linkprot'} = \%newltienc;
16345:             }
16346:         }
16347:     }
16348:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
16349:     if ($putresult eq 'ok') {
16350:         if (keys(%ltienchash)) {
16351:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
16352:         }
16353:         $resulttext = &mt('Changes made:').'<ul>';
16354:         if (keys(%secchanges) > 0) {
16355:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
16356:             if (exists($secchanges{'linkprot'})) {
16357:                 $resulttext .= $linkprotoutput;
16358:             }
16359:         }
16360:         if (keys(%changes) > 0) {
16361:             my $cachetime = 24*60*60;
16362:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
16363:             if (ref($lastactref) eq 'HASH') {
16364:                 $lastactref->{'lti'} = 1;
16365:             }
16366:             my %bynum;
16367:             foreach my $itemid (sort(keys(%changes))) {
16368:                 if (ref($confhash{$itemid}) eq 'HASH') {
16369:                     my $position = $confhash{$itemid}{'order'};
16370:                     $bynum{$position} = $itemid;
16371:                 }
16372:             }
16373:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
16374:                 my $itemid = $bynum{$pos};
16375:                 if (ref($confhash{$itemid}) eq 'HASH') {
16376:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
16377:                     my $position = $pos + 1;
16378:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
16379:                     foreach my $item ('version','lifetime') {
16380:                         if ($confhash{$itemid}{$item} ne '') {
16381:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
16382:                         }
16383:                     }
16384:                     if ($ltienc{$itemid}{'key'} ne '') {
16385:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
16386:                     }
16387:                     if ($ltienc{$itemid}{'secret'} ne '') {
16388:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
16389:                     }
16390:                     if ($confhash{$itemid}{'requser'}) {
16391:                         if ($confhash{$itemid}{'callback'}) {
16392:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
16393:                         } else {
16394:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
16395:                         }
16396:                         if ($confhash{$itemid}{'mapuser'}) {
16397:                             my $shownmapuser;
16398:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
16399:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
16400:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
16401:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
16402:                             } else {
16403:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
16404:                             }
16405:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
16406:                         }
16407:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
16408:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
16409:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
16410:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
16411:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
16412:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
16413:                                 } else {
16414:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
16415:                                                        $confhash{$itemid}{'lcauth'});
16416:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
16417:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
16418:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
16419:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
16420:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
16421:                                         }
16422:                                     } else {
16423:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
16424:                                     }
16425:                                 }
16426:                                 $resulttext .= '</li>';
16427:                             } else {
16428:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
16429:                             }
16430:                         }
16431:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
16432:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
16433:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
16434:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
16435:                             } else {
16436:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user').'</li>';
16437:                             }
16438:                         } else {
16439:                             $resulttext .= '<li>'.&mt('No institutional data used when creating a new user').'</li>';
16440:                         }
16441:                         foreach my $item ('topmenu','inlinemenu') {
16442:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16443:                             if ($confhash{$itemid}{$item}) {
16444:                                 $resulttext .= &mt('Yes');
16445:                             } else {
16446:                                 $resulttext .= &mt('No');
16447:                             }
16448:                             $resulttext .= '</li>';
16449:                         }
16450:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16451:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16452:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
16453:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
16454:                             } else {
16455:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
16456:                             }
16457:                         }
16458:                         if ($confhash{$itemid}{'crsinc'}) {
16459:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16460:                                 my $rolemaps;
16461:                                 foreach my $role (@ltiroles) {
16462:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
16463:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
16464:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
16465:                                                                                 'Course').',';
16466:                                     }
16467:                                 }
16468:                                 if ($rolemaps) {
16469:                                     $rolemaps =~ s/,$//;
16470:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
16471:                                 }
16472:                             }
16473:                             if ($confhash{$itemid}{'mapcrs'}) {
16474:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
16475:                             }
16476:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
16477:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
16478:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
16479:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
16480:                                                    '</li>';
16481:                                 } else {
16482:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
16483:                                 }
16484:                             }
16485:                             if ($confhash{$itemid}{'storecrs'}) {
16486:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
16487:                             }
16488:                             if ($confhash{$itemid}{'makecrs'}) {
16489:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
16490:                             } else {
16491:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
16492:                             }
16493:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
16494:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
16495:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
16496:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
16497:                                                    '</li>';
16498:                                 } else {
16499:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
16500:                                 }
16501:                             }
16502:                             if ($confhash{$itemid}{'section'}) {
16503:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
16504:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
16505:                                                          ' (course_section_sourcedid)'.'</li>';  
16506:                                 } else {
16507:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
16508:                                                           $confhash{$itemid}{'section'}.'</li>';
16509:                                 }
16510:                             } else {
16511:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
16512:                             }
16513:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
16514:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16515:                                 if ($confhash{$itemid}{$item}) {
16516:                                     $resulttext .= &mt('Yes');
16517:                                     if ($item eq 'passback') {
16518:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
16519:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
16520:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
16521:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
16522:                                         }
16523:                                     }
16524:                                 } else {
16525:                                     $resulttext .= &mt('No');
16526:                                 }
16527:                                 $resulttext .= '</li>';
16528:                             }
16529:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16530:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16531:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
16532:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
16533:                                 } else {
16534:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
16535:                                 }
16536:                             }
16537:                         }
16538:                     }
16539:                     $resulttext .= '</ul></li>';
16540:                 }
16541:             }
16542:             if (keys(%deletions)) {
16543:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
16544:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
16545:                 }
16546:             }
16547:         }
16548:         $resulttext .= '</ul>';
16549:         if (ref($lastactref) eq 'HASH') {
16550:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
16551:                 &Apache::lonnet::get_domain_defaults($dom,1);
16552:                 $lastactref->{'domdefaults'} = 1;
16553:             }
16554:         }
16555:     } else {
16556:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
16557:     }
16558:     if ($errors) {
16559:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
16560:                        $errors.'</ul>';
16561:     }
16562:     return $resulttext;
16563: }
16564: 
16565: sub get_priv_creds {
16566:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
16567:     my ($needenc,$cipher,$privnum);
16568:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
16569:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
16570:         $needenc = $encrypt->{'consumers'} 
16571:     } else {
16572:         $needenc = $domdefs{'ltienc_consumers'};
16573:     }
16574:     if ($needenc) {
16575:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
16576:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
16577:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
16578:             my $privkey = $privhash{'key'};
16579:             $privnum = $privhash{'version'};
16580:             if (($privnum) && ($privkey ne '')) {
16581:                 $cipher = Crypt::CBC->new(-pass   => $privkey,
16582:                                           -cipher => 'Crypt::DES');
16583:             }
16584:         }
16585:     }
16586:     return ($cipher,$privnum);
16587: }
16588: 
16589: sub get_lti_id {
16590:     my ($domain,$consumer,$dbname) = @_;
16591:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
16592:         return ('','invalid db');
16593:     }
16594:     # get lock on db
16595:     my $lockhash = {
16596:                       lock => $env{'user.name'}.
16597:                               ':'.$env{'user.domain'},
16598:                    };
16599:     my $tries = 0;
16600:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16601:     my ($id,$error);
16602: 
16603:     while (($gotlock ne 'ok') && ($tries<10)) {
16604:         $tries ++;
16605:         sleep (0.1);
16606:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16607:     }
16608:     if ($gotlock eq 'ok') {
16609:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
16610:         if ($currids{'lock'}) {
16611:             delete($currids{'lock'});
16612:             if (keys(%currids)) {
16613:                 my @curr = sort { $a <=> $b } keys(%currids);
16614:                 if ($curr[-1] =~ /^\d+$/) {
16615:                     $id = 1 + $curr[-1];
16616:                 }
16617:             } else {
16618:                 $id = 1;
16619:             }
16620:             if ($id) {
16621:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
16622:                     $error = 'nostore';
16623:                 }
16624:             } else {
16625:                 $error = 'nonumber';
16626:             }
16627:         }
16628:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
16629:     } else {
16630:         $error = 'nolock';
16631:     }
16632:     return ($id,$error);
16633: }
16634: 
16635: sub modify_autoenroll {
16636:     my ($dom,$lastactref,%domconfig) = @_;
16637:     my ($resulttext,%changes);
16638:     my %currautoenroll;
16639:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
16640:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
16641:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
16642:         }
16643:     }
16644:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
16645:     my %title = ( run => 'Auto-enrollment active',
16646:                   sender => 'Sender for notification messages',
16647:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
16648:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
16649:     my @offon = ('off','on');
16650:     my $sender_uname = $env{'form.sender_uname'};
16651:     my $sender_domain = $env{'form.sender_domain'};
16652:     if ($sender_domain eq '') {
16653:         $sender_uname = '';
16654:     } elsif ($sender_uname eq '') {
16655:         $sender_domain = '';
16656:     }
16657:     my $coowners = $env{'form.autoassign_coowners'};
16658:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
16659:     $autofailsafe =~ s{^\s+|\s+$}{}g;
16660:     if ($autofailsafe =~ /\D/) {
16661:         undef($autofailsafe);
16662:     }
16663:     my $failsafe = $env{'form.autoenroll_failsafe'};
16664:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
16665:         $failsafe = 'off';
16666:         undef($autofailsafe);
16667:     }
16668:     my %autoenrollhash =  (
16669:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
16670:                                        'sender_uname' => $sender_uname,
16671:                                        'sender_domain' => $sender_domain,
16672:                                        'co-owners' => $coowners,
16673:                                        'autofailsafe' => $autofailsafe,
16674:                                        'failsafe' => $failsafe,
16675:                                 }
16676:                      );
16677:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
16678:                                              $dom);
16679:     if ($putresult eq 'ok') {
16680:         if (exists($currautoenroll{'run'})) {
16681:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
16682:                  $changes{'run'} = 1;
16683:              }
16684:         } elsif ($autorun) {
16685:             if ($env{'form.autoenroll_run'} ne '1') {
16686:                  $changes{'run'} = 1;
16687:             }
16688:         }
16689:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
16690:             $changes{'sender'} = 1;
16691:         }
16692:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
16693:             $changes{'sender'} = 1;
16694:         }
16695:         if ($currautoenroll{'co-owners'} ne '') {
16696:             if ($currautoenroll{'co-owners'} ne $coowners) {
16697:                 $changes{'coowners'} = 1;
16698:             }
16699:         } elsif ($coowners) {
16700:             $changes{'coowners'} = 1;
16701:         }
16702:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
16703:             $changes{'autofailsafe'} = 1;
16704:         }
16705:         if ($currautoenroll{'failsafe'} ne $failsafe) {
16706:             $changes{'failsafe'} = 1;
16707:         }
16708:         if (keys(%changes) > 0) {
16709:             $resulttext = &mt('Changes made:').'<ul>';
16710:             if ($changes{'run'}) {
16711:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
16712:             }
16713:             if ($changes{'sender'}) {
16714:                 if ($sender_uname eq '' || $sender_domain eq '') {
16715:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
16716:                 } else {
16717:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
16718:                 }
16719:             }
16720:             if ($changes{'coowners'}) {
16721:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
16722:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16723:                 if (ref($lastactref) eq 'HASH') {
16724:                     $lastactref->{'domainconfig'} = 1;
16725:                 }
16726:             }
16727:             if ($changes{'autofailsafe'}) {
16728:                 if ($autofailsafe ne '') {
16729:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
16730:                 } else {
16731:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16732:                 }
16733:             }
16734:             if ($changes{'failsafe'}) {
16735:                 if ($failsafe eq 'off') {
16736:                     unless ($changes{'autofailsafe'}) {
16737:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16738:                     }
16739:                 } elsif ($failsafe eq 'zero') {
16740:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
16741:                 } else {
16742:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
16743:                 }
16744:             }
16745:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
16746:                 &Apache::lonnet::get_domain_defaults($dom,1);
16747:                 if (ref($lastactref) eq 'HASH') {
16748:                     $lastactref->{'domdefaults'} = 1;
16749:                 }
16750:             }
16751:             $resulttext .= '</ul>';
16752:         } else {
16753:             $resulttext = &mt('No changes made to auto-enrollment settings');
16754:         }
16755:     } else {
16756:         $resulttext = '<span class="LC_error">'.
16757: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16758:     }
16759:     return $resulttext;
16760: }
16761: 
16762: sub modify_autoupdate {
16763:     my ($dom,%domconfig) = @_;
16764:     my ($resulttext,%currautoupdate,%fields,%changes);
16765:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16766:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16767:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16768:         }
16769:     }
16770:     my @offon = ('off','on');
16771:     my %title = &Apache::lonlocal::texthash (
16772:                     run        => 'Auto-update:',
16773:                     classlists => 'Updates to user information in classlists?',
16774:                     unexpired  => 'Skip updates for users without active or future roles?',
16775:                     lastactive => 'Skip updates for inactive users?',
16776:                 );
16777:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16778:     my %fieldtitles = &Apache::lonlocal::texthash (
16779:                         id => 'Student/Employee ID',
16780:                         permanentemail => 'E-mail address',
16781:                         lastname => 'Last Name',
16782:                         firstname => 'First Name',
16783:                         middlename => 'Middle Name',
16784:                         generation => 'Generation',
16785:                       );
16786:     $othertitle = &mt('All users');
16787:     if (keys(%{$usertypes}) >  0) {
16788:         $othertitle = &mt('Other users');
16789:     }
16790:     foreach my $key (keys(%env)) {
16791:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16792:             my ($usertype,$item) = ($1,$2);
16793:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16794:                 if ($usertype eq 'default') {   
16795:                     push(@{$fields{$1}},$2);
16796:                 } elsif (ref($types) eq 'ARRAY') {
16797:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16798:                         push(@{$fields{$1}},$2);
16799:                     }
16800:                 }
16801:             }
16802:         }
16803:     }
16804:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16805:     @lockablenames = sort(@lockablenames);
16806:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16807:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16808:         if (@changed) {
16809:             $changes{'lockablenames'} = 1;
16810:         }
16811:     } else {
16812:         if (@lockablenames) {
16813:             $changes{'lockablenames'} = 1;
16814:         }
16815:     }
16816:     my %updatehash = (
16817:                       autoupdate => { run => $env{'form.autoupdate_run'},
16818:                                       classlists => $env{'form.classlists'},
16819:                                       unexpired  => $env{'form.unexpired'},
16820:                                       fields => {%fields},
16821:                                       lockablenames => \@lockablenames,
16822:                                     }
16823:                      );
16824:     my $lastactivedays;
16825:     if ($env{'form.lastactive'}) {
16826:         $lastactivedays = $env{'form.lastactivedays'};
16827:         $lastactivedays =~ s/^\s+|\s+$//g;
16828:         unless ($lastactivedays =~ /^\d+$/) {
16829:             undef($lastactivedays);
16830:             $env{'form.lastactive'} = 0;
16831:         }
16832:     }
16833:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16834:     foreach my $key (keys(%currautoupdate)) {
16835:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16836:             if (exists($updatehash{autoupdate}{$key})) {
16837:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16838:                     $changes{$key} = 1;
16839:                 }
16840:             }
16841:         } elsif ($key eq 'fields') {
16842:             if (ref($currautoupdate{$key}) eq 'HASH') {
16843:                 foreach my $item (@{$types},'default') {
16844:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16845:                         my $change = 0;
16846:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16847:                             if (!exists($fields{$item})) {
16848:                                 $change = 1;
16849:                                 last;
16850:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16851:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16852:                                     $change = 1;
16853:                                     last;
16854:                                 }
16855:                             }
16856:                         }
16857:                         if ($change) {
16858:                             push(@{$changes{$key}},$item);
16859:                         }
16860:                     } 
16861:                 }
16862:             }
16863:         } elsif ($key eq 'lockablenames') {
16864:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16865:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16866:                 if (@changed) {
16867:                     $changes{'lockablenames'} = 1;
16868:                 }
16869:             } else {
16870:                 if (@lockablenames) {
16871:                     $changes{'lockablenames'} = 1;
16872:                 }
16873:             }
16874:         }
16875:     }
16876:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16877:         if (@lockablenames) {
16878:             $changes{'lockablenames'} = 1;
16879:         }
16880:     }
16881:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16882:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16883:             $changes{'unexpired'} = 1;
16884:         }
16885:     }
16886:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16887:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16888:             $changes{'lastactive'} = 1;
16889:         }
16890:     }
16891:     foreach my $item (@{$types},'default') {
16892:         if (defined($fields{$item})) {
16893:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16894:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16895:                     my $change = 0;
16896:                     if (ref($fields{$item}) eq 'ARRAY') {
16897:                         foreach my $type (@{$fields{$item}}) {
16898:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16899:                                 $change = 1;
16900:                                 last;
16901:                             }
16902:                         }
16903:                     }
16904:                     if ($change) {
16905:                         push(@{$changes{'fields'}},$item);
16906:                     }
16907:                 } else {
16908:                     push(@{$changes{'fields'}},$item);
16909:                 }
16910:             } else {
16911:                 push(@{$changes{'fields'}},$item);
16912:             }
16913:         }
16914:     }
16915:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16916:                                              $dom);
16917:     if ($putresult eq 'ok') {
16918:         if (keys(%changes) > 0) {
16919:             $resulttext = &mt('Changes made:').'<ul>';
16920:             foreach my $key (sort(keys(%changes))) {
16921:                 if ($key eq 'lockablenames') {
16922:                     $resulttext .= '<li>';
16923:                     if (@lockablenames) {
16924:                         $usertypes->{'default'} = $othertitle;
16925:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16926:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16927:                     } else {
16928:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16929:                     }
16930:                     $resulttext .= '</li>';
16931:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16932:                     foreach my $item (@{$changes{$key}}) {
16933:                         my @newvalues;
16934:                         foreach my $type (@{$fields{$item}}) {
16935:                             push(@newvalues,$fieldtitles{$type});
16936:                         }
16937:                         my $newvaluestr;
16938:                         if (@newvalues > 0) {
16939:                             $newvaluestr = join(', ',@newvalues);
16940:                         } else {
16941:                             $newvaluestr = &mt('none');
16942:                         }
16943:                         if ($item eq 'default') {
16944:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16945:                         } else {
16946:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16947:                         }
16948:                     }
16949:                 } else {
16950:                     my $newvalue;
16951:                     if ($key eq 'run') {
16952:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16953:                     } elsif ($key eq 'lastactive') {
16954:                         $newvalue = $offon[$env{'form.lastactive'}];
16955:                         unless ($lastactivedays eq '') {
16956:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16957:                         }
16958:                     } else {
16959:                         $newvalue = $offon[$env{'form.'.$key}];
16960:                     }
16961:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16962:                 }
16963:             }
16964:             $resulttext .= '</ul>';
16965:         } else {
16966:             $resulttext = &mt('No changes made to autoupdates');
16967:         }
16968:     } else {
16969:         $resulttext = '<span class="LC_error">'.
16970: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16971:     }
16972:     return $resulttext;
16973: }
16974: 
16975: sub modify_autocreate {
16976:     my ($dom,%domconfig) = @_;
16977:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16978:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16979:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16980:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16981:         }
16982:     }
16983:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16984:                  req => 'Auto-creation of validated requests for official courses',
16985:                  xmldc => 'Identity of course creator of courses from XML files',
16986:                );
16987:     my @types = ('xml','req');
16988:     foreach my $item (@types) {
16989:         $newvals{$item} = $env{'form.autocreate_'.$item};
16990:         $newvals{$item} =~ s/\D//g;
16991:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16992:     }
16993:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16994:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16995:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16996:         $newvals{'xmldc'} = '';
16997:     } 
16998:     %autocreatehash =  (
16999:                         autocreate => { xml => $newvals{'xml'},
17000:                                         req => $newvals{'req'},
17001:                                       }
17002:                        );
17003:     if ($newvals{'xmldc'} ne '') {
17004:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
17005:     }
17006:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
17007:                                              $dom);
17008:     if ($putresult eq 'ok') {
17009:         my @items = @types;
17010:         if ($newvals{'xml'}) {
17011:             push(@items,'xmldc');
17012:         }
17013:         foreach my $item (@items) {
17014:             if (exists($currautocreate{$item})) {
17015:                 if ($currautocreate{$item} ne $newvals{$item}) {
17016:                     $changes{$item} = 1;
17017:                 }
17018:             } elsif ($newvals{$item}) {
17019:                 $changes{$item} = 1;
17020:             }
17021:         }
17022:         if (keys(%changes) > 0) {
17023:             my @offon = ('off','on'); 
17024:             $resulttext = &mt('Changes made:').'<ul>';
17025:             foreach my $item (@types) {
17026:                 if ($changes{$item}) {
17027:                     my $newtxt = $offon[$newvals{$item}];
17028:                     $resulttext .= '<li>'.
17029:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
17030:                                        '<b>','</b>').
17031:                                    '</li>';
17032:                 }
17033:             }
17034:             if ($changes{'xmldc'}) {
17035:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
17036:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
17037:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
17038:             }
17039:             $resulttext .= '</ul>';
17040:         } else {
17041:             $resulttext = &mt('No changes made to auto-creation settings');
17042:         }
17043:     } else {
17044:         $resulttext = '<span class="LC_error">'.
17045:             &mt('An error occurred: [_1]',$putresult).'</span>';
17046:     }
17047:     return $resulttext;
17048: }
17049: 
17050: sub modify_directorysrch {
17051:     my ($dom,$lastactref,%domconfig) = @_;
17052:     my ($resulttext,%changes);
17053:     my %currdirsrch;
17054:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
17055:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
17056:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
17057:         }
17058:     }
17059:     my %title = ( available => 'Institutional directory search available',
17060:                   localonly => 'Other domains can search institution',
17061:                   lcavailable => 'LON-CAPA directory search available',
17062:                   lclocalonly => 'Other domains can search LON-CAPA domain',
17063:                   searchby => 'Search types',
17064:                   searchtypes => 'Search latitude');
17065:     my @offon = ('off','on');
17066:     my @otherdoms = ('Yes','No');
17067: 
17068:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
17069:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
17070:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
17071: 
17072:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17073:     if (keys(%{$usertypes}) == 0) {
17074:         @cansearch = ('default');
17075:     } else {
17076:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
17077:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
17078:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
17079:                     push(@{$changes{'cansearch'}},$type);
17080:                 }
17081:             }
17082:             foreach my $type (@cansearch) {
17083:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
17084:                     push(@{$changes{'cansearch'}},$type);
17085:                 }
17086:             }
17087:         } else {
17088:             push(@{$changes{'cansearch'}},@cansearch);
17089:         }
17090:     }
17091: 
17092:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
17093:         foreach my $by (@{$currdirsrch{'searchby'}}) {
17094:             if (!grep(/^\Q$by\E$/,@searchby)) {
17095:                 push(@{$changes{'searchby'}},$by);
17096:             }
17097:         }
17098:         foreach my $by (@searchby) {
17099:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
17100:                 push(@{$changes{'searchby'}},$by);
17101:             }
17102:         }
17103:     } else {
17104:         push(@{$changes{'searchby'}},@searchby);
17105:     }
17106: 
17107:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
17108:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
17109:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
17110:                 push(@{$changes{'searchtypes'}},$type);
17111:             }
17112:         }
17113:         foreach my $type (@searchtypes) {
17114:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
17115:                 push(@{$changes{'searchtypes'}},$type);
17116:             }
17117:         }
17118:     } else {
17119:         if (exists($currdirsrch{'searchtypes'})) {
17120:             foreach my $type (@searchtypes) {  
17121:                 if ($type ne $currdirsrch{'searchtypes'}) { 
17122:                     push(@{$changes{'searchtypes'}},$type);
17123:                 }
17124:             }
17125:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
17126:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
17127:             }   
17128:         } else {
17129:             push(@{$changes{'searchtypes'}},@searchtypes); 
17130:         }
17131:     }
17132: 
17133:     my %dirsrch_hash =  (
17134:             directorysrch => { available => $env{'form.dirsrch_available'},
17135:                                cansearch => \@cansearch,
17136:                                localonly => $env{'form.dirsrch_instlocalonly'},
17137:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
17138:                                lcavailable => $env{'form.dirsrch_domavailable'},
17139:                                searchby => \@searchby,
17140:                                searchtypes => \@searchtypes,
17141:                              }
17142:             );
17143:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
17144:                                              $dom);
17145:     if ($putresult eq 'ok') {
17146:         if (exists($currdirsrch{'available'})) {
17147:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
17148:                  $changes{'available'} = 1;
17149:              }
17150:         } else {
17151:             if ($env{'form.dirsrch_available'} eq '1') {
17152:                 $changes{'available'} = 1;
17153:             }
17154:         }
17155:         if (exists($currdirsrch{'lcavailable'})) {
17156:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
17157:                 $changes{'lcavailable'} = 1;
17158:             }
17159:         } else {
17160:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
17161:                 $changes{'lcavailable'} = 1;
17162:             }
17163:         }
17164:         if (exists($currdirsrch{'localonly'})) {
17165:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
17166:                 $changes{'localonly'} = 1;
17167:             }
17168:         } else {
17169:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
17170:                 $changes{'localonly'} = 1;
17171:             }
17172:         }
17173:         if (exists($currdirsrch{'lclocalonly'})) {
17174:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
17175:                 $changes{'lclocalonly'} = 1;
17176:             }
17177:         } else {
17178:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
17179:                 $changes{'lclocalonly'} = 1;
17180:             }
17181:         }
17182:         if (keys(%changes) > 0) {
17183:             $resulttext = &mt('Changes made:').'<ul>';
17184:             if ($changes{'available'}) {
17185:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
17186:             }
17187:             if ($changes{'lcavailable'}) {
17188:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
17189:             }
17190:             if ($changes{'localonly'}) {
17191:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
17192:             }
17193:             if ($changes{'lclocalonly'}) {
17194:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
17195:             }
17196:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
17197:                 my $chgtext;
17198:                 if (ref($usertypes) eq 'HASH') {
17199:                     if (keys(%{$usertypes}) > 0) {
17200:                         foreach my $type (@{$types}) {
17201:                             if (grep(/^\Q$type\E$/,@cansearch)) {
17202:                                 $chgtext .= $usertypes->{$type}.'; ';
17203:                             }
17204:                         }
17205:                         if (grep(/^default$/,@cansearch)) {
17206:                             $chgtext .= $othertitle;
17207:                         } else {
17208:                             $chgtext =~ s/\; $//;
17209:                         }
17210:                         $resulttext .=
17211:                             '<li>'.
17212:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
17213:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
17214:                             '</li>';
17215:                     }
17216:                 }
17217:             }
17218:             if (ref($changes{'searchby'}) eq 'ARRAY') {
17219:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
17220:                 my $chgtext;
17221:                 foreach my $type (@{$titleorder}) {
17222:                     if (grep(/^\Q$type\E$/,@searchby)) {
17223:                         if (defined($searchtitles->{$type})) {
17224:                             $chgtext .= $searchtitles->{$type}.'; ';
17225:                         }
17226:                     }
17227:                 }
17228:                 $chgtext =~ s/\; $//;
17229:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
17230:             }
17231:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
17232:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
17233:                 my $chgtext;
17234:                 foreach my $type (@{$srchtypeorder}) {
17235:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
17236:                         if (defined($srchtypes_desc->{$type})) {
17237:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
17238:                         }
17239:                     }
17240:                 }
17241:                 $chgtext =~ s/\; $//;
17242:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
17243:             }
17244:             $resulttext .= '</ul>';
17245:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
17246:             if (ref($lastactref) eq 'HASH') {
17247:                 $lastactref->{'directorysrch'} = 1;
17248:             }
17249:         } else {
17250:             $resulttext = &mt('No changes made to directory search settings');
17251:         }
17252:     } else {
17253:         $resulttext = '<span class="LC_error">'.
17254:                       &mt('An error occurred: [_1]',$putresult).'</span>';
17255:     }
17256:     return $resulttext;
17257: }
17258: 
17259: sub modify_contacts {
17260:     my ($dom,$lastactref,%domconfig) = @_;
17261:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
17262:     if (ref($domconfig{'contacts'}) eq 'HASH') {
17263:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
17264:             $currsetting{$key} = $domconfig{'contacts'}{$key};
17265:         }
17266:     }
17267:     my (%others,%to,%bcc,%includestr,%includeloc);
17268:     my @contacts = ('supportemail','adminemail');
17269:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
17270:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
17271:     my @toggles = ('reporterrors','reportupdates','reportstatus');
17272:     my @lonstatus = ('threshold','sysmail','weights','excluded');
17273:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
17274:     foreach my $type (@mailings) {
17275:         @{$newsetting{$type}} = 
17276:             &Apache::loncommon::get_env_multiple('form.'.$type);
17277:         foreach my $item (@contacts) {
17278:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
17279:                 $contacts_hash{contacts}{$type}{$item} = 1;
17280:             } else {
17281:                 $contacts_hash{contacts}{$type}{$item} = 0;
17282:             }
17283:         }
17284:         $others{$type} = $env{'form.'.$type.'_others'};
17285:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
17286:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17287:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
17288:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
17289:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
17290:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
17291:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
17292:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17293:             }
17294:         }
17295:     }
17296:     foreach my $item (@contacts) {
17297:         $to{$item} = $env{'form.'.$item};
17298:         $contacts_hash{'contacts'}{$item} = $to{$item};
17299:     }
17300:     foreach my $item (@toggles) {
17301:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
17302:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
17303:         }
17304:     }
17305:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
17306:     foreach my $item (@lonstatus) {
17307:         if ($item eq 'excluded') {
17308:             my (%serverhomes,@excluded);
17309:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
17310:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
17311:             if (@possexcluded) {
17312:                 foreach my $id (sort(@possexcluded)) {
17313:                     if ($serverhomes{$id}) {
17314:                         push(@excluded,$id);
17315:                     }
17316:                 }
17317:             }
17318:             if (@excluded) {
17319:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
17320:             }
17321:         } elsif ($item eq 'weights') {
17322:             foreach my $type ('E','W','N','U') {
17323:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
17324:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
17325:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
17326:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
17327:                             $env{'form.error'.$item.'_'.$type};
17328:                     }
17329:                 }
17330:             }
17331:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
17332:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
17333:             if ($env{'form.error'.$item} =~ /^\d+$/) {
17334:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
17335:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
17336:                 }
17337:             }
17338:         }
17339:     }
17340:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
17341:         foreach my $field (@{$fields}) {
17342:             if (ref($possoptions->{$field}) eq 'ARRAY') {
17343:                 my $value = $env{'form.helpform_'.$field};
17344:                 $value =~ s/^\s+|\s+$//g;
17345:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
17346:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
17347:                     if ($field eq 'screenshot') {
17348:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
17349:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
17350:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
17351:                         }
17352:                     }
17353:                 }
17354:             }
17355:         }
17356:     }
17357:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17358:     my (@statuses,%usertypeshash,@overrides);
17359:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
17360:         @statuses = @{$types};
17361:         if (ref($usertypes) eq 'HASH') {
17362:             %usertypeshash = %{$usertypes};
17363:         }
17364:     }
17365:     if (@statuses) {
17366:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
17367:         foreach my $type (@possoverrides) {
17368:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
17369:                 push(@overrides,$type);
17370:             }
17371:         }
17372:         if (@overrides) {
17373:             foreach my $type (@overrides) {
17374:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
17375:                 foreach my $item (@contacts) {
17376:                     if (grep(/^\Q$item\E$/,@standard)) {
17377:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
17378:                         $newsetting{'override_'.$type}{$item} = 1;
17379:                     } else {
17380:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
17381:                         $newsetting{'override_'.$type}{$item} = 0;
17382:                     }
17383:                 }
17384:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
17385:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17386:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
17387:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17388:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
17389:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
17390:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
17391:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17392:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
17393:                 }
17394:             }
17395:         }
17396:     }
17397:     if (keys(%currsetting) > 0) {
17398:         foreach my $item (@contacts) {
17399:             if ($to{$item} ne $currsetting{$item}) {
17400:                 $changes{$item} = 1;
17401:             }
17402:         }
17403:         foreach my $type (@mailings) {
17404:             foreach my $item (@contacts) {
17405:                 if (ref($currsetting{$type}) eq 'HASH') {
17406:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
17407:                         push(@{$changes{$type}},$item);
17408:                     }
17409:                 } else {
17410:                     push(@{$changes{$type}},@{$newsetting{$type}});
17411:                 }
17412:             }
17413:             if ($others{$type} ne $currsetting{$type}{'others'}) {
17414:                 push(@{$changes{$type}},'others');
17415:             }
17416:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17417:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
17418:                     push(@{$changes{$type}},'bcc'); 
17419:                 }
17420:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
17421:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
17422:                     push(@{$changes{$type}},'include');
17423:                 }
17424:             }
17425:         }
17426:         if (ref($fields) eq 'ARRAY') {
17427:             if (ref($currsetting{'helpform'}) eq 'HASH') {
17428:                 foreach my $field (@{$fields}) {
17429:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
17430:                         push(@{$changes{'helpform'}},$field);
17431:                     }
17432:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17433:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
17434:                             push(@{$changes{'helpform'}},'maxsize');
17435:                         }
17436:                     }
17437:                 }
17438:             } else {
17439:                 foreach my $field (@{$fields}) {
17440:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17441:                         push(@{$changes{'helpform'}},$field);
17442:                     }
17443:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17444:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17445:                             push(@{$changes{'helpform'}},'maxsize');
17446:                         }
17447:                     }
17448:                 }
17449:             }
17450:         }
17451:         if (@statuses) {
17452:             if (ref($currsetting{'overrides'}) eq 'HASH') {
17453:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
17454:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
17455:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
17456:                             foreach my $item (@contacts,'bcc','others','include') {
17457:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
17458:                                     push(@{$changes{'overrides'}},$key);
17459:                                     last;
17460:                                 }
17461:                             }
17462:                         } else {
17463:                             push(@{$changes{'overrides'}},$key);
17464:                         }
17465:                     }
17466:                 }
17467:                 foreach my $key (@overrides) {
17468:                     unless (exists($currsetting{'overrides'}{$key})) {
17469:                         push(@{$changes{'overrides'}},$key);
17470:                     }
17471:                 }
17472:             } else {
17473:                 foreach my $key (@overrides) {
17474:                     push(@{$changes{'overrides'}},$key);
17475:                 }
17476:             }
17477:         }
17478:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
17479:             foreach my $key ('excluded','weights','threshold','sysmail') {
17480:                 if ($key eq 'excluded') {
17481:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17482:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
17483:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17484:                             (@{$currsetting{'lonstatus'}{$key}})) {
17485:                             my @diffs =
17486:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
17487:                                                                    $currsetting{'lonstatus'}{$key});
17488:                             if (@diffs) {
17489:                                 push(@{$changes{'lonstatus'}},$key);
17490:                             }
17491:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
17492:                             push(@{$changes{'lonstatus'}},$key);
17493:                         }
17494:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17495:                              (@{$currsetting{'lonstatus'}{$key}})) {
17496:                         push(@{$changes{'lonstatus'}},$key);
17497:                     }
17498:                 } elsif ($key eq 'weights') {
17499:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17500:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
17501:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17502:                             foreach my $type ('E','W','N','U') {
17503:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
17504:                                         $currsetting{'lonstatus'}{$key}{$type}) {
17505:                                     push(@{$changes{'lonstatus'}},$key);
17506:                                     last;
17507:                                 }
17508:                             }
17509:                         } else {
17510:                             foreach my $type ('E','W','N','U') {
17511:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
17512:                                     push(@{$changes{'lonstatus'}},$key);
17513:                                     last;
17514:                                 }
17515:                             }
17516:                         }
17517:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17518:                         foreach my $type ('E','W','N','U') {
17519:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
17520:                                 push(@{$changes{'lonstatus'}},$key);
17521:                                 last;
17522:                             }
17523:                         }
17524:                     }
17525:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
17526:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17527:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17528:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
17529:                                 push(@{$changes{'lonstatus'}},$key);
17530:                             }
17531:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
17532:                             push(@{$changes{'lonstatus'}},$key);
17533:                         }
17534:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17535:                         push(@{$changes{'lonstatus'}},$key);
17536:                     }
17537:                 }
17538:             }
17539:         } else {
17540:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17541:                 foreach my $key ('excluded','weights','threshold','sysmail') {
17542:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17543:                         push(@{$changes{'lonstatus'}},$key);
17544:                     }
17545:                 }
17546:             }
17547:         }
17548:     } else {
17549:         my %default;
17550:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
17551:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
17552:         $default{'errormail'} = 'adminemail';
17553:         $default{'packagesmail'} = 'adminemail';
17554:         $default{'helpdeskmail'} = 'supportemail';
17555:         $default{'otherdomsmail'} = 'supportemail';
17556:         $default{'lonstatusmail'} = 'adminemail';
17557:         $default{'requestsmail'} = 'adminemail';
17558:         $default{'updatesmail'} = 'adminemail';
17559:         $default{'hostipmail'} = 'adminemail';
17560:         foreach my $item (@contacts) {
17561:            if ($to{$item} ne $default{$item}) {
17562:                $changes{$item} = 1;
17563:            }
17564:         }
17565:         foreach my $type (@mailings) {
17566:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
17567:                 push(@{$changes{$type}},@{$newsetting{$type}});
17568:             }
17569:             if ($others{$type} ne '') {
17570:                 push(@{$changes{$type}},'others');
17571:             }
17572:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17573:                 if ($bcc{$type} ne '') {
17574:                     push(@{$changes{$type}},'bcc');
17575:                 }
17576:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
17577:                     push(@{$changes{$type}},'include');
17578:                 }
17579:             }
17580:         }
17581:         if (ref($fields) eq 'ARRAY') {
17582:             foreach my $field (@{$fields}) {
17583:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17584:                     push(@{$changes{'helpform'}},$field);
17585:                 }
17586:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17587:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17588:                         push(@{$changes{'helpform'}},'maxsize');
17589:                     }
17590:                 }
17591:             }
17592:         }
17593:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17594:             foreach my $key ('excluded','weights','threshold','sysmail') {
17595:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17596:                     push(@{$changes{'lonstatus'}},$key);
17597:                 }
17598:             }
17599:         }
17600:     }
17601:     foreach my $item (@toggles) {
17602:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
17603:             $changes{$item} = 1;
17604:         } elsif ((!$env{'form.'.$item}) &&
17605:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
17606:             $changes{$item} = 1;
17607:         }
17608:     }
17609:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
17610:                                              $dom);
17611:     if ($putresult eq 'ok') {
17612:         if (keys(%changes) > 0) {
17613:             &Apache::loncommon::devalidate_domconfig_cache($dom);
17614:             if (ref($lastactref) eq 'HASH') {
17615:                 $lastactref->{'domainconfig'} = 1;
17616:             }
17617:             my ($titles,$short_titles)  = &contact_titles();
17618:             $resulttext = &mt('Changes made:').'<ul>';
17619:             foreach my $item (@contacts) {
17620:                 if ($changes{$item}) {
17621:                     $resulttext .= '<li>'.$titles->{$item}.
17622:                                     &mt(' set to: ').
17623:                                     '<span class="LC_cusr_emph">'.
17624:                                     $to{$item}.'</span></li>';
17625:                 }
17626:             }
17627:             foreach my $type (@mailings) {
17628:                 if (ref($changes{$type}) eq 'ARRAY') {
17629:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17630:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
17631:                     } else {
17632:                         $resulttext .= '<li>'.$titles->{$type}.': ';
17633:                     }
17634:                     my @text;
17635:                     foreach my $item (@{$newsetting{$type}}) {
17636:                         push(@text,$short_titles->{$item});
17637:                     }
17638:                     if ($others{$type} ne '') {
17639:                         push(@text,$others{$type});
17640:                     }
17641:                     if (@text) {
17642:                         $resulttext .= '<span class="LC_cusr_emph">'.
17643:                                        join(', ',@text).'</span>';
17644:                     }
17645:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17646:                         if ($bcc{$type} ne '') {
17647:                             my $bcctext;
17648:                             if (@text) {
17649:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
17650:                             } else {
17651:                                 $bcctext = '(Bcc)';
17652:                             }
17653:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
17654:                         } elsif (!@text) {
17655:                             $resulttext .= &mt('No one');
17656:                         }
17657:                         if ($includestr{$type} ne '') {
17658:                             if ($includeloc{$type} eq 'b') {
17659:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
17660:                             } elsif ($includeloc{$type} eq 's') {
17661:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
17662:                             }
17663:                         }
17664:                     } elsif (!@text) {
17665:                         $resulttext .= &mt('No recipients');
17666:                     }
17667:                     $resulttext .= '</li>';
17668:                 }
17669:             }
17670:             if (ref($changes{'overrides'}) eq 'ARRAY') {
17671:                 my @deletions;
17672:                 foreach my $type (@{$changes{'overrides'}}) {
17673:                     if ($usertypeshash{$type}) {
17674:                         if (grep(/^\Q$type\E/,@overrides)) {
17675:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
17676:                                                       $usertypeshash{$type}).'<ul><li>';
17677:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
17678:                                 my @text;
17679:                                 foreach my $item (@contacts) {
17680:                                     if ($newsetting{'override_'.$type}{$item}) {
17681:                                         push(@text,$short_titles->{$item});
17682:                                     }
17683:                                 }
17684:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
17685:                                     push(@text,$newsetting{'override_'.$type}{'others'});
17686:                                 }
17687: 
17688:                                 if (@text) {
17689:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
17690:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
17691:                                 }
17692:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
17693:                                     my $bcctext;
17694:                                     if (@text) {
17695:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
17696:                                     } else {
17697:                                         $bcctext = '(Bcc)';
17698:                                     }
17699:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
17700:                                 } elsif (!@text) {
17701:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
17702:                                 }
17703:                                 $resulttext .= '</li>';
17704:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
17705:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
17706:                                     if ($loc eq 'b') {
17707:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
17708:                                     } elsif ($loc eq 's') {
17709:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
17710:                                     }
17711:                                 }
17712:                             }
17713:                             $resulttext .= '</li></ul></li>';
17714:                         } else {
17715:                             push(@deletions,$usertypeshash{$type});
17716:                         }
17717:                     }
17718:                 }
17719:                 if (@deletions) {
17720:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
17721:                                               join(', ',@deletions)).'</li>';
17722:                 }
17723:             }
17724:             my @offon = ('off','on');
17725:             my $corelink = &core_link_msu();
17726:             if ($changes{'reporterrors'}) {
17727:                 $resulttext .= '<li>'.
17728:                                &mt('E-mail error reports to [_1] set to "'.
17729:                                    $offon[$env{'form.reporterrors'}].'".',
17730:                                    $corelink).
17731:                                '</li>';
17732:             }
17733:             if ($changes{'reportupdates'}) {
17734:                 $resulttext .= '<li>'.
17735:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
17736:                                     $offon[$env{'form.reportupdates'}].'".',
17737:                                     $corelink).
17738:                                 '</li>';
17739:             }
17740:             if ($changes{'reportstatus'}) {
17741:                 $resulttext .= '<li>'.
17742:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
17743:                                     $offon[$env{'form.reportstatus'}].'".',
17744:                                     $corelink).
17745:                                 '</li>';
17746:             }
17747:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
17748:                 $resulttext .= '<li>'.
17749:                                &mt('Nightly status check e-mail settings').':<ul>';
17750:                 my (%defval,%use_def,%shown);
17751:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17752:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17753:                 $defval{'weights'} =
17754:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17755:                 $defval{'excluded'} = &mt('None');
17756:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17757:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17758:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17759:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17760:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17761:                             } elsif ($item eq 'weights') {
17762:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17763:                                     foreach my $type ('E','W','N','U') {
17764:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17765:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17766:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17767:                                         } else {
17768:                                             $shown{$item} .= $lonstatus_defs->{$type};
17769:                                         }
17770:                                         $shown{$item} .= ', ';
17771:                                     }
17772:                                     $shown{$item} =~ s/, $//;
17773:                                 } else {
17774:                                     $shown{$item} = $defval{$item};
17775:                                 }
17776:                             } elsif ($item eq 'excluded') {
17777:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17778:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17779:                                 } else {
17780:                                     $shown{$item} = $defval{$item};
17781:                                 }
17782:                             }
17783:                         } else {
17784:                             $shown{$item} = $defval{$item};
17785:                         }
17786:                     }
17787:                 } else {
17788:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17789:                         $shown{$item} = $defval{$item};
17790:                     }
17791:                 }
17792:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17793:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17794:                                           $shown{$item}).'</li>';
17795:                 }
17796:                 $resulttext .= '</ul></li>';
17797:             }
17798:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17799:                 my (@optional,@required,@unused,$maxsizechg);
17800:                 foreach my $field (@{$changes{'helpform'}}) {
17801:                     if ($field eq 'maxsize') {
17802:                         $maxsizechg = 1;
17803:                         next;
17804:                     }
17805:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17806:                         push(@optional,$field);
17807:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17808:                         push(@unused,$field);
17809:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17810:                         push(@required,$field);
17811:                     }
17812:                 }
17813:                 if (@optional) {
17814:                     $resulttext .= '<li>'.
17815:                                    &mt('Help form fields changed to "Optional": [_1].',
17816:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17817:                                    '</li>';
17818:                 }
17819:                 if (@required) {
17820:                     $resulttext .= '<li>'.
17821:                                    &mt('Help form fields changed to "Required": [_1].',
17822:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17823:                                    '</li>';
17824:                 }
17825:                 if (@unused) {
17826:                     $resulttext .= '<li>'.
17827:                                    &mt('Help form fields changed to "Not shown": [_1].',
17828:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17829:                                    '</li>';
17830:                 }
17831:                 if ($maxsizechg) {
17832:                     $resulttext .= '<li>'.
17833:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17834:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17835:                                    '</li>';
17836:                 }
17837:             }
17838:             $resulttext .= '</ul>';
17839:         } else {
17840:             $resulttext = &mt('No changes made to contacts and form settings');
17841:         }
17842:     } else {
17843:         $resulttext = '<span class="LC_error">'.
17844:             &mt('An error occurred: [_1].',$putresult).'</span>';
17845:     }
17846:     return $resulttext;
17847: }
17848: 
17849: sub modify_privacy {
17850:     my ($dom,$lastactref,%domconfig) = @_;
17851:     my ($resulttext,%current,%changes);
17852:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17853:         %current = %{$domconfig{'privacy'}};
17854:     }
17855:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17856:     my @items = ('domain','author','course','community');
17857:     my %names = &Apache::lonlocal::texthash (
17858:                    domain => 'Assigned domain role(s)',
17859:                    author => 'Assigned co-author role(s)',
17860:                    course => 'Assigned course role(s)',
17861:                    community => 'Assigned community role(s)',
17862:                 );
17863:     my %roles = &Apache::lonlocal::texthash (
17864:                    domain => 'Domain role',
17865:                    author => 'Co-author role',
17866:                    course => 'Course role',
17867:                    community => 'Community role',
17868:                 );
17869:     my %titles = &Apache::lonlocal::texthash (
17870:                   approval => 'Approval for role in different domain',
17871:                   othdom   => 'User information available in other domain',
17872:                   priv     => 'Information viewable by privileged user in same domain',
17873:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17874:                   instdom  => 'Other domain shares institution/provider',
17875:                   extdom   => 'Other domain has different institution/provider',
17876:                   none     => 'Not allowed',
17877:                   user     => 'User authorizes',
17878:                   domain   => 'Domain Coordinator authorizes',
17879:                   auto     => 'Unrestricted',
17880:                   notify   => 'Notify when role needs authorization',
17881:     );
17882:     my %fieldnames = &Apache::lonlocal::texthash (
17883:                         id => 'Student/Employee ID',
17884:                         permanentemail => 'E-mail address',
17885:                         lastname => 'Last Name',
17886:                         firstname => 'First Name',
17887:                         middlename => 'Middle Name',
17888:                         generation => 'Generation',
17889:     );
17890:     my ($othertitle,$usertypes,$types) =
17891:         &Apache::loncommon::sorted_inst_types($dom);
17892:     my (%by_ip,%by_location,@intdoms,@instdoms);
17893:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17894: 
17895:     my %privacyhash = (
17896:                        'approval' => {
17897:                                        instdom => {},
17898:                                        extdom  => {},
17899:                                      },
17900:                        'othdom'   => {},
17901:                        'priv'     => {},
17902:                        'unpriv'   => {},
17903:                       );
17904:     foreach my $item (@items) {
17905:         if (@instdoms > 1) {
17906:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17907:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17908:             }
17909:             if (ref($current{'approval'}) eq 'HASH') {
17910:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17911:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17912:                         $changes{'approval'} = 1;
17913:                     }
17914:                 }
17915:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17916:                 $changes{'approval'} = 1;
17917:             }
17918:         }
17919:         if (keys(%by_location) > 0) {
17920:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17921:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17922:             }
17923:             if (ref($current{'approval'}) eq 'HASH') {
17924:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17925:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17926:                         $changes{'approval'} = 1;
17927:                     }
17928:                 }
17929:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17930:                 $changes{'approval'} = 1;
17931:             }
17932:         }
17933:         foreach my $status ('priv','unpriv') {
17934:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17935:             my @newvalues;
17936:             foreach my $field (@possibles) {
17937:                 if (grep(/^\Q$field\E$/,@fields)) {
17938:                     $privacyhash{$status}{$item}{$field} = 1;
17939:                     push(@newvalues,$field);
17940:                 }
17941:             }
17942:             @newvalues = sort(@newvalues);
17943:             if (ref($current{$status}) eq 'HASH') {
17944:                 if (ref($current{$status}{$item}) eq 'HASH') {
17945:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17946:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17947:                     if (@diffs > 0) {
17948:                         $changes{$status} = 1;
17949:                     }
17950:                 } elsif (@newvalues > 0) {
17951:                     $changes{$status} = 1;
17952:                 }
17953:             } else {
17954:                 my @stdfields;
17955:                 foreach my $field (@fields) {
17956:                     if ($field eq 'id') {
17957:                         next if ($status eq 'unpriv');
17958:                         next if (($status eq 'priv') && ($item eq 'community'));
17959:                     }
17960:                     push(@stdfields,$field);
17961:                 }
17962:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17963:                 if (@diffs > 0) {
17964:                     $changes{$status} = 1;
17965:                 }
17966:             }
17967:         }
17968:     }
17969:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17970:         my @statuses;
17971:         if (ref($types) eq 'ARRAY') {
17972:             @statuses = @{$types};
17973:         }
17974:         foreach my $type (@statuses,'default') {
17975:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17976:             my @newvalues;
17977:             foreach my $field (sort(@possfields)) {
17978:                 if (grep(/^\Q$field\E$/,@fields)) {
17979:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17980:                     push(@newvalues,$field);
17981:                 }
17982:             }
17983:             @newvalues = sort(@newvalues);
17984:             if (ref($current{'othdom'}) eq 'HASH') {
17985:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17986:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17987:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17988:                     if (@diffs > 0) {
17989:                         $changes{'othdom'} = 1;
17990:                     }
17991:                 } elsif (@newvalues > 0) {
17992:                     $changes{'othdom'} = 1;
17993:                 }
17994:             } else {
17995:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17996:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17997:                 if (@diffs > 0) {
17998:                     $changes{'othdom'} = 1;
17999:                 }
18000:             }
18001:         }
18002:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
18003:         my %notify;
18004:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
18005:             if (exists($domcoords{$possdc})) {
18006:                 $notify{$possdc} = 1;
18007:             }
18008:         }
18009:         my $notify = join(',',sort(keys(%notify)));
18010:         if ($current{'notify'} ne $notify) {
18011:             $changes{'notify'} = 1;
18012:         }
18013:         $privacyhash{'notify'} = $notify;
18014:     }
18015:     my %confighash = (
18016:                         privacy => \%privacyhash,
18017:                      );
18018:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18019:     if ($putresult eq 'ok') {
18020:         if (keys(%changes) > 0) {
18021:             $resulttext = &mt('Changes made: ').'<ul>';
18022:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
18023:                 if ($changes{$key}) {
18024:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18025:                     if ($key eq 'approval') {
18026:                         if (keys(%{$privacyhash{$key}{instdom}})) {
18027:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
18028:                             foreach my $item (@items) {
18029:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
18030:                             }
18031:                             $resulttext .= '</ul></li>';
18032:                         }
18033:                         if (keys(%{$privacyhash{$key}{extdom}})) {
18034:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
18035:                             foreach my $item (@items) {
18036:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
18037:                             }
18038:                             $resulttext .= '</ul></li>';
18039:                         }
18040:                     } elsif ($key eq 'notify') {
18041:                         if ($privacyhash{$key}) {
18042:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
18043:                                 my ($dcname,$dcdom) = split(/:/,$dc);
18044:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
18045:                             }
18046:                         } else {
18047:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
18048:                         }
18049:                     } elsif ($key eq 'othdom') {
18050:                         my @statuses;
18051:                         if (ref($types) eq 'ARRAY') {
18052:                             @statuses = @{$types};
18053:                         }
18054:                         if (ref($privacyhash{$key}) eq 'HASH') {
18055:                             foreach my $status (@statuses,'default') {
18056:                                 if ($status eq 'default') {
18057:                                     $resulttext .= '<li>'.$othertitle.': ';
18058:                                 } elsif (ref($usertypes) eq 'HASH') {
18059:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
18060:                                 } else {
18061:                                     next;
18062:                                 }
18063:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
18064:                                     if (keys(%{$privacyhash{$key}{$status}})) {
18065:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
18066:                                     } else {
18067:                                         $resulttext .= &mt('none');
18068:                                     }
18069:                                 }
18070:                                 $resulttext .= '</li>';
18071:                             }
18072:                         }
18073:                     } else {
18074:                         foreach my $item (@items) {
18075:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
18076:                                 $resulttext .= '<li>'.$names{$item}.': ';
18077:                                 if (keys(%{$privacyhash{$key}{$item}})) {
18078:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
18079:                                 } else {
18080:                                     $resulttext .= &mt('none');
18081:                                 }
18082:                                 $resulttext .= '</li>';
18083:                             }
18084:                         }
18085:                     }
18086:                     $resulttext .= '</ul></li>';
18087:                 }
18088:             }
18089:             $resulttext .= '</ul>';
18090:             if ($changes{'approval'}) {
18091:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18092:                 delete($domdefaults{'userapprovals'});
18093:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
18094:                     foreach my $domtype ('instdom','extdom') {
18095:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
18096:                             foreach my $roletype ('domain','author','course','community') {
18097:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
18098:                                     $domdefaults{'userapprovals'} = 1;
18099:                                     last;
18100:                                 }
18101:                             }
18102:                         }
18103:                         last if ($domdefaults{'userapprovals'});               
18104:                     }
18105:                 }
18106:                 my $cachetime = 24*60*60;
18107:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18108:                 if (ref($lastactref) eq 'HASH') {
18109:                     $lastactref->{'domdefaults'} = 1;
18110:                 }
18111:             }
18112:         } else {
18113:             $resulttext = &mt('No changes made to user information settings');
18114:         }
18115:     } else {
18116:         $resulttext = '<span class="LC_error">'.
18117:             &mt('An error occurred: [_1]',$putresult).'</span>';
18118:     }
18119:     return $resulttext;
18120: }
18121: 
18122: sub modify_passwords {
18123:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18124:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
18125:         $updatedefaults,$updateconf);
18126:     my $customfn = 'resetpw.html';
18127:     if (ref($domconfig{'passwords'}) eq 'HASH') {
18128:         %current = %{$domconfig{'passwords'}};
18129:     }
18130:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18131:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18132:     if (ref($types) eq 'ARRAY') {
18133:         @oktypes = @{$types};
18134:     }
18135:     push(@oktypes,'default');
18136: 
18137:     my %titles = &Apache::lonlocal::texthash (
18138:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
18139:         intauth_check  => 'Check bcrypt cost if authenticated',
18140:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
18141:         permanent      => 'Permanent e-mail address',
18142:         critical       => 'Critical notification address',
18143:         notify         => 'Notification address',
18144:         min            => 'Minimum password length',
18145:         max            => 'Maximum password length',
18146:         chars          => 'Required characters',
18147:         expire         => 'Password expiration (days)',
18148:         numsaved       => 'Number of previous passwords to save',
18149:         reset          => 'Resetting Forgotten Password',
18150:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
18151:         rules          => 'Rules for LON-CAPA Passwords',
18152:         crsownerchg    => 'Course Owner Changing Student Passwords',
18153:         username       => 'Username',
18154:         email          => 'E-mail address',
18155:     );
18156: 
18157: #
18158: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
18159: #
18160:     my (%curr_defaults,%save_defaults);
18161:     if (ref($domconfig{'defaults'}) eq 'HASH') {
18162:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
18163:             if ($key =~ /^intauth_(cost|check|switch)$/) {
18164:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
18165:             } else {
18166:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
18167:             }
18168:         }
18169:     }
18170:     my %staticdefaults = (
18171:         'resetlink'      => 2,
18172:         'resetcase'      => \@oktypes,
18173:         'resetprelink'   => 'both',
18174:         'resetemail'     => ['critical','notify','permanent'],
18175:         'intauth_cost'   => 10,
18176:         'intauth_check'  => 0,
18177:         'intauth_switch' => 0,
18178:     );
18179:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18180:     foreach my $type (@oktypes) {
18181:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
18182:     }
18183:     my $linklife = $env{'form.passwords_link'};
18184:     $linklife =~ s/^\s+|\s+$//g;
18185:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
18186:         $newvalues{'resetlink'} = $linklife;
18187:         if ($current{'resetlink'}) {
18188:             if ($current{'resetlink'} ne $linklife) {
18189:                 $changes{'reset'} = 1;
18190:             }
18191:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18192:             if ($staticdefaults{'resetlink'} ne $linklife) {
18193:                 $changes{'reset'} = 1;
18194:             }
18195:         }
18196:     } elsif ($current{'resetlink'}) {
18197:         $changes{'reset'} = 1;
18198:     }
18199:     my @casesens;
18200:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
18201:     foreach my $case (sort(@posscase)) {
18202:         if (grep(/^\Q$case\E$/,@oktypes)) {
18203:             push(@casesens,$case);
18204:         }
18205:     }
18206:     $newvalues{'resetcase'} = \@casesens;
18207:     if (ref($current{'resetcase'}) eq 'ARRAY') {
18208:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
18209:         if (@diffs > 0) {
18210:             $changes{'reset'} = 1;
18211:         }
18212:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18213:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
18214:         if (@diffs > 0) {
18215:             $changes{'reset'} = 1;
18216:         }
18217:     }
18218:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
18219:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
18220:         if (exists($current{'resetprelink'})) {
18221:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
18222:                 $changes{'reset'} = 1;
18223:             }
18224:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18225:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
18226:                 $changes{'reset'} = 1;
18227:             }
18228:         }
18229:     } elsif ($current{'resetprelink'}) {
18230:         $changes{'reset'} = 1;
18231:     }
18232:     foreach my $type (@oktypes) {
18233:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
18234:         my @postlink;
18235:         foreach my $item (sort(@possplink)) {
18236:             if ($item =~ /^(email|username)$/) {
18237:                 push(@postlink,$item);
18238:             }
18239:         }
18240:         $newvalues{'resetpostlink'}{$type} = \@postlink;
18241:         unless ($changes{'reset'}) {
18242:             if (ref($current{'resetpostlink'}) eq 'HASH') {
18243:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
18244:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
18245:                     if (@diffs > 0) {
18246:                         $changes{'reset'} = 1;
18247:                     }
18248:                 } else {
18249:                     $changes{'reset'} = 1;
18250:                 }
18251:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18252:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
18253:                 if (@diffs > 0) {
18254:                     $changes{'reset'} = 1;
18255:                 }
18256:             }
18257:         }
18258:     }
18259:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
18260:     my @resetemail;
18261:     foreach my $item (sort(@possemailsrc)) {
18262:         if ($item =~ /^(permanent|critical|notify)$/) {
18263:             push(@resetemail,$item);
18264:         }
18265:     }
18266:     $newvalues{'resetemail'} = \@resetemail;
18267:     unless ($changes{'reset'}) {
18268:         if (ref($current{'resetemail'}) eq 'ARRAY') {
18269:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
18270:             if (@diffs > 0) {
18271:                 $changes{'reset'} = 1;
18272:             }
18273:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18274:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
18275:             if (@diffs > 0) {
18276:                 $changes{'reset'} = 1;
18277:             }
18278:         }
18279:     }
18280:     if ($env{'form.passwords_stdtext'} == 0) {
18281:         $newvalues{'resetremove'} = 1;
18282:         unless ($current{'resetremove'}) {
18283:             $changes{'reset'} = 1;
18284:         }
18285:     } elsif ($current{'resetremove'}) {
18286:         $changes{'reset'} = 1;
18287:     }
18288:     if ($env{'form.passwords_customfile.filename'} ne '') {
18289:         my $servadm = $r->dir_config('lonAdmEMail');
18290:         my ($configuserok,$author_ok,$switchserver) =
18291:             &config_check($dom,$confname,$servadm);
18292:         my $error;
18293:         if ($configuserok eq 'ok') {
18294:             if ($switchserver) {
18295:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
18296:             } else {
18297:                 if ($author_ok eq 'ok') {
18298:                     my $modified = [];
18299:                     my ($result,$customurl) =
18300:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
18301:                                                                 $confname,'customtext/resetpw','','',$customfn,
18302:                                                                 $modified);
18303:                     if ($result eq 'ok') {
18304:                         $newvalues{'resetcustom'} = $customurl;
18305:                         $changes{'reset'} = 1;
18306:                         &update_modify_urls($r,$modified);
18307:                     } else {
18308:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
18309:                     }
18310:                 } else {
18311:                     $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);
18312:                 }
18313:             }
18314:         } else {
18315:             $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);
18316:         }
18317:         if ($error) {
18318:             &Apache::lonnet::logthis($error);
18319:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18320:         }
18321:     } elsif ($current{'resetcustom'}) {
18322:         if ($env{'form.passwords_custom_del'}) {
18323:             $changes{'reset'} = 1;
18324:         } else {
18325:             $newvalues{'resetcustom'} = $current{'resetcustom'};
18326:         }
18327:     }
18328:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
18329:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
18330:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
18331:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
18332:             $changes{'intauth'} = 1;
18333:         }
18334:     } else {
18335:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
18336:     }
18337:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
18338:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
18339:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
18340:             $changes{'intauth'} = 1;
18341:         }
18342:     } else {
18343:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18344:     }
18345:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
18346:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
18347:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
18348:             $changes{'intauth'} = 1;
18349:         }
18350:     } else {
18351:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18352:     }
18353:     foreach my $item ('cost','check','switch') {
18354:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
18355:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
18356:             $updatedefaults = 1;
18357:         }
18358:     }
18359:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
18360:     my %crsownerchg = (
18361:                         by => [],
18362:                         for => [],
18363:                       );
18364:     foreach my $item ('by','for') {
18365:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
18366:         foreach my $type (sort(@posstypes)) {
18367:             if (grep(/^\Q$type\E$/,@oktypes)) {
18368:                 push(@{$crsownerchg{$item}},$type);
18369:             }
18370:         }
18371:     }
18372:     $newvalues{'crsownerchg'} = \%crsownerchg;
18373:     if (ref($current{'crsownerchg'}) eq 'HASH') {
18374:         foreach my $item ('by','for') {
18375:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
18376:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
18377:                 if (@diffs > 0) {
18378:                     $changes{'crsownerchg'} = 1;
18379:                     last;
18380:                 }
18381:             }
18382:         }
18383:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
18384:         foreach my $item ('by','for') {
18385:             if (@{$crsownerchg{$item}} > 0) {
18386:                 $changes{'crsownerchg'} = 1;
18387:                 last;
18388:             }
18389:         }
18390:     }
18391: 
18392:     my %confighash = (
18393:                         defaults  => \%save_defaults,
18394:                         passwords => \%newvalues,
18395:                      );
18396:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
18397: 
18398:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18399:     if ($putresult eq 'ok') {
18400:         if (keys(%changes) > 0) {
18401:             $resulttext = &mt('Changes made: ').'<ul>';
18402:             foreach my $key ('reset','intauth','rules','crsownerchg') {
18403:                 if ($changes{$key}) {
18404:                     unless ($key eq 'intauth') {
18405:                         $updateconf = 1;
18406:                     }
18407:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18408:                     if ($key eq 'reset') {
18409:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
18410:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
18411:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
18412:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
18413:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
18414:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
18415:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
18416:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
18417:                             }
18418:                         } else {
18419:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
18420:                         }
18421:                         if ($confighash{'passwords'}{'resetlink'}) {
18422:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
18423:                         } else {
18424:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
18425:                                                   &mt('Will default to 2 hours').'</li>';
18426:                         }
18427:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
18428:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
18429:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
18430:                             } else {
18431:                                 my $casesens;
18432:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
18433:                                     if ($type eq 'default') {
18434:                                         $casesens .= $othertitle.', ';
18435:                                     } elsif ($usertypes->{$type} ne '') {
18436:                                         $casesens .= $usertypes->{$type}.', ';
18437:                                     }
18438:                                 }
18439:                                 $casesens =~ s/\Q, \E$//;
18440:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
18441:                             }
18442:                         } else {
18443:                             $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>';
18444:                         }
18445:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
18446:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
18447:                         } else {
18448:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
18449:                         }
18450:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
18451:                             my $output;
18452:                             if (ref($types) eq 'ARRAY') {
18453:                                 foreach my $type (@{$types}) {
18454:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
18455:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
18456:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
18457:                                         } else {
18458:                                             $output .= $usertypes->{$type}.' -- '.
18459:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
18460:                                         }
18461:                                     }
18462:                                 }
18463:                             }
18464:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
18465:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
18466:                                     $output .= $othertitle.' -- '.&mt('none');
18467:                                 } else {
18468:                                     $output .= $othertitle.' -- '.
18469:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
18470:                                 }
18471:                             }
18472:                             if ($output) {
18473:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
18474:                             } else {
18475:                                 $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>';
18476:                             }
18477:                         } else {
18478:                             $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>';
18479:                         }
18480:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
18481:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
18482:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
18483:                             } else {
18484:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18485:                             }
18486:                         } else {
18487:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18488:                         }
18489:                         if ($confighash{'passwords'}{'resetremove'}) {
18490:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
18491:                         } else {
18492:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
18493:                         }
18494:                         if ($confighash{'passwords'}{'resetcustom'}) {
18495:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
18496:                                                                             &mt('custom text'),600,500,undef,undef,
18497:                                                                             undef,undef,'background-color:#ffffff');
18498:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
18499:                         } else {
18500:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
18501:                         }
18502:                     } elsif ($key eq 'intauth') {
18503:                         foreach my $item ('cost','switch','check') {
18504:                             my $value = $save_defaults{$key.'_'.$item};
18505:                             if ($item eq 'switch') {
18506:                                 my %optiondesc = &Apache::lonlocal::texthash (
18507:                                                      0 => 'No',
18508:                                                      1 => 'Yes',
18509:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
18510:                                                  );
18511:                                 if ($value =~ /^(0|1|2)$/) {
18512:                                     $value = $optiondesc{$value};
18513:                                 } else {
18514:                                     $value = &mt('none -- defaults to No');
18515:                                 }
18516:                             } elsif ($item eq 'check') {
18517:                                 my %optiondesc = &Apache::lonlocal::texthash (
18518:                                                      0 => 'No',
18519:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
18520:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
18521:                                                  );
18522:                                 if ($value =~ /^(0|1|2)$/) {
18523:                                     $value = $optiondesc{$value};
18524:                                 } else {
18525:                                     $value = &mt('none -- defaults to No');
18526:                                 }
18527:                             }
18528:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
18529:                         }
18530:                     } elsif ($key eq 'rules') {
18531:                         foreach my $rule ('min','max','expire','numsaved') {
18532:                             if ($confighash{'passwords'}{$rule} eq '') {
18533:                                 if ($rule eq 'min') {
18534:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
18535:                                                    ' '.&mt('Default of [_1] will be used',
18536:                                                            $Apache::lonnet::passwdmin).'</li>';
18537:                                 } else {
18538:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
18539:                                 }
18540:                             } else {
18541:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
18542:                             }
18543:                         }
18544:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
18545:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
18546:                                 my %rulenames = &Apache::lonlocal::texthash(
18547:                                                      uc => 'At least one upper case letter',
18548:                                                      lc => 'At least one lower case letter',
18549:                                                      num => 'At least one number',
18550:                                                      spec => 'At least one non-alphanumeric',
18551:                                                    );
18552:                                 my $needed = '<ul><li>'.
18553:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
18554:                                              '</li></ul>';
18555:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
18556:                             } else {
18557:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18558:                             }
18559:                         } else {
18560:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18561:                         }
18562:                     } elsif ($key eq 'crsownerchg') {
18563:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
18564:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
18565:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
18566:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18567:                             } else {
18568:                                 my %crsownerstr;
18569:                                 foreach my $item ('by','for') {
18570:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
18571:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
18572:                                             if ($type eq 'default') {
18573:                                                 $crsownerstr{$item} .= $othertitle.', ';
18574:                                             } elsif ($usertypes->{$type} ne '') {
18575:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
18576:                                             }
18577:                                         }
18578:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
18579:                                     }
18580:                                 }
18581:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
18582:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
18583:                             }
18584:                         } else {
18585:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18586:                         }
18587:                     }
18588:                     $resulttext .= '</ul></li>';
18589:                 }
18590:             }
18591:             $resulttext .= '</ul>';
18592:         } else {
18593:             $resulttext = &mt('No changes made to password settings');
18594:         }
18595:         my $cachetime = 24*60*60;
18596:         if ($updatedefaults) {
18597:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18598:             if (ref($lastactref) eq 'HASH') {
18599:                 $lastactref->{'domdefaults'} = 1;
18600:             }
18601:         }
18602:         if ($updateconf) {
18603:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
18604:             if (ref($lastactref) eq 'HASH') {
18605:                 $lastactref->{'passwdconf'} = 1;
18606:             }
18607:         }
18608:     } else {
18609:         $resulttext = '<span class="LC_error">'.
18610:             &mt('An error occurred: [_1]',$putresult).'</span>';
18611:     }
18612:     if ($errors) {
18613:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18614:                        $errors.'</ul></p>';
18615:     }
18616:     return $resulttext;
18617: }
18618: 
18619: sub password_rule_changes {
18620:     my ($prefix,$newvalues,$current,$changes) = @_;
18621:     return unless ((ref($newvalues) eq 'HASH') &&
18622:                    (ref($current) eq 'HASH') &&
18623:                    (ref($changes) eq 'HASH'));
18624:     my (@rules,%staticdefaults);
18625:     if ($prefix eq 'passwords') {
18626:         @rules = ('min','max','expire','numsaved');
18627:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
18628:         @rules = ('min','max');
18629:     }
18630:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18631:     foreach my $rule (@rules) {
18632:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
18633:         my $ruleok;
18634:         if ($rule eq 'expire') {
18635:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
18636:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18637:                 $ruleok = 1;
18638:             }
18639:         } elsif ($rule eq 'min') {
18640:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
18641:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
18642:                     $ruleok = 1;
18643:                 }
18644:             }
18645:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
18646:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18647:             $ruleok = 1;
18648:         }
18649:         if ($ruleok) {
18650:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
18651:             if (exists($current->{$rule})) {
18652:                 if ($newvalues->{$rule} ne $current->{$rule}) {
18653:                     $changes->{'rules'} = 1;
18654:                 }
18655:             } elsif ($rule eq 'min') {
18656:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
18657:                     $changes->{'rules'} = 1;
18658:                 }
18659:             } else {
18660:                 $changes->{'rules'} = 1;
18661:             }
18662:         } elsif (exists($current->{$rule})) {
18663:             $changes->{'rules'} = 1;
18664:         }
18665:     }
18666:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
18667:     my @chars;
18668:     foreach my $item (sort(@posschars)) {
18669:         if ($item =~ /^(uc|lc|num|spec)$/) {
18670:             push(@chars,$item);
18671:         }
18672:     }
18673:     $newvalues->{'chars'} = \@chars;
18674:     unless ($changes->{'rules'}) {
18675:         if (ref($current->{'chars'}) eq 'ARRAY') {
18676:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
18677:             if (@diffs > 0) {
18678:                 $changes->{'rules'} = 1;
18679:             }
18680:         } else {
18681:             if (@chars > 0) {
18682:                 $changes->{'rules'} = 1;
18683:             }
18684:         }
18685:     }
18686:     return;
18687: }
18688: 
18689: sub modify_usercreation {
18690:     my ($dom,%domconfig) = @_;
18691:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
18692:     my $warningmsg;
18693:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18694:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18695:             if ($key eq 'cancreate') {
18696:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18697:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18698:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
18699:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18700:                         } else {
18701:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18702:                         }
18703:                     }
18704:                 }
18705:             } elsif ($key eq 'email_rule') {
18706:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18707:             } else {
18708:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18709:             }
18710:         }
18711:     }
18712:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
18713:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
18714:     my @contexts = ('author','course','requestcrs');
18715:     foreach my $item(@contexts) {
18716:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
18717:     }
18718:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18719:         foreach my $item (@contexts) {
18720:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
18721:                 push(@{$changes{'cancreate'}},$item);
18722:             }
18723:         }
18724:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
18725:         foreach my $item (@contexts) {
18726:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
18727:                 if ($cancreate{$item} ne 'any') {
18728:                     push(@{$changes{'cancreate'}},$item);
18729:                 }
18730:             } else {
18731:                 if ($cancreate{$item} ne 'none') {
18732:                     push(@{$changes{'cancreate'}},$item);
18733:                 }
18734:             }
18735:         }
18736:     } else {
18737:         foreach my $item (@contexts)  {
18738:             push(@{$changes{'cancreate'}},$item);
18739:         }
18740:     }
18741: 
18742:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
18743:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
18744:             if (!grep(/^\Q$type\E$/,@username_rule)) {
18745:                 push(@{$changes{'username_rule'}},$type);
18746:             }
18747:         }
18748:         foreach my $type (@username_rule) {
18749:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
18750:                 push(@{$changes{'username_rule'}},$type);
18751:             }
18752:         }
18753:     } else {
18754:         push(@{$changes{'username_rule'}},@username_rule);
18755:     }
18756: 
18757:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18758:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18759:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18760:                 push(@{$changes{'id_rule'}},$type);
18761:             }
18762:         }
18763:         foreach my $type (@id_rule) {
18764:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18765:                 push(@{$changes{'id_rule'}},$type);
18766:             }
18767:         }
18768:     } else {
18769:         push(@{$changes{'id_rule'}},@id_rule);
18770:     }
18771: 
18772:     my @authen_contexts = ('author','course','domain');
18773:     my @authtypes = ('int','krb4','krb5','loc','lti');
18774:     my %authhash;
18775:     foreach my $item (@authen_contexts) {
18776:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18777:         foreach my $auth (@authtypes) {
18778:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18779:                 $authhash{$item}{$auth} = 1;
18780:             } else {
18781:                 $authhash{$item}{$auth} = 0;
18782:             }
18783:         }
18784:     }
18785:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18786:         foreach my $item (@authen_contexts) {
18787:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18788:                 foreach my $auth (@authtypes) {
18789:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18790:                         push(@{$changes{'authtypes'}},$item);
18791:                         last;
18792:                     }
18793:                 }
18794:             }
18795:         }
18796:     } else {
18797:         foreach my $item (@authen_contexts) {
18798:             push(@{$changes{'authtypes'}},$item);
18799:         }
18800:     }
18801: 
18802:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18803:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18804:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18805:     $save_usercreate{'id_rule'} = \@id_rule;
18806:     $save_usercreate{'username_rule'} = \@username_rule,
18807:     $save_usercreate{'authtypes'} = \%authhash;
18808: 
18809:     my %usercreation_hash =  (
18810:         usercreation     => \%save_usercreate,
18811:     );
18812: 
18813:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18814:                                              $dom);
18815: 
18816:     if ($putresult eq 'ok') {
18817:         if (keys(%changes) > 0) {
18818:             $resulttext = &mt('Changes made:').'<ul>';
18819:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18820:                 my %lt = &usercreation_types();
18821:                 foreach my $type (@{$changes{'cancreate'}}) {
18822:                     my $chgtext = $lt{$type}.', ';
18823:                     if ($cancreate{$type} eq 'none') {
18824:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18825:                     } elsif ($cancreate{$type} eq 'any') {
18826:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18827:                     } elsif ($cancreate{$type} eq 'official') {
18828:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18829:                     } elsif ($cancreate{$type} eq 'unofficial') {
18830:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18831:                     }
18832:                     $resulttext .= '<li>'.$chgtext.'</li>';
18833:                 }
18834:             }
18835:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18836:                 my ($rules,$ruleorder) = 
18837:                     &Apache::lonnet::inst_userrules($dom,'username');
18838:                 my $chgtext = '<ul>';
18839:                 foreach my $type (@username_rule) {
18840:                     if (ref($rules->{$type}) eq 'HASH') {
18841:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18842:                     }
18843:                 }
18844:                 $chgtext .= '</ul>';
18845:                 if (@username_rule > 0) {
18846:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18847:                 } else {
18848:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18849:                 }
18850:             }
18851:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18852:                 my ($idrules,$idruleorder) = 
18853:                     &Apache::lonnet::inst_userrules($dom,'id');
18854:                 my $chgtext = '<ul>';
18855:                 foreach my $type (@id_rule) {
18856:                     if (ref($idrules->{$type}) eq 'HASH') {
18857:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18858:                     }
18859:                 }
18860:                 $chgtext .= '</ul>';
18861:                 if (@id_rule > 0) {
18862:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18863:                 } else {
18864:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18865:                 }
18866:             }
18867:             my %authname = &authtype_names();
18868:             my %context_title = &context_names();
18869:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18870:                 my $chgtext = '<ul>';
18871:                 foreach my $type (@{$changes{'authtypes'}}) {
18872:                     my @allowed;
18873:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18874:                     foreach my $auth (@authtypes) {
18875:                         if ($authhash{$type}{$auth}) {
18876:                             push(@allowed,$authname{$auth});
18877:                         }
18878:                     }
18879:                     if (@allowed > 0) {
18880:                         $chgtext .= join(', ',@allowed).'</li>';
18881:                     } else {
18882:                         $chgtext .= &mt('none').'</li>';
18883:                     }
18884:                 }
18885:                 $chgtext .= '</ul>';
18886:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18887:                 $resulttext .= '</li>';
18888:             }
18889:             $resulttext .= '</ul>';
18890:         } else {
18891:             $resulttext = &mt('No changes made to user creation settings');
18892:         }
18893:     } else {
18894:         $resulttext = '<span class="LC_error">'.
18895:             &mt('An error occurred: [_1]',$putresult).'</span>';
18896:     }
18897:     if ($warningmsg ne '') {
18898:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18899:     }
18900:     return $resulttext;
18901: }
18902: 
18903: sub modify_selfcreation {
18904:     my ($dom,$lastactref,%domconfig) = @_;
18905:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18906:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18907:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18908:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18909:     if (ref($typesref) eq 'ARRAY') {
18910:         @types = @{$typesref};
18911:     }
18912:     if (ref($usertypesref) eq 'HASH') {
18913:         %usertypes = %{$usertypesref};
18914:     }
18915:     $usertypes{'default'} = $othertitle;
18916: #
18917: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18918: #
18919:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18920:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18921:             if ($key eq 'cancreate') {
18922:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18923:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18924:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18925:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18926:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18927:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18928:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18929:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18930:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18931:                         } else {
18932:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18933:                         }
18934:                     }
18935:                 }
18936:             } elsif ($key eq 'email_rule') {
18937:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18938:             } else {
18939:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18940:             }
18941:         }
18942:     }
18943: #
18944: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18945: #
18946:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18947:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18948:             if ($key eq 'selfcreate') {
18949:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18950:             } else {
18951:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18952:             }
18953:         }
18954:     }
18955: #
18956: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18957: #
18958:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18959:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18960:             if ($key eq 'inststatusguest') {
18961:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18962:             } else {
18963:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18964:             }
18965:         }
18966:     }
18967: 
18968:     my @contexts = ('selfcreate');
18969:     @{$cancreate{'selfcreate'}} = ();
18970:     %{$cancreate{'emailusername'}} = ();
18971:     if (@types) {
18972:         @{$cancreate{'statustocreate'}} = ();
18973:     }
18974:     %{$cancreate{'selfcreateprocessing'}} = ();
18975:     %{$cancreate{'shibenv'}} = ();
18976:     %{$cancreate{'emailverified'}} = ();
18977:     %{$cancreate{'emailoptions'}} = ();
18978:     %{$cancreate{'emaildomain'}} = ();
18979:     my %selfcreatetypes = (
18980:                              sso   => 'users authenticated by institutional single sign on',
18981:                              login => 'users authenticated by institutional log-in',
18982:                              email => 'users verified by e-mail',
18983:                           );
18984: #
18985: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18986: # is permitted.
18987: #
18988:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18989: 
18990:     my (@statuses,%email_rule);
18991:     foreach my $item ('login','sso','email') {
18992:         if ($item eq 'email') {
18993:             if ($env{'form.cancreate_email'}) {
18994:                 if (@types) {
18995:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18996:                     foreach my $status (@poss_statuses) {
18997:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18998:                             push(@statuses,$status);
18999:                         }
19000:                     }
19001:                     $save_inststatus{'inststatusguest'} = \@statuses;
19002:                 } else {
19003:                     push(@statuses,'default');
19004:                 }
19005:                 if (@statuses) {
19006:                     my %curr_rule;
19007:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
19008:                         foreach my $type (@statuses) {
19009:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
19010:                         }
19011:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
19012:                         foreach my $type (@statuses) {
19013:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
19014:                         }
19015:                     }
19016:                     push(@{$cancreate{'selfcreate'}},'email');
19017:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
19018:                     my %curremaildom;
19019:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
19020:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
19021:                     }
19022:                     foreach my $type (@statuses) {
19023:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
19024:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
19025:                         }
19026:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
19027:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
19028:                         }
19029:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
19030: #
19031: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
19032: #
19033:                             my $chosen = $1;
19034:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
19035:                                 my $emaildom;
19036:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
19037:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
19038:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
19039:                                     if (ref($curremaildom{$type}) eq 'HASH') {
19040:                                         if (exists($curremaildom{$type}{$chosen})) {
19041:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
19042:                                                 push(@{$changes{'cancreate'}},'emaildomain');
19043:                                             }
19044:                                         } elsif ($emaildom ne '') {
19045:                                             push(@{$changes{'cancreate'}},'emaildomain');
19046:                                         }
19047:                                     } elsif ($emaildom ne '') {
19048:                                         push(@{$changes{'cancreate'}},'emaildomain');
19049:                                     }
19050:                                 }
19051:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19052:                             } elsif ($chosen eq 'custom') {
19053:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
19054:                                 $email_rule{$type} = [];
19055:                                 if (ref($emailrules) eq 'HASH') {
19056:                                     foreach my $rule (@possemail_rules) {
19057:                                         if (exists($emailrules->{$rule})) {
19058:                                             push(@{$email_rule{$type}},$rule);
19059:                                         }
19060:                                     }
19061:                                 }
19062:                                 if (@{$email_rule{$type}}) {
19063:                                     $cancreate{'emailoptions'}{$type} = 'custom';
19064:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
19065:                                         if (@{$curr_rule{$type}} > 0) {
19066:                                             foreach my $rule (@{$curr_rule{$type}}) {
19067:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
19068:                                                     push(@{$changes{'email_rule'}},$type);
19069:                                                 }
19070:                                             }
19071:                                         }
19072:                                         foreach my $type (@{$email_rule{$type}}) {
19073:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
19074:                                                 push(@{$changes{'email_rule'}},$type);
19075:                                             }
19076:                                         }
19077:                                     } else {
19078:                                         push(@{$changes{'email_rule'}},$type);
19079:                                     }
19080:                                 }
19081:                             } else {
19082:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19083:                             }
19084:                         }
19085:                     }
19086:                     if (@types) {
19087:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19088:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
19089:                             if (@changed) {
19090:                                 push(@{$changes{'inststatus'}},'inststatusguest');
19091:                             }
19092:                         } else {
19093:                             push(@{$changes{'inststatus'}},'inststatusguest');
19094:                         }
19095:                     }
19096:                 } else {
19097:                     delete($env{'form.cancreate_email'});
19098:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19099:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19100:                             push(@{$changes{'inststatus'}},'inststatusguest');
19101:                         }
19102:                     }
19103:                 }
19104:             } else {
19105:                 $save_inststatus{'inststatusguest'} = [];
19106:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19107:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19108:                         push(@{$changes{'inststatus'}},'inststatusguest');
19109:                     }
19110:                 }
19111:             }
19112:         } else {
19113:             if ($env{'form.cancreate_'.$item}) {
19114:                 push(@{$cancreate{'selfcreate'}},$item);
19115:             }
19116:         }
19117:     }
19118:     my (%userinfo,%savecaptcha);
19119:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
19120: #
19121: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
19122: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
19123: #
19124: 
19125:     if ($env{'form.cancreate_email'}) {
19126:         push(@contexts,'emailusername');
19127:         if (@statuses) {
19128:             foreach my $type (@statuses) {
19129:                 if (ref($infofields) eq 'ARRAY') {
19130:                     foreach my $field (@{$infofields}) {
19131:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
19132:                             $cancreate{'emailusername'}{$type}{$field} = $1;
19133:                         }
19134:                     }
19135:                 }
19136:             }
19137:         }
19138: #
19139: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
19140: # queued requests for self-creation of account verified by e-mail.
19141: #
19142: 
19143:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
19144:         @approvalnotify = sort(@approvalnotify);
19145:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
19146:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19147:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
19148:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
19149:                     push(@{$changes{'cancreate'}},'notify');
19150:                 }
19151:             } else {
19152:                 if ($cancreate{'notify'}{'approval'}) {
19153:                     push(@{$changes{'cancreate'}},'notify');
19154:                 }
19155:             }
19156:         } elsif ($cancreate{'notify'}{'approval'}) {
19157:             push(@{$changes{'cancreate'}},'notify');
19158:         }
19159: 
19160:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
19161:     }
19162: #  
19163: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
19164: # institutional log-in.
19165: #
19166:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
19167:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
19168:                ($domdefaults{'auth_def'} eq 'localauth'))) {
19169:             $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.').' '.
19170:                           &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.');
19171:         }
19172:     }
19173:     my @fields = ('lastname','firstname','middlename','generation',
19174:                   'permanentemail','id');
19175:     my @shibfields = (@fields,'inststatus');
19176:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19177: #
19178: # Where usernames may created for institutional log-in and/or institutional single sign on:
19179: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
19180: # may self-create accounts 
19181: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
19182: # which the user may supply, if institutional data is unavailable.
19183: #
19184:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
19185:         if (@types) {
19186:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
19187:             push(@contexts,'statustocreate');
19188:             foreach my $type (@types) {
19189:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
19190:                 foreach my $field (@fields) {
19191:                     if (grep(/^\Q$field\E$/,@modifiable)) {
19192:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
19193:                     } else {
19194:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
19195:                     }
19196:                 }
19197:             }
19198:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
19199:                 foreach my $type (@types) {
19200:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
19201:                         foreach my $field (@fields) {
19202:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
19203:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
19204:                                 push(@{$changes{'selfcreate'}},$type);
19205:                                 last;
19206:                             }
19207:                         }
19208:                     }
19209:                 }
19210:             } else {
19211:                 foreach my $type (@types) {
19212:                     push(@{$changes{'selfcreate'}},$type);
19213:                 }
19214:             }
19215:         }
19216:         foreach my $field (@shibfields) {
19217:             if ($env{'form.shibenv_'.$field} ne '') {
19218:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
19219:             }
19220:         }
19221:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19222:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
19223:                 foreach my $field (@shibfields) {
19224:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
19225:                         push(@{$changes{'cancreate'}},'shibenv');
19226:                     }
19227:                 }
19228:             } else {
19229:                 foreach my $field (@shibfields) {
19230:                     if ($env{'form.shibenv_'.$field}) {
19231:                         push(@{$changes{'cancreate'}},'shibenv');
19232:                         last;
19233:                     }
19234:                 }
19235:             }
19236:         }
19237:     }
19238:     foreach my $item (@contexts) {
19239:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
19240:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
19241:                 if (ref($cancreate{$item}) eq 'ARRAY') {
19242:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
19243:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19244:                             push(@{$changes{'cancreate'}},$item);
19245:                         }
19246:                     }
19247:                 }
19248:             }
19249:             if (ref($cancreate{$item}) eq 'ARRAY') {
19250:                 foreach my $type (@{$cancreate{$item}}) {
19251:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
19252:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19253:                             push(@{$changes{'cancreate'}},$item);
19254:                         }
19255:                     }
19256:                 }
19257:             }
19258:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
19259:             if (ref($cancreate{$item}) eq 'HASH') {
19260:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
19261:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19262:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
19263:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19264:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19265:                                     push(@{$changes{'cancreate'}},$item);
19266:                                 }
19267:                             }
19268:                         }
19269:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19270:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19271:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19272:                                 push(@{$changes{'cancreate'}},$item);
19273:                             }
19274:                         }
19275:                     }
19276:                 }
19277:                 foreach my $type (keys(%{$cancreate{$item}})) {
19278:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19279:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19280:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19281:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19282:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19283:                                         push(@{$changes{'cancreate'}},$item);
19284:                                     }
19285:                                 }
19286:                             } else {
19287:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19288:                                     push(@{$changes{'cancreate'}},$item);
19289:                                 }
19290:                             }
19291:                         }
19292:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19293:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19294:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19295:                                 push(@{$changes{'cancreate'}},$item);
19296:                             }
19297:                         }
19298:                     }
19299:                 }
19300:             }
19301:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
19302:             if (ref($cancreate{$item}) eq 'ARRAY') {
19303:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
19304:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19305:                         push(@{$changes{'cancreate'}},$item);
19306:                     }
19307:                 }
19308:             }
19309:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19310:             if (ref($cancreate{$item}) eq 'HASH') {
19311:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19312:                     push(@{$changes{'cancreate'}},$item);
19313:                 }
19314:             }
19315:         } elsif ($item eq 'emailusername') {
19316:             if (ref($cancreate{$item}) eq 'HASH') {
19317:                 foreach my $type (keys(%{$cancreate{$item}})) {
19318:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19319:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19320:                             if ($cancreate{$item}{$type}{$field}) {
19321:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19322:                                     push(@{$changes{'cancreate'}},$item);
19323:                                 }
19324:                                 last;
19325:                             }
19326:                         }
19327:                     }
19328:                 }
19329:             }
19330:         }
19331:     }
19332: #
19333: # Populate %save_usercreate hash with updates to self-creation configuration.
19334: #
19335:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
19336:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
19337:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
19338:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
19339:     if (ref($cancreate{'notify'}) eq 'HASH') {
19340:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
19341:     }
19342:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
19343:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
19344:     }
19345:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
19346:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
19347:     }
19348:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
19349:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
19350:     }
19351:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
19352:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
19353:     }
19354:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19355:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
19356:     }
19357:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
19358:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
19359:     }
19360:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
19361:     $save_usercreate{'email_rule'} = \%email_rule;
19362: 
19363:     my %userconfig_hash = (
19364:             usercreation     => \%save_usercreate,
19365:             usermodification => \%save_usermodify,
19366:             inststatus       => \%save_inststatus,
19367:     );
19368: 
19369:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
19370:                                              $dom);
19371: #
19372: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
19373: #
19374:     if ($putresult eq 'ok') {
19375:         if (keys(%changes) > 0) {
19376:             $resulttext = &mt('Changes made:').'<ul>';
19377:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
19378:                 my %lt = &selfcreation_types();
19379:                 foreach my $type (@{$changes{'cancreate'}}) {
19380:                     my $chgtext = '';
19381:                     if ($type eq 'selfcreate') {
19382:                         if (@{$cancreate{$type}} == 0) {
19383:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
19384:                         } else {
19385:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
19386:                                         '<ul>';
19387:                             foreach my $case (@{$cancreate{$type}}) {
19388:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
19389:                             }
19390:                             $chgtext .= '</ul>';
19391:                             if (ref($cancreate{$type}) eq 'ARRAY') {
19392:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
19393:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19394:                                         if (@{$cancreate{'statustocreate'}} == 0) {
19395:                                             $chgtext .= '<span class="LC_warning">'.
19396:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
19397:                                                         '</span><br />';
19398:                                         }
19399:                                     }
19400:                                 }
19401:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
19402:                                     if (!@statuses) {
19403:                                         $chgtext .= '<span class="LC_warning">'.
19404:                                                     &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.").
19405:                                                     '</span><br />';
19406: 
19407:                                     }
19408:                                 }
19409:                             }
19410:                         }
19411:                     } elsif ($type eq 'shibenv') {
19412:                         if (keys(%{$cancreate{$type}}) == 0) {
19413:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
19414:                         } else {
19415:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
19416:                                         '<ul>';
19417:                             foreach my $field (@shibfields) {
19418:                                 next if ($cancreate{$type}{$field} eq '');
19419:                                 if ($field eq 'inststatus') {
19420:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
19421:                                 } else {
19422:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
19423:                                 }
19424:                             }
19425:                             $chgtext .= '</ul>';
19426:                         }
19427:                     } elsif ($type eq 'statustocreate') {
19428:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
19429:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
19430:                             if (@{$cancreate{'selfcreate'}} > 0) {
19431:                                 if (@{$cancreate{'statustocreate'}} == 0) {
19432:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
19433:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
19434:                                         $chgtext .= '<br />'.
19435:                                                     '<span class="LC_warning">'.
19436:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
19437:                                                     '</span>';
19438:                                     }
19439:                                 } elsif (keys(%usertypes) > 0) {
19440:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19441:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
19442:                                     } else {
19443:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
19444:                                     }
19445:                                     $chgtext .= '<ul>';
19446:                                     foreach my $case (@{$cancreate{$type}}) {
19447:                                         if ($case eq 'default') {
19448:                                             $chgtext .= '<li>'.$othertitle.'</li>';
19449:                                         } else {
19450:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
19451:                                         }
19452:                                     }
19453:                                     $chgtext .= '</ul>';
19454:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19455:                                         $chgtext .= '<span class="LC_warning">'.
19456:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
19457:                                                     '</span>';
19458:                                     }
19459:                                 }
19460:                             } else {
19461:                                 if (@{$cancreate{$type}} == 0) {
19462:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
19463:                                 } else {
19464:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
19465:                                 }
19466:                             }
19467:                             $chgtext .= '<br />';
19468:                         }
19469:                     } elsif ($type eq 'selfcreateprocessing') {
19470:                         my %choices = &Apache::lonlocal::texthash (
19471:                                                                     automatic => 'Automatic approval',
19472:                                                                     approval  => 'Queued for approval',
19473:                                                                   );
19474:                         if (@types) {
19475:                             if (@statuses) {
19476:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
19477:                                             '<ul>';
19478:                                 foreach my $status (@statuses) {
19479:                                     if ($status eq 'default') {
19480:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19481:                                     } else {
19482:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19483:                                     }
19484:                                 }
19485:                                 $chgtext .= '</ul>';
19486:                             }
19487:                         } else {
19488:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
19489:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
19490:                         }
19491:                     } elsif ($type eq 'emailverified') {
19492:                         my %options = &Apache::lonlocal::texthash (
19493:                                                                     all   => 'Same as e-mail',
19494:                                                                     first => 'Omit @domain',
19495:                                                                     free  => 'Free to choose',
19496:                                                                   );
19497:                         if (@types) {
19498:                             if (@statuses) {
19499:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
19500:                                             '<ul>';
19501:                                 foreach my $status (@statuses) {
19502:                                     if ($status eq 'default') {
19503:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19504:                                     } else {
19505:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19506:                                     }
19507:                                 }
19508:                                 $chgtext .= '</ul>';
19509:                             }
19510:                         } else {
19511:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
19512:                                             $options{$cancreate{'emailverified'}{'default'}});
19513:                         }
19514:                     } elsif ($type eq 'emailoptions') {
19515:                         my %options = &Apache::lonlocal::texthash (
19516:                                                                     any     => 'Any e-mail',
19517:                                                                     inst    => 'Institutional only',
19518:                                                                     noninst => 'Non-institutional only',
19519:                                                                     custom  => 'Custom restrictions',
19520:                                                                   );
19521:                         if (@types) {
19522:                             if (@statuses) {
19523:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
19524:                                             '<ul>';
19525:                                 foreach my $status (@statuses) {
19526:                                     if ($type eq 'default') {
19527:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19528:                                     } else {
19529:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19530:                                     }
19531:                                 }
19532:                                 $chgtext .= '</ul>';
19533:                             }
19534:                         } else {
19535:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
19536:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
19537:                             } else {
19538:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
19539:                                                 $options{$cancreate{'emailoptions'}{'default'}});
19540:                             }
19541:                         }
19542:                     } elsif ($type eq 'emaildomain') {
19543:                         my $output;
19544:                         if (@statuses) {
19545:                             foreach my $type (@statuses) {
19546:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
19547:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
19548:                                         if ($type eq 'default') {
19549:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19550:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19551:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19552:                                             } else {
19553:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
19554:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19555:                                             }
19556:                                         } else {
19557:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19558:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19559:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19560:                                             } else {
19561:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
19562:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19563:                                             }
19564:                                         }
19565:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
19566:                                         if ($type eq 'default') {
19567:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19568:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19569:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19570:                                             } else {
19571:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
19572:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19573:                                             }
19574:                                         } else {
19575:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19576:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19577:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19578:                                             } else {
19579:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
19580:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19581:                                             }
19582:                                         }
19583:                                     }
19584:                                 }
19585:                             }
19586:                         }
19587:                         if ($output ne '') {
19588:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
19589:                                         '<ul>'.$output.'</ul>';
19590:                         }
19591:                     } elsif ($type eq 'captcha') {
19592:                         if ($savecaptcha{$type} eq 'notused') {
19593:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
19594:                         } else {
19595:                             my %captchas = &captcha_phrases();
19596:                             if ($captchas{$savecaptcha{$type}}) {
19597:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
19598:                             } else {
19599:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
19600:                             }
19601:                         }
19602:                     } elsif ($type eq 'recaptchakeys') {
19603:                         my ($privkey,$pubkey);
19604:                         if (ref($savecaptcha{$type}) eq 'HASH') {
19605:                             $pubkey = $savecaptcha{$type}{'public'};
19606:                             $privkey = $savecaptcha{$type}{'private'};
19607:                         }
19608:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
19609:                         if (!$pubkey) {
19610:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
19611:                         } else {
19612:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
19613:                         }
19614:                         if (!$privkey) {
19615:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
19616:                         } else {
19617:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
19618:                         }
19619:                         $chgtext .= '</ul>';
19620:                     } elsif ($type eq 'recaptchaversion') {
19621:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
19622:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
19623:                         }
19624:                     } elsif ($type eq 'emailusername') {
19625:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
19626:                             if (@statuses) {
19627:                                 foreach my $type (@statuses) {
19628:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
19629:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
19630:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
19631:                                                     '<ul>';
19632:                                             foreach my $field (@{$infofields}) {
19633:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
19634:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
19635:                                                 }
19636:                                             }
19637:                                             $chgtext .= '</ul>';
19638:                                         } else {
19639:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19640:                                         }
19641:                                     } else {
19642:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19643:                                     }
19644:                                 }
19645:                             }
19646:                         }
19647:                     } elsif ($type eq 'notify') {
19648:                         my $numapprove = 0;
19649:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
19650:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
19651:                                 if ($cancreate{'notify'}{'approval'}) {
19652:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
19653:                                     $numapprove ++;
19654:                                 }
19655:                             }
19656:                         }
19657:                         unless ($numapprove) {
19658:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
19659:                         }
19660:                     }
19661:                     if ($chgtext) {
19662:                         $resulttext .= '<li>'.$chgtext.'</li>';
19663:                     }
19664:                 }
19665:             }
19666:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
19667:                 my ($emailrules,$emailruleorder) =
19668:                     &Apache::lonnet::inst_userrules($dom,'email');
19669:                 foreach my $type (@{$changes{'email_rule'}}) {
19670:                     if (ref($email_rule{$type}) eq 'ARRAY') {
19671:                         my $chgtext = '<ul>';
19672:                         foreach my $rule (@{$email_rule{$type}}) {
19673:                             if (ref($emailrules->{$rule}) eq 'HASH') {
19674:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
19675:                             }
19676:                         }
19677:                         $chgtext .= '</ul>';
19678:                         my $typename;
19679:                         if (@types) {
19680:                             if ($type eq 'default') {
19681:                                 $typename = $othertitle;
19682:                             } else {
19683:                                 $typename = $usertypes{$type};
19684:                             }
19685:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
19686:                         }
19687:                         if (@{$email_rule{$type}} > 0) {
19688:                             $resulttext .= '<li>'.
19689:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
19690:                                                $usertypes{$type}).
19691:                                            $chgtext.
19692:                                            '</li>';
19693:                         } else {
19694:                             $resulttext .= '<li>'.
19695:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
19696:                                            '</li>'.
19697:                                            &mt('(Affiliation: [_1])',$typename);
19698:                         }
19699:                     }
19700:                 }
19701:             }
19702:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
19703:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
19704:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
19705:                         my $chgtext = '<ul>';
19706:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
19707:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
19708:                         }
19709:                         $chgtext .= '</ul>';
19710:                         $resulttext .= '<li>'.
19711:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
19712:                                           $chgtext.
19713:                                        '</li>';
19714:                     } else {
19715:                         $resulttext .= '<li>'.
19716:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
19717:                                        '</li>';
19718:                     }
19719:                 }
19720:             }
19721:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
19722:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
19723:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19724:                 foreach my $type (@{$changes{'selfcreate'}}) {
19725:                     my $typename = $type;
19726:                     if (keys(%usertypes) > 0) {
19727:                         if ($usertypes{$type} ne '') {
19728:                             $typename = $usertypes{$type};
19729:                         }
19730:                     }
19731:                     my @modifiable;
19732:                     $resulttext .= '<li>'.
19733:                                     &mt('Self-creation of account by users with status: [_1]',
19734:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
19735:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
19736:                     foreach my $field (@fields) {
19737:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
19738:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
19739:                         }
19740:                     }
19741:                     if (@modifiable > 0) {
19742:                         $resulttext .= join(', ',@modifiable);
19743:                     } else {
19744:                         $resulttext .= &mt('none');
19745:                     }
19746:                     $resulttext .= '</li>';
19747:                 }
19748:                 $resulttext .= '</ul></li>';
19749:             }
19750:             $resulttext .= '</ul>';
19751:             my $cachetime = 24*60*60;
19752:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
19753:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19754:             if (ref($lastactref) eq 'HASH') {
19755:                 $lastactref->{'domdefaults'} = 1;
19756:             }
19757:         } else {
19758:             $resulttext = &mt('No changes made to self-creation settings');
19759:         }
19760:     } else {
19761:         $resulttext = '<span class="LC_error">'.
19762:             &mt('An error occurred: [_1]',$putresult).'</span>';
19763:     }
19764:     if ($warningmsg ne '') {
19765:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19766:     }
19767:     return $resulttext;
19768: }
19769: 
19770: sub process_captcha {
19771:     my ($container,$changes,$newsettings,$currsettings) = @_;
19772:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19773:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19774:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19775:         $newsettings->{'captcha'} = 'original';
19776:     }
19777:     my %current;
19778:     if (ref($currsettings) eq 'HASH') {
19779:         %current = %{$currsettings};
19780:     }
19781:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19782:         if ($container eq 'cancreate') {
19783:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19784:                 push(@{$changes->{'cancreate'}},'captcha');
19785:             } elsif (!defined($changes->{'cancreate'})) {
19786:                 $changes->{'cancreate'} = ['captcha'];
19787:             }
19788:         } elsif ($container eq 'passwords') {
19789:             $changes->{'reset'} = 1;
19790:         } else {
19791:             $changes->{'captcha'} = 1;
19792:         }
19793:     }
19794:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19795:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19796:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19797:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19798:         $newpub =~ s/[^\w\-]//g;
19799:         $newpriv =~ s/[^\w\-]//g;
19800:         $newsettings->{'recaptchakeys'} = {
19801:                                              public  => $newpub,
19802:                                              private => $newpriv,
19803:                                           };
19804:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19805:         $newversion =~ s/\D//g;
19806:         if ($newversion ne '2') {
19807:             $newversion = 1;
19808:         }
19809:         $newsettings->{'recaptchaversion'} = $newversion;
19810:     }
19811:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19812:         $currpub = $current{'recaptchakeys'}{'public'};
19813:         $currpriv = $current{'recaptchakeys'}{'private'};
19814:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19815:             $newsettings->{'recaptchakeys'} = {
19816:                                                  public  => '',
19817:                                                  private => '',
19818:                                               }
19819:         }
19820:     }
19821:     if ($current{'captcha'} eq 'recaptcha') {
19822:         $currversion = $current{'recaptchaversion'};
19823:         if ($currversion ne '2') {
19824:             $currversion = 1;
19825:         }
19826:     }
19827:     if ($currversion ne $newversion) {
19828:         if ($container eq 'cancreate') {
19829:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19830:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19831:             } elsif (!defined($changes->{'cancreate'})) {
19832:                 $changes->{'cancreate'} = ['recaptchaversion'];
19833:             }
19834:         } elsif ($container eq 'passwords') {
19835:             $changes->{'reset'} = 1;
19836:         } else {
19837:             $changes->{'recaptchaversion'} = 1;
19838:         }
19839:     }
19840:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19841:         if ($container eq 'cancreate') {
19842:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19843:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19844:             } elsif (!defined($changes->{'cancreate'})) {
19845:                 $changes->{'cancreate'} = ['recaptchakeys'];
19846:             }
19847:         } elsif ($container eq 'passwords') {
19848:             $changes->{'reset'} = 1;
19849:         } else {
19850:             $changes->{'recaptchakeys'} = 1;
19851:         }
19852:     }
19853:     return;
19854: }
19855: 
19856: sub modify_usermodification {
19857:     my ($dom,%domconfig) = @_;
19858:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19859:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19860:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19861:             if ($key eq 'selfcreate') {
19862:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19863:             } else {  
19864:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19865:             }
19866:         }
19867:     }
19868:     my @contexts = ('author','coauthor','course');
19869:     my %context_title = (
19870:                            author => 'In author context',
19871:                            coauthor => 'As co-author manager',
19872:                            course => 'In course context',
19873:                         );
19874:     my @fields = ('lastname','firstname','middlename','generation',
19875:                   'permanentemail','id');
19876:     my %roles = (
19877:                   author => ['ca','aa'],
19878:                   coauthor => ['ca','aa'],
19879:                   course => ['st','ep','ta','in','cr'],
19880:                 );
19881:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19882:     foreach my $context (@contexts) {
19883:         my $prefix = 'canmodify';
19884:         if ($context eq 'coauthor') {
19885:             $prefix = 'cacanmodify';
19886:         }
19887:         foreach my $role (@{$roles{$context}}) {
19888:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$role);
19889:             foreach my $item (@fields) {
19890:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19891:                     $modifyhash{$context}{$role}{$item} = 1;
19892:                 } else {
19893:                     $modifyhash{$context}{$role}{$item} = 0;
19894:                 }
19895:             }
19896:         }
19897:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19898:             foreach my $role (@{$roles{$context}}) {
19899:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19900:                     foreach my $field (@fields) {
19901:                         if ($modifyhash{$context}{$role}{$field} ne 
19902:                                 $curr_usermodification{$context}{$role}{$field}) {
19903:                             push(@{$changes{$context}},$role);
19904:                             last;
19905:                         }
19906:                     }
19907:                 }
19908:             }
19909:         } else {
19910:             foreach my $context (@contexts) {
19911:                 foreach my $role (@{$roles{$context}}) {
19912:                     push(@{$changes{$context}},$role);
19913:                 }
19914:             }
19915:         }
19916:     }
19917:     my %usermodification_hash =  (
19918:                                    usermodification => \%modifyhash,
19919:                                  );
19920:     my $putresult = &Apache::lonnet::put_dom('configuration',
19921:                                              \%usermodification_hash,$dom);
19922:     if ($putresult eq 'ok') {
19923:         if (keys(%changes) > 0) {
19924:             $resulttext = &mt('Changes made: ').'<ul>';
19925:             foreach my $context (@contexts) {
19926:                 if (ref($changes{$context}) eq 'ARRAY') {
19927:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19928:                     if (ref($changes{$context}) eq 'ARRAY') {
19929:                         foreach my $role (@{$changes{$context}}) {
19930:                             my $rolename;
19931:                             if ($role eq 'cr') {
19932:                                 $rolename = &mt('Custom');
19933:                             } else {
19934:                                 $rolename = &Apache::lonnet::plaintext($role);
19935:                             }
19936:                             my @modifiable;
19937:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19938:                             foreach my $field (@fields) {
19939:                                 if ($modifyhash{$context}{$role}{$field}) {
19940:                                     push(@modifiable,$fieldtitles{$field});
19941:                                 }
19942:                             }
19943:                             if (@modifiable > 0) {
19944:                                 $resulttext .= join(', ',@modifiable);
19945:                             } else {
19946:                                 $resulttext .= &mt('none'); 
19947:                             }
19948:                             $resulttext .= '</li>';
19949:                         }
19950:                         $resulttext .= '</ul></li>';
19951:                     }
19952:                 }
19953:             }
19954:             $resulttext .= '</ul>';
19955:         } else {
19956:             $resulttext = &mt('No changes made to user modification settings');
19957:         }
19958:     } else {
19959:         $resulttext = '<span class="LC_error">'.
19960:             &mt('An error occurred: [_1]',$putresult).'</span>';
19961:     }
19962:     return $resulttext;
19963: }
19964: 
19965: sub modify_defaults {
19966:     my ($dom,$lastactref,%domconfig) = @_;
19967:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19968:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19969:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19970:                  'portal_def');
19971:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19972:     foreach my $item (@items) {
19973:         $newvalues{$item} = $env{'form.'.$item};
19974:         if ($item eq 'auth_def') {
19975:             if ($newvalues{$item} ne '') {
19976:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19977:                     push(@errors,$item);
19978:                 }
19979:             }
19980:         } elsif ($item eq 'lang_def') {
19981:             if ($newvalues{$item} ne '') {
19982:                 if ($newvalues{$item} =~ /^(\w+)/) {
19983:                     my $langcode = $1;
19984:                     if ($langcode ne 'x_chef') {
19985:                         if (code2language($langcode) eq '') {
19986:                             push(@errors,$item);
19987:                         }
19988:                     }
19989:                 } else {
19990:                     push(@errors,$item);
19991:                 }
19992:             }
19993:         } elsif ($item eq 'timezone_def') {
19994:             if ($newvalues{$item} ne '') {
19995:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19996:                     push(@errors,$item);   
19997:                 }
19998:             }
19999:         } elsif ($item eq 'datelocale_def') {
20000:             if ($newvalues{$item} ne '') {
20001:                 my @datelocale_ids = DateTime::Locale->ids();
20002:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
20003:                     push(@errors,$item);
20004:                 }
20005:             }
20006:         } elsif ($item eq 'portal_def') {
20007:             if ($newvalues{$item} ne '') {
20008:                 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])\/?$/) {
20009:                     foreach my $field ('email','web') {
20010:                         if ($env{'form.'.$item.'_'.$field}) {
20011:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
20012:                         }
20013:                     }
20014:                 } else {
20015:                     push(@errors,$item);
20016:                 }
20017:             }
20018:         }
20019:         if (grep(/^\Q$item\E$/,@errors)) {
20020:             $newvalues{$item} = $domdefaults{$item};
20021:             if ($item eq 'portal_def') {
20022:                 if ($domdefaults{$item}) {
20023:                     foreach my $field ('email','web') {
20024:                         if (exists($domdefaults{$item.'_'.$field})) {
20025:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
20026:                         }
20027:                     }
20028:                 }
20029:             }
20030:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
20031:             $changes{$item} = 1;
20032:         }
20033:         if ($item eq 'portal_def') {
20034:             unless (grep(/^\Q$item\E$/,@errors)) {
20035:                 if ($newvalues{$item} eq '') {
20036:                     foreach my $field ('email','web') {
20037:                         if (exists($domdefaults{$item.'_'.$field})) {
20038:                             delete($domdefaults{$item.'_'.$field});
20039:                         }
20040:                     }
20041:                 } else {
20042:                     unless ($changes{$item}) {
20043:                         foreach my $field ('email','web') {
20044:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
20045:                                 $changes{$item} = 1;
20046:                                 last;
20047:                             }
20048:                         }
20049:                     }
20050:                     foreach my $field ('email','web') {
20051:                         if ($newvalues{$item.'_'.$field}) {
20052:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
20053:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
20054:                             delete($domdefaults{$item.'_'.$field});
20055:                         }
20056:                     }
20057:                 }
20058:             }
20059:         }
20060:         $domdefaults{$item} = $newvalues{$item};
20061:     }
20062:     my %staticdefaults = (
20063:                            'intauth_cost'   => 10,
20064:                            'intauth_check'  => 0,
20065:                            'intauth_switch' => 0,
20066:                          );
20067:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
20068:         if (exists($domdefaults{$item})) {
20069:             $newvalues{$item} = $domdefaults{$item};
20070:         } else {
20071:             $newvalues{$item} = $staticdefaults{$item};
20072:         }
20073:     }
20074:     my ($unamemaprules,$ruleorder);
20075:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
20076:     if (@possunamemaprules) {
20077:         ($unamemaprules,$ruleorder) =
20078:             &Apache::lonnet::inst_userrules($dom,'unamemap');
20079:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
20080:             if (@{$ruleorder} > 0) {
20081:                 my %possrules;
20082:                 map { $possrules{$_} = 1; } @possunamemaprules;
20083:                 foreach my $rule (@{$ruleorder}) {
20084:                     if ($possrules{$rule}) {
20085:                         push(@{$newvalues{'unamemap_rule'}},$rule);
20086:                     }
20087:                 }
20088:             }
20089:         }
20090:     }
20091:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
20092:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20093:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
20094:                                                                $newvalues{'unamemap_rule'});
20095:             if (@rulediffs) {
20096:                 $changes{'unamemap_rule'} = 1;
20097:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20098:             }
20099:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
20100:             $changes{'unamemap_rule'} = 1;
20101:             delete($domdefaults{'unamemap_rule'});
20102:         }
20103:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20104:         if (@{$newvalues{'unamemap_rule'}} > 0) {
20105:             $changes{'unamemap_rule'} = 1;
20106:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20107:         }
20108:     }
20109:     my %defaults_hash = (
20110:                          defaults => \%newvalues,
20111:                         );
20112:     my $title = &defaults_titles();
20113: 
20114:     my $currinststatus;
20115:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
20116:         $currinststatus = $domconfig{'inststatus'};
20117:     } else {
20118:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20119:         $currinststatus = {
20120:                              inststatustypes => $usertypes,
20121:                              inststatusorder => $types,
20122:                              inststatusguest => [],
20123:                           };
20124:     }
20125:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
20126:     my @allpos;
20127:     my %alltypes;
20128:     my @inststatusguest;
20129:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
20130:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
20131:             unless (grep(/^\Q$type\E$/,@todelete)) {
20132:                 push(@inststatusguest,$type);
20133:             }
20134:         }
20135:     }
20136:     my ($currtitles,$currorder);
20137:     if (ref($currinststatus) eq 'HASH') {
20138:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
20139:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
20140:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
20141:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
20142:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
20143:                     }
20144:                 }
20145:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
20146:                     my $position = $env{'form.inststatus_pos_'.$type};
20147:                     $position =~ s/\D+//g;
20148:                     $allpos[$position] = $type;
20149:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
20150:                     $alltypes{$type} =~ s/`//g;
20151:                 }
20152:             }
20153:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
20154:             $currtitles =~ s/,$//;
20155:         }
20156:     }
20157:     if ($env{'form.addinststatus'}) {
20158:         my $newtype = $env{'form.addinststatus'};
20159:         $newtype =~ s/\W//g;
20160:         unless (exists($alltypes{$newtype})) {
20161:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
20162:             $alltypes{$newtype} =~ s/`//g; 
20163:             my $position = $env{'form.addinststatus_pos'};
20164:             $position =~ s/\D+//g;
20165:             if ($position ne '') {
20166:                 $allpos[$position] = $newtype;
20167:             }
20168:         }
20169:     }
20170:     my @orderedstatus;
20171:     foreach my $type (@allpos) {
20172:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
20173:             push(@orderedstatus,$type);
20174:         }
20175:     }
20176:     foreach my $type (keys(%alltypes)) {
20177:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
20178:             delete($alltypes{$type});
20179:         }
20180:     }
20181:     $defaults_hash{'inststatus'} = {
20182:                                      inststatustypes => \%alltypes,
20183:                                      inststatusorder => \@orderedstatus,
20184:                                      inststatusguest => \@inststatusguest,
20185:                                    };
20186:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
20187:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
20188:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
20189:         }
20190:     }
20191:     if ($currorder ne join(',',@orderedstatus)) {
20192:         $changes{'inststatus'}{'inststatusorder'} = 1;
20193:     }
20194:     my $newtitles;
20195:     foreach my $item (@orderedstatus) {
20196:         $newtitles .= $alltypes{$item}.',';
20197:     }
20198:     $newtitles =~ s/,$//;
20199:     if ($currtitles ne $newtitles) {
20200:         $changes{'inststatus'}{'inststatustypes'} = 1;
20201:     }
20202:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
20203:                                              $dom);
20204:     if ($putresult eq 'ok') {
20205:         if (keys(%changes) > 0) {
20206:             $resulttext = &mt('Changes made:').'<ul>';
20207:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
20208:             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";
20209:             foreach my $item (sort(keys(%changes))) {
20210:                 if ($item eq 'inststatus') {
20211:                     if (ref($changes{'inststatus'}) eq 'HASH') {
20212:                         if (@orderedstatus) {
20213:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
20214:                             foreach my $type (@orderedstatus) { 
20215:                                 $resulttext .= $alltypes{$type}.', ';
20216:                             }
20217:                             $resulttext =~ s/, $//;
20218:                             $resulttext .= '</li>';
20219:                         } else {
20220:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
20221:                         }
20222:                     }
20223:                 } elsif ($item eq 'unamemap_rule') {
20224:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20225:                         my @rulenames;
20226:                         if (ref($unamemaprules) eq 'HASH') {
20227:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
20228:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
20229:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
20230:                                 }
20231:                             }
20232:                         }
20233:                         if (@rulenames) {
20234:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
20235:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
20236:                                            '</li>';
20237:                         } else {
20238:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
20239:                         }
20240:                     } else {
20241:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
20242:                     }
20243:                 } else {
20244:                     my $value = $env{'form.'.$item};
20245:                     if ($value eq '') {
20246:                         $value = &mt('none');
20247:                     } elsif ($item eq 'auth_def') {
20248:                         my %authnames = &authtype_names();
20249:                         my %shortauth = (
20250:                                           internal   => 'int',
20251:                                           krb4       => 'krb4',
20252:                                           krb5       => 'krb5',
20253:                                           localauth  => 'loc',
20254:                                           lti        => 'lti',
20255:                         );
20256:                         $value = $authnames{$shortauth{$value}};
20257:                     }
20258:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
20259:                     $mailmsgtext .= "$title->{$item} set to $value\n";
20260:                     if ($item eq 'portal_def') {
20261:                         if ($env{'form.'.$item} ne '') {
20262:                             foreach my $field ('email','web') {
20263:                                 $value = $env{'form.'.$item.'_'.$field};
20264:                                 if ($value) {
20265:                                     $value = &mt('Yes');
20266:                                 } else {
20267:                                     $value = &mt('No');
20268:                                 }
20269:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
20270:                             }
20271:                         }
20272:                     }
20273:                 }
20274:             }
20275:             $resulttext .= '</ul>';
20276:             $mailmsgtext .= "\n";
20277:             my $cachetime = 24*60*60;
20278:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20279:             if (ref($lastactref) eq 'HASH') {
20280:                 $lastactref->{'domdefaults'} = 1;
20281:             }
20282:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
20283:                 my $notify = 1;
20284:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
20285:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
20286:                         $notify = 0;
20287:                     }
20288:                 }
20289:                 if ($notify) {
20290:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
20291:                                                "LON-CAPA Domain Settings Change - $dom",
20292:                                                $mailmsgtext);
20293:                 }
20294:             }
20295:         } else {
20296:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
20297:         }
20298:     } else {
20299:         $resulttext = '<span class="LC_error">'.
20300:             &mt('An error occurred: [_1]',$putresult).'</span>';
20301:     }
20302:     if (@errors > 0) {
20303:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
20304:         foreach my $item (@errors) {
20305:             $resulttext .= ' "'.$title->{$item}.'",';
20306:         }
20307:         $resulttext =~ s/,$//;
20308:     }
20309:     return $resulttext;
20310: }
20311: 
20312: sub modify_scantron {
20313:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20314:     my ($resulttext,%confhash,%changes,$errors);
20315:     my $custom = 'custom.tab';
20316:     my $default = 'default.tab';
20317:     my $servadm = $r->dir_config('lonAdmEMail');
20318:     my ($configuserok,$author_ok,$switchserver) =
20319:         &config_check($dom,$confname,$servadm);
20320:     if ($env{'form.scantronformat.filename'} ne '') {
20321:         my $error;
20322:         if ($configuserok eq 'ok') {
20323:             if ($switchserver) {
20324:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
20325:             } else {
20326:                 if ($author_ok eq 'ok') {
20327:                     my $modified = [];
20328:                     my ($result,$scantronurl) =
20329:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
20330:                                                                 $confname,'scantron','','',$custom,
20331:                                                                 $modified);
20332:                     if ($result eq 'ok') {
20333:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
20334:                         $changes{'scantronformat'} = 1;
20335:                         &update_modify_urls($r,$modified);
20336:                     } else {
20337:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
20338:                     }
20339:                 } else {
20340:                     $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);
20341:                 }
20342:             }
20343:         } else {
20344:             $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);
20345:         }
20346:         if ($error) {
20347:             &Apache::lonnet::logthis($error);
20348:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
20349:         }
20350:     }
20351:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20352:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
20353:             if ($env{'form.scantronformat_del'}) {
20354:                 $confhash{'scantron'}{'scantronformat'} = '';
20355:                 $changes{'scantronformat'} = 1;
20356:             } else {
20357:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
20358:             }
20359:         }
20360:     }
20361:     my @options = ('hdr','pad','rem');
20362:     my @fields = &scantroncsv_fields();
20363:     my %titles = &scantronconfig_titles();
20364:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
20365:     my ($newdat,$currdat,%newcol,%currcol);
20366:     if (grep(/^dat$/,@formats)) {
20367:         $confhash{'scantron'}{config}{dat} = 1;
20368:         $newdat = 1;
20369:     } else {
20370:         $newdat = 0;
20371:     }
20372:     if (grep(/^csv$/,@formats)) {
20373:         my %bynum;
20374:         foreach my $field (@fields) {
20375:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
20376:                 my $posscol = $1;
20377:                 if (($posscol < 20) && (!$bynum{$posscol})) {
20378:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
20379:                     $bynum{$posscol} = $field;
20380:                     $newcol{$field} = $posscol;
20381:                 }
20382:             }
20383:         }
20384:         if (keys(%newcol)) {
20385:             foreach my $option (@options) {
20386:                 if ($env{'form.scantroncsv_'.$option}) {
20387:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
20388:                 }
20389:             }
20390:         }
20391:     }
20392:     $currdat = 1;
20393:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20394:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
20395:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
20396:                 $currdat = 0;
20397:             }
20398:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20399:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20400:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
20401:                 }
20402:             }
20403:         }
20404:     }
20405:     if ($currdat != $newdat) {
20406:         $changes{'config'} = 1;
20407:     } else {
20408:         foreach my $field (@fields) {
20409:             if ($currcol{$field} ne '') {
20410:                 if ($currcol{$field} ne $newcol{$field}) {
20411:                     $changes{'config'} = 1;
20412:                     last;
20413:                 }
20414:             } elsif ($newcol{$field} ne '') {
20415:                 $changes{'config'} = 1;
20416:                 last;
20417:             }
20418:         }
20419:     }
20420:     if (keys(%confhash) > 0) {
20421:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
20422:                                                  $dom);
20423:         if ($putresult eq 'ok') {
20424:             if (keys(%changes) > 0) {
20425:                 if (ref($confhash{'scantron'}) eq 'HASH') {
20426:                     $resulttext = &mt('Changes made:').'<ul>';
20427:                     if ($changes{'scantronformat'}) {
20428:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
20429:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
20430:                         } else {
20431:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
20432:                         }
20433:                     }
20434:                     if ($changes{'config'}) {
20435:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
20436:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
20437:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
20438:                             }
20439:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20440:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20441:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
20442:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
20443:                                         foreach my $field (@fields) {
20444:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
20445:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
20446:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
20447:                                             }
20448:                                         }
20449:                                         $resulttext .= '</ul></li>';
20450:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
20451:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
20452:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
20453:                                                 foreach my $option (@options) {
20454:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
20455:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
20456:                                                     }
20457:                                                 }
20458:                                                 $resulttext .= '</ul></li>';
20459:                                             }
20460:                                         }
20461:                                     }
20462:                                 }
20463:                             }
20464:                         } else {
20465:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
20466:                         }
20467:                     }
20468:                     $resulttext .= '</ul>';
20469:                 } else {
20470:                     $resulttext = &mt('Changes made to bubblesheet format file.');
20471:                 }
20472:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
20473:                 if (ref($lastactref) eq 'HASH') {
20474:                     $lastactref->{'domainconfig'} = 1;
20475:                 }
20476:             } else {
20477:                 $resulttext = &mt('No changes made to bubblesheet format settings');
20478:             }
20479:         } else {
20480:             $resulttext = '<span class="LC_error">'.
20481:                 &mt('An error occurred: [_1]',$putresult).'</span>';
20482:         }
20483:     } else {
20484:         $resulttext = &mt('No changes made to bubblesheet format file'); 
20485:     }
20486:     if ($errors) {
20487:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
20488:                        $errors.'</ul></p>';
20489:     }
20490:     return $resulttext;
20491: }
20492: 
20493: sub modify_coursecategories {
20494:     my ($dom,$lastactref,%domconfig) = @_;
20495:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
20496:         $cathash);
20497:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
20498:     my @catitems = ('unauth','auth');
20499:     my @cattypes = ('std','domonly','codesrch','none');
20500:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20501:         $cathash = $domconfig{'coursecategories'}{'cats'};
20502:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
20503:             $changes{'togglecats'} = 1;
20504:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
20505:         }
20506:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
20507:             $changes{'categorize'} = 1;
20508:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
20509:         }
20510:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
20511:             $changes{'togglecatscomm'} = 1;
20512:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
20513:         }
20514:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
20515:             $changes{'categorizecomm'} = 1;
20516:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
20517: 
20518:         }
20519:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
20520:             $changes{'togglecatsplace'} = 1;
20521:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
20522:         }
20523:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
20524:             $changes{'categorizeplace'} = 1;
20525:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
20526:         }
20527:         foreach my $item (@catitems) {
20528:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20529:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
20530:                     $changes{$item} = 1;
20531:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20532:                 }
20533:             }
20534:         }
20535:     } else {
20536:         $changes{'togglecats'} = 1;
20537:         $changes{'categorize'} = 1;
20538:         $changes{'togglecatscomm'} = 1;
20539:         $changes{'categorizecomm'} = 1;
20540:         $changes{'togglecatsplace'} = 1;
20541:         $changes{'categorizeplace'} = 1;
20542:         $domconfig{'coursecategories'} = {
20543:                                              togglecats => $env{'form.togglecats'},
20544:                                              categorize => $env{'form.categorize'},
20545:                                              togglecatscomm => $env{'form.togglecatscomm'},
20546:                                              categorizecomm => $env{'form.categorizecomm'},
20547:                                              togglecatsplace => $env{'form.togglecatsplace'},
20548:                                              categorizeplace => $env{'form.categorizeplace'},
20549:                                          };
20550:         foreach my $item (@catitems) {
20551:             if ($env{'form.coursecat_'.$item} ne 'std') {
20552:                 $changes{$item} = 1;
20553:             }
20554:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20555:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20556:             }
20557:         }
20558:     }
20559:     if (ref($cathash) eq 'HASH') {
20560:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
20561:             push (@deletecategory,'instcode::0');
20562:         }
20563:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
20564:             push(@deletecategory,'communities::0');
20565:         }
20566:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
20567:             push(@deletecategory,'placement::0');
20568:         }
20569:     }
20570:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
20571:     if (ref($cathash) eq 'HASH') {
20572:         if (@deletecategory > 0) {
20573:             #FIXME Need to remove category from all courses using a deleted category 
20574:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
20575:             foreach my $item (@deletecategory) {
20576:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
20577:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
20578:                     $deletions{$item} = 1;
20579:                     &recurse_cat_deletes($item,$cathash,\%deletions);
20580:                 }
20581:             }
20582:         }
20583:         foreach my $item (keys(%{$cathash})) {
20584:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20585:             if ($cathash->{$item} ne $env{'form.'.$item}) {
20586:                 $reorderings{$item} = 1;
20587:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
20588:             }
20589:             if ($env{'form.addcategory_name_'.$item} ne '') {
20590:                 my $newcat = $env{'form.addcategory_name_'.$item};
20591:                 my $newdepth = $depth+1;
20592:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20593:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
20594:                 $adds{$newitem} = 1; 
20595:             }
20596:             if ($env{'form.subcat_'.$item} ne '') {
20597:                 my $newcat = $env{'form.subcat_'.$item};
20598:                 my $newdepth = $depth+1;
20599:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20600:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
20601:                 $adds{$newitem} = 1;
20602:             }
20603:         }
20604:     }
20605:     if ($env{'form.instcode'} eq '1') {
20606:         if (ref($cathash) eq 'HASH') {
20607:             my $newitem = 'instcode::0';
20608:             if ($cathash->{$newitem} eq '') {  
20609:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20610:                 $adds{$newitem} = 1;
20611:             }
20612:         } else {
20613:             my $newitem = 'instcode::0';
20614:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20615:             $adds{$newitem} = 1;
20616:         }
20617:     }
20618:     if ($env{'form.communities'} eq '1') {
20619:         if (ref($cathash) eq 'HASH') {
20620:             my $newitem = 'communities::0';
20621:             if ($cathash->{$newitem} eq '') {
20622:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20623:                 $adds{$newitem} = 1;
20624:             }
20625:         } else {
20626:             my $newitem = 'communities::0';
20627:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20628:             $adds{$newitem} = 1;
20629:         }
20630:     }
20631:     if ($env{'form.placement'} eq '1') {
20632:         if (ref($cathash) eq 'HASH') {
20633:             my $newitem = 'placement::0';
20634:             if ($cathash->{$newitem} eq '') {
20635:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20636:                 $adds{$newitem} = 1;
20637:             }
20638:         } else {
20639:             my $newitem = 'placement::0';
20640:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20641:             $adds{$newitem} = 1;
20642:         }
20643:     }
20644:     if ($env{'form.addcategory_name'} ne '') {
20645:         if (($env{'form.addcategory_name'} ne 'instcode') &&
20646:             ($env{'form.addcategory_name'} ne 'communities') &&
20647:             ($env{'form.addcategory_name'} ne 'placement')) {
20648:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
20649:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
20650:             $adds{$newitem} = 1;
20651:         }
20652:     }
20653:     my $putresult;
20654:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20655:         if (keys(%deletions) > 0) {
20656:             foreach my $key (keys(%deletions)) {
20657:                 if ($predelallitems{$key} ne '') {
20658:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
20659:                 }
20660:             }
20661:         }
20662:         my (@chkcats,@chktrails,%chkallitems);
20663:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
20664:         if (ref($chkcats[0]) eq 'ARRAY') {
20665:             my $depth = 0;
20666:             my $chg = 0;
20667:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
20668:                 my $name = $chkcats[0][$i];
20669:                 my $item;
20670:                 if ($name eq '') {
20671:                     $chg ++;
20672:                 } else {
20673:                     $item = &escape($name).'::0';
20674:                     if ($chg) {
20675:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
20676:                     }
20677:                     $depth ++; 
20678:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
20679:                     $depth --;
20680:                 }
20681:             }
20682:         }
20683:     }
20684:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20685:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
20686:         if ($putresult eq 'ok') {
20687:             my %title = (
20688:                          togglecats     => 'Show/Hide a course in catalog',
20689:                          categorize     => 'Assign a category to a course',
20690:                          togglecatscomm => 'Show/Hide a community in catalog',
20691:                          categorizecomm => 'Assign a category to a community',
20692:                         );
20693:             my %level = (
20694:                          dom  => 'set in Domain ("Modify Course/Community")',
20695:                          crs  => 'set in Course ("Course Configuration")',
20696:                          comm => 'set in Community ("Community Configuration")',
20697:                          none     => 'No catalog',
20698:                          std      => 'Standard catalog',
20699:                          domonly  => 'Domain-only catalog',
20700:                          codesrch => 'Code search form',
20701:                         );
20702:             $resulttext = &mt('Changes made:').'<ul>';
20703:             if ($changes{'togglecats'}) {
20704:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
20705:             }
20706:             if ($changes{'categorize'}) {
20707:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
20708:             }
20709:             if ($changes{'togglecatscomm'}) {
20710:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
20711:             }
20712:             if ($changes{'categorizecomm'}) {
20713:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
20714:             }
20715:             if ($changes{'unauth'}) {
20716:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
20717:             }
20718:             if ($changes{'auth'}) {
20719:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
20720:             }
20721:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20722:                 my $cathash;
20723:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20724:                     $cathash = $domconfig{'coursecategories'}{'cats'};
20725:                 } else {
20726:                     $cathash = {};
20727:                 } 
20728:                 my (@cats,@trails,%allitems);
20729:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
20730:                 if (keys(%deletions) > 0) {
20731:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
20732:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
20733:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
20734:                     }
20735:                     $resulttext .= '</ul></li>';
20736:                 }
20737:                 if (keys(%reorderings) > 0) {
20738:                     my %sort_by_trail;
20739:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
20740:                     foreach my $key (keys(%reorderings)) {
20741:                         if ($allitems{$key} ne '') {
20742:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20743:                         }
20744:                     }
20745:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20746:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20747:                     }
20748:                     $resulttext .= '</ul></li>';
20749:                 }
20750:                 if (keys(%adds) > 0) {
20751:                     my %sort_by_trail;
20752:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
20753:                     foreach my $key (keys(%adds)) {
20754:                         if ($allitems{$key} ne '') {
20755:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20756:                         }
20757:                     }
20758:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20759:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20760:                     }
20761:                     $resulttext .= '</ul></li>';
20762:                 }
20763:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20764:                 if (ref($lastactref) eq 'HASH') {
20765:                     $lastactref->{'cats'} = 1;
20766:                 }
20767:             }
20768:             $resulttext .= '</ul>';
20769:             if ($changes{'unauth'} || $changes{'auth'}) {
20770:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20771:                 if ($changes{'auth'}) {
20772:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20773:                 }
20774:                 if ($changes{'unauth'}) {
20775:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20776:                 }
20777:                 my $cachetime = 24*60*60;
20778:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20779:                 if (ref($lastactref) eq 'HASH') {
20780:                     $lastactref->{'domdefaults'} = 1;
20781:                 }
20782:             }
20783:         } else {
20784:             $resulttext = '<span class="LC_error">'.
20785:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20786:         }
20787:     } else {
20788:         $resulttext = &mt('No changes made to course and community categories');
20789:     }
20790:     return $resulttext;
20791: }
20792: 
20793: sub modify_serverstatuses {
20794:     my ($dom,%domconfig) = @_;
20795:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20796:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20797:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20798:     }
20799:     my @pages = &serverstatus_pages();
20800:     foreach my $type (@pages) {
20801:         $newserverstatus{$type}{'namedusers'} = '';
20802:         $newserverstatus{$type}{'machines'} = '';
20803:         if (defined($env{'form.'.$type.'_namedusers'})) {
20804:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20805:             my @okusers;
20806:             foreach my $user (@users) {
20807:                 my ($uname,$udom) = split(/:/,$user);
20808:                 if (($udom =~ /^$match_domain$/) &&   
20809:                     (&Apache::lonnet::domain($udom)) &&
20810:                     ($uname =~ /^$match_username$/)) {
20811:                     if (!grep(/^\Q$user\E/,@okusers)) {
20812:                         push(@okusers,$user);
20813:                     }
20814:                 }
20815:             }
20816:             if (@okusers > 0) {
20817:                  @okusers = sort(@okusers);
20818:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20819:             }
20820:         }
20821:         if (defined($env{'form.'.$type.'_machines'})) {
20822:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20823:             my @okmachines;
20824:             foreach my $ip (@machines) {
20825:                 my @parts = split(/\./,$ip);
20826:                 next if (@parts < 4);
20827:                 my $badip = 0;
20828:                 for (my $i=0; $i<4; $i++) {
20829:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20830:                         $badip = 1;
20831:                         last;
20832:                     }
20833:                 }
20834:                 if (!$badip) {
20835:                     push(@okmachines,$ip);     
20836:                 }
20837:             }
20838:             @okmachines = sort(@okmachines);
20839:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20840:         }
20841:     }
20842:     my %serverstatushash =  (
20843:                                 serverstatuses => \%newserverstatus,
20844:                             );
20845:     foreach my $type (@pages) {
20846:         foreach my $setting ('namedusers','machines') {
20847:             my (@current,@new);
20848:             if (ref($currserverstatus{$type}) eq 'HASH') {
20849:                 if ($currserverstatus{$type}{$setting} ne '') { 
20850:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20851:                 }
20852:             }
20853:             if ($newserverstatus{$type}{$setting} ne '') {
20854:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20855:             }
20856:             if (@current > 0) {
20857:                 if (@new > 0) {
20858:                     foreach my $item (@current) {
20859:                         if (!grep(/^\Q$item\E$/,@new)) {
20860:                             $changes{$type}{$setting} = 1;
20861:                             last;
20862:                         }
20863:                     }
20864:                     foreach my $item (@new) {
20865:                         if (!grep(/^\Q$item\E$/,@current)) {
20866:                             $changes{$type}{$setting} = 1;
20867:                             last;
20868:                         }
20869:                     }
20870:                 } else {
20871:                     $changes{$type}{$setting} = 1;
20872:                 }
20873:             } elsif (@new > 0) {
20874:                 $changes{$type}{$setting} = 1;
20875:             }
20876:         }
20877:     }
20878:     if (keys(%changes) > 0) {
20879:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20880:         my $putresult = &Apache::lonnet::put_dom('configuration',
20881:                                                  \%serverstatushash,$dom);
20882:         if ($putresult eq 'ok') {
20883:             $resulttext .= &mt('Changes made:').'<ul>';
20884:             foreach my $type (@pages) {
20885:                 if (ref($changes{$type}) eq 'HASH') {
20886:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20887:                     if ($changes{$type}{'namedusers'}) {
20888:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20889:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20890:                         } else {
20891:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20892:                         }
20893:                     }
20894:                     if ($changes{$type}{'machines'}) {
20895:                         if ($newserverstatus{$type}{'machines'} eq '') {
20896:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20897:                         } else {
20898:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20899:                         }
20900: 
20901:                     }
20902:                     $resulttext .= '</ul></li>';
20903:                 }
20904:             }
20905:             $resulttext .= '</ul>';
20906:         } else {
20907:             $resulttext = '<span class="LC_error">'.
20908:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20909: 
20910:         }
20911:     } else {
20912:         $resulttext = &mt('No changes made to access to server status pages');
20913:     }
20914:     return $resulttext;
20915: }
20916: 
20917: sub modify_helpsettings {
20918:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20919:     my ($resulttext,$errors,%changes,%helphash);
20920:     my %defaultchecked = ('submitbugs' => 'on');
20921:     my @offon = ('off','on');
20922:     my @toggles = ('submitbugs');
20923:     my %current = ('submitbugs' => '',
20924:                    'adhoc'      => {},
20925:                   );
20926:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20927:         %current = %{$domconfig{'helpsettings'}};
20928:     }
20929:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20930:     foreach my $item (@toggles) {
20931:         if ($defaultchecked{$item} eq 'on') { 
20932:             if ($current{$item} eq '') {
20933:                 if ($env{'form.'.$item} eq '0') {
20934:                     $changes{$item} = 1;
20935:                 }
20936:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20937:                 $changes{$item} = 1;
20938:             }
20939:         } elsif ($defaultchecked{$item} eq 'off') {
20940:             if ($current{$item} eq '') {
20941:                 if ($env{'form.'.$item} eq '1') {
20942:                     $changes{$item} = 1;
20943:                 }
20944:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20945:                 $changes{$item} = 1;
20946:             }
20947:         }
20948:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20949:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20950:         }
20951:     }
20952:     my $maxnum = $env{'form.helproles_maxnum'};
20953:     my $confname = $dom.'-domainconfig';
20954:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20955:     my (@allpos,%newsettings,%changedprivs,$newrole);
20956:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20957:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20958:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20959:     my %lt = &Apache::lonlocal::texthash(
20960:                     s      => 'system',
20961:                     d      => 'domain',
20962:                     order  => 'Display order',
20963:                     access => 'Role usage',
20964:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20965:                     dh     => 'All with domain helpdesk role',
20966:                     da     => 'All with domain helpdesk assistant role',
20967:                     none   => 'None',
20968:                     status => 'Determined based on institutional status',
20969:                     inc    => 'Include all, but exclude specific personnel',
20970:                     exc    => 'Exclude all, but include specific personnel',
20971:     );
20972:     for (my $num=0; $num<=$maxnum; $num++) {
20973:         my ($prefix,$identifier,$rolename,%curr);
20974:         if ($num == $maxnum) {
20975:             next unless ($env{'form.newcusthelp'} == $maxnum);
20976:             $identifier = 'custhelp'.$num;
20977:             $prefix = 'helproles_'.$num;
20978:             $rolename = $env{'form.custhelpname'.$num};
20979:             $rolename=~s/[^A-Za-z0-9]//gs;
20980:             next if ($rolename eq '');
20981:             next if (exists($existing{'rolesdef_'.$rolename}));
20982:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20983:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20984:                                                      $newprivs{'c'},$confname,$dom);
20985:             if ($result ne 'ok') {
20986:                 $errors .= '<li><span class="LC_error">'.
20987:                            &mt('An error occurred storing the new custom role: [_1]',
20988:                            $result).'</span></li>';
20989:                 next;
20990:             } else {
20991:                 $changedprivs{$rolename} = \%newprivs;
20992:                 $newrole = $rolename;
20993:             }
20994:         } else {
20995:             $prefix = 'helproles_'.$num;
20996:             $rolename = $env{'form.'.$prefix};
20997:             next if ($rolename eq '');
20998:             next unless (exists($existing{'rolesdef_'.$rolename}));
20999:             $identifier = 'custhelp'.$num;
21000:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
21001:             my %currprivs;
21002:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
21003:                 split(/\_/,$existing{'rolesdef_'.$rolename});
21004:             foreach my $level ('c','d','s') {
21005:                 if ($newprivs{$level} ne $currprivs{$level}) {
21006:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
21007:                                                              $newprivs{'c'},$confname,$dom);
21008:                     if ($result ne 'ok') {
21009:                         $errors .= '<li><span class="LC_error">'.
21010:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
21011:                                        $rolename,$result).'</span></li>';
21012:                     } else {
21013:                         $changedprivs{$rolename} = \%newprivs;
21014:                     }
21015:                     last;
21016:                 }
21017:             }
21018:             if (ref($current{'adhoc'}) eq 'HASH') {
21019:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21020:                     %curr = %{$current{'adhoc'}{$rolename}};
21021:                 }
21022:             }
21023:         }
21024:         my $newpos = $env{'form.'.$prefix.'_pos'};
21025:         $newpos =~ s/\D+//g;
21026:         $allpos[$newpos] = $rolename;
21027:         my $newdesc = $env{'form.'.$prefix.'_desc'};
21028:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
21029:         if ($curr{'desc'}) {
21030:             if ($curr{'desc'} ne $newdesc) {
21031:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
21032:                 $newsettings{$rolename}{'desc'} = $newdesc;
21033:             }
21034:         } elsif ($newdesc ne '') {
21035:             $changes{'customrole'}{$rolename}{'desc'} = 1;
21036:             $newsettings{$rolename}{'desc'} = $newdesc;
21037:         }
21038:         my $access = $env{'form.'.$prefix.'_access'};
21039:         if (grep(/^\Q$access\E$/,@accesstypes)) {
21040:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
21041:             if ($access eq 'status') {
21042:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
21043:                 if (scalar(@statuses) == 0) {
21044:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
21045:                 } else {
21046:                     my (@shownstatus,$numtypes);
21047:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21048:                     if (ref($types) eq 'ARRAY') {
21049:                         $numtypes = scalar(@{$types});
21050:                         foreach my $type (sort(@statuses)) {
21051:                             if ($type eq 'default') {
21052:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21053:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
21054:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21055:                                 push(@shownstatus,$usertypes->{$type});
21056:                             }
21057:                         }
21058:                     }
21059:                     if (grep(/^default$/,@statuses)) {
21060:                         push(@shownstatus,$othertitle);
21061:                     }
21062:                     if (scalar(@shownstatus) == 1+$numtypes) {
21063:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
21064:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
21065:                     } else {
21066:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
21067:                         if (ref($curr{'status'}) eq 'ARRAY') {
21068:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21069:                             if (@diffs) {
21070:                                 $changes{'customrole'}{$rolename}{$access} = 1;
21071:                             }
21072:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21073:                             $changes{'customrole'}{$rolename}{$access} = 1;
21074:                         }
21075:                     }
21076:                 }
21077:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
21078:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
21079:                 my @newspecstaff;
21080:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21081:                 foreach my $person (sort(@personnel)) {
21082:                     if ($domhelpdesk{$person}) {
21083:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
21084:                     }
21085:                 }
21086:                 if (ref($curr{$access}) eq 'ARRAY') {
21087:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21088:                     if (@diffs) {
21089:                         $changes{'customrole'}{$rolename}{$access} = 1;
21090:                     }
21091:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21092:                     $changes{'customrole'}{$rolename}{$access} = 1;
21093:                 }
21094:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21095:                     my ($uname,$udom) = split(/:/,$person);
21096:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
21097:                 }
21098:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
21099:             }
21100:         } else {
21101:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
21102:         }
21103:         unless ($curr{'access'} eq $access) {
21104:             $changes{'customrole'}{$rolename}{'access'} = 1;
21105:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
21106:         }
21107:     }
21108:     if (@allpos > 0) {
21109:         my $idx = 0;
21110:         foreach my $rolename (@allpos) {
21111:             if ($rolename ne '') {
21112:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
21113:                 if (ref($current{'adhoc'}) eq 'HASH') {
21114:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21115:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
21116:                             $changes{'customrole'}{$rolename}{'order'} = 1;
21117:                             $newsettings{$rolename}{'order'} = $idx+1;
21118:                         }
21119:                     }
21120:                 }
21121:                 $idx ++;
21122:             }
21123:         }
21124:     }
21125:     my $putresult;
21126:     if (keys(%changes) > 0) {
21127:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
21128:         if ($putresult eq 'ok') {
21129:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
21130:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
21131:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
21132:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
21133:                 }
21134:             }
21135:             my $cachetime = 24*60*60;
21136:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21137:             if (ref($lastactref) eq 'HASH') {
21138:                 $lastactref->{'domdefaults'} = 1;
21139:             }
21140:         } else {
21141:             $errors .= '<li><span class="LC_error">'.
21142:                        &mt('An error occurred storing the settings: [_1]',
21143:                            $putresult).'</span></li>';
21144:         }
21145:     }
21146:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
21147:         $resulttext = &mt('Changes made:').'<ul>';
21148:         my (%shownprivs,@levelorder);
21149:         @levelorder = ('c','d','s');
21150:         if ((keys(%changes)) && ($putresult eq 'ok')) {
21151:             foreach my $item (sort(keys(%changes))) {
21152:                 if ($item eq 'submitbugs') {
21153:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
21154:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
21155:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
21156:                 } elsif ($item eq 'customrole') {
21157:                     if (ref($changes{'customrole'}) eq 'HASH') {
21158:                         my @keyorder = ('order','desc','access','status','exc','inc');
21159:                         my %keytext = &Apache::lonlocal::texthash(
21160:                                                                    order  => 'Order',
21161:                                                                    desc   => 'Role description',
21162:                                                                    access => 'Role usage',
21163:                                                                    status => 'Allowed institutional types',
21164:                                                                    exc    => 'Allowed personnel',
21165:                                                                    inc    => 'Disallowed personnel',
21166:                         );
21167:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
21168:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
21169:                                 if ($role eq $newrole) {
21170:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
21171:                                                               $role).'<ul>';
21172:                                 } else {
21173:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21174:                                                               $role).'<ul>';
21175:                                 }
21176:                                 foreach my $key (@keyorder) {
21177:                                     if ($changes{'customrole'}{$role}{$key}) {
21178:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
21179:                                                                   $keytext{$key},$newsettings{$role}{$key}).
21180:                                                        '</li>';
21181:                                     }
21182:                                 }
21183:                                 if (ref($changedprivs{$role}) eq 'HASH') {
21184:                                     $shownprivs{$role} = 1;
21185:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
21186:                                     foreach my $level (@levelorder) {
21187:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21188:                                             next if ($item eq '');
21189:                                             my ($priv) = split(/\&/,$item,2);
21190:                                             if (&Apache::lonnet::plaintext($priv)) {
21191:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21192:                                                 unless ($level eq 'c') {
21193:                                                     $resulttext .= ' ('.$lt{$level}.')';
21194:                                                 }
21195:                                                 $resulttext .= '</li>';
21196:                                             }
21197:                                         }
21198:                                     }
21199:                                     $resulttext .= '</ul>';
21200:                                 }
21201:                                 $resulttext .= '</ul></li>';
21202:                             }
21203:                         }
21204:                     }
21205:                 }
21206:             }
21207:         }
21208:         if (keys(%changedprivs)) {
21209:             foreach my $role (sort(keys(%changedprivs))) {
21210:                 unless ($shownprivs{$role}) {
21211:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21212:                                               $role).'<ul>'.
21213:                                    '<li>'.&mt('Privileges set to :').'<ul>';
21214:                     foreach my $level (@levelorder) {
21215:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21216:                             next if ($item eq '');
21217:                             my ($priv) = split(/\&/,$item,2);
21218:                             if (&Apache::lonnet::plaintext($priv)) {
21219:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21220:                                 unless ($level eq 'c') {
21221:                                     $resulttext .= ' ('.$lt{$level}.')';
21222:                                 }
21223:                                 $resulttext .= '</li>';
21224:                             }
21225:                         }
21226:                     }
21227:                     $resulttext .= '</ul></li></ul></li>';
21228:                 }
21229:             }
21230:         }
21231:         $resulttext .= '</ul>';
21232:     } else {
21233:         $resulttext = &mt('No changes made to help settings');
21234:     }
21235:     if ($errors) {
21236:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
21237:                                     $errors.'</ul>';
21238:     }
21239:     return $resulttext;
21240: }
21241: 
21242: sub modify_coursedefaults {
21243:     my ($dom,$lastactref,%domconfig) = @_;
21244:     my ($resulttext,$errors,%changes,%defaultshash);
21245:     my %defaultchecked = (
21246:                            'canuse_pdfforms' => 'off',
21247:                            'uselcmath'       => 'on',
21248:                            'usejsme'         => 'on',
21249:                            'inline_chem'     => 'on',
21250:                            'ltiauth'         => 'off',
21251:                          );
21252:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
21253:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
21254:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
21255:                    'coursequota_official','coursequota_unofficial','coursequota_community',
21256:                    'coursequota_textbook','coursequota_placement','mysqltables_official',
21257:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook',
21258:                    'mysqltables_placement');
21259:     my @types = ('official','unofficial','community','textbook','placement');
21260:     my %staticdefaults = (
21261:                            anonsurvey_threshold => 10,
21262:                            uploadquota          => 500,
21263:                            coursequota          => 20,
21264:                            postsubmit           => 60,
21265:                            mysqltables          => 172800,
21266:                            domexttool           => 1,
21267:                            crsauthor            => 1,
21268:                            crseditors           => ['edit','xml'],
21269:                          );
21270:     my %texoptions = (
21271:                         MathJax  => 'MathJax',
21272:                         mimetex  => &mt('Convert to Images'),
21273:                         tth      => &mt('TeX to HTML'),
21274:                      );
21275: 
21276:     my @editors = ('edit','xml','daxe');
21277:     my %editornames = &crseditor_titles();
21278: 
21279:     $defaultshash{'coursedefaults'} = {};
21280: 
21281:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
21282:         if ($domconfig{'coursedefaults'} eq '') {
21283:             $domconfig{'coursedefaults'} = {};
21284:         }
21285:     }
21286: 
21287:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
21288:         foreach my $item (@toggles) {
21289:             if ($defaultchecked{$item} eq 'on') {
21290:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21291:                     ($env{'form.'.$item} eq '0')) {
21292:                     $changes{$item} = 1;
21293:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21294:                     $changes{$item} = 1;
21295:                 }
21296:             } elsif ($defaultchecked{$item} eq 'off') {
21297:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21298:                     ($env{'form.'.$item} eq '1')) {
21299:                     $changes{$item} = 1;
21300:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21301:                     $changes{$item} = 1;
21302:                 }
21303:             }
21304:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
21305:         }
21306:         foreach my $item (@numbers) {
21307:             my ($currdef,$newdef);
21308:             $newdef = $env{'form.'.$item};
21309:             if ($item eq 'anonsurvey_threshold') {
21310:                 $currdef = $domconfig{'coursedefaults'}{$item};
21311:                 $newdef =~ s/\D//g;
21312:                 if ($newdef eq '' || $newdef < 1) {
21313:                     $newdef = 1;
21314:                 }
21315:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
21316:             } else {
21317:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
21318:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
21319:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
21320:                 }
21321:                 $newdef =~ s/[^\w.\-]//g;
21322:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
21323:             }
21324:             if ($currdef ne $newdef) {
21325:                 if ($item eq 'anonsurvey_threshold') {
21326:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
21327:                         $changes{$item} = 1;
21328:                     }
21329:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
21330:                     my $setting = $1;
21331:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
21332:                         $changes{$setting} = 1;
21333:                     }
21334:                 }
21335:             }
21336:         }
21337:         my $texengine;
21338:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
21339:             $texengine = $env{'form.texengine'};
21340:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
21341:             if ($currdef eq '') {
21342:                 unless ($texengine eq $Apache::lonnet::deftex) {
21343:                     $changes{'texengine'} = 1;
21344:                 }
21345:             } elsif ($currdef ne $texengine) {
21346:                 $changes{'texengine'} = 1;
21347:             }
21348:         }
21349:         if ($texengine ne '') {
21350:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
21351:         }
21352:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
21353:         my @currclonecode;
21354:         if (ref($currclone) eq 'HASH') {
21355:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
21356:                 @currclonecode = @{$currclone->{'instcode'}};
21357:             }
21358:         }
21359:         my $newclone;
21360:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
21361:             $newclone = $env{'form.canclone'};
21362:         }
21363:         if ($newclone eq 'instcode') {
21364:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
21365:             my (%codedefaults,@code_order,@clonecode);
21366:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
21367:                                                     \@code_order);
21368:             foreach my $item (@code_order) {
21369:                 if (grep(/^\Q$item\E$/,@newcodes)) {
21370:                     push(@clonecode,$item);
21371:                 }
21372:             }
21373:             if (@clonecode) {
21374:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
21375:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
21376:                 if (@diffs) {
21377:                     $changes{'canclone'} = 1;
21378:                 }
21379:             } else {
21380:                 $newclone eq '';
21381:             }
21382:         } elsif ($newclone ne '') {
21383:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
21384:         }
21385:         if ($newclone ne $currclone) {
21386:             $changes{'canclone'} = 1;
21387:         }
21388:         my %credits;
21389:         foreach my $type (@types) {
21390:             unless ($type eq 'community') {
21391:                 $credits{$type} = $env{'form.'.$type.'_credits'};
21392:                 $credits{$type} =~ s/[^\d.]+//g;
21393:             }
21394:         }
21395:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
21396:             ($env{'form.coursecredits'} eq '1')) {
21397:             $changes{'coursecredits'} = 1;
21398:             foreach my $type (keys(%credits)) {
21399:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21400:             }
21401:         } else {
21402:             if ($env{'form.coursecredits'} eq '1') {
21403:                 foreach my $type (@types) {
21404:                     unless ($type eq 'community') {
21405:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
21406:                             $changes{'coursecredits'} = 1;
21407:                         }
21408:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21409:                     }
21410:                 }
21411:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21412:                 foreach my $type (@types) {
21413:                     unless ($type eq 'community') {
21414:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
21415:                             $changes{'coursecredits'} = 1;
21416:                             last;
21417:                         }
21418:                     }
21419:                 }
21420:             }
21421:         }
21422:         if ($env{'form.postsubmit'} eq '1') {
21423:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
21424:             my %currtimeout;
21425:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21426:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
21427:                     $changes{'postsubmit'} = 1;
21428:                 }
21429:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21430:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
21431:                 }
21432:             } else {
21433:                 $changes{'postsubmit'} = 1;
21434:             }
21435:             foreach my $type (@types) {
21436:                 my $timeout = $env{'form.'.$type.'_timeout'};
21437:                 $timeout =~ s/\D//g;
21438:                 if ($timeout == $staticdefaults{'postsubmit'}) {
21439:                     $timeout = '';
21440:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
21441:                     $timeout = '0';
21442:                 }
21443:                 unless ($timeout eq '') {
21444:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
21445:                 }
21446:                 if (exists($currtimeout{$type})) {
21447:                     if ($timeout ne $currtimeout{$type}) {
21448:                         $changes{'postsubmit'} = 1;
21449:                     }
21450:                 } elsif ($timeout ne '') {
21451:                     $changes{'postsubmit'} = 1;
21452:                 }
21453:             }
21454:         } else {
21455:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
21456:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21457:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
21458:                     $changes{'postsubmit'} = 1;
21459:                 }
21460:             } else {
21461:                 $changes{'postsubmit'} = 1;
21462:             }
21463:         }
21464:         my (%newdomexttool,%newexttool,%newcrsauthor,%olddomexttool,%oldexttool,%oldcrsauthor,
21465:             %posscrseditors);
21466:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
21467:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
21468:         map { $newcrsauthor{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crsauthor');
21469:         map { $posscrseditors{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crseditors');
21470:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21471:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
21472:         } else {
21473:            foreach my $type (@types) {
21474:                if ($staticdefaults{'domexttool'}) {
21475:                    $olddomexttool{$type} = 1;
21476:                } else {
21477:                    $olddomexttool{$type} = 0;
21478:                }
21479:             }
21480:         }
21481:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
21482:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
21483:         } else {
21484:             foreach my $type (@types) {
21485:                if ($staticdefaults{'exttool'}) {
21486:                    $oldexttool{$type} = 1;
21487:                } else {
21488:                    $oldexttool{$type} = 0;
21489:                }
21490:             }
21491:         }
21492:         if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21493:             %oldcrsauthor = %{$domconfig{'coursedefaults'}{'crsauthor'}};
21494:         } else {
21495:             foreach my $type (@types) {
21496:                if ($staticdefaults{'crsauthor'}) {
21497:                    $oldcrsauthor{$type} = 1;
21498:                } else {
21499:                    $oldcrsauthor{$type} = 0;
21500:                }
21501:             }
21502:         }
21503:         my @newcrseditors = ();
21504:         foreach my $editor (@editors) {
21505:             if ($posscrseditors{$editor}) {
21506:                 push(@newcrseditors,$editor);
21507:             }
21508:         }
21509:         if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21510:             my @diffs =
21511:                 &Apache::loncommon::compare_arrays($domconfig{'coursedefaults'}{'crseditors'},
21512:                                                    \@newcrseditors);
21513:             if (@diffs) {
21514:                 $changes{'crseditors'} = 1; 
21515:             }
21516:         } else {
21517:             my @diffs =
21518:                 &Apache::loncommon::compare_arrays($staticdefaults{'crseditors'},
21519:                                                    \@newcrseditors);
21520:             unless (@diffs == 0) {
21521:                 $changes{'crseditors'} = 1;
21522:             }       
21523:         }
21524:         foreach my $type (@types) {
21525:             unless ($newdomexttool{$type}) {
21526:                 $newdomexttool{$type} = 0;
21527:             }
21528:             unless ($newexttool{$type}) {
21529:                 $newexttool{$type} = 0;
21530:             }
21531:             unless ($newcrsauthor{$type}) {
21532:                 $newcrsauthor{$type} = 0;
21533:             }
21534:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
21535:                 $changes{'domexttool'} = 1;
21536:             }
21537:             if ($newexttool{$type} != $oldexttool{$type}) {
21538:                 $changes{'exttool'} = 1;
21539:             }
21540:             if ($newcrsauthor{$type} != $oldcrsauthor{$type}) {
21541:                 $changes{'crsauthor'} = 1;
21542:             }
21543:         }
21544:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
21545:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
21546:         $defaultshash{'coursedefaults'}{'crsauthor'} = \%newcrsauthor;
21547:         $defaultshash{'coursedefaults'}{'crseditors'} = \@newcrseditors;
21548:     }
21549:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21550:                                              $dom);
21551:     if ($putresult eq 'ok') {
21552:         if (keys(%changes) > 0) {
21553:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21554:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
21555:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
21556:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
21557:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
21558:                 ($changes{'exttool'}) || ($changes{'coursequota'}) || ($changes{'crsauthor'})) {
21559:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
21560:                                   'ltiauth') {
21561:                     if ($changes{$item}) {
21562:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
21563:                     }
21564:                 }
21565:                 if ($changes{'coursecredits'}) {
21566:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21567:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
21568:                             $domdefaults{$type.'credits'} =
21569:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
21570:                         }
21571:                     }
21572:                 }
21573:                 if ($changes{'postsubmit'}) {
21574:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21575:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
21576:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21577:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
21578:                                 $domdefaults{$type.'postsubtimeout'} =
21579:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21580:                             }
21581:                         }
21582:                     }
21583:                 }
21584:                 if ($changes{'uploadquota'}) {
21585:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21586:                         foreach my $type (@types) {
21587:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
21588:                         }
21589:                     }
21590:                 }
21591:                 if ($changes{'coursequota'}) {
21592:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21593:                         foreach my $type (@types) {
21594:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
21595:                         }
21596:                     }
21597:                 }
21598:                 if ($changes{'canclone'}) {
21599:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21600:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21601:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
21602:                             if (@clonecodes) {
21603:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
21604:                             }
21605:                         }
21606:                     } else {
21607:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
21608:                     }
21609:                 }
21610:                 if ($changes{'domexttool'}) {
21611:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21612:                         foreach my $type (@types) {
21613:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
21614:                         }
21615:                     }
21616:                 }
21617:                 if ($changes{'exttool'}) {
21618:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21619:                         foreach my $type (@types) {
21620:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
21621:                         }
21622:                     }
21623:                 }
21624:                 if ($changes{'crsauthor'}) {
21625:                     if (ref($defaultshash{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21626:                         foreach my $type (@types) {
21627:                             $domdefaults{$type.'crsauthor'}=$defaultshash{'coursedefaults'}{'crsauthor'}{$type};
21628:                         }
21629:                     }
21630:                 }
21631:                 if ($changes{'crseditors'}) {
21632:                     if (ref($defaultshash{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21633:                         $domdefaults{'crseditors'}=join(',',@{$defaultshash{'coursedefaults'}{'crseditors'}});
21634:                     }
21635:                 }
21636:                 my $cachetime = 24*60*60;
21637:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21638:                 if (ref($lastactref) eq 'HASH') {
21639:                     $lastactref->{'domdefaults'} = 1;
21640:                 }
21641:             }
21642:             $resulttext = &mt('Changes made:').'<ul>';
21643:             foreach my $item (sort(keys(%changes))) {
21644:                 if ($item eq 'canuse_pdfforms') {
21645:                     if ($env{'form.'.$item} eq '1') {
21646:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
21647:                     } else {
21648:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
21649:                     }
21650:                 } elsif ($item eq 'uselcmath') {
21651:                     if ($env{'form.'.$item} eq '1') {
21652:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
21653:                     } else {
21654:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
21655:                     }
21656:                 } elsif ($item eq 'usejsme') {
21657:                     if ($env{'form.'.$item} eq '1') {
21658:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
21659:                     } else {
21660:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
21661:                     }
21662:                 } elsif ($item eq 'inline_chem') {
21663:                     if ($env{'form.'.$item} eq '1') {
21664:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
21665:                     } else {
21666:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
21667:                     }
21668:                 } elsif ($item eq 'texengine') {
21669:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
21670:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
21671:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
21672:                     }
21673:                 } elsif ($item eq 'anonsurvey_threshold') {
21674:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
21675:                 } elsif ($item eq 'uploadquota') {
21676:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21677:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
21678:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
21679:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
21680:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
21681:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
21682:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
21683:                                        '</ul>'.
21684:                                        '</li>';
21685:                     } else {
21686:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
21687:                     }
21688:                 } elsif ($item eq 'coursequota') {
21689:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21690:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
21691:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
21692:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
21693:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
21694:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'placement'}.'</b>').'</li>'.
21695:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
21696:                                        '</ul>'.
21697:                                        '</li>';
21698:                     } else {
21699:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
21700:                     }
21701:                 } elsif ($item eq 'mysqltables') {
21702:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
21703:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
21704:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
21705:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
21706:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
21707:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
21708:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
21709:                                        '</ul>'.
21710:                                        '</li>';
21711:                     } else {
21712:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
21713:                     }
21714:                 } elsif ($item eq 'postsubmit') {
21715:                     if ($domdefaults{'postsubmit'} eq 'off') {
21716:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
21717:                     } else {
21718:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
21719:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21720:                             $resulttext .= &mt('durations:').'<ul>';
21721:                             foreach my $type (@types) {
21722:                                 $resulttext .= '<li>';
21723:                                 my $timeout;
21724:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21725:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21726:                                 }
21727:                                 my $display;
21728:                                 if ($timeout eq '0') {
21729:                                     $display = &mt('unlimited');
21730:                                 } elsif ($timeout eq '') {
21731:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
21732:                                 } else {
21733:                                     $display = &mt('[quant,_1,second]',$timeout);
21734:                                 }
21735:                                 if ($type eq 'community') {
21736:                                     $resulttext .= &mt('Communities');
21737:                                 } elsif ($type eq 'official') {
21738:                                     $resulttext .= &mt('Official courses');
21739:                                 } elsif ($type eq 'unofficial') {
21740:                                     $resulttext .= &mt('Unofficial courses');
21741:                                 } elsif ($type eq 'textbook') {
21742:                                     $resulttext .= &mt('Textbook courses');
21743:                                 } elsif ($type eq 'placement') {
21744:                                     $resulttext .= &mt('Placement tests');
21745:                                 }
21746:                                 $resulttext .= ' -- '.$display.'</li>';
21747:                             }
21748:                             $resulttext .= '</ul>';
21749:                         }
21750:                         $resulttext .= '</li>';
21751:                     }
21752:                 } elsif ($item eq 'coursecredits') {
21753:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21754:                         if (($domdefaults{'officialcredits'} eq '') &&
21755:                             ($domdefaults{'unofficialcredits'} eq '') &&
21756:                             ($domdefaults{'textbookcredits'} eq '')) {
21757:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21758:                         } else {
21759:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
21760:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
21761:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
21762:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
21763:                                            '</ul>'.
21764:                                            '</li>';
21765:                         }
21766:                     } else {
21767:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21768:                     }
21769:                 } elsif ($item eq 'canclone') {
21770:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21771:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21772:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
21773:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
21774:                         }
21775:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
21776:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
21777:                     } else {
21778:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
21779:                     }
21780:                 } elsif ($item eq 'ltiauth') {
21781:                     if ($env{'form.'.$item} eq '1') {
21782:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
21783:                     } else {
21784:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
21785:                     }
21786:                 } elsif (($item eq 'domexttool') || ($item eq 'exttool') || ($item eq 'crsauthor')) {
21787:                     my @noyes = (&mt('no'),&mt('yes'));
21788:                     my %status = (
21789:                                    domexttool => {
21790:                                                   ishash => &mt('External Tools defined in the domain may be used as follows:'), 
21791:                                                   default => &mt('External Tools defined in the domain may be used in all course types, by default'),
21792:                                                  },
21793:                                    exttool => {
21794:                                                   ishash => &mt('External Tools can be defined and configured in course containers as follows:'),
21795:                                                   default => &mt('External Tools can not be defined in any course types, by default'),
21796:                                               },
21797:                                    crsauthor => {
21798:                                                   ishash => &mt('Standard Problems can be created within course containers as follows:'),
21799:                                                   default => &mt('Standard Problems can be created within any course type, by default'),
21800:                                                 },
21801:                                  );
21802:                                    
21803:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'HASH') {
21804:                         $resulttext .= '<li>'.$status{$item}{'ishash'}.'<ul>'.
21805:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'official'}].'</b>').'</li>'.
21806:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'unofficial'}].'</b>').'</li>'.
21807:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'textbook'}].'</b>').'</li>'.
21808:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'placement'}].'</b>').'</li>'.
21809:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'community'}].'</b>').'</li>'.
21810:                                        '</ul>'.
21811:                                        '</li>';
21812:                     } else {
21813:                         $resulttext .= '<li>'.$status{$item}{'default'}.'</li>';
21814:                     }
21815:                 } elsif ($item eq 'crseditors') {
21816:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'ARRAY') {
21817:                         my $shown;
21818:                         if (@{$defaultshash{'coursedefaults'}{$item}}) {
21819:                             $shown = join(', ', map { $editornames{$_} } @{$defaultshash{'coursedefaults'}{$item}});
21820:                         } else {
21821:                             $shown = &mt('None');
21822:                         }
21823:                         $resulttext .= '<li>'.&mt('Available editors for course/community resources: [_1]',$shown).'</li>';
21824:                     }
21825:                 }
21826:             }
21827:             $resulttext .= '</ul>';
21828:         } else {
21829:             $resulttext = &mt('No changes made to course defaults');
21830:         }
21831:     } else {
21832:         $resulttext = '<span class="LC_error">'.
21833:             &mt('An error occurred: [_1]',$putresult).'</span>';
21834:     }
21835:     return $resulttext;
21836: }
21837: 
21838: sub modify_selfenrollment {
21839:     my ($dom,$lastactref,%domconfig) = @_;
21840:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
21841:     my @types = ('official','unofficial','community','textbook','placement');
21842:     my %titles = &tool_titles();
21843:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
21844:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
21845:     $ordered{'default'} = ['types','registered','approval','limit'];
21846: 
21847:     my (%roles,%shown,%toplevel);
21848:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
21849: 
21850:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
21851:         if ($domconfig{'selfenrollment'} eq '') {
21852:             $domconfig{'selfenrollment'} = {};
21853:         }
21854:     }
21855:     %toplevel = (
21856:                   admin      => 'Configuration Rights',
21857:                   default    => 'Default settings',
21858:                   validation => 'Validation of self-enrollment requests',
21859:                 );
21860:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21861: 
21862:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21863:         foreach my $item (@{$ordered{'admin'}}) {
21864:             foreach my $type (@types) {
21865:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21866:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21867:                 } else {
21868:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21869:                 }
21870:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21871:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21872:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21873:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21874:                             push(@{$changes{'admin'}{$type}},$item);
21875:                         }
21876:                     } else {
21877:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21878:                             push(@{$changes{'admin'}{$type}},$item);
21879:                         }
21880:                     }
21881:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21882:                     push(@{$changes{'admin'}{$type}},$item);
21883:                 }
21884:             }
21885:         }
21886:     }
21887: 
21888:     foreach my $item (@{$ordered{'default'}}) {
21889:         foreach my $type (@types) {
21890:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21891:             if ($item eq 'types') {
21892:                 unless (($value eq 'all') || ($value eq 'dom')) {
21893:                     $value = '';
21894:                 }
21895:             } elsif ($item eq 'registered') {
21896:                 unless ($value eq '1') {
21897:                     $value = 0;
21898:                 }
21899:             } elsif ($item eq 'approval') {
21900:                 unless ($value =~ /^[012]$/) {
21901:                     $value = 0;
21902:                 }
21903:             } else {
21904:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21905:                     $value = 'none';
21906:                 }
21907:             }
21908:             $selfenrollhash{'default'}{$type}{$item} = $value;
21909:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21910:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21911:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21912:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21913:                          push(@{$changes{'default'}{$type}},$item);
21914:                     }
21915:                 } else {
21916:                     push(@{$changes{'default'}{$type}},$item);
21917:                 }
21918:             } else {
21919:                 push(@{$changes{'default'}{$type}},$item);
21920:             }
21921:             if ($item eq 'limit') {
21922:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21923:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21924:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21925:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21926:                     }
21927:                 } else {
21928:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21929:                 }
21930:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21931:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21932:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21933:                          push(@{$changes{'default'}{$type}},'cap');
21934:                     }
21935:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21936:                     push(@{$changes{'default'}{$type}},'cap');
21937:                 }
21938:             }
21939:         }
21940:     }
21941: 
21942:     foreach my $item (@{$itemsref}) {
21943:         if ($item eq 'fields') {
21944:             my @changed;
21945:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21946:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21947:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21948:             }
21949:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21950:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21951:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21952:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21953:                 } else {
21954:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21955:                 }
21956:             } else {
21957:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21958:             }
21959:             if (@changed) {
21960:                 if ($selfenrollhash{'validation'}{$item}) { 
21961:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21962:                 } else {
21963:                     $changes{'validation'}{$item} = &mt('None');
21964:                 }
21965:             }
21966:         } else {
21967:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
21968:             if ($item eq 'markup') {
21969:                if ($env{'form.selfenroll_validation_'.$item}) {
21970:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
21971:                }
21972:             }
21973:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21974:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
21975:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
21976:                 }
21977:             }
21978:         }
21979:     }
21980: 
21981:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
21982:                                              $dom);
21983:     if ($putresult eq 'ok') {
21984:         if (keys(%changes) > 0) {
21985:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21986:             $resulttext = &mt('Changes made:').'<ul>';
21987:             foreach my $key ('admin','default','validation') {
21988:                 if (ref($changes{$key}) eq 'HASH') {
21989:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
21990:                     if ($key eq 'validation') {
21991:                         foreach my $item (@{$itemsref}) {
21992:                             if (exists($changes{$key}{$item})) {
21993:                                 if ($item eq 'markup') {
21994:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21995:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
21996:                                 } else {  
21997:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21998:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
21999:                                 }
22000:                             }
22001:                         }
22002:                     } else {
22003:                         foreach my $type (@types) {
22004:                             if ($type eq 'community') {
22005:                                 $roles{'1'} = &mt('Community personnel');
22006:                             } else {
22007:                                 $roles{'1'} = &mt('Course personnel');
22008:                             }
22009:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
22010:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
22011:                                     if ($key eq 'admin') {
22012:                                         my @mgrdc = ();
22013:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22014:                                             foreach my $item (@{$ordered{'admin'}}) {
22015:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
22016:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
22017:                                                         push(@mgrdc,$item);
22018:                                                     }
22019:                                                 }
22020:                                             }
22021:                                             if (@mgrdc) {
22022:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
22023:                                             } else {
22024:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
22025:                                             }
22026:                                         }
22027:                                     } else {
22028:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22029:                                             foreach my $item (@{$ordered{$key}}) {
22030:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22031:                                                     $domdefaults{$type.'selfenroll'.$item} =
22032:                                                         $selfenrollhash{$key}{$type}{$item};
22033:                                                 }
22034:                                             }
22035:                                         }
22036:                                     }
22037:                                 }
22038:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
22039:                                 foreach my $item (@{$ordered{$key}}) {
22040:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22041:                                         $resulttext .= '<li>';
22042:                                         if ($key eq 'admin') {
22043:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
22044:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
22045:                                         } else {
22046:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
22047:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
22048:                                         }
22049:                                         $resulttext .= '</li>';
22050:                                     }
22051:                                 }
22052:                                 $resulttext .= '</ul></li>';
22053:                             }
22054:                         }
22055:                         $resulttext .= '</ul></li>'; 
22056:                     }
22057:                 }
22058:             }
22059:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
22060:                 my $cachetime = 24*60*60;
22061:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22062:                 if (ref($lastactref) eq 'HASH') {
22063:                     $lastactref->{'domdefaults'} = 1;
22064:                 }
22065:             }
22066:             $resulttext .= '</ul>';
22067:         } else {
22068:             $resulttext = &mt('No changes made to self-enrollment settings');
22069:         }
22070:     } else {
22071:         $resulttext = '<span class="LC_error">'.
22072:             &mt('An error occurred: [_1]',$putresult).'</span>';
22073:     }
22074:     return $resulttext;
22075: }
22076: 
22077: sub modify_wafproxy {
22078:     my ($dom,$action,$lastactref,%domconfig) = @_;
22079:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22080:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
22081:         %wafproxy,%changes,%expirecache,%expiresaml);
22082:     foreach my $server (sort(keys(%servers))) {
22083:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22084:         if ($serverhome eq $server) {
22085:             my $serverdom = &Apache::lonnet::host_domain($server);
22086:             if ($serverdom eq $dom) {
22087:                 $canset{$server} = 1;
22088:             }
22089:         }
22090:     }
22091:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
22092:         %{$values{$dom}} = ();
22093:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
22094:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
22095:         }
22096:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
22097:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
22098:         }
22099:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22100:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
22101:         }
22102:     }
22103:     my $output;
22104:     if (keys(%canset)) {
22105:         %{$wafproxy{'alias'}} = ();
22106:         %{$wafproxy{'saml'}} = ();
22107:         foreach my $key (sort(keys(%canset))) {
22108:             if ($env{'form.wafproxy_'.$dom}) {
22109:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
22110:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
22111:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
22112:                     $changes{'alias'} = 1;
22113:                 }
22114:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
22115:                     $wafproxy{'saml'}{$key} = 1;
22116:                 }
22117:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
22118:                     $changes{'saml'} = 1;
22119:                 }
22120:             } else {
22121:                 $wafproxy{'alias'}{$key} = '';
22122:                 $wafproxy{'saml'}{$key} = '';
22123:                 if ($curralias{$key}) {
22124:                     $changes{'alias'} = 1;
22125:                 }
22126:                 if ($currsaml{$key}) {
22127:                     $changes{'saml'} = 1;
22128:                 }
22129:             }
22130:             if ($wafproxy{'alias'}{$key} eq '') {
22131:                 if ($curralias{$key}) {
22132:                     $expirecache{$key} = 1;
22133:                 }
22134:                 delete($wafproxy{'alias'}{$key});
22135:             }
22136:             if ($wafproxy{'saml'}{$key} eq '') {
22137:                 if ($currsaml{$key}) {
22138:                     $expiresaml{$key} = 1;
22139:                 }
22140:                 delete($wafproxy{'saml'}{$key});
22141:             }
22142:         }
22143:         unless (keys(%{$wafproxy{'alias'}})) {
22144:             delete($wafproxy{'alias'});
22145:         }
22146:         unless (keys(%{$wafproxy{'saml'}})) {
22147:             delete($wafproxy{'saml'});
22148:         }
22149:         # Localization for values in %warn occurs in &mt() calls separately.
22150:         my %warn = (
22151:                      trusted => 'trusted IP range(s)',
22152:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
22153:                      vpnext => 'IP range(s) for backend WAF connections',
22154:                    );
22155:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22156:             my $possible = $env{'form.wafproxy_'.$item};
22157:             $possible =~ s/^\s+|\s+$//g;
22158:             if ($possible ne '') {
22159:                 if ($item eq 'remoteip') {
22160:                     if ($possible =~ /^[mhn]$/) {
22161:                         $wafproxy{$item} = $possible;
22162:                     }
22163:                 } elsif ($item eq 'ipheader') {
22164:                     if ($wafproxy{'remoteip'} eq 'h') {
22165:                         $wafproxy{$item} = $possible;
22166:                     }
22167:                 } elsif ($item eq 'sslopt') {
22168:                     if ($possible =~ /^0|1$/) {
22169:                         $wafproxy{$item} = $possible;
22170:                     }
22171:                 } else {
22172:                     my (@ok,$count);
22173:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
22174:                         unless ($env{'form.wafproxy_vpnaccess'}) {
22175:                             $possible = '';
22176:                         }
22177:                     } elsif ($item eq 'trusted') {
22178:                         unless ($wafproxy{'remoteip'} eq 'h') {
22179:                             $possible = '';
22180:                         }
22181:                     }
22182:                     unless ($possible eq '') {
22183:                         $possible =~ s/[\r\n]+/\s/g;
22184:                         $possible =~ s/\s*-\s*/-/g;
22185:                         $possible =~ s/\s+/,/g;
22186:                         $possible =~ s/,+/,/g;
22187:                     }
22188:                     $count = 0;
22189:                     if ($possible ne '') {
22190:                         foreach my $poss (split(/\,/,$possible)) {
22191:                             $count ++;
22192:                             $poss = &validate_ip_pattern($poss);
22193:                             if ($poss ne '') {
22194:                                 push(@ok,$poss);
22195:                             }
22196:                         }
22197:                         my $diff = $count - scalar(@ok);
22198:                         if ($diff) {
22199:                             push(@warnings,'<li>'.
22200:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
22201:                                      $diff,$warn{$item}).
22202:                                  '</li>');
22203:                         }
22204:                         if (@ok) {
22205:                             my @cidr_list;
22206:                             foreach my $item (@ok) {
22207:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
22208:                             }
22209:                             $wafproxy{$item} = join(',',@cidr_list);
22210:                         }
22211:                     }
22212:                 }
22213:                 if ($wafproxy{$item} ne $currvalue{$item}) {
22214:                     $changes{$item} = 1;
22215:                 }
22216:             } elsif ($currvalue{$item}) {
22217:                 $changes{$item} = 1;
22218:             }
22219:         }
22220:     } else {
22221:         if (keys(%curralias)) {
22222:             $changes{'alias'} = 1;
22223:         }
22224:         if (keys(%currsaml)) {
22225:             $changes{'saml'} = 1;
22226:         }
22227:         if (keys(%currvalue)) {
22228:             foreach my $key (keys(%currvalue)) {
22229:                 $changes{$key} = 1;
22230:             }
22231:         }
22232:     }
22233:     if (keys(%changes)) {
22234:         my %defaultshash = (
22235:                               wafproxy => \%wafproxy,
22236:                            );
22237:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22238:                                                  $dom);
22239:         if ($putresult eq 'ok') {
22240:             my $cachetime = 24*60*60;
22241:             my (%domdefaults,$updatedomdefs);
22242:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
22243:                 if ($changes{$item}) {
22244:                     unless ($updatedomdefs) {
22245:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
22246:                         $updatedomdefs = 1;
22247:                     }
22248:                     if ($wafproxy{$item}) {
22249:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
22250:                     } elsif (exists($domdefaults{'waf_'.$item})) {
22251:                         delete($domdefaults{'waf_'.$item});
22252:                     }
22253:                 }
22254:             }
22255:             if ($updatedomdefs) {
22256:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22257:                 if (ref($lastactref) eq 'HASH') {
22258:                     $lastactref->{'domdefaults'} = 1;
22259:                 }
22260:             }
22261:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
22262:                 my %updates = %expirecache;
22263:                 foreach my $key (keys(%expirecache)) {
22264:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
22265:                 }
22266:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
22267:                     my $cachetime = 24*60*60;
22268:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
22269:                         $updates{$key} = 1;
22270:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
22271:                                                       $cachetime);
22272:                     }
22273:                 }
22274:                 if (ref($lastactref) eq 'HASH') {
22275:                     $lastactref->{'proxyalias'} = \%updates;
22276:                 }
22277:             }
22278:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
22279:                 my %samlupdates = %expiresaml;
22280:                 foreach my $key (keys(%expiresaml)) {
22281:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
22282:                 }
22283:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
22284:                     my $cachetime = 24*60*60;
22285:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
22286:                         $samlupdates{$key} = 1;
22287:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
22288:                                                       $cachetime);
22289:                     }
22290:                 }
22291:                 if (ref($lastactref) eq 'HASH') {
22292:                     $lastactref->{'proxysaml'} = \%samlupdates;
22293:                 }
22294:             }
22295:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
22296:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22297:                 if ($changes{$item}) {
22298:                     if ($item eq 'alias') {
22299:                         my $numaliased = 0;
22300:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
22301:                             my $shown;
22302:                             if (keys(%{$wafproxy{'alias'}})) {
22303:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
22304:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
22305:                                                          &Apache::lonnet::hostname($server),
22306:                                                          $wafproxy{'alias'}{$server}).'</li>';
22307:                                     $numaliased ++;
22308:                                 }
22309:                                 if ($numaliased) {
22310:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
22311:                                                           '<ul>'.$shown.'</ul>').'</li>';
22312:                                 }
22313:                             }
22314:                         }
22315:                         unless ($numaliased) {
22316:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
22317:                         }
22318:                     } elsif ($item eq 'saml') {
22319:                         my $shown;
22320:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
22321:                             if (keys(%{$wafproxy{'saml'}})) {
22322:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
22323:                             }
22324:                         }
22325:                         if ($shown) {
22326:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
22327:                                                   $shown).'</li>';
22328:                         } else {
22329:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
22330:                         }
22331:                     } else {
22332:                         if ($item eq 'remoteip') {
22333:                             my %ip_methods = &remoteip_methods();
22334:                             if ($wafproxy{$item} =~ /^[mh]$/) {
22335:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
22336:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
22337:                             } else {
22338:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
22339:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
22340:                                                '</li>';
22341:                                 } else {
22342:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
22343:                                 }
22344:                             }
22345:                         } elsif ($item eq 'ipheader') {
22346:                             if ($wafproxy{$item}) {
22347:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
22348:                                                       $wafproxy{$item}).'</li>';
22349:                             } else {
22350:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
22351:                             }
22352:                         } elsif ($item eq 'trusted') {
22353:                             if ($wafproxy{$item}) {
22354:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
22355:                                                       $wafproxy{$item}).'</li>';
22356:                             } else {
22357:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
22358:                             }
22359:                         } elsif ($item eq 'vpnint') {
22360:                             if ($wafproxy{$item}) {
22361:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
22362:                                                        $wafproxy{$item}).'</li>';
22363:                             } else {
22364:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
22365:                             }
22366:                         } elsif ($item eq 'vpnext') {
22367:                             if ($wafproxy{$item}) {
22368:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
22369:                                                        $wafproxy{$item}).'</li>';
22370:                             } else {
22371:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
22372:                             }
22373:                         } elsif ($item eq 'sslopt') {
22374:                             if ($wafproxy{$item}) {
22375:                                 $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>';
22376:                             } else {
22377:                                 $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>';
22378:                             }
22379:                         }
22380:                     }
22381:                 }
22382:             }
22383:             $output .= '</ul>';
22384:         } else {
22385:             $output = '<span class="LC_error">'.
22386:                       &mt('An error occurred: [_1]',$putresult).'</span>';
22387:         }
22388:     } elsif (keys(%canset)) {
22389:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
22390:     }
22391:     if (@warnings) {
22392:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
22393:                        join("\n",@warnings).'</ul>';
22394:     }
22395:     return $output;
22396: }
22397: 
22398: sub validate_ip_pattern {
22399:     my ($pattern) = @_;
22400:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
22401:         my ($start,$end) = ($1,$2);
22402:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
22403:             if (($start !~ m{/}) && ($end !~ m{/})) {
22404:                 return $start.'-'.$end;
22405:             }
22406:         }
22407:     } elsif ($pattern ne '') {
22408:         $pattern = &Net::CIDR::cidrvalidate($pattern);
22409:         if ($pattern ne '') {
22410:             return $pattern;
22411:         }
22412:     }
22413:     return;
22414: }
22415: 
22416: sub modify_usersessions {
22417:     my ($dom,$lastactref,%domconfig) = @_;
22418:     my @hostingtypes = ('version','excludedomain','includedomain');
22419:     my @offloadtypes = ('primary','default');
22420:     my %types = (
22421:                   remote => \@hostingtypes,
22422:                   hosted => \@hostingtypes,
22423:                   spares => \@offloadtypes,
22424:                 );
22425:     my @prefixes = ('remote','hosted','spares');
22426:     my @lcversions = &Apache::lonnet::all_loncaparevs();
22427:     my (%by_ip,%by_location,@intdoms,@instdoms);
22428:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22429:     my @locations = sort(keys(%by_location));
22430:     my (%defaultshash,%changes);
22431:     foreach my $prefix (@prefixes) {
22432:         $defaultshash{'usersessions'}{$prefix} = {};
22433:     }
22434:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22435:     my $resulttext;
22436:     my %iphost = &Apache::lonnet::get_iphost();
22437:     foreach my $prefix (@prefixes) {
22438:         next if ($prefix eq 'spares');
22439:         foreach my $type (@{$types{$prefix}}) {
22440:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22441:             if ($type eq 'version') {
22442:                 my $value = $env{'form.'.$prefix.'_'.$type};
22443:                 my $okvalue;
22444:                 if ($value ne '') {
22445:                     if (grep(/^\Q$value\E$/,@lcversions)) {
22446:                         $okvalue = $value;
22447:                     }
22448:                 }
22449:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22450:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22451:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
22452:                             if ($inuse == 0) {
22453:                                 $changes{$prefix}{$type} = 1;
22454:                             } else {
22455:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
22456:                                     $changes{$prefix}{$type} = 1;
22457:                                 }
22458:                                 if ($okvalue ne '') {
22459:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22460:                                 } 
22461:                             }
22462:                         } else {
22463:                             if (($inuse == 1) && ($okvalue ne '')) {
22464:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22465:                                 $changes{$prefix}{$type} = 1;
22466:                             }
22467:                         }
22468:                     } else {
22469:                         if (($inuse == 1) && ($okvalue ne '')) {
22470:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22471:                             $changes{$prefix}{$type} = 1;
22472:                         }
22473:                     }
22474:                 } else {
22475:                     if (($inuse == 1) && ($okvalue ne '')) {
22476:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22477:                         $changes{$prefix}{$type} = 1;
22478:                     }
22479:                 }
22480:             } else {
22481:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22482:                 my @okvals;
22483:                 foreach my $val (@vals) {
22484:                     if ($val =~ /:/) {
22485:                         my @items = split(/:/,$val);
22486:                         foreach my $item (@items) {
22487:                             if (ref($by_location{$item}) eq 'ARRAY') {
22488:                                 push(@okvals,$item);
22489:                             }
22490:                         }
22491:                     } else {
22492:                         if (ref($by_location{$val}) eq 'ARRAY') {
22493:                             push(@okvals,$val);
22494:                         }
22495:                     }
22496:                 }
22497:                 @okvals = sort(@okvals);
22498:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22499:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22500:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22501:                             if ($inuse == 0) {
22502:                                 $changes{$prefix}{$type} = 1; 
22503:                             } else {
22504:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22505:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
22506:                                 if (@changed > 0) {
22507:                                     $changes{$prefix}{$type} = 1;
22508:                                 }
22509:                             }
22510:                         } else {
22511:                             if ($inuse == 1) {
22512:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22513:                                 $changes{$prefix}{$type} = 1;
22514:                             }
22515:                         } 
22516:                     } else {
22517:                         if ($inuse == 1) {
22518:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22519:                             $changes{$prefix}{$type} = 1;
22520:                         }
22521:                     }
22522:                 } else {
22523:                     if ($inuse == 1) {
22524:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22525:                         $changes{$prefix}{$type} = 1;
22526:                     }
22527:                 }
22528:             }
22529:         }
22530:     }
22531: 
22532:     my @alldoms = &Apache::lonnet::all_domains();
22533:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22534:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
22535:     my $savespares;
22536: 
22537:     foreach my $lonhost (sort(keys(%servers))) {
22538:         my $serverhomeID =
22539:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
22540:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
22541:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
22542:         my %spareschg;
22543:         foreach my $type (@{$types{'spares'}}) {
22544:             my @okspares;
22545:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
22546:             foreach my $server (@checked) {
22547:                 if (&Apache::lonnet::hostname($server) ne '') {
22548:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
22549:                         unless (grep(/^\Q$server\E$/,@okspares)) {
22550:                             push(@okspares,$server);
22551:                         }
22552:                     }
22553:                 }
22554:             }
22555:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
22556:             my $newspare;
22557:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
22558:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
22559:                     $newspare = $new;
22560:                 }
22561:             }
22562:             my @spares;
22563:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
22564:                 @spares = sort(@okspares,$newspare);
22565:             } else {
22566:                 @spares = sort(@okspares);
22567:             }
22568:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
22569:             if (ref($spareid{$lonhost}) eq 'HASH') {
22570:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
22571:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
22572:                     if (@diffs > 0) {
22573:                         $spareschg{$type} = 1;
22574:                     }
22575:                 }
22576:             }
22577:         }
22578:         if (keys(%spareschg) > 0) {
22579:             $changes{'spares'}{$lonhost} = \%spareschg;
22580:         }
22581:     }
22582:     $defaultshash{'usersessions'}{'offloadnow'} = {};
22583:     $defaultshash{'usersessions'}{'offloadoth'} = {};
22584:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
22585:     my @okoffload;
22586:     if (@offloadnow) {
22587:         foreach my $server (@offloadnow) {
22588:             if (&Apache::lonnet::hostname($server) ne '') {
22589:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
22590:                     push(@okoffload,$server);
22591:                 }
22592:             }
22593:         }
22594:         if (@okoffload) {
22595:             foreach my $lonhost (@okoffload) {
22596:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
22597:             }
22598:         }
22599:     }
22600:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
22601:     my @okoffloadoth;
22602:     if (@offloadoth) {
22603:         foreach my $server (@offloadoth) {
22604:             if (&Apache::lonnet::hostname($server) ne '') {
22605:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
22606:                     push(@okoffloadoth,$server);
22607:                 }
22608:             }
22609:         }
22610:         if (@okoffloadoth) {
22611:             foreach my $lonhost (@okoffloadoth) {
22612:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
22613:             }
22614:         }
22615:     }
22616:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
22617:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
22618:             if (ref($changes{'spares'}) eq 'HASH') {
22619:                 if (keys(%{$changes{'spares'}}) > 0) {
22620:                     $savespares = 1;
22621:                 }
22622:             }
22623:         } else {
22624:             $savespares = 1;
22625:         }
22626:         foreach my $offload ('offloadnow','offloadoth') {
22627:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
22628:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
22629:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
22630:                         $changes{$offload} = 1;
22631:                         last;
22632:                     }
22633:                 }
22634:                 unless ($changes{$offload}) {
22635:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
22636:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
22637:                             $changes{$offload} = 1;
22638:                             last;
22639:                         }
22640:                     }
22641:                 }
22642:             } else {
22643:                 if (($offload eq 'offloadnow') && (@okoffload)) {
22644:                      $changes{'offloadnow'} = 1;
22645:                 }
22646:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
22647:                     $changes{'offloadoth'} = 1;
22648:                 }
22649:             }
22650:         }
22651:     } else {
22652:         if (@okoffload) {
22653:             $changes{'offloadnow'} = 1;
22654:         }
22655:         if (@okoffloadoth) {
22656:             $changes{'offloadoth'} = 1;
22657:         }
22658:     }
22659:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
22660:     if ((keys(%changes) > 0) || ($savespares)) {
22661:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22662:                                                  $dom);
22663:         if ($putresult eq 'ok') {
22664:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22665:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
22666:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
22667:                 }
22668:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
22669:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
22670:                 }
22671:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22672:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
22673:                 }
22674:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22675:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
22676:                 }
22677:             }
22678:             my $cachetime = 24*60*60;
22679:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22680:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
22681:             if (ref($lastactref) eq 'HASH') {
22682:                 $lastactref->{'domdefaults'} = 1;
22683:                 $lastactref->{'usersessions'} = 1;
22684:             }
22685:             if (keys(%changes) > 0) {
22686:                 my %lt = &usersession_titles();
22687:                 $resulttext = &mt('Changes made:').'<ul>';
22688:                 foreach my $prefix (@prefixes) {
22689:                     if (ref($changes{$prefix}) eq 'HASH') {
22690:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22691:                         if ($prefix eq 'spares') {
22692:                             if (ref($changes{$prefix}) eq 'HASH') {
22693:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
22694:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
22695:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
22696:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
22697:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22698:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
22699:                                         foreach my $type (@{$types{$prefix}}) {
22700:                                             if ($changes{$prefix}{$lonhost}{$type}) {
22701:                                                 my $offloadto = &mt('None');
22702:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
22703:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
22704:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
22705:                                                     }
22706:                                                 }
22707:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
22708:                                             }
22709:                                         }
22710:                                     }
22711:                                     $resulttext .= '</li>';
22712:                                 }
22713:                             }
22714:                         } else {
22715:                             foreach my $type (@{$types{$prefix}}) {
22716:                                 if (defined($changes{$prefix}{$type})) {
22717:                                     my ($newvalue,$notinuse);
22718:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22719:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
22720:                                             if ($type eq 'version') {
22721:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
22722:                                             } else {
22723:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22724:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
22725:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
22726:                                                     }
22727:                                                 } else {
22728:                                                     $notinuse = 1;
22729:                                                 }
22730:                                             }
22731:                                         }
22732:                                     }
22733:                                     if ($newvalue eq '') {
22734:                                         if ($type eq 'version') {
22735:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
22736:                                         } elsif ($notinuse) {
22737:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22738:                                         } else {
22739:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22740:                                         }
22741:                                     } else {
22742:                                         if ($type eq 'version') {
22743:                                             $newvalue .= ' '.&mt('(or later)');
22744:                                         }
22745:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22746:                                     }
22747:                                 }
22748:                             }
22749:                         }
22750:                         $resulttext .= '</ul>';
22751:                     }
22752:                 }
22753:                 if ($changes{'offloadnow'}) {
22754:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22755:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
22756:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
22757:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
22758:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22759:                             }
22760:                             $resulttext .= '</ul>';
22761:                         } else {
22762:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
22763:                         }
22764:                     } else {
22765:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
22766:                     }
22767:                 }
22768:                 if ($changes{'offloadoth'}) {
22769:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22770:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
22771:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
22772:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
22773:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22774:                             }
22775:                             $resulttext .= '</ul>';
22776:                         } else {
22777:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
22778:                         }
22779:                     } else {
22780:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
22781:                     }
22782:                 }
22783:                 $resulttext .= '</ul>';
22784:             } else {
22785:                 $resulttext = $nochgmsg;
22786:             }
22787:         } else {
22788:             $resulttext = '<span class="LC_error">'.
22789:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22790:         }
22791:     } else {
22792:         $resulttext = $nochgmsg;
22793:     }
22794:     return $resulttext;
22795: }
22796: 
22797: sub modify_ssl {
22798:     my ($dom,$lastactref,%domconfig) = @_;
22799:     my (%by_ip,%by_location,@intdoms,@instdoms);
22800:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22801:     my @locations = sort(keys(%by_location));
22802:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22803:     my (%defaultshash,%changes);
22804:     my $action = 'ssl';
22805:     my @prefixes = ('connto','connfrom','replication');
22806:     foreach my $prefix (@prefixes) {
22807:         $defaultshash{$action}{$prefix} = {};
22808:     }
22809:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22810:     my $resulttext;
22811:     my %iphost = &Apache::lonnet::get_iphost();
22812:     my @reptypes = ('certreq','nocertreq');
22813:     my @connecttypes = ('dom','intdom','other');
22814:     my %types = (
22815:                   connto      => \@connecttypes,
22816:                   connfrom    => \@connecttypes,
22817:                   replication => \@reptypes,
22818:                 );
22819:     foreach my $prefix (sort(keys(%types))) {
22820:         foreach my $type (@{$types{$prefix}}) {
22821:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22822:                 my $value = 'yes';
22823:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
22824:                     $value = $env{'form.'.$prefix.'_'.$type};
22825:                 }
22826:                 if (ref($domconfig{$action}) eq 'HASH') {
22827:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22828:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
22829:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
22830:                                 $changes{$prefix}{$type} = 1;
22831:                             }
22832:                             $defaultshash{$action}{$prefix}{$type} = $value;
22833:                         } else {
22834:                             $defaultshash{$action}{$prefix}{$type} = $value;
22835:                             $changes{$prefix}{$type} = 1;
22836:                         }
22837:                     } else {
22838:                         $defaultshash{$action}{$prefix}{$type} = $value;
22839:                         $changes{$prefix}{$type} = 1;
22840:                     }
22841:                 } else {
22842:                     $defaultshash{$action}{$prefix}{$type} = $value;
22843:                     $changes{$prefix}{$type} = 1;
22844:                 }
22845:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
22846:                     delete($changes{$prefix}{$type});
22847:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
22848:                     delete($changes{$prefix}{$type});
22849:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
22850:                     delete($changes{$prefix}{$type});
22851:                 }
22852:             } elsif ($prefix eq 'replication') {
22853:                 if (@locations > 0) {
22854:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22855:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22856:                     my @okvals;
22857:                     foreach my $val (@vals) {
22858:                         if ($val =~ /:/) {
22859:                             my @items = split(/:/,$val);
22860:                             foreach my $item (@items) {
22861:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22862:                                     push(@okvals,$item);
22863:                                 }
22864:                             }
22865:                         } else {
22866:                             if (ref($by_location{$val}) eq 'ARRAY') {
22867:                                 push(@okvals,$val);
22868:                             }
22869:                         }
22870:                     }
22871:                     @okvals = sort(@okvals);
22872:                     if (ref($domconfig{$action}) eq 'HASH') {
22873:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22874:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22875:                                 if ($inuse == 0) {
22876:                                     $changes{$prefix}{$type} = 1;
22877:                                 } else {
22878:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22879:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22880:                                     if (@changed > 0) {
22881:                                         $changes{$prefix}{$type} = 1;
22882:                                     }
22883:                                 }
22884:                             } else {
22885:                                 if ($inuse == 1) {
22886:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22887:                                     $changes{$prefix}{$type} = 1;
22888:                                 }
22889:                             }
22890:                         } else {
22891:                             if ($inuse == 1) {
22892:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22893:                                 $changes{$prefix}{$type} = 1;
22894:                             }
22895:                         }
22896:                     } else {
22897:                         if ($inuse == 1) {
22898:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22899:                             $changes{$prefix}{$type} = 1;
22900:                         }
22901:                     }
22902:                 }
22903:             }
22904:         }
22905:     }
22906:     if (keys(%changes)) {
22907:         foreach my $prefix (keys(%changes)) {
22908:             if (ref($changes{$prefix}) eq 'HASH') {
22909:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22910:                     delete($changes{$prefix});
22911:                 }
22912:             } else {
22913:                 delete($changes{$prefix});
22914:             }
22915:         }
22916:     }
22917:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22918:     if (keys(%changes) > 0) {
22919:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22920:                                                  $dom);
22921:         if ($putresult eq 'ok') {
22922:             if (ref($defaultshash{$action}) eq 'HASH') {
22923:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22924:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22925:                 }
22926:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22927:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22928:                 }
22929:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22930:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22931:                 }
22932:             }
22933:             my $cachetime = 24*60*60;
22934:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22935:             if (ref($lastactref) eq 'HASH') {
22936:                 $lastactref->{'domdefaults'} = 1;
22937:             }
22938:             if (keys(%changes) > 0) {
22939:                 my %titles = &ssl_titles();
22940:                 $resulttext = &mt('Changes made:').'<ul>';
22941:                 foreach my $prefix (@prefixes) {
22942:                     if (ref($changes{$prefix}) eq 'HASH') {
22943:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22944:                         foreach my $type (@{$types{$prefix}}) {
22945:                             if (defined($changes{$prefix}{$type})) {
22946:                                 my ($newvalue,$notinuse);
22947:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22948:                                     if (ref($defaultshash{$action}{$prefix})) {
22949:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22950:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22951:                                         } else {
22952:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22953:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22954:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22955:                                                 }
22956:                                             } else {
22957:                                                 $notinuse = 1;
22958:                                             }
22959:                                         }
22960:                                     }
22961:                                     if ($notinuse) {
22962:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22963:                                     } elsif ($newvalue eq '') {
22964:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
22965:                                     } else {
22966:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
22967:                                     }
22968:                                 }
22969:                             }
22970:                         }
22971:                         $resulttext .= '</ul>';
22972:                     }
22973:                 }
22974:             } else {
22975:                 $resulttext = $nochgmsg;
22976:             }
22977:         } else {
22978:             $resulttext = '<span class="LC_error">'.
22979:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22980:         }
22981:     } else {
22982:         $resulttext = $nochgmsg;
22983:     }
22984:     return $resulttext;
22985: }
22986: 
22987: sub modify_trust {
22988:     my ($dom,$lastactref,%domconfig) = @_;
22989:     my (%by_ip,%by_location,@intdoms,@instdoms);
22990:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22991:     my @locations = sort(keys(%by_location));
22992:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
22993:     my @types = ('exc','inc');
22994:     my (%defaultshash,%changes);
22995:     foreach my $prefix (@prefixes) {
22996:         $defaultshash{'trust'}{$prefix} = {};
22997:     }
22998:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22999:     my $resulttext;
23000:     foreach my $prefix (@prefixes) {
23001:         foreach my $type (@types) {
23002:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
23003:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
23004:             my @okvals;
23005:             foreach my $val (@vals) {
23006:                 if ($val =~ /:/) {
23007:                     my @items = split(/:/,$val);
23008:                     foreach my $item (@items) {
23009:                         if (ref($by_location{$item}) eq 'ARRAY') {
23010:                             push(@okvals,$item);
23011:                         }
23012:                     }
23013:                 } else {
23014:                     if (ref($by_location{$val}) eq 'ARRAY') {
23015:                         push(@okvals,$val);
23016:                     }
23017:                 }
23018:             }
23019:             @okvals = sort(@okvals);
23020:             if (ref($domconfig{'trust'}) eq 'HASH') {
23021:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
23022:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23023:                         if ($inuse == 0) {
23024:                             $changes{$prefix}{$type} = 1;
23025:                         } else {
23026:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23027:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
23028:                             if (@changed > 0) {
23029:                                 $changes{$prefix}{$type} = 1;
23030:                             }
23031:                         }
23032:                     } else {
23033:                         if ($inuse == 1) {
23034:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23035:                             $changes{$prefix}{$type} = 1;
23036:                         }
23037:                     }
23038:                 } else {
23039:                     if ($inuse == 1) {
23040:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23041:                         $changes{$prefix}{$type} = 1;
23042:                     }
23043:                 }
23044:             } else {
23045:                 if ($inuse == 1) {
23046:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23047:                     $changes{$prefix}{$type} = 1;
23048:                 }
23049:             }
23050:         }
23051:     }
23052:     my $nochgmsg = &mt('No changes made to trust settings.');
23053:     if (keys(%changes) > 0) {
23054:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
23055:                                                  $dom);
23056:         if ($putresult eq 'ok') {
23057:             if (ref($defaultshash{'trust'}) eq 'HASH') {
23058:                 foreach my $prefix (@prefixes) {
23059:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
23060:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
23061:                     }
23062:                 }
23063:             }
23064:             my $cachetime = 24*60*60;
23065:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
23066:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
23067:             if (ref($lastactref) eq 'HASH') {
23068:                 $lastactref->{'domdefaults'} = 1;
23069:                 $lastactref->{'trust'} = 1;
23070:             }
23071:             if (keys(%changes) > 0) {
23072:                 my %lt = &trust_titles();
23073:                 $resulttext = &mt('Changes made:').'<ul>';
23074:                 foreach my $prefix (@prefixes) {
23075:                     if (ref($changes{$prefix}) eq 'HASH') {
23076:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
23077:                         foreach my $type (@types) {
23078:                             if (defined($changes{$prefix}{$type})) {
23079:                                 my ($newvalue,$notinuse);
23080:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
23081:                                     if (ref($defaultshash{'trust'}{$prefix})) {
23082:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23083:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
23084:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
23085:                                             }
23086:                                         } else {
23087:                                             $notinuse = 1;
23088:                                         }
23089:                                     }
23090:                                 }
23091:                                 if ($notinuse) {
23092:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
23093:                                 } elsif ($newvalue eq '') {
23094:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
23095:                                 } else {
23096:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
23097:                                 }
23098:                             }
23099:                         }
23100:                         $resulttext .= '</ul>';
23101:                     }
23102:                 }
23103:                 $resulttext .= '</ul>';
23104:             } else {
23105:                 $resulttext = $nochgmsg;
23106:             }
23107:         } else {
23108:             $resulttext = '<span class="LC_error">'.
23109:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23110:         }
23111:     } else {
23112:         $resulttext = $nochgmsg;
23113:     }
23114:     return $resulttext;
23115: }
23116: 
23117: sub modify_loadbalancing {
23118:     my ($dom,%domconfig) = @_;
23119:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
23120:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
23121:     my ($othertitle,$usertypes,$types) =
23122:         &Apache::loncommon::sorted_inst_types($dom);
23123:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23124:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
23125:     my @sparestypes = ('primary','default');
23126:     my %typetitles = &sparestype_titles();
23127:     my $resulttext;
23128:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23129:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23130:         %existing = %{$domconfig{'loadbalancing'}};
23131:     }
23132:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
23133:                               \%currtargets,\%currrules,\%currcookies);
23134:     my ($saveloadbalancing,%defaultshash,%changes);
23135:     my ($alltypes,$othertypes,$titles) =
23136:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
23137:     my %ruletitles = &offloadtype_text();
23138:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
23139:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
23140:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
23141:         if ($balancer eq '') {
23142:             next;
23143:         }
23144:         if (!exists($servers{$balancer})) {
23145:             if (exists($currbalancer{$balancer})) {
23146:                 push(@{$changes{'delete'}},$balancer);
23147:             }
23148:             next;
23149:         }
23150:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
23151:             push(@{$changes{'delete'}},$balancer);
23152:             next;
23153:         }
23154:         if (!exists($currbalancer{$balancer})) {
23155:             push(@{$changes{'add'}},$balancer);
23156:         }
23157:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
23158:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
23159:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
23160:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23161:             $saveloadbalancing = 1;
23162:         }
23163:         foreach my $sparetype (@sparestypes) {
23164:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
23165:             my @offloadto;
23166:             foreach my $target (@targets) {
23167:                 if (($servers{$target}) && ($target ne $balancer)) {
23168:                     if ($sparetype eq 'default') {
23169:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
23170:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
23171:                         }
23172:                     }
23173:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
23174:                         push(@offloadto,$target);
23175:                     }
23176:                 }
23177:             }
23178:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
23179:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
23180:                     push(@offloadto,$balancer);
23181:                 }
23182:             }
23183:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
23184:         }
23185:         if ($env{'form.loadbalancing_cookie_'.$i}) {
23186:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
23187:             if (exists($currbalancer{$balancer})) {
23188:                 unless ($currcookies{$balancer}) {
23189:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
23190:                 }
23191:             }
23192:         } elsif (exists($currbalancer{$balancer})) {
23193:             if ($currcookies{$balancer}) {
23194:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
23195:             }
23196:         }
23197:         if (ref($currtargets{$balancer}) eq 'HASH') {
23198:             foreach my $sparetype (@sparestypes) {
23199:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
23200:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
23201:                     if (@targetdiffs > 0) {
23202:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23203:                     }
23204:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23205:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23206:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23207:                     }
23208:                 }
23209:             }
23210:         } else {
23211:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
23212:                 foreach my $sparetype (@sparestypes) {
23213:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23214:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23215:                             $changes{'curr'}{$balancer}{'targets'} = 1;
23216:                         }
23217:                     }
23218:                 }
23219:             }
23220:         }
23221:         my $ishomedom;
23222:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
23223:             $ishomedom = 1;
23224:         }
23225:         if (ref($alltypes) eq 'ARRAY') {
23226:             foreach my $type (@{$alltypes}) {
23227:                 my $rule;
23228:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
23229:                          (!$ishomedom)) {
23230:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
23231:                 }
23232:                 if ($rule eq 'specific') {
23233:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
23234:                     if (exists($servers{$specifiedhost})) {
23235:                         $rule = $specifiedhost;
23236:                     }
23237:                 }
23238:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
23239:                 if (ref($currrules{$balancer}) eq 'HASH') {
23240:                     if ($rule ne $currrules{$balancer}{$type}) {
23241:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23242:                     }
23243:                 } elsif ($rule ne '') {
23244:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23245:                 }
23246:             }
23247:         }
23248:     }
23249:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
23250:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
23251:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
23252:             $defaultshash{'loadbalancing'} = {};
23253:         }
23254:         my $putresult = &Apache::lonnet::put_dom('configuration',
23255:                                                  \%defaultshash,$dom);
23256:         if ($putresult eq 'ok') {
23257:             if (keys(%changes) > 0) {
23258:                 my %toupdate;
23259:                 if (ref($changes{'delete'}) eq 'ARRAY') {
23260:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
23261:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
23262:                         $toupdate{$balancer} = 1;
23263:                     }
23264:                 }
23265:                 if (ref($changes{'add'}) eq 'ARRAY') {
23266:                     foreach my $balancer (sort(@{$changes{'add'}})) {
23267:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
23268:                         $toupdate{$balancer} = 1;
23269:                     }
23270:                 }
23271:                 if (ref($changes{'curr'}) eq 'HASH') {
23272:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
23273:                         $toupdate{$balancer} = 1;
23274:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
23275:                             if ($changes{'curr'}{$balancer}{'targets'}) {
23276:                                 my %offloadstr;
23277:                                 foreach my $sparetype (@sparestypes) {
23278:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23279:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23280:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23281:                                         }
23282:                                     }
23283:                                 }
23284:                                 if (keys(%offloadstr) == 0) {
23285:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
23286:                                 } else {
23287:                                     my $showoffload;
23288:                                     foreach my $sparetype (@sparestypes) {
23289:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
23290:                                         if (defined($offloadstr{$sparetype})) {
23291:                                             $showoffload .= $offloadstr{$sparetype};
23292:                                         } else {
23293:                                             $showoffload .= &mt('None');
23294:                                         }
23295:                                         $showoffload .= ('&nbsp;'x3);
23296:                                     }
23297:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
23298:                                 }
23299:                             }
23300:                         }
23301:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
23302:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
23303:                                 foreach my $type (@{$alltypes}) {
23304:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
23305:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23306:                                         my $balancetext;
23307:                                         if ($rule eq '') {
23308:                                             $balancetext =  $ruletitles{'default'};
23309:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
23310:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23311:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23312:                                                 foreach my $sparetype (@sparestypes) {
23313:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23314:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23315:                                                     }
23316:                                                 }
23317:                                                 foreach my $item (@{$alltypes}) {
23318:                                                     next if ($item =~  /^_LC_ipchange/);
23319:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
23320:                                                     if ($hasrule eq 'homeserver') {
23321:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
23322:                                                     } else {
23323:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
23324:                                                             if ($servers{$hasrule}) {
23325:                                                                 $toupdate{$hasrule} = 1;
23326:                                                             }
23327:                                                         }
23328:                                                     }
23329:                                                 }
23330:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
23331:                                                     $balancetext =  $ruletitles{$rule};
23332:                                                 } else {
23333:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23334:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
23335:                                                     if ($receiver) {
23336:                                                         $toupdate{$receiver};
23337:                                                     }
23338:                                                 }
23339:                                             } else {
23340:                                                 $balancetext =  $ruletitles{$rule};
23341:                                             }
23342:                                         } else {
23343:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
23344:                                         }
23345:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
23346:                                     }
23347:                                 }
23348:                             }
23349:                         }
23350:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
23351:                             if ($currcookies{$balancer}) {
23352:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
23353:                                                           $balancer).'</li>';
23354:                             } else {
23355:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
23356:                                                           $balancer).'</li>';
23357:                             }
23358:                         }
23359:                     }
23360:                 }
23361:                 if (keys(%toupdate)) {
23362:                     my %thismachine;
23363:                     my $updatedhere;
23364:                     my $cachetime = 60*60*24;
23365:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23366:                     foreach my $lonhost (keys(%toupdate)) {
23367:                         if ($thismachine{$lonhost}) {
23368:                             unless ($updatedhere) {
23369:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
23370:                                                               $defaultshash{'loadbalancing'},
23371:                                                               $cachetime);
23372:                                 $updatedhere = 1;
23373:                             }
23374:                         } else {
23375:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
23376:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
23377:                         }
23378:                     }
23379:                 }
23380:                 if ($resulttext ne '') {
23381:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
23382:                 } else {
23383:                     $resulttext = $nochgmsg;
23384:                 }
23385:             } else {
23386:                 $resulttext = $nochgmsg;
23387:             }
23388:         } else {
23389:             $resulttext = '<span class="LC_error">'.
23390:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23391:         }
23392:     } else {
23393:         $resulttext = $nochgmsg;
23394:     }
23395:     return $resulttext;
23396: }
23397: 
23398: sub recurse_check {
23399:     my ($chkcats,$categories,$depth,$name) = @_;
23400:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
23401:         my $chg = 0;
23402:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
23403:             my $category = $chkcats->[$depth]{$name}[$j];
23404:             my $item;
23405:             if ($category eq '') {
23406:                 $chg ++;
23407:             } else {
23408:                 my $deeper = $depth + 1;
23409:                 $item = &escape($category).':'.&escape($name).':'.$depth;
23410:                 if ($chg) {
23411:                     $categories->{$item} -= $chg;
23412:                 }
23413:                 &recurse_check($chkcats,$categories,$deeper,$category);
23414:                 $deeper --;
23415:             }
23416:         }
23417:     }
23418:     return;
23419: }
23420: 
23421: sub recurse_cat_deletes {
23422:     my ($item,$coursecategories,$deletions) = @_;
23423:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
23424:     my $subdepth = $depth + 1;
23425:     if (ref($coursecategories) eq 'HASH') {
23426:         foreach my $subitem (keys(%{$coursecategories})) {
23427:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
23428:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
23429:                 delete($coursecategories->{$subitem});
23430:                 $deletions->{$subitem} = 1;
23431:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
23432:             }
23433:         }
23434:     }
23435:     return;
23436: }
23437: 
23438: sub active_dc_picker {
23439:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
23440:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
23441:     my @domcoord = keys(%domcoords);
23442:     if (keys(%currhash)) {
23443:         foreach my $dc (keys(%currhash)) {
23444:             unless (exists($domcoords{$dc})) {
23445:                 push(@domcoord,$dc);
23446:             }
23447:         }
23448:     }
23449:     @domcoord = sort(@domcoord);
23450:     my $numdcs = scalar(@domcoord);
23451:     my $rows = 0;
23452:     my $table;
23453:     if ($numdcs > 1) {
23454:         $table = '<table>';
23455:         for (my $i=0; $i<@domcoord; $i++) {
23456:             my $rem = $i%($numinrow);
23457:             if ($rem == 0) {
23458:                 if ($i > 0) {
23459:                     $table .= '</tr>';
23460:                 }
23461:                 $table .= '<tr>';
23462:                 $rows ++;
23463:             }
23464:             my $check = '';
23465:             if ($inputtype eq 'radio') {
23466:                 if (keys(%currhash) == 0) {
23467:                     if (!$i) {
23468:                         $check = ' checked="checked"';
23469:                     }
23470:                 } elsif (exists($currhash{$domcoord[$i]})) {
23471:                     $check = ' checked="checked"';
23472:                 }
23473:             } else {
23474:                 if (exists($currhash{$domcoord[$i]})) {
23475:                     $check = ' checked="checked"';
23476:                 }
23477:             }
23478:             if ($i == @domcoord - 1) {
23479:                 my $colsleft = $numinrow - $rem;
23480:                 if ($colsleft > 1) {
23481:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
23482:                 } else {
23483:                     $table .= '<td class="LC_left_item">';
23484:                 }
23485:             } else {
23486:                 $table .= '<td class="LC_left_item">';
23487:             }
23488:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
23489:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23490:             $table .= '<span class="LC_nobreak"><label>'.
23491:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
23492:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
23493:             if ($user ne $dcname.':'.$dcdom) {
23494:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23495:             }
23496:             $table .= '</label></span></td>';
23497:         }
23498:         $table .= '</tr></table>';
23499:     } elsif ($numdcs == 1) {
23500:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
23501:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23502:         if ($inputtype eq 'radio') {
23503:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
23504:             if ($user ne $dcname.':'.$dcdom) {
23505:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23506:             }
23507:         } else {
23508:             my $check;
23509:             if (exists($currhash{$domcoord[0]})) {
23510:                 $check = ' checked="checked"';
23511:             }
23512:             $table = '<span class="LC_nobreak"><label>'.
23513:                      '<input type="checkbox" name="'.$name.'" '.
23514:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
23515:             if ($user ne $dcname.':'.$dcdom) {
23516:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23517:             }
23518:             $table .= '</label></span>';
23519:             $rows ++;
23520:         }
23521:     }
23522:     return ($numdcs,$table,$rows);
23523: }
23524: 
23525: sub usersession_titles {
23526:     return &Apache::lonlocal::texthash(
23527:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
23528:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
23529:                spares => 'Servers offloaded to, when busy',
23530:                version => 'LON-CAPA version requirement',
23531:                excludedomain => 'Allow all, but exclude specific domains',
23532:                includedomain => 'Deny all, but include specific domains',
23533:                primary => 'Primary (checked first)',
23534:                default => 'Default',
23535:            );
23536: }
23537: 
23538: sub id_for_thisdom {
23539:     my (%servers) = @_;
23540:     my %altids;
23541:     foreach my $server (keys(%servers)) {
23542:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
23543:         if ($serverhome ne $server) {
23544:             $altids{$serverhome} = $server;
23545:         }
23546:     }
23547:     return %altids;
23548: }
23549: 
23550: sub count_servers {
23551:     my ($currbalancer,%servers) = @_;
23552:     my (@spares,$numspares);
23553:     foreach my $lonhost (sort(keys(%servers))) {
23554:         next if ($currbalancer eq $lonhost);
23555:         push(@spares,$lonhost);
23556:     }
23557:     if ($currbalancer) {
23558:         $numspares = scalar(@spares);
23559:     } else {
23560:         $numspares = scalar(@spares) - 1;
23561:     }
23562:     return ($numspares,@spares);
23563: }
23564: 
23565: sub lonbalance_targets_js {
23566:     my ($dom,$types,$servers,$settings) = @_;
23567:     my $select = &mt('Select');
23568:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
23569:     if (ref($servers) eq 'HASH') {
23570:         $alltargets = join("','",sort(keys(%{$servers})));
23571:         my @homedoms;
23572:         foreach my $server (sort(keys(%{$servers}))) {
23573:             if (&Apache::lonnet::host_domain($server) eq $dom) {
23574:                 push(@homedoms,'1');
23575:             } else {
23576:                 push(@homedoms,'0');
23577:             }
23578:         }
23579:         $allishome = join("','",@homedoms);
23580:     }
23581:     if (ref($types) eq 'ARRAY') {
23582:         if (@{$types} > 0) {
23583:             @alltypes = @{$types};
23584:         }
23585:     }
23586:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
23587:     $allinsttypes = join("','",@alltypes);
23588:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23589:     if (ref($settings) eq 'HASH') {
23590:         %existing = %{$settings};
23591:     }
23592:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
23593:                               \%currtargets,\%currrules,\%currcookies);
23594:     my $balancers = join("','",sort(keys(%currbalancer)));
23595:     return <<"END";
23596: 
23597: <script type="text/javascript">
23598: // <![CDATA[
23599: 
23600: currBalancers = new Array('$balancers');
23601: 
23602: function toggleTargets(balnum) {
23603:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23604:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
23605:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
23606:     var prevbalancer = prevhostitem.value;
23607:     var baltotal = document.getElementById('loadbalancing_total').value;
23608:     prevhostitem.value = balancer;
23609:     if (prevbalancer != '') {
23610:         var prevIdx = currBalancers.indexOf(prevbalancer);
23611:         if (prevIdx != -1) {
23612:             currBalancers.splice(prevIdx,1);
23613:         }
23614:     }
23615:     if (balancer == '') {
23616:         hideSpares(balnum);
23617:     } else {
23618:         var currIdx = currBalancers.indexOf(balancer);
23619:         if (currIdx == -1) {
23620:             currBalancers.push(balancer);
23621:         }
23622:         var homedoms = new Array('$allishome');
23623:         var ishomedom = homedoms[lonhostitem.selectedIndex];
23624:         showSpares(balancer,ishomedom,balnum);
23625:     }
23626:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
23627:     return;
23628: }
23629: 
23630: function showSpares(balancer,ishomedom,balnum) {
23631:     var alltargets = new Array('$alltargets');
23632:     var insttypes = new Array('$allinsttypes');
23633:     var offloadtypes = new Array('primary','default');
23634: 
23635:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
23636:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
23637:  
23638:     for (var i=0; i<offloadtypes.length; i++) {
23639:         var count = 0;
23640:         for (var j=0; j<alltargets.length; j++) {
23641:             if (alltargets[j] != balancer) {
23642:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
23643:                 item.value = alltargets[j];
23644:                 item.style.textAlign='left';
23645:                 item.style.textFace='normal';
23646:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
23647:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
23648:                     item.disabled = '';
23649:                 } else {
23650:                     item.disabled = 'disabled';
23651:                     item.checked = false;
23652:                 }
23653:                 count ++;
23654:             }
23655:         }
23656:     }
23657:     for (var k=0; k<insttypes.length; k++) {
23658:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
23659:             if (ishomedom == 1) {
23660:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23661:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23662:             } else {
23663:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23664:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23665:             }
23666:         } else {
23667:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23668:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23669:         }
23670:         if ((insttypes[k] != '_LC_external') && 
23671:             ((insttypes[k] != '_LC_internetdom') ||
23672:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
23673:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
23674:             item.options.length = 0;
23675:             item.options[0] = new Option("","",true,true);
23676:             var idx = 0;
23677:             for (var m=0; m<alltargets.length; m++) {
23678:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
23679:                     idx ++;
23680:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23681:                 }
23682:             }
23683:         }
23684:     }
23685:     return;
23686: }
23687: 
23688: function hideSpares(balnum) {
23689:     var alltargets = new Array('$alltargets');
23690:     var insttypes = new Array('$allinsttypes');
23691:     var offloadtypes = new Array('primary','default');
23692: 
23693:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
23694:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
23695: 
23696:     var total = alltargets.length - 1;
23697:     for (var i=0; i<offloadtypes; i++) {
23698:         for (var j=0; j<total; j++) {
23699:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
23700:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
23701:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
23702:         }
23703:     }
23704:     for (var k=0; k<insttypes.length; k++) {
23705:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23706:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23707:         if (insttypes[k] != '_LC_external') {
23708:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
23709:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
23710:         }
23711:     }
23712:     return;
23713: }
23714: 
23715: function checkOffloads(item,balnum,type) {
23716:     var alltargets = new Array('$alltargets');
23717:     var offloadtypes = new Array('primary','default');
23718:     if (item.checked) {
23719:         var total = alltargets.length - 1;
23720:         var other;
23721:         if (type == offloadtypes[0]) {
23722:             other = offloadtypes[1];
23723:         } else {
23724:             other = offloadtypes[0];
23725:         }
23726:         for (var i=0; i<total; i++) {
23727:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
23728:             if (server == item.value) {
23729:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
23730:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
23731:                 }
23732:             }
23733:         }
23734:     }
23735:     return;
23736: }
23737: 
23738: function singleServerToggle(balnum,type) {
23739:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
23740:     if (offloadtoSelIdx == 0) {
23741:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
23742:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23743: 
23744:     } else {
23745:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
23746:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23747:     }
23748:     return;
23749: }
23750: 
23751: function balanceruleChange(formname,balnum,type) {
23752:     if (type == '_LC_external') {
23753:         return;
23754:     }
23755:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
23756:     for (var i=0; i<typesRules.length; i++) {
23757:         if (formname.elements[typesRules[i]].checked) {
23758:             if (formname.elements[typesRules[i]].value != 'specific') {
23759:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
23760:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23761:             } else {
23762:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23763:             }
23764:         }
23765:     }
23766:     return;
23767: }
23768: 
23769: function balancerDeleteChange(balnum) {
23770:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23771:     var baltotal = document.getElementById('loadbalancing_total').value;
23772:     var addtarget;
23773:     var removetarget;
23774:     var action = 'delete';
23775:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
23776:         var lonhost = hostitem.value;
23777:         var currIdx = currBalancers.indexOf(lonhost);
23778:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
23779:             if (currIdx != -1) {
23780:                 currBalancers.splice(currIdx,1);
23781:             }
23782:             addtarget = lonhost;
23783:         } else {
23784:             if (currIdx == -1) {
23785:                 currBalancers.push(lonhost);
23786:             }
23787:             removetarget = lonhost;
23788:             action = 'undelete';
23789:         }
23790:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
23791:     }
23792:     return;
23793: }
23794: 
23795: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
23796:     if (baltotal > 1) {
23797:         var offloadtypes = new Array('primary','default');
23798:         var alltargets = new Array('$alltargets');
23799:         var insttypes = new Array('$allinsttypes');
23800:         for (var i=0; i<baltotal; i++) {
23801:             if (i != balnum) {
23802:                 for (var j=0; j<offloadtypes.length; j++) {
23803:                     var total = alltargets.length - 1;
23804:                     for (var k=0; k<total; k++) {
23805:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
23806:                         var server = serveritem.value;
23807:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
23808:                             if (server == addtarget) {
23809:                                 serveritem.disabled = '';
23810:                             }
23811:                         }
23812:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23813:                             if (server == removetarget) {
23814:                                 serveritem.disabled = 'disabled';
23815:                                 serveritem.checked = false;
23816:                             }
23817:                         }
23818:                     }
23819:                 }
23820:                 for (var j=0; j<insttypes.length; j++) {
23821:                     if (insttypes[j] != '_LC_external') {
23822:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
23823:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
23824:                             var currSel = singleserver.selectedIndex;
23825:                             var currVal = singleserver.options[currSel].value;
23826:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
23827:                                 var numoptions = singleserver.options.length;
23828:                                 var needsnew = 1;
23829:                                 for (var k=0; k<numoptions; k++) {
23830:                                     if (singleserver.options[k] == addtarget) {
23831:                                         needsnew = 0;
23832:                                         break;
23833:                                     }
23834:                                 }
23835:                                 if (needsnew == 1) {
23836:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
23837:                                 }
23838:                             }
23839:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23840:                                 singleserver.options.length = 0;
23841:                                 if ((currVal) && (currVal != removetarget)) {
23842:                                     singleserver.options[0] = new Option("","",false,false);
23843:                                 } else {
23844:                                     singleserver.options[0] = new Option("","",true,true);
23845:                                 }
23846:                                 var idx = 0;
23847:                                 for (var m=0; m<alltargets.length; m++) {
23848:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
23849:                                         idx ++;
23850:                                         if (currVal == alltargets[m]) {
23851:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
23852:                                         } else {
23853:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23854:                                         }
23855:                                     }
23856:                                 }
23857:                             }
23858:                         }
23859:                     }
23860:                 }
23861:             }
23862:         }
23863:     }
23864:     return;
23865: }
23866: 
23867: // ]]>
23868: </script>
23869: 
23870: END
23871: }
23872: 
23873: sub new_spares_js {
23874:     my @sparestypes = ('primary','default');
23875:     my $types = join("','",@sparestypes);
23876:     my $select = &mt('Select');
23877:     return <<"END";
23878: 
23879: <script type="text/javascript">
23880: // <![CDATA[
23881: 
23882: function updateNewSpares(formname,lonhost) {
23883:     var types = new Array('$types');
23884:     var include = new Array();
23885:     var exclude = new Array();
23886:     for (var i=0; i<types.length; i++) {
23887:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23888:         for (var j=0; j<spareboxes.length; j++) {
23889:             if (formname.elements[spareboxes[j]].checked) {
23890:                 exclude.push(formname.elements[spareboxes[j]].value);
23891:             } else {
23892:                 include.push(formname.elements[spareboxes[j]].value);
23893:             }
23894:         }
23895:     }
23896:     for (var i=0; i<types.length; i++) {
23897:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23898:         var selIdx = newSpare.selectedIndex;
23899:         var currnew = newSpare.options[selIdx].value;
23900:         var okSpares = new Array();
23901:         for (var j=0; j<newSpare.options.length; j++) {
23902:             var possible = newSpare.options[j].value;
23903:             if (possible != '') {
23904:                 if (exclude.indexOf(possible) == -1) {
23905:                     okSpares.push(possible);
23906:                 } else {
23907:                     if (currnew == possible) {
23908:                         selIdx = 0;
23909:                     }
23910:                 }
23911:             }
23912:         }
23913:         for (var k=0; k<include.length; k++) {
23914:             if (okSpares.indexOf(include[k]) == -1) {
23915:                 okSpares.push(include[k]);
23916:             }
23917:         }
23918:         okSpares.sort();
23919:         newSpare.options.length = 0;
23920:         if (selIdx == 0) {
23921:             newSpare.options[0] = new Option("$select","",true,true);
23922:         } else {
23923:             newSpare.options[0] = new Option("$select","",false,false);
23924:         }
23925:         for (var m=0; m<okSpares.length; m++) {
23926:             var idx = m+1;
23927:             var selThis = 0;
23928:             if (selIdx != 0) {
23929:                 if (okSpares[m] == currnew) {
23930:                     selThis = 1;
23931:                 }
23932:             }
23933:             if (selThis == 1) {
23934:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23935:             } else {
23936:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23937:             }
23938:         }
23939:     }
23940:     return;
23941: }
23942: 
23943: function checkNewSpares(lonhost,type) {
23944:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23945:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23946:     if (chosen != '') {
23947:         var othertype;
23948:         var othernewSpare;
23949:         if (type == 'primary') {
23950:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23951:         }
23952:         if (type == 'default') {
23953:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23954:         }
23955:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23956:             othernewSpare.selectedIndex = 0;
23957:         }
23958:     }
23959:     return;
23960: }
23961: 
23962: // ]]>
23963: </script>
23964: 
23965: END
23966: 
23967: }
23968: 
23969: sub common_domprefs_js {
23970:     return <<"END";
23971: 
23972: <script type="text/javascript">
23973: // <![CDATA[
23974: 
23975: function getIndicesByName(formname,item) {
23976:     var group = new Array();
23977:     for (var i=0;i<formname.elements.length;i++) {
23978:         if (formname.elements[i].name == item) {
23979:             group.push(formname.elements[i].id);
23980:         }
23981:     }
23982:     return group;
23983: }
23984: 
23985: // ]]>
23986: </script>
23987: 
23988: END
23989: 
23990: }
23991: 
23992: sub recaptcha_js {
23993:     my %lt = &captcha_phrases();
23994:     return <<"END";
23995: 
23996: <script type="text/javascript">
23997: // <![CDATA[
23998: 
23999: function updateCaptcha(caller,context) {
24000:     var privitem;
24001:     var pubitem;
24002:     var privtext;
24003:     var pubtext;
24004:     var versionitem;
24005:     var versiontext;
24006:     if (document.getElementById(context+'_recaptchapub')) {
24007:         pubitem = document.getElementById(context+'_recaptchapub');
24008:     } else {
24009:         return;
24010:     }
24011:     if (document.getElementById(context+'_recaptchapriv')) {
24012:         privitem = document.getElementById(context+'_recaptchapriv');
24013:     } else {
24014:         return;
24015:     }
24016:     if (document.getElementById(context+'_recaptchapubtxt')) {
24017:         pubtext = document.getElementById(context+'_recaptchapubtxt');
24018:     } else {
24019:         return;
24020:     }
24021:     if (document.getElementById(context+'_recaptchaprivtxt')) {
24022:         privtext = document.getElementById(context+'_recaptchaprivtxt');
24023:     } else {
24024:         return;
24025:     }
24026:     if (document.getElementById(context+'_recaptchaversion')) {
24027:         versionitem = document.getElementById(context+'_recaptchaversion');
24028:     } else {
24029:         return;
24030:     }
24031:     if (document.getElementById(context+'_recaptchavertxt')) {
24032:         versiontext = document.getElementById(context+'_recaptchavertxt');
24033:     } else {
24034:         return;
24035:     }
24036:     if (caller.checked) {
24037:         if (caller.value == 'recaptcha') {
24038:             pubitem.type = 'text';
24039:             privitem.type = 'text';
24040:             pubitem.size = '40';
24041:             privitem.size = '40';
24042:             pubtext.innerHTML = "$lt{'pub'}";
24043:             privtext.innerHTML = "$lt{'priv'}";
24044:             versionitem.type = 'text';
24045:             versionitem.size = '3';
24046:             versiontext.innerHTML = "$lt{'ver'}";
24047:         } else {
24048:             pubitem.type = 'hidden';
24049:             privitem.type = 'hidden';
24050:             versionitem.type = 'hidden';
24051:             pubtext.innerHTML = '';
24052:             privtext.innerHTML = '';
24053:             versiontext.innerHTML = '';
24054:         }
24055:     }
24056:     return;
24057: }
24058: 
24059: // ]]>
24060: </script>
24061: 
24062: END
24063: 
24064: }
24065: 
24066: sub toggle_display_js {
24067:     return <<"END";
24068: 
24069: <script type="text/javascript">
24070: // <![CDATA[
24071: 
24072: function toggleDisplay(domForm,caller) {
24073:     if (document.getElementById(caller)) {
24074:         var divitem = document.getElementById(caller);
24075:         var optionsElement = domForm.coursecredits;
24076:         var checkval = 1;
24077:         var dispval = 'block';
24078:         var selfcreateRegExp = /^cancreate_emailverified/;
24079:         if (caller == 'emailoptions') {
24080:             optionsElement = domForm.cancreate_email;
24081:         }
24082:         if (caller == 'studentsubmission') {
24083:             optionsElement = domForm.postsubmit;
24084:         }
24085:         if (caller == 'cloneinstcode') {
24086:             optionsElement = domForm.canclone;
24087:             checkval = 'instcode';
24088:         }
24089:         if (selfcreateRegExp.test(caller)) {
24090:             optionsElement = domForm.elements[caller];
24091:             checkval = 'other';
24092:             dispval = 'inline'
24093:         }
24094:         if (optionsElement.length) {
24095:             var currval;
24096:             for (var i=0; i<optionsElement.length; i++) {
24097:                 if (optionsElement[i].checked) {
24098:                    currval = optionsElement[i].value;
24099:                 }
24100:             }
24101:             if (currval == checkval) {
24102:                 divitem.style.display = dispval;
24103:             } else {
24104:                 divitem.style.display = 'none';
24105:             }
24106:         }
24107:     }
24108:     return;
24109: }
24110: 
24111: // ]]>
24112: </script>
24113: 
24114: END
24115: 
24116: }
24117: 
24118: sub captcha_phrases {
24119:     return &Apache::lonlocal::texthash (
24120:                  priv => 'Private key',
24121:                  pub  => 'Public key',
24122:                  original  => 'original (CAPTCHA)',
24123:                  recaptcha => 'successor (ReCAPTCHA)',
24124:                  notused   => 'unused',
24125:                  ver => 'ReCAPTCHA version (1 or 2)',
24126:     );
24127: }
24128: 
24129: sub devalidate_remote_domconfs {
24130:     my ($dom,$cachekeys) = @_;
24131:     return unless (ref($cachekeys) eq 'HASH');
24132:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
24133:     my %thismachine;
24134:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
24135:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
24136:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
24137:                       'ipaccess','trust');
24138:     my %cache_by_lonhost;
24139:     if (exists($cachekeys->{'samllanding'})) {
24140:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
24141:             my %landing = %{$cachekeys->{'samllanding'}};
24142:             my %domservers = &Apache::lonnet::get_servers($dom);
24143:             if (keys(%domservers)) {
24144:                 foreach my $server (keys(%domservers)) {
24145:                     my @cached;
24146:                     next if ($thismachine{$server});
24147:                     if ($landing{$server}) {
24148:                         push(@cached,&escape('samllanding').':'.&escape($server));
24149:                     }
24150:                     if (@cached) {
24151:                         $cache_by_lonhost{$server} = \@cached;
24152:                     }
24153:                 }
24154:             }
24155:         }
24156:     }
24157:     if (keys(%servers)) {
24158:         foreach my $server (keys(%servers)) {
24159:             next if ($thismachine{$server});
24160:             my @cached;
24161:             foreach my $name (@posscached) {
24162:                 if ($cachekeys->{$name}) {
24163:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
24164:                         if (ref($cachekeys->{$name}) eq 'HASH') {
24165:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
24166:                                 push(@cached,&escape($name).':'.&escape($key));
24167:                             }
24168:                         }
24169:                     } else {
24170:                         push(@cached,&escape($name).':'.&escape($dom));
24171:                     }
24172:                 }
24173:             }
24174:             if ((exists($cache_by_lonhost{$server})) &&
24175:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
24176:                 push(@cached,@{$cache_by_lonhost{$server}});
24177:             }
24178:             if (@cached) {
24179:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
24180:             }
24181:         }
24182:     }
24183:     return;
24184: }
24185: 
24186: 1;

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