File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.439: download - view: text, annotated - select for diffs
Wed Apr 17 01:02:47 2024 UTC (7 weeks, 2 days ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Fix typo. Display changes to Available Editors in case where domain defaults
  for Authoring Space have not previously been set.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.439 2024/04/17 01:02:47 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, textbook, placement, and lti).  
  107: In each case the radio buttons allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use Apache::courseprefs();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: use Crypt::CBC;
  181: 
  182: my $registered_cleanup;
  183: my $modified_urls;
  184: 
  185: sub handler {
  186:     my $r=shift;
  187:     if ($r->header_only) {
  188:         &Apache::loncommon::content_type($r,'text/html');
  189:         $r->send_http_header;
  190:         return OK;
  191:     }
  192: 
  193:     my $context = 'domain';
  194:     my $dom = $env{'request.role.domain'};
  195:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  196:     if (&Apache::lonnet::allowed('mau',$dom)) {
  197:         &Apache::loncommon::content_type($r,'text/html');
  198:         $r->send_http_header;
  199:     } else {
  200:         $env{'user.error.msg'}=
  201:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  202:         return HTTP_NOT_ACCEPTABLE;
  203:     }
  204: 
  205:     $registered_cleanup=0;
  206:     @{$modified_urls}=();
  207: 
  208:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                             ['phase','actions']);
  211:     my $phase = 'pickactions';
  212:     if ( exists($env{'form.phase'}) ) {
  213:         $phase = $env{'form.phase'};
  214:     }
  215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  216:     my %domconfig =
  217:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  218:                 'quotas','autoenroll','autoupdate','autocreate',
  219:                 'directorysrch','usercreation','usermodification',
  220:                 'contacts','defaults','scantron','coursecategories',
  221:                 'serverstatuses','requestcourses','helpsettings',
  222:                 'coursedefaults','usersessions','loadbalancing',
  223:                 'requestauthor','selfenrollment','inststatus',
  224:                 'ltitools','toolsec','ssl','trust','lti','ltisec',
  225:                 'privacy','passwords','proctoring','wafproxy',
  226:                 'ipaccess','authordefaults'],$dom);
  227:     my %encconfig =
  228:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  229:     my ($checked_is_home,$is_home);
  230:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  231:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  232:             my $home = &Apache::lonnet::domain($dom,'primary');
  233:             unless (($home eq 'no_host') || ($home eq '')) {
  234:                 my @ids=&Apache::lonnet::current_machine_ids();
  235:                 if (grep(/^\Q$home\E$/,@ids)) {
  236:                     $is_home = 1;
  237:                 }
  238:             }
  239:             $checked_is_home = 1;
  240:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  241:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  243:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  244:                     if (($is_home) && ($phase eq 'process')) {
  245:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'lti'}) eq 'HASH') {
  252:         if (ref($encconfig{'lti'}) eq 'HASH') {
  253:             unless ($checked_is_home) {
  254:                 my $home = &Apache::lonnet::domain($dom,'primary');
  255:                 unless (($home eq 'no_host') || ($home eq '')) {
  256:                     my @ids=&Apache::lonnet::current_machine_ids();
  257:                     if (grep(/^\Q$home\E$/,@ids)) {
  258:                         $is_home = 1;
  259:                     }
  260:                 }
  261:                 $checked_is_home = 1;
  262:             }
  263:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  264:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  265:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  266:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  267:                     if (($is_home) && ($phase eq 'process')) {
  268:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  269:                     }
  270:                 }
  271:             }
  272:         }
  273:     }
  274:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  275:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  276:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  277:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  278:                     unless ($id =~ /^\d+$/) {
  279:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  280:                     }
  281:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  282:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  283:                         foreach my $item ('key','secret') {
  284:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  285:                         }
  286:                     }
  287:                 }
  288:             }
  289:         }
  290:     }
  291:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  292:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  293:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  294:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  295:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  296:                     foreach my $item ('key','secret') {
  297:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  298:                     }
  299:                 }
  300:             }
  301:         }
  302:     }
  303:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  304:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  305:                        'contacts','privacy','usercreation','selfcreation',
  306:                        'usermodification','scantron','requestcourses','requestauthor',
  307:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  308:                        'authordefaults','ltitools','proctoring','selfenrollment',
  309:                        'usersessions','ssl','trust','lti');
  310:     my %existing;
  311:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  312:         %existing = %{$domconfig{'loadbalancing'}};
  313:     }
  314:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  315:         push(@prefs_order,'loadbalancing');
  316:     }
  317:     my %prefs = (
  318:         'rolecolors' =>
  319:                    { text => 'Default color schemes',
  320:                      help => 'Domain_Configuration_Color_Schemes',
  321:                      header => [{col1 => 'Student Settings',
  322:                                  col2 => '',},
  323:                                 {col1 => 'Coordinator Settings',
  324:                                  col2 => '',},
  325:                                 {col1 => 'Author Settings',
  326:                                  col2 => '',},
  327:                                 {col1 => 'Administrator Settings',
  328:                                  col2 => '',}],
  329:                       print => \&print_rolecolors,
  330:                       modify => \&modify_rolecolors,
  331:                     },
  332:         'login' =>
  333:                     { text => 'Log-in page options',
  334:                       help => 'Domain_Configuration_Login_Page',
  335:                       header => [{col1 => 'Log-in Page Items',
  336:                                   col2 => '',},
  337:                                  {col1 => 'Log-in Help',
  338:                                   col2 => 'Value'},
  339:                                  {col1 => 'Custom HTML in document head',
  340:                                   col2 => 'Value'},
  341:                                  {col1 => 'SSO',
  342:                                   col2 => 'Dual login: SSO and non-SSO options'},
  343:                                 ],
  344:                       print => \&print_login,
  345:                       modify => \&modify_login,
  346:                     },
  347:         'defaults' => 
  348:                     { text => 'Default authentication/language/timezone/portal/types',
  349:                       help => 'Domain_Configuration_LangTZAuth',
  350:                       header => [{col1 => 'Setting',
  351:                                   col2 => 'Value'},
  352:                                  {col1 => 'Institutional user types',
  353:                                   col2 => 'Name displayed'},
  354:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  355:                                   col2 => 'Rules in use'}],
  356:                       print => \&print_defaults,
  357:                       modify => \&modify_defaults,
  358:                     },
  359:         'wafproxy' =>
  360:                     { text => 'Web Application Firewall/Reverse Proxy',
  361:                       help => 'Domain_Configuration_WAF_Proxy',
  362:                       header => [{col1 => 'Domain(s)',
  363:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  364:                                  },
  365:                                  {col1 => 'Domain(s)',
  366:                                   col2 => 'WAF Configuration',}],
  367:                       print => \&print_wafproxy,
  368:                       modify => \&modify_wafproxy,
  369:                     },
  370:         'passwords' =>
  371:                     { text => 'Passwords (Internal authentication)',
  372:                       help => 'Domain_Configuration_Passwords',
  373:                       header => [{col1 => 'Resetting Forgotten Password',
  374:                                   col2 => 'Settings'},
  375:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  376:                                   col2 => 'Settings'},
  377:                                  {col1 => 'Rules for LON-CAPA Passwords',
  378:                                   col2 => 'Settings'},
  379:                                  {col1 => 'Course Owner Changing Student Passwords',
  380:                                   col2 => 'Settings'}],
  381:                       print => \&print_passwords,
  382:                       modify => \&modify_passwords,
  383:                     },
  384:         'quotas' => 
  385:                     { text => 'Blogs, personal pages/timezones, portfolio/quotas',
  386:                       help => 'Domain_Configuration_Quotas',
  387:                       header => [{col1 => 'User affiliation',
  388:                                   col2 => 'Available tools',
  389:                                   col3 => 'Portfolio quota (MB)',}],
  390:                       print => \&print_quotas,
  391:                       modify => \&modify_quotas,
  392:                     },
  393:         'autoenroll' =>
  394:                    { text => 'Auto-enrollment settings',
  395:                      help => 'Domain_Configuration_Auto_Enrollment',
  396:                      header => [{col1 => 'Configuration setting',
  397:                                  col2 => 'Value(s)'}],
  398:                      print => \&print_autoenroll,
  399:                      modify => \&modify_autoenroll,
  400:                    },
  401:         'autoupdate' => 
  402:                    { text => 'Auto-update settings',
  403:                      help => 'Domain_Configuration_Auto_Updates',
  404:                      header => [{col1 => 'Setting',
  405:                                  col2 => 'Value',},
  406:                                 {col1 => 'Setting',
  407:                                  col2 => 'Affiliation'},
  408:                                 {col1 => 'User population',
  409:                                  col2 => 'Updatable user data'}],
  410:                      print => \&print_autoupdate,
  411:                      modify => \&modify_autoupdate,
  412:                   },
  413:         'autocreate' => 
  414:                   { text => 'Auto-course creation settings',
  415:                      help => 'Domain_Configuration_Auto_Creation',
  416:                      header => [{col1 => 'Configuration Setting',
  417:                                  col2 => 'Value',}],
  418:                      print => \&print_autocreate,
  419:                      modify => \&modify_autocreate,
  420:                   },
  421:         'directorysrch' => 
  422:                   { text => 'Directory searches',
  423:                     help => 'Domain_Configuration_InstDirectory_Search',
  424:                     header => [{col1 => 'Institutional Directory Setting',
  425:                                 col2 => 'Value',},
  426:                                {col1 => 'LON-CAPA Directory Setting',
  427:                                 col2 => 'Value',}],
  428:                     print => \&print_directorysrch,
  429:                     modify => \&modify_directorysrch,
  430:                   },
  431:         'contacts' =>
  432:                   { text => 'E-mail addresses and helpform',
  433:                     help => 'Domain_Configuration_Contact_Info',
  434:                     header => [{col1 => 'Default e-mail addresses',
  435:                                 col2 => 'Value',},
  436:                                {col1 => 'Recipient(s) for notifications',
  437:                                 col2 => 'Value',},
  438:                                {col1 => 'Nightly status check e-mail',
  439:                                 col2 => 'Settings',},
  440:                                {col1 => 'Ask helpdesk form settings',
  441:                                 col2 => 'Value',},],
  442:                     print => \&print_contacts,
  443:                     modify => \&modify_contacts,
  444:                   },
  445:         'usercreation' => 
  446:                   { text => 'User creation',
  447:                     help => 'Domain_Configuration_User_Creation',
  448:                     header => [{col1 => 'Format rule type',
  449:                                 col2 => 'Format rules in force'},
  450:                                {col1 => 'User account creation',
  451:                                 col2 => 'Usernames which may be created',},
  452:                                {col1 => 'Context',
  453:                                 col2 => 'Assignable authentication types'}],
  454:                     print => \&print_usercreation,
  455:                     modify => \&modify_usercreation,
  456:                   },
  457:         'selfcreation' => 
  458:                   { text => 'Users self-creating accounts',
  459:                     help => 'Domain_Configuration_Self_Creation', 
  460:                     header => [{col1 => 'Self-creation with institutional username',
  461:                                 col2 => 'Enabled?'},
  462:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  463:                                 col2 => 'Information user can enter'},
  464:                                {col1 => 'Self-creation with e-mail verification',
  465:                                 col2 => 'Settings'}],
  466:                     print => \&print_selfcreation,
  467:                     modify => \&modify_selfcreation,
  468:                   },
  469:         'usermodification' =>
  470:                   { text => 'User modification',
  471:                     help => 'Domain_Configuration_User_Modification',
  472:                     header => [{col1 => 'Target user has role',
  473:                                 col2 => 'User information updatable in author context'},
  474:                                {col1 => 'Target user has role',
  475:                                 col2 => 'User information updatable in course context'}],
  476:                     print => \&print_usermodification,
  477:                     modify => \&modify_usermodification,
  478:                   },
  479:         'scantron' =>
  480:                   { text => 'Bubblesheet format',
  481:                     help => 'Domain_Configuration_Scantron_Format',
  482:                     header => [ {col1 => 'Bubblesheet format file',
  483:                                  col2 => ''},
  484:                                 {col1 => 'Bubblesheet data upload formats',
  485:                                  col2 => 'Settings'}],
  486:                     print => \&print_scantron,
  487:                     modify => \&modify_scantron,
  488:                   },
  489:         'requestcourses' => 
  490:                  {text => 'Request creation of courses',
  491:                   help => 'Domain_Configuration_Request_Courses',
  492:                   header => [{col1 => 'User affiliation',
  493:                               col2 => 'Availability/Processing of requests',},
  494:                              {col1 => 'Setting',
  495:                               col2 => 'Value'},
  496:                              {col1 => 'Available textbooks',
  497:                               col2 => ''},
  498:                              {col1 => 'Available templates',
  499:                               col2 => ''},
  500:                              {col1 => 'Validation (not official courses)',
  501:                               col2 => 'Value'},],
  502:                   print => \&print_quotas,
  503:                   modify => \&modify_quotas,
  504:                  },
  505:         'requestauthor' =>
  506:                  {text => 'Request Authoring Space',
  507:                   help => 'Domain_Configuration_Request_Author',
  508:                   header => [{col1 => 'User affiliation',
  509:                               col2 => 'Availability/Processing of requests',},
  510:                              {col1 => 'Setting',
  511:                               col2 => 'Value'}],
  512:                   print => \&print_quotas,
  513:                   modify => \&modify_quotas,
  514:                  },
  515:         'coursecategories' =>
  516:                   { text => 'Cataloging of courses/communities',
  517:                     help => 'Domain_Configuration_Cataloging_Courses',
  518:                     header => [{col1 => 'Catalog type/availability',
  519:                                 col2 => '',},
  520:                                {col1 => 'Category settings for standard catalog',
  521:                                 col2 => '',},
  522:                                {col1 => 'Categories',
  523:                                 col2 => '',
  524:                                }],
  525:                     print => \&print_coursecategories,
  526:                     modify => \&modify_coursecategories,
  527:                   },
  528:         'serverstatuses' =>
  529:                  {text   => 'Access to server status pages',
  530:                   help   => 'Domain_Configuration_Server_Status',
  531:                   header => [{col1 => 'Status Page',
  532:                               col2 => 'Other named users',
  533:                               col3 => 'Specific IPs',
  534:                             }],
  535:                   print => \&print_serverstatuses,
  536:                   modify => \&modify_serverstatuses,
  537:                  },
  538:         'helpsettings' =>
  539:                  {text   => 'Support settings',
  540:                   help   => 'Domain_Configuration_Help_Settings',
  541:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  542:                               col2 => 'Value'},
  543:                              {col1 => 'Helpdesk Roles',
  544:                               col2 => 'Settings'},],
  545:                   print  => \&print_helpsettings,
  546:                   modify => \&modify_helpsettings,
  547:                  },
  548:         'coursedefaults' => 
  549:                  {text => 'Course/Community defaults',
  550:                   help => 'Domain_Configuration_Course_Defaults',
  551:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  552:                               col2 => 'Value',},
  553:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  554:                               col2 => 'Value',},],
  555:                   print => \&print_coursedefaults,
  556:                   modify => \&modify_coursedefaults,
  557:                  },
  558:         'selfenrollment' => 
  559:                  {text   => 'Self-enrollment in Course/Community',
  560:                   help   => 'Domain_Configuration_Selfenrollment',
  561:                   header => [{col1 => 'Configuration Rights',
  562:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  563:                              {col1 => 'Defaults',
  564:                               col2 => 'Value'},
  565:                              {col1 => 'Self-enrollment validation (optional)',
  566:                               col2 => 'Value'},],
  567:                   print => \&print_selfenrollment,
  568:                   modify => \&modify_selfenrollment,
  569:                  },
  570:         'privacy' => 
  571:                  {text   => 'Role assignments and user privacy',
  572:                   help   => 'Domain_Configuration_User_Privacy',
  573:                   header => [{col1 => 'Role assigned in different domain',
  574:                               col2 => 'Approval options'},
  575:                              {col1 => 'Role assigned in different domain to user of type',
  576:                               col2 => 'User information available in that domain'},
  577:                              {col1 => "Role assigned in user's domain",
  578:                               col2 => 'Information viewable by privileged user'},
  579:                              {col1 => "Role assigned in user's domain",
  580:                               col2 => 'Information viewable by unprivileged user'}],
  581:                   print => \&print_privacy,
  582:                   modify => \&modify_privacy,
  583:                  },
  584:         'usersessions' =>
  585:                  {text  => 'User session hosting/offloading',
  586:                   help  => 'Domain_Configuration_User_Sessions',
  587:                   header => [{col1 => 'Domain server',
  588:                               col2 => 'Servers to offload sessions to when busy'},
  589:                              {col1 => 'Hosting of users from other domains',
  590:                               col2 => 'Rules'},
  591:                              {col1 => "Hosting domain's own users elsewhere",
  592:                               col2 => 'Rules'}],
  593:                   print => \&print_usersessions,
  594:                   modify => \&modify_usersessions,
  595:                  },
  596:         'loadbalancing' =>
  597:                  {text  => 'Dedicated Load Balancer(s)',
  598:                   help  => 'Domain_Configuration_Load_Balancing',
  599:                   header => [{col1 => 'Balancers',
  600:                               col2 => 'Default destinations',
  601:                               col3 => 'User affiliation',
  602:                               col4 => 'Overrides'},
  603:                             ],
  604:                   print => \&print_loadbalancing,
  605:                   modify => \&modify_loadbalancing,
  606:                  },
  607:         'ltitools' =>
  608:                  {text => 'External Tools (LTI)',
  609:                   help => 'Domain_Configuration_LTI_Tools',
  610:                   header => [{col1 => 'Encryption of shared secrets',
  611:                               col2 => 'Settings'},
  612:                              {col1 => 'Rules for shared secrets',
  613:                               col2 => 'Settings'},
  614:                              {col1 => 'Providers',
  615:                               col2 => 'Settings',}],
  616:                   print => \&print_ltitools,
  617:                   modify => \&modify_ltitools,
  618:                  },
  619:         'proctoring' =>
  620:                  {text => 'Remote Proctoring Integration',
  621:                   help => 'Domain_Configuration_Proctoring',
  622:                   header => [{col1 => 'Name',
  623:                               col2 => 'Configuration'}],
  624:                   print => \&print_proctoring,
  625:                   modify => \&modify_proctoring,
  626:                  },
  627:         'ssl' =>
  628:                  {text  => 'LON-CAPA Network (SSL)',
  629:                   help  => 'Domain_Configuration_Network_SSL',
  630:                   header => [{col1 => 'Server',
  631:                               col2 => 'Certificate Status'},
  632:                              {col1 => 'Connections to other servers',
  633:                               col2 => 'Rules'},
  634:                              {col1 => 'Connections from other servers',
  635:                               col2 => 'Rules'},
  636:                              {col1 => "Replicating domain's published content",
  637:                               col2 => 'Rules'}],
  638:                   print => \&print_ssl,
  639:                   modify => \&modify_ssl,
  640:                  },
  641:         'trust' =>
  642:                  {text   => 'Trust Settings',
  643:                   help   => 'Domain_Configuration_Trust',
  644:                   header => [{col1 => "Access to this domain's content by others",
  645:                               col2 => 'Rules'},
  646:                              {col1 => "Access to other domain's content by this domain",
  647:                               col2 => 'Rules'},
  648:                              {col1 => "Enrollment in this domain's courses by others",
  649:                               col2 => 'Rules',},
  650:                              {col1 => "Co-author roles in this domain for others",
  651:                               col2 => 'Rules',},
  652:                              {col1 => "Co-author roles for this domain's users elsewhere",
  653:                               col2 => 'Rules',},
  654:                              {col1 => "Domain roles in this domain assignable to others",
  655:                               col2 => 'Rules'},
  656:                              {col1 => "Course catalog for this domain displayed elsewhere",
  657:                               col2 => 'Rules'},
  658:                              {col1 => "Requests for creation of courses in this domain by others",
  659:                               col2 => 'Rules'},
  660:                              {col1 => "Users in other domains can send messages to this domain",
  661:                               col2 => 'Rules'},],
  662:                   print => \&print_trust,
  663:                   modify => \&modify_trust,
  664:                  },
  665:         'lti' =>
  666:                  {text => 'LTI Link Protection and LTI Consumers',
  667:                   help => 'Domain_Configuration_LTI_Provider',
  668:                   header => [{col1 => 'Encryption of shared secrets',
  669:                               col2 => 'Settings'},
  670:                              {col1 => 'Rules for shared secrets',
  671:                               col2 => 'Settings'},
  672:                              {col1 => 'Link Protectors in Courses',
  673:                               col2 => 'Values'},
  674:                              {col1 => 'Link Protectors',
  675:                               col2 => 'Settings'},
  676:                              {col1 => 'Consumers',
  677:                               col2 => 'Settings'},],
  678:                   print => \&print_lti,
  679:                   modify => \&modify_lti,
  680:                  },
  681:         'ipaccess' =>
  682:                        {text => 'IP-based access control',
  683:                         help => 'Domain_Configuration_IP_Access',
  684:                         header => [{col1 => 'Setting',
  685:                                     col2 => 'Value'},],
  686:                         print  => \&print_ipaccess,
  687:                         modify => \&modify_ipaccess,
  688:                        },
  689:         'authordefaults' =>
  690:                             {text => 'Authoring Space defaults',
  691:                              help => 'Domain_Configuration_Author_Defaults',
  692:                              header => [{col1 => 'Defaults which can be overridden by Author',
  693:                                          col2 => 'Settings',},
  694:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  695:                                          col2 => 'Settings',},],
  696:                              print => \&print_authordefaults,
  697:                              modify => \&modify_authordefaults,
  698:                             },
  699:     );
  700:     if (keys(%servers) > 1) {
  701:         $prefs{'login'}  = { text   => 'Log-in page options',
  702:                              help   => 'Domain_Configuration_Login_Page',
  703:                             header => [{col1 => 'Log-in Service',
  704:                                         col2 => 'Server Setting',},
  705:                                        {col1 => 'Log-in Page Items',
  706:                                         col2 => 'Settings'},
  707:                                        {col1 => 'Log-in Help',
  708:                                         col2 => 'Value'},
  709:                                        {col1 => 'Custom HTML in document head',
  710:                                         col2 => 'Value'},
  711:                                        {col1 => 'SSO',
  712:                                         col2 => 'Dual login: SSO and non-SSO options'},
  713:                                       ],
  714:                             print => \&print_login,
  715:                             modify => \&modify_login,
  716:                            };
  717:     }
  718: 
  719:     my @roles = ('student','coordinator','author','admin');
  720:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  721:     &Apache::lonhtmlcommon::add_breadcrumb
  722:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  723:       text=>"Settings to display/modify"});
  724:     my $confname = $dom.'-domainconfig';
  725: 
  726:     if ($phase eq 'process') {
  727:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  728:                                                               \%prefs,\%domconfig,$confname,\@roles);
  729:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  730:             $r->rflush();
  731:             &devalidate_remote_domconfs($dom,$result);
  732:         }
  733:     } elsif ($phase eq 'display') {
  734:         my $js = &recaptcha_js().
  735:                  &toggle_display_js();
  736:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  737:             my ($othertitle,$usertypes,$types) =
  738:                 &Apache::loncommon::sorted_inst_types($dom);
  739:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  740:                                           $domconfig{'loadbalancing'}).
  741:                    &new_spares_js().
  742:                    &common_domprefs_js().
  743:                    &Apache::loncommon::javascript_array_indexof();
  744:         }
  745:         if (grep(/^requestcourses$/,@actions)) {
  746:             my $javascript_validations;
  747:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  748:             $js .= <<END;
  749: <script type="text/javascript">
  750: $javascript_validations
  751: </script>
  752: $coursebrowserjs
  753: END
  754:         } elsif (grep(/^ipaccess$/,@actions)) {
  755:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  756:         }
  757:         if (grep(/^selfcreation$/,@actions)) {
  758:             $js .= &selfcreate_javascript();
  759:         }
  760:         if (grep(/^contacts$/,@actions)) {
  761:             $js .= &contacts_javascript();
  762:         }
  763:         if (grep(/^scantron$/,@actions)) {
  764:             $js .= &scantron_javascript();
  765:         }
  766:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  767:     } else {
  768: # check if domconfig user exists for the domain.
  769:         my $servadm = $r->dir_config('lonAdmEMail');
  770:         my ($configuserok,$author_ok,$switchserver) =
  771:             &config_check($dom,$confname,$servadm);
  772:         unless ($configuserok eq 'ok') {
  773:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  774:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  775:                           $confname).
  776:                       '<br />'
  777:             );
  778:             if ($switchserver) {
  779:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  780:                           '<br />'.
  781:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  782:                           '<br />'.
  783:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  784:                           '<br />'.
  785:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  786:                 );
  787:             } else {
  788:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  789:                           '<br />'.
  790:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  791:                 );
  792:             }
  793:             $r->print(&Apache::loncommon::end_page());
  794:             return OK;
  795:         }
  796:         if (keys(%domconfig) == 0) {
  797:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  798:             my @ids=&Apache::lonnet::current_machine_ids();
  799:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  800:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  801:                 my @loginimages = ('img','logo','domlogo','login');
  802:                 my $custom_img_count = 0;
  803:                 foreach my $img (@loginimages) {
  804:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  805:                         $custom_img_count ++;
  806:                     }
  807:                 }
  808:                 foreach my $role (@roles) {
  809:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  810:                         $custom_img_count ++;
  811:                     }
  812:                 }
  813:                 if ($custom_img_count > 0) {
  814:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  815:                     my $switch_server = &check_switchserver($dom,$confname);
  816:                     $r->print(
  817:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  818:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  819:     &mt("Thereafter, (with a Domain Coordinator role selected in the domain) you will be able to update settings when logged in to any server in the LON-CAPA network.").'<br />'.
  820:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  821:                     if ($switch_server) {
  822:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  823:                     }
  824:                     $r->print(&Apache::loncommon::end_page());
  825:                     return OK;
  826:                 }
  827:             }
  828:         }
  829:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  830:     }
  831:     return OK;
  832: }
  833: 
  834: sub process_changes {
  835:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  836:     my %domconfig;
  837:     if (ref($values) eq 'HASH') {
  838:         %domconfig = %{$values};
  839:     }
  840:     my $output;
  841:     if ($action eq 'login') {
  842:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  843:     } elsif ($action eq 'rolecolors') {
  844:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  845:                                      $lastactref,%domconfig);
  846:     } elsif ($action eq 'quotas') {
  847:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  848:     } elsif ($action eq 'autoenroll') {
  849:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  850:     } elsif ($action eq 'autoupdate') {
  851:         $output = &modify_autoupdate($dom,%domconfig);
  852:     } elsif ($action eq 'autocreate') {
  853:         $output = &modify_autocreate($dom,%domconfig);
  854:     } elsif ($action eq 'directorysrch') {
  855:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  856:     } elsif ($action eq 'usercreation') {
  857:         $output = &modify_usercreation($dom,%domconfig);
  858:     } elsif ($action eq 'selfcreation') {
  859:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  860:     } elsif ($action eq 'usermodification') {
  861:         $output = &modify_usermodification($dom,%domconfig);
  862:     } elsif ($action eq 'contacts') {
  863:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  864:     } elsif ($action eq 'defaults') {
  865:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  866:     } elsif ($action eq 'scantron') {
  867:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  868:     } elsif ($action eq 'coursecategories') {
  869:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  870:     } elsif ($action eq 'serverstatuses') {
  871:         $output = &modify_serverstatuses($dom,%domconfig);
  872:     } elsif ($action eq 'requestcourses') {
  873:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  874:     } elsif ($action eq 'requestauthor') {
  875:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  876:     } elsif ($action eq 'helpsettings') {
  877:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  878:     } elsif ($action eq 'coursedefaults') {
  879:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  880:     } elsif ($action eq 'selfenrollment') {
  881:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  882:     } elsif ($action eq 'usersessions') {
  883:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  884:     } elsif ($action eq 'loadbalancing') {
  885:         $output = &modify_loadbalancing($dom,%domconfig);
  886:     } elsif ($action eq 'ltitools') {
  887:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  888:     } elsif ($action eq 'proctoring') {
  889:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  890:     } elsif ($action eq 'ssl') {
  891:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  892:     } elsif ($action eq 'trust') {
  893:         $output = &modify_trust($dom,$lastactref,%domconfig);
  894:     } elsif ($action eq 'lti') {
  895:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  896:     } elsif ($action eq 'privacy') {
  897:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  898:     } elsif ($action eq 'passwords') {
  899:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  900:     } elsif ($action eq 'wafproxy') {
  901:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  902:     } elsif ($action eq 'ipaccess') {
  903:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  904:     } elsif ($action eq 'authordefaults') {
  905:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  906:     }
  907:     return $output;
  908: }
  909: 
  910: sub print_config_box {
  911:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  912:     my $rowtotal = 0;
  913:     my $output;
  914:     if ($action eq 'coursecategories') {
  915:         $output = &coursecategories_javascript($settings);
  916:     } elsif ($action eq 'defaults') {
  917:         $output = &defaults_javascript($settings); 
  918:     } elsif ($action eq 'passwords') {
  919:         $output = &passwords_javascript($action);
  920:     } elsif ($action eq 'helpsettings') {
  921:         my (%privs,%levelscurrent);
  922:         my %full=();
  923:         my %levels=(
  924:                      course => {},
  925:                      domain => {},
  926:                      system => {},
  927:                    );
  928:         my $context = 'domain';
  929:         my $crstype = 'Course';
  930:         my $formname = 'display';
  931:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  932:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  933:         $output =
  934:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  935:                                                       \@templateroles);
  936:     } elsif ($action eq 'ltitools') {
  937:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  938:     } elsif ($action eq 'lti') {
  939:         $output .= &passwords_javascript('ltisecrets')."\n".
  940:                    &lti_javascript($dom,$settings);
  941:     } elsif ($action eq 'proctoring') {
  942:         $output .= &proctoring_javascript($settings);
  943:     } elsif ($action eq 'wafproxy') {
  944:         $output .= &wafproxy_javascript($dom);
  945:     } elsif ($action eq 'autoupdate') {
  946:         $output .= &autoupdate_javascript();
  947:     } elsif ($action eq 'autoenroll') {
  948:         $output .= &autoenroll_javascript();
  949:     } elsif ($action eq 'login') {
  950:         $output .= &saml_javascript();
  951:     } elsif ($action eq 'ipaccess') {
  952:         $output .= &ipaccess_javascript($settings);
  953:     } elsif ($action eq 'authordefaults') {
  954:         $output .= &authordefaults_javascript();
  955:     }
  956:     $output .=
  957:          '<table class="LC_nested_outer">
  958:           <tr>
  959:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  960:            &mt($item->{text}).'&nbsp;'.
  961:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  962:           '</tr>';
  963:     $rowtotal ++;
  964:     my $numheaders = 1;
  965:     if (ref($item->{'header'}) eq 'ARRAY') {
  966:         $numheaders = scalar(@{$item->{'header'}});
  967:     }
  968:     if ($numheaders > 1) {
  969:         my $colspan = '';
  970:         my $rightcolspan = '';
  971:         my $leftnobr = '';
  972:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  973:             ($action eq 'directorysrch') ||
  974:             (($action eq 'login') && ($numheaders < 5))) {
  975:             $colspan = ' colspan="2"';
  976:         }
  977:         if ($action eq 'usersessions') {
  978:             $rightcolspan = ' colspan="3"'; 
  979:         }
  980:         if ($action eq 'passwords') {
  981:             $leftnobr = ' LC_nobreak';
  982:         }
  983:         $output .= '
  984:           <tr>
  985:            <td>
  986:             <table class="LC_nested">
  987:              <tr class="LC_info_row">
  988:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  989:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  990:              </tr>';
  991:         $rowtotal ++;
  992:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  993:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  994:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  995:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  996:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  997:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
  998:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  999:         } elsif ($action eq 'passwords') {
 1000:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
 1001:         } elsif ($action eq 'coursecategories') {
 1002:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'scantron') {
 1004:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
 1005:         } elsif ($action eq 'login') {
 1006:             if ($numheaders == 5) {
 1007:                 $colspan = ' colspan="2"';
 1008:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
 1009:             } else {
 1010:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
 1011:             }
 1012:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 1013:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1014:         } elsif ($action eq 'rolecolors') {
 1015:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1016:         }
 1017:         $output .= '
 1018:            </table>
 1019:           </td>
 1020:          </tr>
 1021:          <tr>
 1022:            <td>
 1023:             <table class="LC_nested">
 1024:              <tr class="LC_info_row">
 1025:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1026:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1027:              </tr>';
 1028:             $rowtotal ++;
 1029:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1030:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1031:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1032:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1033:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1034:             ($action eq 'ltitools')) {
 1035:             if ($action eq 'coursecategories') {
 1036:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1037:                 $colspan = ' colspan="2"';
 1038:             } elsif ($action eq 'trust') {
 1039:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1040:             } elsif ($action eq 'passwords') {
 1041:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1042:             } elsif ($action eq 'lti') {
 1043:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
 1044:                            </table>
 1045:                           </td>
 1046:                          </tr>
 1047:                          <tr>
 1048:                           <td>
 1049:                           <table class="LC_nested">
 1050:                            <tr class="LC_info_row">
 1051:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1052:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1053:                            </tr>'."\n".
 1054:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1055:             } else {
 1056:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1057:             }
 1058:             if ($action eq 'trust') {
 1059:                 $output .= '
 1060:             </table>
 1061:           </td>
 1062:          </tr>';
 1063:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1064:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1065:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1066:                     $output .= '
 1067:          <tr>
 1068:            <td>
 1069:             <table class="LC_nested">
 1070:              <tr class="LC_info_row">
 1071:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1072:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1073:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1074:             </table>
 1075:           </td>
 1076:          </tr>';
 1077:                 }
 1078:                 $output .= '
 1079:          <tr>
 1080:            <td>
 1081:             <table class="LC_nested">
 1082:              <tr class="LC_info_row">
 1083:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1084:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1085:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1086:             } else {
 1087:                 my $hdridx = 2;
 1088:                 if ($action eq 'lti') {
 1089:                     $hdridx = 3;
 1090:                 }
 1091:                 $output .= '
 1092:            </table>
 1093:           </td>
 1094:          </tr>
 1095:          <tr>
 1096:            <td>
 1097:             <table class="LC_nested">
 1098:              <tr class="LC_info_row">
 1099:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1100:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
 1101:              </tr>'."\n";
 1102:                 if ($action eq 'coursecategories') {
 1103:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1104:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1105:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1106:                     if ($action eq 'passwords') {
 1107:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1108:                     } else {
 1109:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1110:                     }
 1111:                     $hdridx ++;
 1112:                     $output .= '
 1113:              </tr>
 1114:             </table>
 1115:            </td>
 1116:           </tr>
 1117:           <tr>
 1118:            <td>
 1119:             <table class="LC_nested">
 1120:              <tr class="LC_info_row">
 1121:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1122:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
 1123:                     if ($action eq 'passwords') {
 1124:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1125:                     } else {
 1126:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1127:                     }
 1128:                     $output .= '
 1129:             </table>
 1130:           </td>
 1131:          </tr>
 1132:          <tr>';
 1133:                 } else {
 1134:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1135:                 }
 1136:             }
 1137:             $rowtotal ++;
 1138:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1139:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1140:                  ($action eq 'wafproxy') || ($action eq 'authordefaults')) {
 1141:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1142:         } elsif ($action eq 'scantron') {
 1143:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1144:         } elsif ($action eq 'ssl') {
 1145:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1146:             </table>
 1147:           </td>
 1148:          </tr>
 1149:          <tr>
 1150:            <td>
 1151:             <table class="LC_nested">
 1152:              <tr class="LC_info_row">
 1153:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1154:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1155:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1156:             </table>
 1157:           </td>
 1158:          </tr>
 1159:          <tr>
 1160:            <td>
 1161:             <table class="LC_nested">
 1162:              <tr class="LC_info_row">
 1163:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1164:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1165:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1166:         } elsif ($action eq 'login') {
 1167:             if ($numheaders == 5) {
 1168:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1169:            </table>
 1170:           </td>
 1171:          </tr>
 1172:          <tr>
 1173:            <td>
 1174:             <table class="LC_nested">
 1175:              <tr class="LC_info_row">
 1176:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1177:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1178:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1179:                 $rowtotal ++;
 1180:             } else {
 1181:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1182:             }
 1183:             $output .= '
 1184:            </table>
 1185:           </td>
 1186:          </tr>
 1187:          <tr>
 1188:            <td>
 1189:             <table class="LC_nested">
 1190:              <tr class="LC_info_row">';
 1191:             if ($numheaders == 5) {
 1192:                 $output .= '
 1193:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1194:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1195:              </tr>';
 1196:             } else {
 1197:                 $output .= '
 1198:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1199:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1200:              </tr>';
 1201:             }
 1202:             $rowtotal ++;
 1203:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1204:            </table>
 1205:           </td>
 1206:          </tr>
 1207:          <tr>
 1208:            <td>
 1209:             <table class="LC_nested">
 1210:              <tr class="LC_info_row">';
 1211:             if ($numheaders == 5) {
 1212:                 $output .= '
 1213:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1214:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1215:              </tr>';
 1216:             } else {
 1217:                 $output .= '
 1218:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1219:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1220:              </tr>';
 1221:             }
 1222:             $rowtotal ++;
 1223:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1224:         } elsif ($action eq 'requestcourses') {
 1225:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1226:             $rowtotal ++;
 1227:             $output .= &print_studentcode($settings,\$rowtotal).'
 1228:            </table>
 1229:           </td>
 1230:          </tr>
 1231:          <tr>
 1232:            <td>
 1233:             <table class="LC_nested">
 1234:              <tr class="LC_info_row">
 1235:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1236:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1237:                        &textbookcourses_javascript($settings).
 1238:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1239:             </table>
 1240:            </td>
 1241:           </tr>
 1242:          <tr>
 1243:            <td>
 1244:             <table class="LC_nested">
 1245:              <tr class="LC_info_row">
 1246:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1247:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1248:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1249:             </table>
 1250:            </td>
 1251:           </tr>
 1252:           <tr>
 1253:            <td>
 1254:             <table class="LC_nested">
 1255:              <tr class="LC_info_row">
 1256:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1257:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1258:              </tr>'.
 1259:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1260:         } elsif ($action eq 'requestauthor') {
 1261:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1262:             $rowtotal ++;
 1263:         } elsif ($action eq 'rolecolors') {
 1264:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1265:            </table>
 1266:           </td>
 1267:          </tr>
 1268:          <tr>
 1269:            <td>
 1270:             <table class="LC_nested">
 1271:              <tr class="LC_info_row">
 1272:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1273:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1274:               <td class="LC_right_item" style="vertical-align: top">'.
 1275:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1276:              </tr>'.
 1277:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1278:            </table>
 1279:           </td>
 1280:          </tr>
 1281:          <tr>
 1282:            <td>
 1283:             <table class="LC_nested">
 1284:              <tr class="LC_info_row">
 1285:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1286:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1287:              </tr>'.
 1288:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1289:             $rowtotal += 2;
 1290:         }
 1291:     } else {
 1292:         $output .= '
 1293:           <tr>
 1294:            <td>
 1295:             <table class="LC_nested">
 1296:              <tr class="LC_info_row">';
 1297:         if ($action eq 'login') {
 1298:             $output .= '  
 1299:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1300:         } elsif ($action eq 'serverstatuses') {
 1301:             $output .= '
 1302:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1303:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1304: 
 1305:         } else {
 1306:             $output .= '
 1307:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1308:         }
 1309:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1310:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1311:                        &mt($item->{'header'}->[0]->{'col2'});
 1312:             if ($action eq 'serverstatuses') {
 1313:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1314:             } 
 1315:         } else {
 1316:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1317:                        &mt($item->{'header'}->[0]->{'col2'});
 1318:         }
 1319:         $output .= '</td>';
 1320:         if ($item->{'header'}->[0]->{'col3'}) {
 1321:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1322:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1323:                             &mt($item->{'header'}->[0]->{'col3'});
 1324:             } else {
 1325:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1326:                            &mt($item->{'header'}->[0]->{'col3'});
 1327:             }
 1328:             if ($action eq 'serverstatuses') {
 1329:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1330:             }
 1331:             $output .= '</td>';
 1332:         }
 1333:         if ($item->{'header'}->[0]->{'col4'}) {
 1334:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1335:                        &mt($item->{'header'}->[0]->{'col4'});
 1336:         }
 1337:         $output .= '</tr>';
 1338:         $rowtotal ++;
 1339:         if ($action eq 'quotas') {
 1340:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1341:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1342:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1343:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1344:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1345:         }
 1346:     }
 1347:     $output .= '
 1348:    </table>
 1349:   </td>
 1350:  </tr>
 1351: </table><br />';
 1352:     return ($output,$rowtotal);
 1353: }
 1354: 
 1355: sub print_login {
 1356:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1357:     my ($css_class,$datatable,$switchserver,%lt);
 1358:     my %choices = &login_choices();
 1359:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1360:         %lt = &login_file_options();
 1361:         $switchserver = &check_switchserver($dom,$confname);
 1362:     }
 1363:     if ($caller eq 'service') {
 1364:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1365:         my $choice = $choices{'disallowlogin'};
 1366:         $css_class = ' class="LC_odd_row"';
 1367:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1368:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1369:                       '<th>'.$choices{'server'}.'</th>'.
 1370:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1371:                       '<th>'.$choices{'custompath'}.'</th>'.
 1372:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1373:         my %disallowed;
 1374:         if (ref($settings) eq 'HASH') {
 1375:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1376:                %disallowed = %{$settings->{'loginvia'}};
 1377:             }
 1378:         }
 1379:         foreach my $lonhost (sort(keys(%servers))) {
 1380:             my $direct = 'selected="selected"';
 1381:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1382:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1383:                     $direct = '';
 1384:                 }
 1385:             }
 1386:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1387:                           '<td><select name="'.$lonhost.'_server">'.
 1388:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1389:                           '</option>';
 1390:             foreach my $hostid (sort(keys(%servers))) {
 1391:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1392:                 my $selected = '';
 1393:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1394:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1395:                         $selected = 'selected="selected"';
 1396:                     }
 1397:                 }
 1398:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1399:                               $servers{$hostid}.'</option>';
 1400:             }
 1401:             $datatable .= '</select></td>'.
 1402:                           '<td><select name="'.$lonhost.'_serverpath">';
 1403:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1404:                 my $pathname = $path;
 1405:                 if ($path eq 'custom') {
 1406:                     $pathname = &mt('Custom Path').' ->';
 1407:                 }
 1408:                 my $selected = '';
 1409:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1410:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1411:                         $selected = 'selected="selected"';
 1412:                     }
 1413:                 } elsif ($path eq '') {
 1414:                     $selected = 'selected="selected"';
 1415:                 }
 1416:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1417:             }
 1418:             $datatable .= '</select></td>';
 1419:             my ($custom,$exempt);
 1420:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1421:                 $custom = $disallowed{$lonhost}{'custompath'};
 1422:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1423:             }
 1424:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1425:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1426:                           '</tr>';
 1427:         }
 1428:         $datatable .= '</table></td></tr>';
 1429:         return $datatable;
 1430:     } elsif ($caller eq 'page') {
 1431:         my %defaultchecked = ( 
 1432:                                'coursecatalog' => 'on',
 1433:                                'helpdesk'      => 'on',
 1434:                                'adminmail'     => 'off',
 1435:                                'newuser'       => 'off',
 1436:                              );
 1437:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1438:         my (%checkedon,%checkedoff);
 1439:         foreach my $item (@toggles) {
 1440:             if ($defaultchecked{$item} eq 'on') { 
 1441:                 $checkedon{$item} = ' checked="checked" ';
 1442:                 $checkedoff{$item} = ' ';
 1443:             } elsif ($defaultchecked{$item} eq 'off') {
 1444:                 $checkedoff{$item} = ' checked="checked" ';
 1445:                 $checkedon{$item} = ' ';
 1446:             }
 1447:         }
 1448:         my @images = ('img','logo','domlogo','login');
 1449:         my @alttext = ('img','logo','domlogo');
 1450:         my @logintext = ('textcol','bgcol');
 1451:         my @bgs = ('pgbg','mainbg','sidebg');
 1452:         my @links = ('link','alink','vlink');
 1453:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1454:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1455:         my (%is_custom,%designs);
 1456:         my %defaults = (
 1457:                        font => $defaultdesign{'login.font'},
 1458:                        );
 1459:         foreach my $item (@images) {
 1460:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1461:             $defaults{'showlogo'}{$item} = 1;
 1462:         }
 1463:         foreach my $item (@bgs) {
 1464:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1465:         }
 1466:         foreach my $item (@logintext) {
 1467:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1468:         }
 1469:         foreach my $item (@links) {
 1470:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1471:         }
 1472:         if (ref($settings) eq 'HASH') {
 1473:             foreach my $item (@toggles) {
 1474:                 if ($settings->{$item} eq '1') {
 1475:                     $checkedon{$item} =  ' checked="checked" ';
 1476:                     $checkedoff{$item} = ' ';
 1477:                 } elsif ($settings->{$item} eq '0') {
 1478:                     $checkedoff{$item} =  ' checked="checked" ';
 1479:                     $checkedon{$item} = ' ';
 1480:                 }
 1481:             }
 1482:             foreach my $item (@images) {
 1483:                 if (defined($settings->{$item})) {
 1484:                     $designs{$item} = $settings->{$item};
 1485:                     $is_custom{$item} = 1;
 1486:                 }
 1487:                 if (defined($settings->{'showlogo'}{$item})) {
 1488:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1489:                 }
 1490:             }
 1491:             foreach my $item (@alttext) {
 1492:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1493:                     if ($settings->{'alttext'}->{$item} ne '') {
 1494:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1495:                     }
 1496:                 }
 1497:             }
 1498:             foreach my $item (@logintext) {
 1499:                 if ($settings->{$item} ne '') {
 1500:                     $designs{'logintext'}{$item} = $settings->{$item};
 1501:                     $is_custom{$item} = 1;
 1502:                 }
 1503:             }
 1504:             if ($settings->{'font'} ne '') {
 1505:                 $designs{'font'} = $settings->{'font'};
 1506:                 $is_custom{'font'} = 1;
 1507:             }
 1508:             foreach my $item (@bgs) {
 1509:                 if ($settings->{$item} ne '') {
 1510:                     $designs{'bgs'}{$item} = $settings->{$item};
 1511:                     $is_custom{$item} = 1;
 1512:                 }
 1513:             }
 1514:             foreach my $item (@links) {
 1515:                 if ($settings->{$item} ne '') {
 1516:                     $designs{'links'}{$item} = $settings->{$item};
 1517:                     $is_custom{$item} = 1;
 1518:                 }
 1519:             }
 1520:         } else {
 1521:             if ($designhash{$dom.'.login.font'} ne '') {
 1522:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1523:                 $is_custom{'font'} = 1;
 1524:             }
 1525:             foreach my $item (@images) {
 1526:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1527:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1528:                     $is_custom{$item} = 1;
 1529:                 }
 1530:             }
 1531:             foreach my $item (@bgs) {
 1532:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1533:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1534:                     $is_custom{$item} = 1;
 1535:                 }
 1536:             }
 1537:             foreach my $item (@links) {
 1538:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1539:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1540:                     $is_custom{$item} = 1;
 1541:                 }
 1542:             }
 1543:         }
 1544:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1545:                                                       logo => 'Institution Logo',
 1546:                                                       domlogo => 'Domain Logo',
 1547:                                                       login => 'Login box');
 1548:         my $itemcount = 1;
 1549:         foreach my $item (@toggles) {
 1550:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1551:             $datatable .=  
 1552:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1553:                 '</td><td>'.
 1554:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1555:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1556:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1557:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1558:                 '</tr>';
 1559:             $itemcount ++;
 1560:         }
 1561:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1562:         $datatable .= '</tr></table></td></tr>';
 1563:     } elsif ($caller eq 'help') {
 1564:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1565:         my $itemcount = 1;
 1566:         $defaulturl = '/adm/loginproblems.html';
 1567:         $defaulttype = 'default';
 1568:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1569:         my @currlangs;
 1570:         if (ref($settings) eq 'HASH') {
 1571:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1572:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1573:                     next if ($settings->{'helpurl'}{$key} eq '');
 1574:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1575:                     $type{$key} = 'custom';
 1576:                     unless ($key eq 'nolang') {
 1577:                         push(@currlangs,$key);
 1578:                     }
 1579:                 }
 1580:             } elsif ($settings->{'helpurl'} ne '') {
 1581:                 $type{'nolang'} = 'custom';
 1582:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1583:             }
 1584:         }
 1585:         foreach my $lang ('nolang',sort(@currlangs)) {
 1586:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1587:             $datatable .= '<tr'.$css_class.'>';
 1588:             if ($url{$lang} eq '') {
 1589:                 $url{$lang} = $defaulturl;
 1590:             }
 1591:             if ($type{$lang} eq '') {
 1592:                 $type{$lang} = $defaulttype;
 1593:             }
 1594:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1595:             if ($lang eq 'nolang') {
 1596:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1597:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1598:             } else {
 1599:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1600:                                   $langchoices{$lang},
 1601:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1602:             }
 1603:             $datatable .= '</span></td>'."\n".
 1604:                           '<td class="LC_left_item">';
 1605:             if ($type{$lang} eq 'custom') {
 1606:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1607:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1608:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1609:             } else {
 1610:                 $datatable .= $lt{'upl'};
 1611:             }
 1612:             $datatable .='<br />';
 1613:             if ($switchserver) {
 1614:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1615:             } else {
 1616:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1617:             }
 1618:             $datatable .= '</td></tr>';
 1619:             $itemcount ++;
 1620:         }
 1621:         my @addlangs;
 1622:         foreach my $lang (sort(keys(%langchoices))) {
 1623:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1624:             push(@addlangs,$lang);
 1625:         }
 1626:         if (@addlangs > 0) {
 1627:             my %toadd;
 1628:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1629:             $toadd{''} = &mt('Select');
 1630:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1631:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1632:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1633:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1634:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1635:             if ($switchserver) {
 1636:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1637:             } else {
 1638:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1639:             }
 1640:             $datatable .= '</td></tr>';
 1641:             $itemcount ++;
 1642:         }
 1643:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1644:     } elsif ($caller eq 'headtag') {
 1645:         my %domservers = &Apache::lonnet::get_servers($dom);
 1646:         my $choice = $choices{'headtag'};
 1647:         $css_class = ' class="LC_odd_row"';
 1648:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1649:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1650:                       '<th>'.$choices{'current'}.'</th>'.
 1651:                       '<th>'.$choices{'action'}.'</th>'.
 1652:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1653:         my (%currurls,%currexempt);
 1654:         if (ref($settings) eq 'HASH') {
 1655:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1656:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1657:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1658:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1659:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1660:                     }
 1661:                 }
 1662:             }
 1663:         }
 1664:         foreach my $lonhost (sort(keys(%domservers))) {
 1665:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1666:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1667:             if ($currurls{$lonhost}) {
 1668:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1669:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1670:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1671:                               '">'.$lt{'curr'}.'</a></td>'.
 1672:                               '<td><span class="LC_nobreak"><label>'.
 1673:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1674:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1675:             } else {
 1676:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1677:             }
 1678:             $datatable .='<br />';
 1679:             if ($switchserver) {
 1680:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1681:             } else {
 1682:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1683:             }
 1684:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1685:         }
 1686:         $datatable .= '</table></td></tr>';
 1687:     } elsif ($caller eq 'saml') {
 1688:         my %domservers = &Apache::lonnet::get_servers($dom);
 1689:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1690:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1691:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1692:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1693:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1694:         foreach my $lonhost (keys(%domservers)) {
 1695:             $samlurl{$lonhost} = '/adm/sso';
 1696:             $styleon{$lonhost} = 'display:none';
 1697:             $styleoff{$lonhost} = '';
 1698:         }
 1699:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1700:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1701:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1702:                     $saml{$lonhost} = 1;
 1703:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1704:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1705:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1706:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1707:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1708:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1709:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1710:                     $styleon{$lonhost} = '';
 1711:                     $styleoff{$lonhost} = 'display:none';
 1712:                 } else {
 1713:                     $styleon{$lonhost} = 'display:none';
 1714:                     $styleoff{$lonhost} = '';
 1715:                 }
 1716:             }
 1717:         }
 1718:         my $itemcount = 1;
 1719:         foreach my $lonhost (sort(keys(%domservers))) {
 1720:             my $samlon = ' ';
 1721:             my $samloff = ' checked="checked" ';
 1722:             if ($saml{$lonhost}) {
 1723:                 $samlon = $samloff;
 1724:                 $samloff = ' ';
 1725:             }
 1726:             my $samlwinon = '';
 1727:             my $samlwinoff = ' checked="checked"';
 1728:             if ($samlwindow{$lonhost}) {
 1729:                 $samlwinon = $samlwinoff;
 1730:                 $samlwinoff = '';
 1731:             }
 1732:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1733:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1734:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1735:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1736:                           &mt('No').'</label>'.('&nbsp;'x2).
 1737:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1738:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1739:                           &mt('Yes').'</label></span></td>'.
 1740:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1741:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1742:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1743:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1744:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1745:                           $samltext{$lonhost}.'" /></td><td>';
 1746:             if ($samlimg{$lonhost}) {
 1747:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1748:                               '<span class="LC_nobreak"><label>'.
 1749:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1750:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1751:             } else {
 1752:                 $datatable .= $lt{'upl'};
 1753:             }
 1754:             $datatable .='<br />';
 1755:             if ($switchserver) {
 1756:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1757:             } else {
 1758:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1759:             }
 1760:             $datatable .= '</td>'.
 1761:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1762:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1763:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1764:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1765:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1766:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1767:                           '<tr'.$css_class.'>'.
 1768:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1769:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1770:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1771:                           $samltitle{$lonhost}.'</textarea></td>'.
 1772:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1773:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1774:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1775:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1776:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1777:                           '</table></td>'.
 1778:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1779:            $itemcount ++;
 1780:         }
 1781:         $datatable .= '</table></td></tr>';
 1782:     }
 1783:     return $datatable;
 1784: }
 1785: 
 1786: sub login_choices {
 1787:     my %choices =
 1788:         &Apache::lonlocal::texthash (
 1789:             coursecatalog => 'Display Course/Community Catalog link?',
 1790:             adminmail     => "Display Administrator's E-mail Address?",
 1791:             helpdesk      => 'Display "Contact Helpdesk" link',
 1792:             disallowlogin => "Login page requests redirected",
 1793:             hostid        => "Server",
 1794:             server        => "Redirect to:",
 1795:             serverpath    => "Path",
 1796:             custompath    => "Custom", 
 1797:             exempt        => "Exempt IP(s)",
 1798:             directlogin   => "No redirect",
 1799:             newuser       => "Link to create a user account",
 1800:             img           => "Header",
 1801:             logo          => "Main Logo",
 1802:             domlogo       => "Domain Logo",
 1803:             login         => "Log-in Header", 
 1804:             textcol       => "Text color",
 1805:             bgcol         => "Box color",
 1806:             bgs           => "Background colors",
 1807:             links         => "Link colors",
 1808:             font          => "Font color",
 1809:             pgbg          => "Header",
 1810:             mainbg        => "Page",
 1811:             sidebg        => "Login box",
 1812:             link          => "Link",
 1813:             alink         => "Active link",
 1814:             vlink         => "Visited link",
 1815:             headtag       => "Custom markup",
 1816:             action        => "Action",
 1817:             current       => "Current",
 1818:             samllanding   => "Dual login?",
 1819:             samloptions   => "Options",
 1820:             alttext       => "Alt text",
 1821:         );
 1822:     return %choices;
 1823: }
 1824: 
 1825: sub login_file_options {
 1826:       return &Apache::lonlocal::texthash(
 1827:                                            del     => 'Delete?',
 1828:                                            rep     => 'Replace:',
 1829:                                            upl     => 'Upload:',
 1830:                                            curr    => 'View contents',
 1831:                                            default => 'Default',
 1832:                                            custom  => 'Custom',
 1833:                                            none    => 'None',
 1834:       );
 1835: }
 1836: 
 1837: sub print_ipaccess {
 1838:     my ($dom,$settings,$rowtotal) = @_;
 1839:     my $css_class;
 1840:     my $itemcount = 0;
 1841:     my $datatable;
 1842:     my %ordered;
 1843:     if (ref($settings) eq 'HASH') {
 1844:         foreach my $item (keys(%{$settings})) {
 1845:             if (ref($settings->{$item}) eq 'HASH') {
 1846:                 my $num = $settings->{$item}{'order'};
 1847:                 if ($num eq '') {
 1848:                     $num = scalar(keys(%{$settings}));
 1849:                 }
 1850:                 $ordered{$num} = $item;
 1851:             }
 1852:         }
 1853:     }
 1854:     my $maxnum = scalar(keys(%ordered));
 1855:     if (keys(%ordered)) {
 1856:         my @items = sort { $a <=> $b } keys(%ordered);
 1857:         for (my $i=0; $i<@items; $i++) {
 1858:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1859:             my $item = $ordered{$items[$i]};
 1860:             my ($name,$ipranges,%commblocks,%courses);
 1861:             if (ref($settings->{$item}) eq 'HASH') {
 1862:                 $name = $settings->{$item}->{'name'};
 1863:                 $ipranges = $settings->{$item}->{'ip'};
 1864:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1865:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1866:                 }
 1867:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1868:                     %courses = %{$settings->{$item}->{'courses'}};
 1869:                 }
 1870:             }
 1871:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1872:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1873:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1874:             for (my $k=0; $k<=$maxnum; $k++) {
 1875:                 my $vpos = $k+1;
 1876:                 my $selstr;
 1877:                 if ($k == $i) {
 1878:                     $selstr = ' selected="selected" ';
 1879:                 }
 1880:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1881:             }
 1882:             $datatable .= '</select>'.('&nbsp;'x2).
 1883:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1884:                 &mt('Delete?').'</label></span></td>'.
 1885:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1886:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1887:                 '</td></tr>';
 1888:             $itemcount ++;
 1889:         }
 1890:     }
 1891:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1892:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1893:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1894:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1895:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1896:     for (my $k=0; $k<$maxnum+1; $k++) {
 1897:         my $vpos = $k+1;
 1898:         my $selstr;
 1899:         if ($k == $maxnum) {
 1900:             $selstr = ' selected="selected" ';
 1901:         }
 1902:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1903:     }
 1904:     $datatable .= '</select>&nbsp;'."\n".
 1905:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1906:                   '<td colspan="2">'.
 1907:                   &ipaccess_options('add',$itemcount,$dom).
 1908:                   '</td>'."\n".
 1909:                   '</tr>'."\n";
 1910:     $$rowtotal ++;
 1911:     return $datatable;
 1912: }
 1913: 
 1914: sub ipaccess_options {
 1915:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1916:     my (%currblocks,%currcourses,$output);
 1917:     if (ref($blocksref) eq 'HASH') {
 1918:         %currblocks = %{$blocksref};
 1919:     }
 1920:     if (ref($coursesref) eq 'HASH') {
 1921:         %currcourses = %{$coursesref};
 1922:     }
 1923:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1924:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1925:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1926:               '</span></fieldset>'.
 1927:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1928:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1929:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1930:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1931:               $ipranges.'</textarea></fieldset>'.
 1932:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1933:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1934:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1935:               '<table>';
 1936:     foreach my $cid (sort(keys(%currcourses))) {
 1937:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1938:         $output .= '<tr><td><span class="LC_nobreak">'.
 1939:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1940:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1941:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1942:     }
 1943:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1944:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1945:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1946:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1947:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1948:                '</span></td></tr></table>'."\n".
 1949:                '</fieldset>';
 1950:     return $output;
 1951: }
 1952: 
 1953: sub blocker_checkboxes {
 1954:     my ($num,$blocks) = @_;
 1955:     my ($typeorder,$types) = &commblocktype_text();
 1956:     my $numinrow = 6;
 1957:     my $output = '<table>';
 1958:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1959:         my $block = $typeorder->[$i];
 1960:         my $blockstatus;
 1961:         if (ref($blocks) eq 'HASH') {
 1962:             if ($blocks->{$block} eq 'on') {
 1963:                 $blockstatus = 'checked="checked"';
 1964:             }
 1965:         }
 1966:         my $rem = $i%($numinrow);
 1967:         if ($rem == 0) {
 1968:             if ($i > 0) {
 1969:                 $output .= '</tr>';
 1970:             }
 1971:             $output .= '<tr>';
 1972:         }
 1973:         if ($i == scalar(@{$typeorder})-1) {
 1974:             my $colsleft = $numinrow-$rem;
 1975:             if ($colsleft > 1) {
 1976:                 $output .= '<td colspan="'.$colsleft.'">';
 1977:             } else {
 1978:                 $output .= '<td>';
 1979:             }
 1980:         } else {
 1981:             $output .= '<td>';
 1982:         }
 1983:         my $item = 'ipaccess_block_'.$num;
 1984:         if ($blockstatus) {
 1985:             $blockstatus = ' '.$blockstatus;
 1986:         }
 1987:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1988:                    '<input type="checkbox" name="'.$item.'"'.
 1989:                    $blockstatus.' value="'.$block.'"'.' />'.
 1990:                    $types->{$block}.'</label></span>'."\n".
 1991:                    '<br /></td>';
 1992:     }
 1993:     $output .= '</tr></table>';
 1994:     return $output;
 1995: }
 1996: 
 1997: sub commblocktype_text {
 1998:     my %types = &Apache::lonlocal::texthash(
 1999:         'com' => 'Messaging',
 2000:         'chat' => 'Chat Room',
 2001:         'boards' => 'Discussion',
 2002:         'port' => 'Portfolio',
 2003:         'groups' => 'Groups',
 2004:         'blogs' => 'Blogs',
 2005:         'about' => 'User Information',
 2006:         'printout' => 'Printouts',
 2007:         'passwd' => 'Change Password',
 2008:         'grades' => 'Gradebook',
 2009:         'search' => 'Course search',
 2010:         'wishlist' => 'Stored links',
 2011:         'annotate' => 'Annotations',
 2012:     );
 2013:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 2014:     return ($typeorder,\%types);
 2015: }
 2016: 
 2017: sub print_rolecolors {
 2018:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 2019:     my %choices = &color_font_choices();
 2020:     my @bgs = ('pgbg','tabbg','sidebg');
 2021:     my @links = ('link','alink','vlink');
 2022:     my @images = ('img');
 2023:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 2024:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 2025:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2026:     my (%is_custom,%designs);
 2027:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 2028:     if (ref($settings) eq 'HASH') {
 2029:         if (ref($settings->{$role}) eq 'HASH') {
 2030:             if ($settings->{$role}->{'img'} ne '') {
 2031:                 $designs{'img'} = $settings->{$role}->{'img'};
 2032:                 $is_custom{'img'} = 1;
 2033:             }
 2034:             if ($settings->{$role}->{'font'} ne '') {
 2035:                 $designs{'font'} = $settings->{$role}->{'font'};
 2036:                 $is_custom{'font'} = 1;
 2037:             }
 2038:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2039:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2040:                 $is_custom{'fontmenu'} = 1;
 2041:             }
 2042:             foreach my $item (@bgs) {
 2043:                 if ($settings->{$role}->{$item} ne '') {
 2044:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2045:                     $is_custom{$item} = 1;
 2046:                 }
 2047:             }
 2048:             foreach my $item (@links) {
 2049:                 if ($settings->{$role}->{$item} ne '') {
 2050:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2051:                     $is_custom{$item} = 1;
 2052:                 }
 2053:             }
 2054:         }
 2055:     } else {
 2056:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 2057:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 2058:             $is_custom{'img'} = 1;
 2059:         }
 2060:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2061:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2062:             $is_custom{'fontmenu'} = 1; 
 2063:         }
 2064:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2065:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2066:             $is_custom{'font'} = 1;
 2067:         }
 2068:         foreach my $item (@bgs) {
 2069:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2070:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2071:                 $is_custom{$item} = 1;
 2072:             
 2073:             }
 2074:         }
 2075:         foreach my $item (@links) {
 2076:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2077:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2078:                 $is_custom{$item} = 1;
 2079:             }
 2080:         }
 2081:     }
 2082:     my $itemcount = 1;
 2083:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2084:     $datatable .= '</tr></table></td></tr>';
 2085:     return $datatable;
 2086: }
 2087: 
 2088: sub role_defaults {
 2089:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2090:     my %defaults;
 2091:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2092:         return %defaults;
 2093:     }
 2094:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2095:     if ($role eq 'login') {
 2096:         %defaults = (
 2097:                        font => $defaultdesign{$role.'.font'},
 2098:                     );
 2099:         if (ref($logintext) eq 'ARRAY') {
 2100:             foreach my $item (@{$logintext}) {
 2101:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2102:             }
 2103:         }
 2104:         foreach my $item (@{$images}) {
 2105:             $defaults{'showlogo'}{$item} = 1;
 2106:         }
 2107:     } else {
 2108:         %defaults = (
 2109:                        img => $defaultdesign{$role.'.img'},
 2110:                        font => $defaultdesign{$role.'.font'},
 2111:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2112:                     );
 2113:     }
 2114:     foreach my $item (@{$bgs}) {
 2115:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2116:     }
 2117:     foreach my $item (@{$links}) {
 2118:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2119:     }
 2120:     foreach my $item (@{$images}) {
 2121:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2122:     }
 2123:     return %defaults;
 2124: }
 2125: 
 2126: sub display_color_options {
 2127:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2128:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2129:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2130:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2131:     my $datatable = '<tr'.$css_class.'>'.
 2132:         '<td>'.$choices->{'font'}.'</td>';
 2133:     if (!$is_custom->{'font'}) {
 2134:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2135:     } else {
 2136:         $datatable .= '<td>&nbsp;</td>';
 2137:     }
 2138:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2139: 
 2140:     $datatable .= '<td><span class="LC_nobreak">'.
 2141:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2142:                   ' value="'.$current_color.'" />&nbsp;'.
 2143:                   '&nbsp;</span></td></tr>';
 2144:     unless ($role eq 'login') { 
 2145:         $datatable .= '<tr'.$css_class.'>'.
 2146:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2147:         if (!$is_custom->{'fontmenu'}) {
 2148:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2149:         } else {
 2150:             $datatable .= '<td>&nbsp;</td>';
 2151:         }
 2152: 	$current_color = $designs->{'fontmenu'} ?
 2153: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2154:         $datatable .= '<td><span class="LC_nobreak">'.
 2155:                       '<input class="colorchooser" type="text" size="10" name="'
 2156: 		      .$role.'_fontmenu"'.
 2157:                       ' value="'.$current_color.'" />&nbsp;'.
 2158:                       '&nbsp;</span></td></tr>';
 2159:     }
 2160:     my $switchserver = &check_switchserver($dom,$confname);
 2161:     foreach my $img (@{$images}) {
 2162: 	$itemcount ++;
 2163:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2164:         $datatable .= '<tr'.$css_class.'>'.
 2165:                       '<td>'.$choices->{$img};
 2166:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2167:         if ($role eq 'login') {
 2168:             if ($img eq 'login') {
 2169:                 $login_hdr_pick =
 2170:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2171:                 $logincolors =
 2172:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2173:                                        $designs,$defaults);
 2174:             } else {
 2175:                 if ($img ne 'domlogo') {
 2176:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2177:                 }
 2178:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2179:                     $alttext = $designs->{'alttext'}{$img};
 2180:                 }
 2181:             }
 2182:         }
 2183:         $datatable .= '</td>';
 2184:         if ($designs->{$img} ne '') {
 2185:             $imgfile = $designs->{$img};
 2186: 	    $img_import = ($imgfile =~ m{^/adm/});
 2187:         } else {
 2188:             $imgfile = $defaults->{$img};
 2189:         }
 2190:         if ($imgfile) {
 2191:             my ($showfile,$fullsize);
 2192:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2193:                 my $urldir = $1;
 2194:                 my $filename = $2;
 2195:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2196:                 if (@info) {
 2197:                     my $thumbfile = 'tn-'.$filename;
 2198:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2199:                     if (@thumb) {
 2200:                         $showfile = $urldir.'/'.$thumbfile;
 2201:                     } else {
 2202:                         $showfile = $imgfile;
 2203:                     }
 2204:                 } else {
 2205:                     $showfile = '';
 2206:                 }
 2207:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2208:                 $showfile = $imgfile;
 2209:                 my $imgdir = $1;
 2210:                 my $filename = $2;
 2211:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2212:                     $showfile = "/$imgdir/tn-".$filename;
 2213:                 } else {
 2214:                     my $input = $londocroot.$imgfile;
 2215:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2216:                     if (!-e $output) {
 2217:                         my ($width,$height) = &thumb_dimensions();
 2218:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2219:                         if ($fullwidth ne '' && $fullheight ne '') {
 2220:                             if ($fullwidth > $width && $fullheight > $height) { 
 2221:                                 my $size = $width.'x'.$height;
 2222:                                 my @args = ('convert','-sample',$size,$input,$output);
 2223:                                 system({$args[0]} @args);
 2224:                                 $showfile = "/$imgdir/tn-".$filename;
 2225:                             }
 2226:                         }
 2227:                     }
 2228:                 }
 2229:             }
 2230:             if ($showfile) {
 2231:                 if ($showfile =~ m{^/(adm|res)/}) {
 2232:                     if ($showfile =~ m{^/res/}) {
 2233:                         my $local_showfile =
 2234:                             &Apache::lonnet::filelocation('',$showfile);
 2235:                         &Apache::lonnet::repcopy($local_showfile);
 2236:                     }
 2237:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2238:                 }
 2239:                 if ($imgfile) {
 2240:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2241:                         if ($imgfile =~ m{^/res/}) {
 2242:                             my $local_imgfile =
 2243:                                 &Apache::lonnet::filelocation('',$imgfile);
 2244:                             &Apache::lonnet::repcopy($local_imgfile);
 2245:                         }
 2246:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2247:                     } else {
 2248:                         $fullsize = $imgfile;
 2249:                     }
 2250:                 }
 2251:                 $datatable .= '<td>';
 2252:                 if ($img eq 'login') {
 2253:                     $datatable .= $login_hdr_pick;
 2254:                 } 
 2255:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2256:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2257:             } else {
 2258:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2259:                               &mt('Upload:').'<br />';
 2260:             }
 2261:         } else {
 2262:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2263:                           &mt('Upload:').'<br />';
 2264:         }
 2265:         if ($switchserver) {
 2266:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2267:         } else {
 2268:             if ($img ne 'login') { # suppress file selection for Log-in header
 2269:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2270:             }
 2271:         }
 2272:         if (($role eq 'login') && ($img ne 'login')) {
 2273:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2274:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2275:                           '</label></span>';
 2276:         }
 2277:         $datatable .= '</td></tr>';
 2278:     }
 2279:     $itemcount ++;
 2280:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2281:     $datatable .= '<tr'.$css_class.'>'.
 2282:                   '<td>'.$choices->{'bgs'}.'</td>';
 2283:     my $bgs_def;
 2284:     foreach my $item (@{$bgs}) {
 2285:         if (!$is_custom->{$item}) {
 2286:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span class="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 2287:         }
 2288:     }
 2289:     if ($bgs_def) {
 2290:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2291:     } else {
 2292:         $datatable .= '<td>&nbsp;</td>';
 2293:     }
 2294:     $datatable .= '<td class="LC_right_item">'.
 2295:                   '<table border="0"><tr>';
 2296: 
 2297:     foreach my $item (@{$bgs}) {
 2298:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2299: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2300:         if ($designs->{'bgs'}{$item}) {
 2301:             $datatable .= '&nbsp;';
 2302:         }
 2303:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2304:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2305:     }
 2306:     $datatable .= '</tr></table></td></tr>';
 2307:     $itemcount ++;
 2308:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2309:     $datatable .= '<tr'.$css_class.'>'.
 2310:                   '<td>'.$choices->{'links'}.'</td>';
 2311:     my $links_def;
 2312:     foreach my $item (@{$links}) {
 2313:         if (!$is_custom->{$item}) {
 2314:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2315:         }
 2316:     }
 2317:     if ($links_def) {
 2318:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2319:     } else {
 2320:         $datatable .= '<td>&nbsp;</td>';
 2321:     }
 2322:     $datatable .= '<td class="LC_right_item">'.
 2323:                   '<table border="0"><tr>';
 2324:     foreach my $item (@{$links}) {
 2325: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2326:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2327:         if ($designs->{'links'}{$item}) {
 2328:             $datatable.='&nbsp;';
 2329:         }
 2330:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2331:                       '" /></td>';
 2332:     }
 2333:     $$rowtotal += $itemcount;
 2334:     return $datatable;
 2335: }
 2336: 
 2337: sub logo_display_options {
 2338:     my ($img,$defaults,$designs) = @_;
 2339:     my $checkedon;
 2340:     if (ref($defaults) eq 'HASH') {
 2341:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2342:             if ($defaults->{'showlogo'}{$img}) {
 2343:                 $checkedon = 'checked="checked" ';     
 2344:             }
 2345:         } 
 2346:     }
 2347:     if (ref($designs) eq 'HASH') {
 2348:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2349:             if (defined($designs->{'showlogo'}{$img})) {
 2350:                 if ($designs->{'showlogo'}{$img} == 0) {
 2351:                     $checkedon = '';
 2352:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2353:                     $checkedon = 'checked="checked" ';
 2354:                 }
 2355:             }
 2356:         }
 2357:     }
 2358:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2359:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2360:            &mt('show').'</label>'."\n";
 2361: }
 2362: 
 2363: sub login_header_options  {
 2364:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2365:     my $output = '';
 2366:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2367:         $output .= &mt('Text default(s):').'<br />';
 2368:         if (!$is_custom->{'textcol'}) {
 2369:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2370:                        '&nbsp;&nbsp;&nbsp;';
 2371:         }
 2372:         if (!$is_custom->{'bgcol'}) {
 2373:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2374:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2375:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2376:         }
 2377:         $output .= '<br />';
 2378:     }
 2379:     $output .='<br />';
 2380:     return $output;
 2381: }
 2382: 
 2383: sub login_text_colors {
 2384:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2385:     my $color_menu = '<table border="0"><tr>';
 2386:     foreach my $item (@{$logintext}) {
 2387:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2388:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2389:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2390:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2391:     }
 2392:     $color_menu .= '</tr></table><br />';
 2393:     return $color_menu;
 2394: }
 2395: 
 2396: sub image_changes {
 2397:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2398:     my $output;
 2399:     if ($img eq 'login') {
 2400:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2401:     } elsif (!$is_custom) {
 2402:         if ($img ne 'domlogo') {
 2403:             $output = &mt('Default image:').'<br />';
 2404:         } else {
 2405:             $output = &mt('Default in use:').'<br />';
 2406:         }
 2407:     }
 2408:     if ($img ne 'login') {
 2409:         if ($img_import) {
 2410:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2411:         }
 2412:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2413:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2414:         if ($is_custom) {
 2415:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2416:                        '<input type="checkbox" name="'.
 2417:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2418:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2419:         } else {
 2420:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2421:         }
 2422:     }
 2423:     return $output;
 2424: }
 2425: 
 2426: sub print_quotas {
 2427:     my ($dom,$settings,$rowtotal,$action) = @_;
 2428:     my $context;
 2429:     if ($action eq 'quotas') {
 2430:         $context = 'tools';
 2431:     } else {
 2432:         $context = $action;
 2433:     }
 2434:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2435:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2436:     my $typecount = 0;
 2437:     my ($css_class,%titles);
 2438:     if ($context eq 'requestcourses') {
 2439:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2440:         @options =('norequest','approval','validate','autolimit');
 2441:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2442:         %titles = &courserequest_titles();
 2443:     } elsif ($context eq 'requestauthor') {
 2444:         @usertools = ('author');
 2445:         @options = ('norequest','approval','automatic');
 2446:         %titles = &authorrequest_titles();
 2447:     } else {
 2448:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
 2449:         %titles = &tool_titles();
 2450:     }
 2451:     if (ref($types) eq 'ARRAY') {
 2452:         foreach my $type (@{$types}) {
 2453:             my $currdefquota;
 2454:             unless (($context eq 'requestcourses') ||
 2455:                     ($context eq 'requestauthor')) {
 2456:                 if (ref($settings) eq 'HASH') {
 2457:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2458:                         $currdefquota = $settings->{defaultquota}->{$type};
 2459:                     } else {
 2460:                         $currdefquota = $settings->{$type};
 2461:                     }
 2462:                 }
 2463:             }
 2464:             if (defined($usertypes->{$type})) {
 2465:                 $typecount ++;
 2466:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2467:                 $datatable .= '<tr'.$css_class.'>'.
 2468:                               '<td>'.$usertypes->{$type}.'</td>'.
 2469:                               '<td class="LC_left_item">';
 2470:                 if ($context eq 'requestcourses') {
 2471:                     $datatable .= '<table><tr>';
 2472:                 }
 2473:                 my %cell;  
 2474:                 foreach my $item (@usertools) {
 2475:                     if ($context eq 'requestcourses') {
 2476:                         my ($curroption,$currlimit);
 2477:                         if (ref($settings) eq 'HASH') {
 2478:                             if (ref($settings->{$item}) eq 'HASH') {
 2479:                                 $curroption = $settings->{$item}->{$type};
 2480:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2481:                                     $currlimit = $1; 
 2482:                                 }
 2483:                             }
 2484:                         }
 2485:                         if (!$curroption) {
 2486:                             $curroption = 'norequest';
 2487:                         }
 2488:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2489:                         foreach my $option (@options) {
 2490:                             my $val = $option;
 2491:                             if ($option eq 'norequest') {
 2492:                                 $val = 0;  
 2493:                             }
 2494:                             if ($option eq 'validate') {
 2495:                                 my $canvalidate = 0;
 2496:                                 if (ref($validations{$item}) eq 'HASH') { 
 2497:                                     if ($validations{$item}{$type}) {
 2498:                                         $canvalidate = 1;
 2499:                                     }
 2500:                                 }
 2501:                                 next if (!$canvalidate);
 2502:                             }
 2503:                             my $checked = '';
 2504:                             if ($option eq $curroption) {
 2505:                                 $checked = ' checked="checked"';
 2506:                             } elsif ($option eq 'autolimit') {
 2507:                                 if ($curroption =~ /^autolimit/) {
 2508:                                     $checked = ' checked="checked"';
 2509:                                 }                       
 2510:                             } 
 2511:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2512:                                   '<input type="radio" name="crsreq_'.$item.
 2513:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2514:                                   $titles{$option}.'</label>';
 2515:                             if ($option eq 'autolimit') {
 2516:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2517:                                                 $item.'_limit_'.$type.'" size="1" '.
 2518:                                                 'value="'.$currlimit.'" />';
 2519:                             }
 2520:                             $cell{$item} .= '</span> ';
 2521:                             if ($option eq 'autolimit') {
 2522:                                 $cell{$item} .= $titles{'unlimited'};
 2523:                             }
 2524:                         }
 2525:                     } elsif ($context eq 'requestauthor') {
 2526:                         my $curroption;
 2527:                         if (ref($settings) eq 'HASH') {
 2528:                             $curroption = $settings->{$type};
 2529:                         }
 2530:                         if (!$curroption) {
 2531:                             $curroption = 'norequest';
 2532:                         }
 2533:                         foreach my $option (@options) {
 2534:                             my $val = $option;
 2535:                             if ($option eq 'norequest') {
 2536:                                 $val = 0;
 2537:                             }
 2538:                             my $checked = '';
 2539:                             if ($option eq $curroption) {
 2540:                                 $checked = ' checked="checked"';
 2541:                             }
 2542:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2543:                                   '<input type="radio" name="authorreq_'.$type.
 2544:                                   '" value="'.$val.'"'.$checked.' />'.
 2545:                                   $titles{$option}.'</label></span>&nbsp; ';
 2546:                         }
 2547:                     } else {
 2548:                         my $checked = 'checked="checked" ';
 2549:                         if ($item eq 'timezone') {
 2550:                             $checked = '';
 2551:                         }
 2552:                         if (ref($settings) eq 'HASH') {
 2553:                             if (ref($settings->{$item}) eq 'HASH') {
 2554:                                 if (!$settings->{$item}->{$type}) {
 2555:                                     $checked = '';
 2556:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2557:                                     $checked =  'checked="checked" ';
 2558:                                 }
 2559:                             }
 2560:                         }
 2561:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2562:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2563:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2564:                                       '</label></span>&nbsp; ';
 2565:                     }
 2566:                 }
 2567:                 if ($context eq 'requestcourses') {
 2568:                     $datatable .= '</tr><tr>';
 2569:                     foreach my $item (@usertools) {
 2570:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2571:                     }
 2572:                     $datatable .= '</tr></table>';
 2573:                 }
 2574:                 $datatable .= '</td>';
 2575:                 unless (($context eq 'requestcourses') ||
 2576:                         ($context eq 'requestauthor')) {
 2577:                     $datatable .= 
 2578:                               '<td class="LC_right_item">'.
 2579:                               '<span class="LC_nobreak">'.
 2580:                               '<input type="text" name="quota_'.$type.
 2581:                               '" value="'.$currdefquota.
 2582:                               '" size="5" /></span></td>';
 2583:                 }
 2584:                 $datatable .= '</tr>';
 2585:             }
 2586:         }
 2587:     }
 2588:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2589:         $defaultquota = '20';
 2590:         if (ref($settings) eq 'HASH') {
 2591:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2592:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2593:             } elsif (defined($settings->{'default'})) {
 2594:                 $defaultquota = $settings->{'default'};
 2595:             }
 2596:         }
 2597:     }
 2598:     $typecount ++;
 2599:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2600:     $datatable .= '<tr'.$css_class.'>'.
 2601:                   '<td>'.$othertitle.'</td>'.
 2602:                   '<td class="LC_left_item">';
 2603:     if ($context eq 'requestcourses') {
 2604:         $datatable .= '<table><tr>';
 2605:     }
 2606:     my %defcell;
 2607:     foreach my $item (@usertools) {
 2608:         if ($context eq 'requestcourses') {
 2609:             my ($curroption,$currlimit);
 2610:             if (ref($settings) eq 'HASH') {
 2611:                 if (ref($settings->{$item}) eq 'HASH') {
 2612:                     $curroption = $settings->{$item}->{'default'};
 2613:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2614:                         $currlimit = $1;
 2615:                     }
 2616:                 }
 2617:             }
 2618:             if (!$curroption) {
 2619:                 $curroption = 'norequest';
 2620:             }
 2621:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2622:             foreach my $option (@options) {
 2623:                 my $val = $option;
 2624:                 if ($option eq 'norequest') {
 2625:                     $val = 0;
 2626:                 }
 2627:                 if ($option eq 'validate') {
 2628:                     my $canvalidate = 0;
 2629:                     if (ref($validations{$item}) eq 'HASH') {
 2630:                         if ($validations{$item}{'default'}) {
 2631:                             $canvalidate = 1;
 2632:                         }
 2633:                     }
 2634:                     next if (!$canvalidate);
 2635:                 }
 2636:                 my $checked = '';
 2637:                 if ($option eq $curroption) {
 2638:                     $checked = ' checked="checked"';
 2639:                 } elsif ($option eq 'autolimit') {
 2640:                     if ($curroption =~ /^autolimit/) {
 2641:                         $checked = ' checked="checked"';
 2642:                     }
 2643:                 }
 2644:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2645:                                   '<input type="radio" name="crsreq_'.$item.
 2646:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2647:                                   $titles{$option}.'</label>';
 2648:                 if ($option eq 'autolimit') {
 2649:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2650:                                        $item.'_limit_default" size="1" '.
 2651:                                        'value="'.$currlimit.'" />';
 2652:                 }
 2653:                 $defcell{$item} .= '</span> ';
 2654:                 if ($option eq 'autolimit') {
 2655:                     $defcell{$item} .= $titles{'unlimited'};
 2656:                 }
 2657:             }
 2658:         } elsif ($context eq 'requestauthor') {
 2659:             my $curroption;
 2660:             if (ref($settings) eq 'HASH') {
 2661:                 $curroption = $settings->{'default'};
 2662:             }
 2663:             if (!$curroption) {
 2664:                 $curroption = 'norequest';
 2665:             }
 2666:             foreach my $option (@options) {
 2667:                 my $val = $option;
 2668:                 if ($option eq 'norequest') {
 2669:                     $val = 0;
 2670:                 }
 2671:                 my $checked = '';
 2672:                 if ($option eq $curroption) {
 2673:                     $checked = ' checked="checked"';
 2674:                 }
 2675:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2676:                               '<input type="radio" name="authorreq_default"'.
 2677:                               ' value="'.$val.'"'.$checked.' />'.
 2678:                               $titles{$option}.'</label></span>&nbsp; ';
 2679:             }
 2680:         } else {
 2681:             my $checked = 'checked="checked" ';
 2682:             if (ref($settings) eq 'HASH') {
 2683:                 if (ref($settings->{$item}) eq 'HASH') {
 2684:                     if ($settings->{$item}->{'default'} == 0) {
 2685:                         $checked = '';
 2686:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2687:                         $checked = 'checked="checked" ';
 2688:                     }
 2689:                 }
 2690:             }
 2691:             $datatable .= '<span class="LC_nobreak"><label>'.
 2692:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2693:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2694:                           '</label></span>&nbsp; ';
 2695:         }
 2696:     }
 2697:     if ($context eq 'requestcourses') {
 2698:         $datatable .= '</tr><tr>';
 2699:         foreach my $item (@usertools) {
 2700:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2701:         }
 2702:         $datatable .= '</tr></table>';
 2703:     }
 2704:     $datatable .= '</td>';
 2705:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2706:         $datatable .= '<td class="LC_right_item">'.
 2707:                       '<span class="LC_nobreak">'.
 2708:                       '<input type="text" name="defaultquota" value="'.
 2709:                       $defaultquota.'" size="5" /></span></td>';
 2710:     }
 2711:     $datatable .= '</tr>';
 2712:     $typecount ++;
 2713:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2714:     $datatable .= '<tr'.$css_class.'>'.
 2715:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2716:     if ($context eq 'requestcourses') {
 2717:         $datatable .= &mt('(overrides affiliation, if set)').
 2718:                       '</td>'.
 2719:                       '<td class="LC_left_item">'.
 2720:                       '<table><tr>';
 2721:     } else {
 2722:         $datatable .= &mt('(overrides affiliation, if checked)').
 2723:                       '</td>'.
 2724:                       '<td class="LC_left_item" colspan="2">'.
 2725:                       '<br />';
 2726:     }
 2727:     my %advcell;
 2728:     foreach my $item (@usertools) {
 2729:         if ($context eq 'requestcourses') {
 2730:             my ($curroption,$currlimit);
 2731:             if (ref($settings) eq 'HASH') {
 2732:                 if (ref($settings->{$item}) eq 'HASH') {
 2733:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2734:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2735:                         $currlimit = $1;
 2736:                     }
 2737:                 }
 2738:             }
 2739:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2740:             my $checked = '';
 2741:             if ($curroption eq '') {
 2742:                 $checked = ' checked="checked"';
 2743:             }
 2744:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2745:                                '<input type="radio" name="crsreq_'.$item.
 2746:                                '__LC_adv" value=""'.$checked.' />'.
 2747:                                &mt('No override set').'</label></span>&nbsp; ';
 2748:             foreach my $option (@options) {
 2749:                 my $val = $option;
 2750:                 if ($option eq 'norequest') {
 2751:                     $val = 0;
 2752:                 }
 2753:                 if ($option eq 'validate') {
 2754:                     my $canvalidate = 0;
 2755:                     if (ref($validations{$item}) eq 'HASH') {
 2756:                         if ($validations{$item}{'_LC_adv'}) {
 2757:                             $canvalidate = 1;
 2758:                         }
 2759:                     }
 2760:                     next if (!$canvalidate);
 2761:                 }
 2762:                 my $checked = '';
 2763:                 if ($val eq $curroption) {
 2764:                     $checked = ' checked="checked"';
 2765:                 } elsif ($option eq 'autolimit') {
 2766:                     if ($curroption =~ /^autolimit/) {
 2767:                         $checked = ' checked="checked"';
 2768:                     }
 2769:                 }
 2770:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2771:                                   '<input type="radio" name="crsreq_'.$item.
 2772:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2773:                                   $titles{$option}.'</label>';
 2774:                 if ($option eq 'autolimit') {
 2775:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2776:                                        $item.'_limit__LC_adv" size="1" '.
 2777:                                        'value="'.$currlimit.'" />';
 2778:                 }
 2779:                 $advcell{$item} .= '</span> ';
 2780:                 if ($option eq 'autolimit') {
 2781:                     $advcell{$item} .= $titles{'unlimited'};
 2782:                 }
 2783:             }
 2784:         } elsif ($context eq 'requestauthor') {
 2785:             my $curroption;
 2786:             if (ref($settings) eq 'HASH') {
 2787:                 $curroption = $settings->{'_LC_adv'};
 2788:             }
 2789:             my $checked = '';
 2790:             if ($curroption eq '') {
 2791:                 $checked = ' checked="checked"';
 2792:             }
 2793:             $datatable .= '<span class="LC_nobreak"><label>'.
 2794:                           '<input type="radio" name="authorreq__LC_adv"'.
 2795:                           ' value=""'.$checked.' />'.
 2796:                           &mt('No override set').'</label></span>&nbsp; ';
 2797:             foreach my $option (@options) {
 2798:                 my $val = $option;
 2799:                 if ($option eq 'norequest') {
 2800:                     $val = 0;
 2801:                 }
 2802:                 my $checked = '';
 2803:                 if ($val eq $curroption) {
 2804:                     $checked = ' checked="checked"';
 2805:                 }
 2806:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2807:                               '<input type="radio" name="authorreq__LC_adv"'.
 2808:                               ' value="'.$val.'"'.$checked.' />'.
 2809:                               $titles{$option}.'</label></span>&nbsp; ';
 2810:             }
 2811:         } else {
 2812:             my $checked = 'checked="checked" ';
 2813:             if (ref($settings) eq 'HASH') {
 2814:                 if (ref($settings->{$item}) eq 'HASH') {
 2815:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2816:                         $checked = '';
 2817:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2818:                         $checked = 'checked="checked" ';
 2819:                     }
 2820:                 }
 2821:             }
 2822:             $datatable .= '<span class="LC_nobreak"><label>'.
 2823:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2824:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2825:                           '</label></span>&nbsp; ';
 2826:         }
 2827:     }
 2828:     if ($context eq 'requestcourses') {
 2829:         $datatable .= '</tr><tr>';
 2830:         foreach my $item (@usertools) {
 2831:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2832:         }
 2833:         $datatable .= '</tr></table>';
 2834:     }
 2835:     $datatable .= '</td></tr>';
 2836:     $$rowtotal += $typecount;
 2837:     return $datatable;
 2838: }
 2839: 
 2840: sub print_requestmail {
 2841:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2842:     my ($now,$datatable,%currapp);
 2843:     $now = time;
 2844:     if (ref($settings) eq 'HASH') {
 2845:         if (ref($settings->{'notify'}) eq 'HASH') {
 2846:             if ($settings->{'notify'}{'approval'} ne '') {
 2847:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2848:             }
 2849:         }
 2850:     }
 2851:     my $numinrow = 2;
 2852:     my $css_class;
 2853:     if ($$rowtotal%2) {
 2854:         $css_class = 'LC_odd_row';
 2855:     }
 2856:     if ($customcss) {
 2857:         $css_class .= " $customcss";
 2858:     }
 2859:     $css_class =~ s/^\s+//;
 2860:     if ($css_class) {
 2861:         $css_class = ' class="'.$css_class.'"';
 2862:     }
 2863:     if ($rowstyle) {
 2864:         $css_class .= ' style="'.$rowstyle.'"';
 2865:     }
 2866:     my $text;
 2867:     if ($action eq 'requestcourses') {
 2868:         $text = &mt('Receive notification of course requests requiring approval');
 2869:     } elsif ($action eq 'requestauthor') {
 2870:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2871:     } else {
 2872:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2873:     }
 2874:     $datatable = '<tr'.$css_class.'>'.
 2875:                  ' <td>'.$text.'</td>'.
 2876:                  ' <td class="LC_left_item">';
 2877:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2878:                                                  $action.'notifyapproval',%currapp);
 2879:     if ($numdc > 0) {
 2880:         $datatable .= $table;
 2881:     } else {
 2882:         $datatable .= &mt('There are no active Domain Coordinators');
 2883:     }
 2884:     $datatable .='</td></tr>';
 2885:     return $datatable;
 2886: }
 2887: 
 2888: sub print_studentcode {
 2889:     my ($settings,$rowtotal) = @_;
 2890:     my $rownum = 0; 
 2891:     my ($output,%current);
 2892:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2893:     if (ref($settings) eq 'HASH') {
 2894:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2895:             foreach my $type (@crstypes) {
 2896:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2897:             }
 2898:         }
 2899:     }
 2900:     $output .= '<tr>'.
 2901:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2902:                '<td class="LC_left_item">';
 2903:     foreach my $type (@crstypes) {
 2904:         my $check = ' ';
 2905:         if ($current{$type}) {
 2906:             $check = ' checked="checked" ';
 2907:         }
 2908:         $output .= '<span class="LC_nobreak"><label>'.
 2909:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2910:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2911:     }
 2912:     $output .= '</td></tr>';
 2913:     $$rowtotal ++;
 2914:     return $output;
 2915: }
 2916: 
 2917: sub print_textbookcourses {
 2918:     my ($dom,$type,$settings,$rowtotal) = @_;
 2919:     my $rownum = 0;
 2920:     my $css_class;
 2921:     my $itemcount = 1;
 2922:     my $maxnum = 0;
 2923:     my $bookshash;
 2924:     if (ref($settings) eq 'HASH') {
 2925:         $bookshash = $settings->{$type};
 2926:     }
 2927:     my %ordered;
 2928:     if (ref($bookshash) eq 'HASH') {
 2929:         foreach my $item (keys(%{$bookshash})) {
 2930:             if (ref($bookshash->{$item}) eq 'HASH') {
 2931:                 my $num = $bookshash->{$item}{'order'};
 2932:                 $ordered{$num} = $item;
 2933:             }
 2934:         }
 2935:     }
 2936:     my $confname = $dom.'-domainconfig';
 2937:     my $switchserver = &check_switchserver($dom,$confname);
 2938:     my $maxnum = scalar(keys(%ordered));
 2939:     my $datatable;
 2940:     if (keys(%ordered)) {
 2941:         my @items = sort { $a <=> $b } keys(%ordered);
 2942:         for (my $i=0; $i<@items; $i++) {
 2943:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2944:             my $key = $ordered{$items[$i]};
 2945:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2946:             my $coursetitle = $coursehash{'description'};
 2947:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2948:             if (ref($bookshash->{$key}) eq 'HASH') {
 2949:                 $subject = $bookshash->{$key}->{'subject'};
 2950:                 $title = $bookshash->{$key}->{'title'};
 2951:                 if ($type eq 'textbooks') {
 2952:                     $publisher = $bookshash->{$key}->{'publisher'};
 2953:                     $author = $bookshash->{$key}->{'author'};
 2954:                     $image = $bookshash->{$key}->{'image'};
 2955:                     if ($image ne '') {
 2956:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2957:                         my $imagethumb = "$path/tn-".$imagefile;
 2958:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2959:                     }
 2960:                 }
 2961:             }
 2962:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2963:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2964:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2965:             for (my $k=0; $k<=$maxnum; $k++) {
 2966:                 my $vpos = $k+1;
 2967:                 my $selstr;
 2968:                 if ($k == $i) {
 2969:                     $selstr = ' selected="selected" ';
 2970:                 }
 2971:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2972:             }
 2973:             $datatable .= '</select>'.('&nbsp;'x2).
 2974:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2975:                 &mt('Delete?').'</label></span></td>'.
 2976:                 '<td colspan="2">'.
 2977:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2978:                 ('&nbsp;'x2).
 2979:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2980:             if ($type eq 'textbooks') {
 2981:                 $datatable .= ('&nbsp;'x2).
 2982:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2983:                               ('&nbsp;'x2).
 2984:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2985:                               ('&nbsp;'x2).
 2986:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2987:                 if ($image) {
 2988:                     $datatable .= $imgsrc.
 2989:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2990:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2991:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2992:                 }
 2993:                 if ($switchserver) {
 2994:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2995:                 } else {
 2996:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2997:                 }
 2998:             }
 2999:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 3000:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3001:                           $coursetitle.'</span></td></tr>'."\n";
 3002:             $itemcount ++;
 3003:         }
 3004:     }
 3005:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3006:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 3007:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3008:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 3009:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 3010:     for (my $k=0; $k<$maxnum+1; $k++) {
 3011:         my $vpos = $k+1;
 3012:         my $selstr;
 3013:         if ($k == $maxnum) {
 3014:             $selstr = ' selected="selected" ';
 3015:         }
 3016:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3017:     }
 3018:     $datatable .= '</select>&nbsp;'."\n".
 3019:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3020:                   '<td colspan="2">'.
 3021:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3022:                   ('&nbsp;'x2).
 3023:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3024:                   ('&nbsp;'x2);
 3025:     if ($type eq 'textbooks') {
 3026:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3027:                       ('&nbsp;'x2).
 3028:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3029:                       ('&nbsp;'x2).
 3030:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3031:         if ($switchserver) {
 3032:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3033:         } else {
 3034:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3035:         }
 3036:         $datatable .= '</span>'."\n";
 3037:     }
 3038:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3039:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3040:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3041:                   &Apache::loncommon::selectcourse_link
 3042:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3043:                   '</span></td>'."\n".
 3044:                   '</tr>'."\n";
 3045:     $itemcount ++;
 3046:     return $datatable;
 3047: }
 3048: 
 3049: sub textbookcourses_javascript {
 3050:     my ($settings) = @_;
 3051:     return unless(ref($settings) eq 'HASH');
 3052:     my (%ordered,%total,%jstext);
 3053:     foreach my $type ('textbooks','templates') {
 3054:         $total{$type} = 0;
 3055:         if (ref($settings->{$type}) eq 'HASH') {
 3056:             foreach my $item (keys(%{$settings->{$type}})) {
 3057:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3058:                     my $num = $settings->{$type}->{$item}{'order'};
 3059:                     $ordered{$type}{$num} = $item;
 3060:                 }
 3061:             }
 3062:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3063:         }
 3064:         my @jsarray = ();
 3065:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3066:             push(@jsarray,$ordered{$type}{$item});
 3067:         }
 3068:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3069:     }
 3070:     return <<"ENDSCRIPT";
 3071: <script type="text/javascript">
 3072: // <![CDATA[
 3073: function reorderBooks(form,item,caller) {
 3074:     var changedVal;
 3075: $jstext{'textbooks'};
 3076: $jstext{'templates'};
 3077:     var newpos;
 3078:     var maxh;
 3079:     if (caller == 'textbooks') {  
 3080:         newpos = 'textbooks_addbook_pos';
 3081:         maxh = 1 + $total{'textbooks'};
 3082:     } else {
 3083:         newpos = 'templates_addbook_pos';
 3084:         maxh = 1 + $total{'templates'};
 3085:     }
 3086:     var current = new Array;
 3087:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3088:     if (item == newpos) {
 3089:         changedVal = newitemVal;
 3090:     } else {
 3091:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3092:         current[newitemVal] = newpos;
 3093:     }
 3094:     if (caller == 'textbooks') {
 3095:         for (var i=0; i<textbooks.length; i++) {
 3096:             var elementName = 'textbooks_'+textbooks[i];
 3097:             if (elementName != item) {
 3098:                 if (form.elements[elementName]) {
 3099:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3100:                     current[currVal] = elementName;
 3101:                 }
 3102:             }
 3103:         }
 3104:     }
 3105:     if (caller == 'templates') {
 3106:         for (var i=0; i<templates.length; i++) {
 3107:             var elementName = 'templates_'+templates[i];
 3108:             if (elementName != item) {
 3109:                 if (form.elements[elementName]) {
 3110:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3111:                     current[currVal] = elementName;
 3112:                 }
 3113:             }
 3114:         }
 3115:     }
 3116:     var oldVal;
 3117:     for (var j=0; j<maxh; j++) {
 3118:         if (current[j] == undefined) {
 3119:             oldVal = j;
 3120:         }
 3121:     }
 3122:     if (oldVal < changedVal) {
 3123:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3124:            var elementName = current[k];
 3125:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3126:         }
 3127:     } else {
 3128:         for (var k=changedVal; k<oldVal; k++) {
 3129:             var elementName = current[k];
 3130:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3131:         }
 3132:     }
 3133:     return;
 3134: }
 3135: 
 3136: // ]]>
 3137: </script>
 3138: 
 3139: ENDSCRIPT
 3140: }
 3141: 
 3142: sub ltitools_javascript {
 3143:     my ($settings) = @_;
 3144:     my $togglejs = &ltitools_toggle_js();
 3145:     unless (ref($settings) eq 'HASH') {
 3146:         return $togglejs;
 3147:     }
 3148:     my (%ordered,$total,%jstext);
 3149:     $total = 0;
 3150:     foreach my $item (keys(%{$settings})) {
 3151:         if (ref($settings->{$item}) eq 'HASH') {
 3152:             my $num = $settings->{$item}{'order'};
 3153:             $ordered{$num} = $item;
 3154:         }
 3155:     }
 3156:     $total = scalar(keys(%{$settings}));
 3157:     my @jsarray = ();
 3158:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3159:         push(@jsarray,$ordered{$item});
 3160:     }
 3161:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3162:     return <<"ENDSCRIPT";
 3163: <script type="text/javascript">
 3164: // <![CDATA[
 3165: function reorderLTITools(form,item) {
 3166:     var changedVal;
 3167: $jstext
 3168:     var newpos = 'ltitools_add_pos';
 3169:     var maxh = 1 + $total;
 3170:     var current = new Array;
 3171:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3172:     if (item == newpos) {
 3173:         changedVal = newitemVal;
 3174:     } else {
 3175:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3176:         current[newitemVal] = newpos;
 3177:     }
 3178:     for (var i=0; i<ltitools.length; i++) {
 3179:         var elementName = 'ltitools_'+ltitools[i];
 3180:         if (elementName != item) {
 3181:             if (form.elements[elementName]) {
 3182:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3183:                 current[currVal] = elementName;
 3184:             }
 3185:         }
 3186:     }
 3187:     var oldVal;
 3188:     for (var j=0; j<maxh; j++) {
 3189:         if (current[j] == undefined) {
 3190:             oldVal = j;
 3191:         }
 3192:     }
 3193:     if (oldVal < changedVal) {
 3194:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3195:            var elementName = current[k];
 3196:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3197:         }
 3198:     } else {
 3199:         for (var k=changedVal; k<oldVal; k++) {
 3200:             var elementName = current[k];
 3201:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3202:         }
 3203:     }
 3204:     return;
 3205: }
 3206: 
 3207: // ]]>
 3208: </script>
 3209: 
 3210: $togglejs
 3211: 
 3212: ENDSCRIPT
 3213: }
 3214: 
 3215: sub ltitools_toggle_js {
 3216:     return <<"ENDSCRIPT";
 3217: <script type="text/javascript">
 3218: // <![CDATA[
 3219: 
 3220: function toggleLTITools(form,setting,item) {
 3221:     var radioname = '';
 3222:     var divid = '';
 3223:     if ((setting == 'passback') || (setting == 'roster')) {
 3224:         radioname = 'ltitools_'+setting+'_'+item;
 3225:         divid = 'ltitools_'+setting+'time_'+item;
 3226:         var num = form.elements[radioname].length;
 3227:         if (num) {
 3228:             var setvis = '';
 3229:             for (var i=0; i<num; i++) {
 3230:                 if (form.elements[radioname][i].checked) {
 3231:                     if (form.elements[radioname][i].value == '1') {
 3232:                         if (document.getElementById(divid)) {
 3233:                             document.getElementById(divid).style.display = 'inline-block';
 3234:                         }
 3235:                         setvis = 1;
 3236:                     }
 3237:                     break;
 3238:                 }
 3239:             }
 3240:         }
 3241:         if (!setvis) {
 3242:             if (document.getElementById(divid)) {
 3243:                 document.getElementById(divid).style.display = 'none';
 3244:             }
 3245:         }
 3246:     }
 3247:     if (setting == 'user') {
 3248:         divid = 'ltitools_'+setting+'_div_'+item;
 3249:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3250:         if (document.getElementById(divid)) {
 3251:             if (document.getElementById(checkid)) {
 3252:                 if (document.getElementById(checkid).checked) {
 3253:                     document.getElementById(divid).style.display = 'inline-block';
 3254:                 } else {
 3255:                     document.getElementById(divid).style.display = 'none';
 3256:                 }
 3257:             }
 3258:         }
 3259:     }
 3260:     return;
 3261: }
 3262: // ]]>
 3263: </script>
 3264: 
 3265: ENDSCRIPT
 3266: }
 3267: 
 3268: sub wafproxy_javascript {
 3269:     my ($dom) = @_;
 3270:     return <<"ENDSCRIPT";
 3271: <script type="text/javascript">
 3272: // <![CDATA[
 3273: function updateWAF() {
 3274:     if (document.getElementById('wafproxy_remoteip')) {
 3275:         var wafremote = 0;
 3276:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3277:             wafremote = 1;
 3278:         }
 3279:         var fields = new Array('header','trust');
 3280:         for (var i=0; i<fields.length; i++) {
 3281:             if (document.getElementById('wafproxy_'+fields[i])) {
 3282:                 if (wafremote == 1) {
 3283:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3284:                 }
 3285:                 else {
 3286:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3287:                 }
 3288:             }
 3289:         }
 3290:         if (document.getElementById('wafproxyranges_$dom')) {
 3291:             if (wafremote == 1) {
 3292:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3293:             } else {
 3294:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3295:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3296:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3297:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3298:                         }
 3299:                     }
 3300:                 }
 3301:             }
 3302:         }
 3303:     }
 3304:     return;
 3305: }
 3306: 
 3307: function checkWAF() {
 3308:     if (document.getElementById('wafproxy_remoteip')) {
 3309:         var wafvpn = 0;
 3310:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3311:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3312:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3313:                     wafvpn = 1;
 3314:                 }
 3315:                 break;
 3316:             }
 3317:         }
 3318:         var vpn = new Array('vpnint','vpnext');
 3319:         for (var i=0; i<vpn.length; i++) {
 3320:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3321:                 if (wafvpn == 1) {
 3322:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3323:                 }
 3324:                 else {
 3325:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3326:                 }
 3327:             }
 3328:         }
 3329:         if (document.getElementById('wafproxyranges_$dom')) {
 3330:             if (wafvpn == 1) {
 3331:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3332:             }
 3333:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3334:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3335:             }
 3336:         }
 3337:     }
 3338:     return;
 3339: }
 3340: 
 3341: function toggleWAF() {
 3342:     if (document.getElementById('wafproxy_table')) {
 3343:         var wafproxy = 0;
 3344:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3345:              if (document.display.wafproxy_${dom}[i].checked) {
 3346:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3347:                      wafproxy = 1;
 3348:                      break;
 3349:                 }
 3350:             }
 3351:         }
 3352:         if (wafproxy == 1) {
 3353:             document.getElementById('wafproxy_table').style.display='inline';
 3354:         }
 3355:         else {
 3356:            document.getElementById('wafproxy_table').style.display='none';
 3357:         }
 3358:         if (document.getElementById('wafproxyrow_${dom}')) {
 3359:             if (wafproxy == 1) {
 3360:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3361:             }
 3362:             else {
 3363:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3364:             }
 3365:         }
 3366:         if (document.getElementById('nowafproxyrow_$dom')) {
 3367:             if (wafproxy == 1) {
 3368:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3369:             }
 3370:             else {
 3371:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3372:             }
 3373:         }
 3374:     }
 3375:     return;
 3376: }
 3377: // ]]>
 3378: </script>
 3379: 
 3380: ENDSCRIPT
 3381: }
 3382: 
 3383: sub proctoring_javascript {
 3384:     my ($settings) = @_;
 3385:     my (%ordered,$total,%jstext);
 3386:     $total = 0;
 3387:     if (ref($settings) eq 'HASH') {
 3388:         foreach my $item (keys(%{$settings})) {
 3389:             if (ref($settings->{$item}) eq 'HASH') {
 3390:                 my $num = $settings->{$item}{'order'};
 3391:                 $ordered{$num} = $item;
 3392:             }
 3393:         }
 3394:         $total = scalar(keys(%{$settings}));
 3395:     } else {
 3396:         %ordered = (
 3397:                        0 => 'proctorio',
 3398:                        1 => 'examity',
 3399:                    );
 3400:         $total = 2; 
 3401:     }
 3402:     my @jsarray = ();
 3403:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3404:         push(@jsarray,$ordered{$item});
 3405:     }
 3406:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3407:     return <<"ENDSCRIPT";
 3408: <script type="text/javascript">
 3409: // <![CDATA[
 3410: function reorderProctoring(form,item) {
 3411:     var changedVal;
 3412: $jstext
 3413:     var maxh = $total;
 3414:     var current = new Array;
 3415:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3416:     for (var i=0; i<proctors.length; i++) {
 3417:         var elementName = 'proctoring_pos_'+proctors[i];
 3418:         if (elementName != item) {
 3419:             if (form.elements[elementName]) {
 3420:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3421:                 current[currVal] = elementName;
 3422:             }
 3423:         }
 3424:     }
 3425:     var oldVal;
 3426:     for (var j=0; j<maxh; j++) {
 3427:         if (current[j] == undefined) {
 3428:             oldVal = j;
 3429:         }
 3430:     }
 3431:     if (oldVal < changedVal) {
 3432:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3433:            var elementName = current[k];
 3434:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3435:         }
 3436:     } else {
 3437:         for (var k=changedVal; k<oldVal; k++) {
 3438:             var elementName = current[k];
 3439:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3440:         }
 3441:     }
 3442:     return;
 3443: }
 3444: 
 3445: function toggleProctoring(form,item) {
 3446:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3447:     if (fieldsets.length) {
 3448:         var radioname = 'proctoring_available_'+item;
 3449:         var num = form.elements[radioname].length;
 3450:         if (num) {
 3451:             var setvis = '';
 3452:             for (var i=0; i<num; i++) {
 3453:                 if (form.elements[radioname][i].checked) {
 3454:                     if (form.elements[radioname][i].value == '1') {
 3455:                        setvis = 1;
 3456:                        break;
 3457:                     }
 3458:                 }
 3459:             }
 3460:             for (var j=0; j<fieldsets.length; j++) {
 3461:                 if (setvis) {
 3462:                     fieldsets[j].style.display = 'block';
 3463:                 } else {
 3464:                     fieldsets[j].style.display = 'none';
 3465:                 }
 3466:             }
 3467:         }
 3468:     }
 3469:     return;
 3470: }
 3471: 
 3472: // ]]>
 3473: </script>
 3474: 
 3475: ENDSCRIPT
 3476: }
 3477: 
 3478: 
 3479: sub lti_javascript {
 3480:     my ($dom,$settings) = @_;
 3481:     my $togglejs = &lti_toggle_js($dom);
 3482:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3483:     unless (ref($settings) eq 'HASH') {
 3484:         return $togglejs.'
 3485: <script type="text/javascript">
 3486: // <![CDATA[
 3487: 
 3488: '.$linkprot_js.'
 3489: 
 3490: // ]]>
 3491: </script>
 3492: ';
 3493:     }
 3494:     my (%ordered,$total,%jstext);
 3495:     $total = scalar(keys(%{$settings}));
 3496:     foreach my $item (keys(%{$settings})) {
 3497:         if (ref($settings->{$item}) eq 'HASH') {
 3498:             my $num = $settings->{$item}{'order'};
 3499:             if ($num eq '') {
 3500:                 $num = $total - 1;
 3501:             }
 3502:             $ordered{$num} = $item;
 3503:         }
 3504:     }
 3505:     my @jsarray = ();
 3506:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3507:         push(@jsarray,$ordered{$item});
 3508:     }
 3509:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3510:     return <<"ENDSCRIPT";
 3511: <script type="text/javascript">
 3512: // <![CDATA[
 3513: function reorderLTI(form,item) {
 3514:     var changedVal;
 3515: $jstext
 3516:     var newpos = 'lti_pos_add';
 3517:     var maxh = 1 + $total;
 3518:     var current = new Array;
 3519:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3520:     if (item == newpos) {
 3521:         changedVal = newitemVal;
 3522:     } else {
 3523:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3524:         current[newitemVal] = newpos;
 3525:     }
 3526:     for (var i=0; i<lti.length; i++) {
 3527:         var elementName = 'lti_pos_'+lti[i];
 3528:         if (elementName != item) {
 3529:             if (form.elements[elementName]) {
 3530:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3531:                 current[currVal] = elementName;
 3532:             }
 3533:         }
 3534:     }
 3535:     var oldVal;
 3536:     for (var j=0; j<maxh; j++) {
 3537:         if (current[j] == undefined) {
 3538:             oldVal = j;
 3539:         }
 3540:     }
 3541:     if (oldVal < changedVal) {
 3542:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3543:            var elementName = current[k];
 3544:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3545:         }
 3546:     } else {
 3547:         for (var k=changedVal; k<oldVal; k++) {
 3548:             var elementName = current[k];
 3549:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3550:         }
 3551:     }
 3552:     return;
 3553: }
 3554: 
 3555: $linkprot_js
 3556: 
 3557: // ]]>
 3558: </script>
 3559: 
 3560: $togglejs
 3561: 
 3562: ENDSCRIPT
 3563: }
 3564: 
 3565: sub lti_toggle_js {
 3566:     my ($dom) = @_;
 3567:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3568:                             localauth => 'Local auth argument',
 3569:                             krb       => 'Kerberos domain',
 3570:                          );
 3571:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3572:     &js_escape(\$crsincalert);
 3573:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3574:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3575:     my $course_servers = "'".join("','",keys(%servers))."'";
 3576:     return <<"ENDSCRIPT";
 3577: <script type="text/javascript">
 3578: // <![CDATA[
 3579: 
 3580: function toggleLTI(form,setting,item) {
 3581:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3582:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3583:         var setvis = '';
 3584:         var radioname = 'lti_requser_'+item;
 3585:         var num = form.elements[radioname].length;
 3586:         if (num) {
 3587:             for (var i=0; i<num; i++) {
 3588:                 if (form.elements[radioname][i].checked) {
 3589:                     if (form.elements[radioname][i].value == '1') {
 3590:                         setvis = 1;
 3591:                         break;
 3592:                     }
 3593:                 }
 3594:             }
 3595:         }
 3596:         if (usrfieldsets.length) {
 3597:             for (var j=0; j<usrfieldsets.length; j++) {
 3598:                 if (setvis) {
 3599:                     usrfieldsets[j].style.display = 'block';
 3600:                 } else {
 3601:                     usrfieldsets[j].style.display = 'none';
 3602:                 }
 3603:             }
 3604:         }
 3605:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3606:         if (crsfieldsets.length) {
 3607:             radioname = 'lti_crsinc_'+item;
 3608:             var num = form.elements[radioname].length;
 3609:             if (num) {
 3610:                 var crsvis = '';
 3611:                 for (var i=0; i<num; i++) {
 3612:                     if (form.elements[radioname][i].checked) {
 3613:                         if (form.elements[radioname][i].value == '1') {
 3614:                             if (setvis == '') {
 3615:                                 if (setting == 'crsinc'){
 3616:                                     alert("$crsincalert");
 3617:                                     form.elements[radioname][0].checked = true;
 3618:                                 }
 3619:                             } else {
 3620:                                 crsvis = 1;
 3621:                             }
 3622:                             break;
 3623:                         }
 3624:                     }
 3625:                 }
 3626:                 setvis = crsvis;
 3627:             }
 3628:             for (var j=0; j<crsfieldsets.length; j++) {
 3629:                 if (setvis) {
 3630:                     crsfieldsets[j].style.display = 'block';
 3631:                 } else {
 3632:                     crsfieldsets[j].style.display = 'none';
 3633:                 }
 3634:             }
 3635:         }
 3636:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3637:         var radioname = '';
 3638:         var divid = '';
 3639:         if (setting == 'user') {
 3640:             radioname = 'lti_mapuser_'+item;
 3641:             divid = 'lti_userfield_'+item;
 3642:         } else if (setting == 'crs') {
 3643:             radioname = 'lti_mapcrs_'+item;
 3644:             divid = 'lti_crsfield_'+item;
 3645:         } else if (setting == 'callback') {
 3646:             radioname = 'lti_callback_'+item;
 3647:             divid = 'lti_callbackfield_'+item;
 3648:         } else {
 3649:             radioname = 'lti_passback_'+item;
 3650:             divid =  'lti_passback_'+item;
 3651:         }
 3652:         var num = form.elements[radioname].length;
 3653:         if (num) {
 3654:             var setvis = '';
 3655:             for (var i=0; i<num; i++) {
 3656:                if (form.elements[radioname][i].checked) {
 3657:                    if ((setting == 'passback') || (setting == 'callback')) {
 3658:                        if (form.elements[radioname][i].value == '1') {
 3659:                            if (document.getElementById(divid)) {
 3660:                                document.getElementById(divid).style.display = 'inline-block';
 3661:                            }
 3662:                            setvis = 1;
 3663:                            break;
 3664:                        }
 3665:                    } else {
 3666:                        if (form.elements[radioname][i].value == 'other') {
 3667:                            if (document.getElementById(divid)) {
 3668:                                document.getElementById(divid).style.display = 'inline-block';
 3669:                            }
 3670:                            setvis = 1;
 3671:                            break;
 3672:                        }
 3673:                    }
 3674:                }
 3675:             }
 3676:             if (!setvis) {
 3677:                 if (document.getElementById(divid)) {
 3678:                     document.getElementById(divid).style.display = 'none';
 3679:                 }
 3680:             }
 3681:         }
 3682:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3683:         var numsec = form.elements['lti_crssec_'+item].length;
 3684:         if (numsec) {
 3685:             var setvis = '';
 3686:             for (var i=0; i<numsec; i++) {
 3687:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3688:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3689:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3690:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3691:                             setvis = 1;
 3692:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3693:                             if (numsrcsec) {
 3694:                                 var setsrcvis = '';
 3695:                                 for (var j=0; j<numsrcsec; j++) {
 3696:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3697:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3698:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3699:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3700:                                                 setsrcvis = 1;
 3701:                                             }
 3702:                                         }
 3703:                                     }
 3704:                                 }
 3705:                                 if (!setsrcvis) {
 3706:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3707:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3708:                                     }
 3709:                                 }
 3710:                             }
 3711:                         }
 3712:                     }
 3713:                 }
 3714:             }
 3715:             if (!setvis) {
 3716:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3717:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3718:                 }
 3719:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3720:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3721:                 }
 3722:             }
 3723:         }
 3724:     } else if (setting == 'lcauth') {
 3725:         var numauth = form.elements['lti_lcauth_'+item].length;
 3726:         if (numauth) {
 3727:             for (var i=0; i<numauth; i++) {
 3728:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3729:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3730:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3731:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3732:                         } else {
 3733:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3734:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3735:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3736:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3737:                                 } else {
 3738:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3739:                                 }
 3740:                             }
 3741:                         }
 3742:                     }
 3743:                 }
 3744:             }
 3745:         }
 3746:     } else if (setting == 'lcmenu') {
 3747:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3748:         var divid = 'lti_menufield_'+item;
 3749:         var setvis = '';
 3750:         for (var i=0; i<menus.length; i++) {
 3751:             var radioname = menus[i];
 3752:             var num = form.elements[radioname].length;
 3753:             if (num) {
 3754:                 for (var j=0; j<num; j++) {
 3755:                     if (form.elements[radioname][j].checked) {
 3756:                         if (form.elements[radioname][j].value == '1') {
 3757:                             if (document.getElementById(divid)) {
 3758:                                 document.getElementById(divid).style.display = 'inline-block';
 3759:                             }
 3760:                             setvis = 1;
 3761:                             break;
 3762:                         }
 3763:                     }
 3764:                 }
 3765:             }
 3766:             if (setvis == 1) {
 3767:                 break;
 3768:             }
 3769:         }
 3770:         if (!setvis) {
 3771:             if (document.getElementById(divid)) {
 3772:                 document.getElementById(divid).style.display = 'none';
 3773:             }
 3774:         }
 3775:     }
 3776:     return;
 3777: }
 3778: 
 3779: // ]]>
 3780: </script>
 3781: 
 3782: ENDSCRIPT
 3783: }
 3784: 
 3785: sub autoupdate_javascript {
 3786:     return <<"ENDSCRIPT";
 3787: <script type="text/javascript">
 3788: // <![CDATA[
 3789: function toggleLastActiveDays(form) {
 3790:     var radioname = 'lastactive';
 3791:     var divid = 'lastactive_div';
 3792:     var num = form.elements[radioname].length;
 3793:     if (num) {
 3794:         var setvis = '';
 3795:         for (var i=0; i<num; i++) {
 3796:             if (form.elements[radioname][i].checked) {
 3797:                 if (form.elements[radioname][i].value == '1') {
 3798:                     if (document.getElementById(divid)) {
 3799:                         document.getElementById(divid).style.display = 'inline-block';
 3800:                     }
 3801:                     setvis = 1;
 3802:                 }
 3803:                 break;
 3804:             }
 3805:         }
 3806:         if (!setvis) {
 3807:             if (document.getElementById(divid)) {
 3808:                 document.getElementById(divid).style.display = 'none';
 3809:             }
 3810:         }
 3811:     }
 3812:     return;
 3813: }
 3814: // ]]>
 3815: </script>
 3816: 
 3817: ENDSCRIPT
 3818: }
 3819: 
 3820: sub autoenroll_javascript {
 3821:     return <<"ENDSCRIPT";
 3822: <script type="text/javascript">
 3823: // <![CDATA[
 3824: function toggleFailsafe(form) {
 3825:     var radioname = 'autoenroll_failsafe';
 3826:     var divid = 'autoenroll_failsafe_div';
 3827:     var num = form.elements[radioname].length;
 3828:     if (num) {
 3829:         var setvis = '';
 3830:         for (var i=0; i<num; i++) {
 3831:             if (form.elements[radioname][i].checked) {
 3832:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3833:                     if (document.getElementById(divid)) {
 3834:                         document.getElementById(divid).style.display = 'inline-block';
 3835:                     }
 3836:                     setvis = 1;
 3837:                 }
 3838:                 break;
 3839:             }
 3840:         }
 3841:         if (!setvis) {
 3842:             if (document.getElementById(divid)) {
 3843:                 document.getElementById(divid).style.display = 'none';
 3844:             }
 3845:         }
 3846:     }
 3847:     return;
 3848: }
 3849: // ]]>
 3850: </script>
 3851: 
 3852: ENDSCRIPT
 3853: }
 3854: 
 3855: sub saml_javascript {
 3856:     return <<"ENDSCRIPT";
 3857: <script type="text/javascript">
 3858: // <![CDATA[
 3859: function toggleSamlOptions(form,hostid) {
 3860:     var radioname = 'saml_'+hostid;
 3861:     var tablecellon = 'samloptionson_'+hostid;
 3862:     var tablecelloff = 'samloptionsoff_'+hostid;
 3863:     var num = form.elements[radioname].length;
 3864:     if (num) {
 3865:         var setvis = '';
 3866:         for (var i=0; i<num; i++) {
 3867:             if (form.elements[radioname][i].checked) {
 3868:                 if (form.elements[radioname][i].value == '1') {
 3869:                     if (document.getElementById(tablecellon)) {
 3870:                         document.getElementById(tablecellon).style.display='';
 3871:                     }
 3872:                     if (document.getElementById(tablecelloff)) {
 3873:                         document.getElementById(tablecelloff).style.display='none';
 3874:                     }
 3875:                     setvis = 1;
 3876:                 }
 3877:                 break;
 3878:             }
 3879:         }
 3880:         if (!setvis) {
 3881:             if (document.getElementById(tablecellon)) {
 3882:                 document.getElementById(tablecellon).style.display='none';
 3883:             }
 3884:             if (document.getElementById(tablecelloff)) {
 3885:                 document.getElementById(tablecelloff).style.display='';
 3886:             }
 3887:         }
 3888:     }
 3889:     return;
 3890: }
 3891: // ]]>
 3892: </script>
 3893: 
 3894: ENDSCRIPT
 3895: }
 3896: 
 3897: sub ipaccess_javascript {
 3898:     my ($settings) = @_;
 3899:     my (%ordered,$total,%jstext);
 3900:     $total = 0;
 3901:     if (ref($settings) eq 'HASH') {
 3902:         foreach my $item (keys(%{$settings})) {
 3903:             if (ref($settings->{$item}) eq 'HASH') {
 3904:                 my $num = $settings->{$item}{'order'};
 3905:                 $ordered{$num} = $item;
 3906:             }
 3907:         }
 3908:         $total = scalar(keys(%{$settings}));
 3909:     }
 3910:     my @jsarray = ();
 3911:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3912:         push(@jsarray,$ordered{$item});
 3913:     }
 3914:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3915:     return <<"ENDSCRIPT";
 3916: <script type="text/javascript">
 3917: // <![CDATA[
 3918: function reorderIPaccess(form,item) {
 3919:     var changedVal;
 3920: $jstext
 3921:     var newpos = 'ipaccess_pos_add';
 3922:     var maxh = 1 + $total;
 3923:     var current = new Array;
 3924:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3925:     if (item == newpos) {
 3926:         changedVal = newitemVal;
 3927:     } else {
 3928:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3929:         current[newitemVal] = newpos;
 3930:     }
 3931:     for (var i=0; i<ipaccess.length; i++) {
 3932:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3933:         if (elementName != item) {
 3934:             if (form.elements[elementName]) {
 3935:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3936:                 current[currVal] = elementName;
 3937:             }
 3938:         }
 3939:     }
 3940:     var oldVal;
 3941:     for (var j=0; j<maxh; j++) {
 3942:         if (current[j] == undefined) {
 3943:             oldVal = j;
 3944:         }
 3945:     }
 3946:     if (oldVal < changedVal) {
 3947:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3948:            var elementName = current[k];
 3949:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3950:         }
 3951:     } else {
 3952:         for (var k=changedVal; k<oldVal; k++) {
 3953:             var elementName = current[k];
 3954:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3955:         }
 3956:     }
 3957:     return;
 3958: }
 3959: // ]]>
 3960: </script>
 3961: 
 3962: ENDSCRIPT
 3963: }
 3964: 
 3965: sub authordefaults_javascript {
 3966:     my %alert = &Apache::lonlocal::texthash (
 3967:                     reqd => 'Warning: at least one editor needs to be available.',
 3968:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3969:     );
 3970:     &js_escape(\%alert);
 3971:     return <<"ENDSCRIPT";
 3972: <script type="text/javascript">
 3973: // <![CDATA[
 3974: 
 3975: function checkEditors(form,checkbox,current) {
 3976:     if (form.elements[checkbox].length != undefined) {
 3977:         var count = 0;
 3978:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3979:             if (form.elements[checkbox][i].checked) {
 3980:                 count ++;
 3981:             }
 3982:         }
 3983:         if (count == 0) {
 3984:             if (current.type =='radio') {
 3985:                 current.checked = true;
 3986:                 alert('$alert{reqd}\\n$alert{rest}');
 3987:             }
 3988:         }
 3989:     }
 3990:     return;
 3991: }
 3992: // ]]>
 3993: </script>
 3994: 
 3995: ENDSCRIPT
 3996: }
 3997: 
 3998: sub print_autoenroll {
 3999:     my ($dom,$settings,$rowtotal) = @_;
 4000:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 4001:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 4002:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 4003:     $failsafesty = 'none';
 4004:     %failsafechecked = (
 4005:         off => ' checked="checked"',
 4006:     );
 4007:     if (ref($settings) eq 'HASH') {
 4008:         if (exists($settings->{'run'})) {
 4009:             if ($settings->{'run'} eq '0') {
 4010:                 $runoff = ' checked="checked" ';
 4011:                 $runon = ' ';
 4012:             } else {
 4013:                 $runon = ' checked="checked" ';
 4014:                 $runoff = ' ';
 4015:             }
 4016:         } else {
 4017:             if ($autorun) {
 4018:                 $runon = ' checked="checked" ';
 4019:                 $runoff = ' ';
 4020:             } else {
 4021:                 $runoff = ' checked="checked" ';
 4022:                 $runon = ' ';
 4023:             }
 4024:         }
 4025:         if (exists($settings->{'co-owners'})) {
 4026:             if ($settings->{'co-owners'} eq '0') {
 4027:                 $coownersoff = ' checked="checked" ';
 4028:                 $coownerson = ' ';
 4029:             } else {
 4030:                 $coownerson = ' checked="checked" ';
 4031:                 $coownersoff = ' ';
 4032:             }
 4033:         } else {
 4034:             $coownersoff = ' checked="checked" ';
 4035:             $coownerson = ' ';
 4036:         }
 4037:         if (exists($settings->{'sender_domain'})) {
 4038:             $defdom = $settings->{'sender_domain'};
 4039:         }
 4040:         if (exists($settings->{'failsafe'})) {
 4041:             $failsafe = $settings->{'failsafe'};
 4042:             if ($failsafe eq 'zero') {
 4043:                 $failsafechecked{'zero'} = ' checked="checked"';
 4044:                 $failsafechecked{'off'} = '';
 4045:                 $failsafesty = 'inline-block';
 4046:             } elsif ($failsafe eq 'any') {
 4047:                 $failsafechecked{'any'} = ' checked="checked"';
 4048:                 $failsafechecked{'off'} = '';
 4049:             }
 4050:             $autofailsafe = $settings->{'autofailsafe'};
 4051:         } elsif (exists($settings->{'autofailsafe'})) {
 4052:             $autofailsafe = $settings->{'autofailsafe'};
 4053:             if ($autofailsafe ne '') {
 4054:                 $failsafechecked{'zero'} = ' checked="checked"';
 4055:                 $failsafe = 'zero';
 4056:                 $failsafechecked{'off'} = '';
 4057:             }
 4058:         }
 4059:     } else {
 4060:         if ($autorun) {
 4061:             $runon = ' checked="checked" ';
 4062:             $runoff = ' ';
 4063:         } else {
 4064:             $runoff = ' checked="checked" ';
 4065:             $runon = ' ';
 4066:         }
 4067:     }
 4068:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4069:     my $notif_sender;
 4070:     if (ref($settings) eq 'HASH') {
 4071:         $notif_sender = $settings->{'sender_uname'};
 4072:     }
 4073:     my $datatable='<tr class="LC_odd_row">'.
 4074:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4075:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4076:                   '<input type="radio" name="autoenroll_run"'.
 4077:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4078:                   '<label><input type="radio" name="autoenroll_run"'.
 4079:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4080:                   '</tr><tr>'.
 4081:                   '<td>'.&mt('Notification messages - sender').
 4082:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4083:                   &mt('username').':&nbsp;'.
 4084:                   '<input type="text" name="sender_uname" value="'.
 4085:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4086:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4087:                   '<tr class="LC_odd_row">'.
 4088:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4089:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4090:                   '<input type="radio" name="autoassign_coowners"'.
 4091:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4092:                   '<label><input type="radio" name="autoassign_coowners"'.
 4093:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4094:                   '</tr><tr>'.
 4095:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4096:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4097:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="off" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'off'}.' />'.&mt('Not in use').'</label></span>&nbsp;&nbsp;&nbsp; '.
 4098:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="zero" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'zero'}.' />'.&mt('Retrieved section enrollment is zero').'</label></span><br />'.
 4099:                   '<span class="LC_nobreak"><label><input type="radio" name="autoenroll_failsafe" value="any" onclick="toggleFailsafe(this.form)"'.$failsafechecked{'any'}.' />'.&mt('Retrieved section enrollment is zero or greater').'</label></span>'.
 4100:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4101:                   '<span class="LC_nobreak">'.
 4102:                   &mt('Threshold for number of students in section to drop: [_1]',
 4103:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4104:                   '</span></div></td></tr>';
 4105:     $$rowtotal += 4;
 4106:     return $datatable;
 4107: }
 4108: 
 4109: sub print_autoupdate {
 4110:     my ($position,$dom,$settings,$rowtotal) = @_;
 4111:     my ($enable,$datatable);
 4112:     if ($position eq 'top') {
 4113:         my %choices = &Apache::lonlocal::texthash (
 4114:                           run        => 'Auto-update active?',
 4115:                           classlists => 'Update information in classlists?',
 4116:                           unexpired  => 'Skip updates for users without active or future roles?',
 4117:                           lastactive => 'Skip updates for inactive users?',
 4118:         );
 4119:         my $itemcount = 0;
 4120:         my $updateon = ' ';
 4121:         my $updateoff = ' checked="checked" ';
 4122:         if (ref($settings) eq 'HASH') {
 4123:             if ($settings->{'run'} eq '1') {
 4124:                 $updateon = $updateoff;
 4125:                 $updateoff = ' ';
 4126:             }
 4127:         }
 4128:         $enable = '<tr class="LC_odd_row">'.
 4129:                   '<td>'.$choices{'run'}.'</td>'.
 4130:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4131:                   '<input type="radio" name="autoupdate_run"'.
 4132:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4133:                   '<label><input type="radio" name="autoupdate_run"'.
 4134:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4135:                   '</tr>';
 4136:         my @toggles = ('classlists','unexpired');
 4137:         my %defaultchecked = ('classlists' => 'off',
 4138:                               'unexpired'  => 'off'
 4139:                               );
 4140:         $$rowtotal ++;
 4141:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4142:                                                      \%choices,$itemcount,'','','left','no');
 4143:         $datatable = $enable.$datatable;
 4144:         $$rowtotal += $itemcount;
 4145:         my $lastactiveon = ' ';
 4146:         my $lastactiveoff = ' checked="checked" ';
 4147:         my $lastactivestyle = 'none';
 4148:         my $lastactivedays;
 4149:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4150:         if (ref($settings) eq 'HASH') {
 4151:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4152:                 $lastactiveon = $lastactiveoff;
 4153:                 $lastactiveoff = ' ';
 4154:                 $lastactivestyle = 'inline-block';
 4155:                 $lastactivedays = $settings->{'lastactive'};
 4156:             }
 4157:         }
 4158:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4159:         $datatable .= '<tr'.$css_class.'>'.
 4160:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4161:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4162:                       '<input type="radio" name="lastactive"'.
 4163:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4164:                       '&nbsp;<label>'.
 4165:                       '<input type="radio" name="lastactive"'.
 4166:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4167:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4168:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4169:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4170:                           $lastactivedays.'" />').
 4171:                       '</span></td>'.
 4172:                       '</tr>';
 4173:         $$rowtotal ++;
 4174:     } elsif ($position eq 'middle') {
 4175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4176:         my $numinrow = 3;
 4177:         my $locknamesettings;
 4178:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4179:                                      $dom,$numinrow,$othertitle,
 4180:                                     'lockablenames',$rowtotal);
 4181:         $$rowtotal ++;
 4182:     } else {
 4183:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4184:         my @fields = ('lastname','firstname','middlename','generation',
 4185:                       'permanentemail','id');
 4186:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4187:         my $numrows = 0;
 4188:         if (ref($types) eq 'ARRAY') {
 4189:             if (@{$types} > 0) {
 4190:                 $datatable = 
 4191:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4192:                                          \@fields,$types,\$numrows);
 4193:                     $$rowtotal += @{$types}; 
 4194:             }
 4195:         }
 4196:         $datatable .= 
 4197:             &usertype_update_row($settings,{'default' => $othertitle},
 4198:                                  \%fieldtitles,\@fields,['default'],
 4199:                                  \$numrows);
 4200:         $$rowtotal ++;     
 4201:     }
 4202:     return $datatable;
 4203: }
 4204: 
 4205: sub print_autocreate {
 4206:     my ($dom,$settings,$rowtotal) = @_;
 4207:     my (%createon,%createoff,%currhash);
 4208:     my @types = ('xml','req');
 4209:     if (ref($settings) eq 'HASH') {
 4210:         foreach my $item (@types) {
 4211:             $createoff{$item} = ' checked="checked" ';
 4212:             $createon{$item} = ' ';
 4213:             if (exists($settings->{$item})) {
 4214:                 if ($settings->{$item}) {
 4215:                     $createon{$item} = ' checked="checked" ';
 4216:                     $createoff{$item} = ' ';
 4217:                 }
 4218:             }
 4219:         }
 4220:         if ($settings->{'xmldc'} ne '') {
 4221:             $currhash{$settings->{'xmldc'}} = 1;
 4222:         }
 4223:     } else {
 4224:         foreach my $item (@types) {
 4225:             $createoff{$item} = ' checked="checked" ';
 4226:             $createon{$item} = ' ';
 4227:         }
 4228:     }
 4229:     $$rowtotal += 2;
 4230:     my $numinrow = 2;
 4231:     my $datatable='<tr class="LC_odd_row">'.
 4232:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4233:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4234:                   '<input type="radio" name="autocreate_xml"'.
 4235:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4236:                   '<label><input type="radio" name="autocreate_xml"'.
 4237:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4238:                   '</td></tr><tr>'.
 4239:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4240:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4241:                   '<input type="radio" name="autocreate_req"'.
 4242:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4243:                   '<label><input type="radio" name="autocreate_req"'.
 4244:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4245:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4246:                                                    'autocreate_xmldc',%currhash);
 4247:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4248:     if ($numdc > 1) {
 4249:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4250:                       '</td><td class="LC_left_item">';
 4251:     } else {
 4252:         $datatable .= &mt('Course creation processed as:').
 4253:                       '</td><td class="LC_right_item">';
 4254:     }
 4255:     $datatable .= $dctable.'</td></tr>';
 4256:     $$rowtotal += $rows;
 4257:     return $datatable;
 4258: }
 4259: 
 4260: sub print_directorysrch {
 4261:     my ($position,$dom,$settings,$rowtotal) = @_;
 4262:     my $datatable;
 4263:     if ($position eq 'top') {
 4264:         my $instsrchon = ' ';
 4265:         my $instsrchoff = ' checked="checked" ';
 4266:         my ($exacton,$containson,$beginson);
 4267:         my $instlocalon = ' ';
 4268:         my $instlocaloff = ' checked="checked" ';
 4269:         if (ref($settings) eq 'HASH') {
 4270:             if ($settings->{'available'} eq '1') {
 4271:                 $instsrchon = $instsrchoff;
 4272:                 $instsrchoff = ' ';
 4273:             }
 4274:             if ($settings->{'localonly'} eq '1') {
 4275:                 $instlocalon = $instlocaloff;
 4276:                 $instlocaloff = ' ';
 4277:             }
 4278:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4279:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4280:                     if ($type eq 'exact') {
 4281:                         $exacton = ' checked="checked" ';
 4282:                     } elsif ($type eq 'contains') {
 4283:                         $containson = ' checked="checked" ';
 4284:                     } elsif ($type eq 'begins') {
 4285:                         $beginson = ' checked="checked" ';
 4286:                     }
 4287:                 }
 4288:             } else {
 4289:                 if ($settings->{'searchtypes'} eq 'exact') {
 4290:                     $exacton = ' checked="checked" ';
 4291:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4292:                     $containson = ' checked="checked" ';
 4293:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4294:                     $exacton = ' checked="checked" ';
 4295:                     $containson = ' checked="checked" ';
 4296:                 }
 4297:             }
 4298:         }
 4299:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4300:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4301: 
 4302:         my $numinrow = 4;
 4303:         my $cansrchrow = 0;
 4304:         $datatable='<tr class="LC_odd_row">'.
 4305:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4306:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4307:                    '<input type="radio" name="dirsrch_available"'.
 4308:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4309:                    '<label><input type="radio" name="dirsrch_available"'.
 4310:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4311:                    '</tr><tr>'.
 4312:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4313:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4314:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4315:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4316:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4317:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4318:                    '</tr>';
 4319:         $$rowtotal += 2;
 4320:         if (ref($usertypes) eq 'HASH') {
 4321:             if (keys(%{$usertypes}) > 0) {
 4322:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4323:                                              $numinrow,$othertitle,'cansearch',
 4324:                                              $rowtotal);
 4325:                 $cansrchrow = 1;
 4326:             }
 4327:         }
 4328:         if ($cansrchrow) {
 4329:             $$rowtotal ++;
 4330:             $datatable .= '<tr>';
 4331:         } else {
 4332:             $datatable .= '<tr class="LC_odd_row">';
 4333:         }
 4334:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4335:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4336:         foreach my $title (@{$titleorder}) {
 4337:             if (defined($searchtitles->{$title})) {
 4338:                 my $check = ' ';
 4339:                 if (ref($settings) eq 'HASH') {
 4340:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4341:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4342:                             $check = ' checked="checked" ';
 4343:                         }
 4344:                     }
 4345:                 }
 4346:                 $datatable .= '<td class="LC_left_item">'.
 4347:                               '<span class="LC_nobreak"><label>'.
 4348:                               '<input type="checkbox" name="searchby" '.
 4349:                               'value="'.$title.'"'.$check.'/>'.
 4350:                               $searchtitles->{$title}.'</label></span></td>';
 4351:             }
 4352:         }
 4353:         $datatable .= '</tr></table></td></tr>';
 4354:         $$rowtotal ++;
 4355:         if ($cansrchrow) {
 4356:             $datatable .= '<tr class="LC_odd_row">';
 4357:         } else {
 4358:             $datatable .= '<tr>';
 4359:         }
 4360:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4361:                       '<td class="LC_left_item" colspan="2">'.
 4362:                       '<span class="LC_nobreak"><label>'.
 4363:                       '<input type="checkbox" name="searchtypes" '.
 4364:                       $exacton.' value="exact" />'.&mt('Exact match').
 4365:                       '</label>&nbsp;'.
 4366:                       '<label><input type="checkbox" name="searchtypes" '.
 4367:                       $beginson.' value="begins" />'.&mt('Begins with').
 4368:                       '</label>&nbsp;'.
 4369:                       '<label><input type="checkbox" name="searchtypes" '.
 4370:                       $containson.' value="contains" />'.&mt('Contains').
 4371:                       '</label></span></td></tr>';
 4372:         $$rowtotal ++;
 4373:     } else {
 4374:         my $domsrchon = ' checked="checked" ';
 4375:         my $domsrchoff = ' ';
 4376:         my $domlocalon = ' ';
 4377:         my $domlocaloff = ' checked="checked" ';
 4378:         if (ref($settings) eq 'HASH') {
 4379:             if ($settings->{'lclocalonly'} eq '1') {
 4380:                 $domlocalon = $domlocaloff;
 4381:                 $domlocaloff = ' ';
 4382:             }
 4383:             if ($settings->{'lcavailable'} eq '0') {
 4384:                 $domsrchoff = $domsrchon;
 4385:                 $domsrchon = ' ';
 4386:             }
 4387:         }
 4388:         $datatable='<tr class="LC_odd_row">'.
 4389:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4390:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4391:                       '<input type="radio" name="dirsrch_domavailable"'.
 4392:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4393:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4394:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4395:                       '</tr><tr>'.
 4396:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4397:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4398:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4399:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4400:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4401:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4402:                       '</tr>';
 4403:         $$rowtotal += 2;
 4404:     }
 4405:     return $datatable;
 4406: }
 4407: 
 4408: sub print_contacts {
 4409:     my ($position,$dom,$settings,$rowtotal) = @_;
 4410:     my $datatable;
 4411:     my @contacts = ('adminemail','supportemail');
 4412:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4413:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4414:     if ($position eq 'top') {
 4415:         if (ref($settings) eq 'HASH') {
 4416:             foreach my $item (@contacts) {
 4417:                 if (exists($settings->{$item})) {
 4418:                     $to{$item} = $settings->{$item};
 4419:                 }
 4420:             }
 4421:         }
 4422:     } elsif ($position eq 'middle') {
 4423:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4424:                      'updatesmail','idconflictsmail','hostipmail');
 4425:         foreach my $type (@mailings) {
 4426:             $otheremails{$type} = '';
 4427:         }
 4428:     } elsif ($position eq 'lower') {
 4429:         if (ref($settings) eq 'HASH') {
 4430:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4431:                 %lonstatus = %{$settings->{'lonstatus'}};
 4432:             }
 4433:         }
 4434:     } else {
 4435:         @mailings = ('helpdeskmail','otherdomsmail');
 4436:         foreach my $type (@mailings) {
 4437:             $otheremails{$type} = '';
 4438:         }
 4439:         $bccemails{'helpdeskmail'} = '';
 4440:         $bccemails{'otherdomsmail'} = '';
 4441:         $includestr{'helpdeskmail'} = '';
 4442:         $includestr{'otherdomsmail'} = '';
 4443:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4444:     }
 4445:     if (ref($settings) eq 'HASH') {
 4446:         unless (($position eq 'top') || ($position eq 'lower')) {
 4447:             foreach my $type (@mailings) {
 4448:                 if (exists($settings->{$type})) {
 4449:                     if (ref($settings->{$type}) eq 'HASH') {
 4450:                         foreach my $item (@contacts) {
 4451:                             if ($settings->{$type}{$item}) {
 4452:                                 $checked{$type}{$item} = ' checked="checked" ';
 4453:                             }
 4454:                         }
 4455:                         $otheremails{$type} = $settings->{$type}{'others'};
 4456:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4457:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4458:                             if ($settings->{$type}{'include'} ne '') {
 4459:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4460:                                 $includestr{$type} = &unescape($includestr{$type});
 4461:                             }
 4462:                         }
 4463:                     }
 4464:                 } elsif ($type eq 'lonstatusmail') {
 4465:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4466:                 }
 4467:             }
 4468:         }
 4469:         if ($position eq 'bottom') {
 4470:             foreach my $type (@mailings) {
 4471:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4472:                 if ($settings->{$type}{'include'} ne '') {
 4473:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4474:                     $includestr{$type} = &unescape($includestr{$type});
 4475:                 }
 4476:             }
 4477:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4478:                 if (ref($fields) eq 'ARRAY') {
 4479:                     foreach my $field (@{$fields}) {
 4480:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4481:                     }
 4482:                 }
 4483:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4484:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4485:                 } else {
 4486:                     $maxsize = '1.0';
 4487:                 }
 4488:             } else {
 4489:                 if (ref($fields) eq 'ARRAY') {
 4490:                     foreach my $field (@{$fields}) {
 4491:                         $currfield{$field} = 'yes';
 4492:                     }
 4493:                 }
 4494:                 $maxsize = '1.0';
 4495:             }
 4496:         }
 4497:     } else {
 4498:         if ($position eq 'top') {
 4499:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4500:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4501:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4502:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4503:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4504:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4505:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4506:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4507:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4508:         } elsif ($position eq 'bottom') {
 4509:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4510:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4511:             if (ref($fields) eq 'ARRAY') {
 4512:                 foreach my $field (@{$fields}) {
 4513:                     $currfield{$field} = 'yes';
 4514:                 }
 4515:             }
 4516:             $maxsize = '1.0';
 4517:         }
 4518:     }
 4519:     my ($titles,$short_titles) = &contact_titles();
 4520:     my $rownum = 0;
 4521:     my $css_class;
 4522:     if ($position eq 'top') {
 4523:         foreach my $item (@contacts) {
 4524:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4525:             $datatable .= '<tr'.$css_class.'>'. 
 4526:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4527:                           '</span></td><td class="LC_right_item">'.
 4528:                           '<input type="text" name="'.$item.'" value="'.
 4529:                           $to{$item}.'" /></td></tr>';
 4530:             $rownum ++;
 4531:         }
 4532:     } elsif ($position eq 'bottom') {
 4533:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4534:         $datatable .= '<tr'.$css_class.'>'.
 4535:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4536:                       &mt('(e-mail, subject, and description always shown)').
 4537:                       '</td><td class="LC_left_item">';
 4538:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4539:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4540:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4541:             foreach my $field (@{$fields}) {
 4542:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4543:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4544:                     $datatable .= ' '.&mt('(logged-in users)');
 4545:                 }
 4546:                 $datatable .='</td><td>';
 4547:                 my $clickaction;
 4548:                 if ($field eq 'screenshot') {
 4549:                     $clickaction = ' onclick="screenshotSize(this);"';
 4550:                 }
 4551:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4552:                     foreach my $option (@{$possoptions->{$field}}) {
 4553:                         my $checked;
 4554:                         if ($currfield{$field} eq $option) {
 4555:                             $checked = ' checked="checked"';
 4556:                         }
 4557:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4558:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4559:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4560:                                       '</label></span>'.('&nbsp;'x2);
 4561:                     }
 4562:                 }
 4563:                 if ($field eq 'screenshot') {
 4564:                     my $display;
 4565:                     if ($currfield{$field} eq 'no') {
 4566:                         $display = ' style="display:none"';
 4567:                     }
 4568:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4569:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4570:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4571:                 }
 4572:                 $datatable .= '</td></tr>';
 4573:             }
 4574:             $datatable .= '</table>';
 4575:         }
 4576:         $datatable .= '</td></tr>'."\n";
 4577:         $rownum ++;
 4578:     }
 4579:     unless (($position eq 'top') || ($position eq 'lower')) {
 4580:         foreach my $type (@mailings) {
 4581:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4582:             $datatable .= '<tr'.$css_class.'>'.
 4583:                           '<td><span class="LC_nobreak">'.
 4584:                           $titles->{$type}.': </span></td>'.
 4585:                           '<td class="LC_left_item">';
 4586:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4587:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4588:             }
 4589:             $datatable .= '<span class="LC_nobreak">';
 4590:             foreach my $item (@contacts) {
 4591:                 $datatable .= '<label>'.
 4592:                               '<input type="checkbox" name="'.$type.'"'.
 4593:                               $checked{$type}{$item}.
 4594:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4595:                               '</label>&nbsp;';
 4596:             }
 4597:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4598:                           '<input type="text" name="'.$type.'_others" '.
 4599:                           'value="'.$otheremails{$type}.'"  />';
 4600:             my %locchecked;
 4601:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4602:                 foreach my $loc ('s','b') {
 4603:                     if ($includeloc{$type} eq $loc) {
 4604:                         $locchecked{$loc} = ' checked="checked"';
 4605:                         last;
 4606:                     }
 4607:                 }
 4608:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4609:                               '<input type="text" name="'.$type.'_bcc" '.
 4610:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4611:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4612:                               &mt('Text automatically added to e-mail:').' '.
 4613:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4614:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4615:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4616:                               ('&nbsp;'x2).
 4617:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4618:                               '</span></fieldset>';
 4619:             }
 4620:             $datatable .= '</td></tr>'."\n";
 4621:             $rownum ++;
 4622:         }
 4623:     }
 4624:     if ($position eq 'middle') {
 4625:         my %choices;
 4626:         my $corelink = &core_link_msu();
 4627:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4628:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4629:                                         $corelink);
 4630:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4631:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4632:         my %defaultchecked = ('reporterrors'  => 'on',
 4633:                               'reportupdates' => 'on',
 4634:                               'reportstatus'  => 'on');
 4635:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4636:                                                    \%choices,$rownum);
 4637:         $datatable .= $reports;
 4638:     } elsif ($position eq 'lower') {
 4639:         my (%current,%excluded,%weights);
 4640:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4641:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4642:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4643:         } else {
 4644:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4645:         }
 4646:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4647:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4648:         } else {
 4649:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4650:         }
 4651:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4652:             foreach my $type ('E','W','N','U') {
 4653:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4654:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4655:                 } else {
 4656:                     $weights{$type} = $defaults->{$type};
 4657:                 }
 4658:             }
 4659:         } else {
 4660:             foreach my $type ('E','W','N','U') {
 4661:                 $weights{$type} = $defaults->{$type};
 4662:             }
 4663:         }
 4664:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4665:             if (@{$lonstatus{'excluded'}} > 0) {
 4666:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4667:             }
 4668:         }
 4669:         foreach my $item ('errorthreshold','errorsysmail') {
 4670:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4671:             $datatable .= '<tr'.$css_class.'>'.
 4672:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4673:                           $titles->{$item}.
 4674:                           '</span></td><td class="LC_left_item">'.
 4675:                           '<input type="text" name="'.$item.'" value="'.
 4676:                           $current{$item}.'" size="5" /></td></tr>';
 4677:             $rownum ++;
 4678:         }
 4679:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4680:         $datatable .= '<tr'.$css_class.'>'.
 4681:                       '<td class="LC_left_item">'.
 4682:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4683:                       '</span></td><td class="LC_left_item"><table><tr>';
 4684:         foreach my $type ('E','W','N','U') {
 4685:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4686:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4687:                           $weights{$type}.'" size="5" /></td>';
 4688:         }
 4689:         $datatable .= '</tr></table></tr>';
 4690:         $rownum ++;
 4691:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4692:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4693:                       $titles->{'errorexcluded'}.'</td>'.
 4694:                       '<td class="LC_left_item"><table>';
 4695:         my $numinrow = 4;
 4696:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4697:         for (my $i=0; $i<@ids; $i++) {
 4698:             my $rem = $i%($numinrow);
 4699:             if ($rem == 0) {
 4700:                 if ($i > 0) {
 4701:                     $datatable .= '</tr>';
 4702:                 }
 4703:                 $datatable .= '<tr>';
 4704:             }
 4705:             my $check;
 4706:             if ($excluded{$ids[$i]}) {
 4707:                 $check = ' checked="checked" ';
 4708:             }
 4709:             $datatable .= '<td class="LC_left_item">'.
 4710:                           '<span class="LC_nobreak"><label>'.
 4711:                           '<input type="checkbox" name="errorexcluded" '.
 4712:                           'value="'.$ids[$i].'"'.$check.' />'.
 4713:                           $ids[$i].'</label></span></td>';
 4714:         }
 4715:         my $colsleft = $numinrow - @ids%($numinrow);
 4716:         if ($colsleft > 1 ) {
 4717:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4718:                           '&nbsp;</td>';
 4719:         } elsif ($colsleft == 1) {
 4720:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4721:         }
 4722:         $datatable .= '</tr></table></td></tr>';
 4723:         $rownum ++;
 4724:     } elsif ($position eq 'bottom') {
 4725:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4726:         my (@posstypes,%usertypeshash);
 4727:         if (ref($types) eq 'ARRAY') {
 4728:             @posstypes = @{$types};
 4729:         }
 4730:         if (@posstypes) {
 4731:             if (ref($usertypes) eq 'HASH') {
 4732:                 %usertypeshash = %{$usertypes};
 4733:             }
 4734:             my @overridden;
 4735:             my $numinrow = 4;
 4736:             if (ref($settings) eq 'HASH') {
 4737:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4738:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4739:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4740:                             push(@overridden,$key);
 4741:                             foreach my $item (@contacts) {
 4742:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4743:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4744:                                 }
 4745:                             }
 4746:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4747:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4748:                             $includeloc{'override_'.$key} = '';
 4749:                             $includestr{'override_'.$key} = '';
 4750:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4751:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4752:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4753:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4754:                             }
 4755:                         }
 4756:                     }
 4757:                 }
 4758:             }
 4759:             my $customclass = 'LC_helpdesk_override';
 4760:             my $optionsprefix = 'LC_options_helpdesk_';
 4761: 
 4762:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4763:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4764:                                          $numinrow,$othertitle,'overrides',
 4765:                                          \$rownum,$onclicktypes,$customclass);
 4766:             $rownum ++;
 4767:             $usertypeshash{'default'} = $othertitle;
 4768:             foreach my $status (@posstypes) {
 4769:                 my $css_class;
 4770:                 if ($rownum%2) {
 4771:                     $css_class = 'LC_odd_row ';
 4772:                 }
 4773:                 $css_class .= $customclass;
 4774:                 my $rowid = $optionsprefix.$status;
 4775:                 my $hidden = 1;
 4776:                 my $currstyle = 'display:none';
 4777:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4778:                     $currstyle = 'display:table-row';
 4779:                     $hidden = 0;
 4780:                 }
 4781:                 my $key = 'override_'.$status;
 4782:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4783:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4784:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4785:                                                   \@contacts,$short_titles);
 4786:                 unless ($hidden) {
 4787:                     $rownum ++;
 4788:                 }
 4789:             }
 4790:         }
 4791:     }
 4792:     $$rowtotal += $rownum;
 4793:     return $datatable;
 4794: }
 4795: 
 4796: sub core_link_msu {
 4797:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4798:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4799: }
 4800: 
 4801: sub overridden_helpdesk {
 4802:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4803:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4804:     my $class = 'LC_left_item';
 4805:     if ($css_class) {
 4806:         $css_class = ' class="'.$css_class.'"';
 4807:     }
 4808:     if ($rowid) {
 4809:         $rowid = ' id="'.$rowid.'"';
 4810:     }
 4811:     if ($rowstyle) {
 4812:         $rowstyle = ' style="'.$rowstyle.'"';
 4813:     }
 4814:     my ($output,$description);
 4815:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4816:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4817:               "<td>$description</td>\n".
 4818:               '<td class="'.$class.'" colspan="2">'.
 4819:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4820:               '<span class="LC_nobreak">';
 4821:     if (ref($contacts) eq 'ARRAY') {
 4822:         foreach my $item (@{$contacts}) {
 4823:             my $check;
 4824:             if (ref($checked) eq 'HASH') {
 4825:                $check = $checked->{$item};
 4826:             }
 4827:             my $title;
 4828:             if (ref($short_titles) eq 'HASH') {
 4829:                 $title = $short_titles->{$item};
 4830:             }
 4831:             $output .= '<label>'.
 4832:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4833:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4834:         }
 4835:     }
 4836:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4837:                '<input type="text" name="override_'.$type.'_others" '.
 4838:                'value="'.$otheremails.'"  />';
 4839:     my %locchecked;
 4840:     foreach my $loc ('s','b') {
 4841:         if ($includeloc eq $loc) {
 4842:             $locchecked{$loc} = ' checked="checked"';
 4843:             last;
 4844:         }
 4845:     }
 4846:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4847:                '<input type="text" name="override_'.$type.'_bcc" '.
 4848:                'value="'.$bccemails.'"  /></fieldset>'.
 4849:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4850:                &mt('Text automatically added to e-mail:').' '.
 4851:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4852:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4853:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4854:                ('&nbsp;'x2).
 4855:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4856:                '</span></fieldset>'.
 4857:                '</td></tr>'."\n";
 4858:     return $output;
 4859: }
 4860: 
 4861: sub contacts_javascript {
 4862:     return <<"ENDSCRIPT";
 4863: 
 4864: <script type="text/javascript">
 4865: // <![CDATA[
 4866: 
 4867: function screenshotSize(field) {
 4868:     if (document.getElementById('help_screenshotsize')) {
 4869:         if (field.value == 'no') {
 4870:             document.getElementById('help_screenshotsize').style.display="none";
 4871:         } else {
 4872:             document.getElementById('help_screenshotsize').style.display="";
 4873:         }
 4874:     }
 4875:     return;
 4876: }
 4877: 
 4878: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4879:     if (form.elements[checkbox].length != undefined) {
 4880:         var count = 0;
 4881:         if (docount) {
 4882:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4883:                 if (form.elements[checkbox][i].checked) {
 4884:                     count ++;
 4885:                 }
 4886:             }
 4887:         }
 4888:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4889:             var type = form.elements[checkbox][i].value;
 4890:             if (document.getElementById(prefix+type)) {
 4891:                 if (form.elements[checkbox][i].checked) {
 4892:                     document.getElementById(prefix+type).style.display = 'table-row';
 4893:                     if (count % 2 == 1) {
 4894:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4895:                     } else {
 4896:                         document.getElementById(prefix+type).className = target;
 4897:                     }
 4898:                     count ++;
 4899:                 } else {
 4900:                     document.getElementById(prefix+type).style.display = 'none';
 4901:                 }
 4902:             }
 4903:         }
 4904:     }
 4905:     return;
 4906: }
 4907: 
 4908: // ]]>
 4909: </script>
 4910: 
 4911: ENDSCRIPT
 4912: }
 4913: 
 4914: sub print_helpsettings {
 4915:     my ($position,$dom,$settings,$rowtotal) = @_;
 4916:     my $confname = $dom.'-domainconfig';
 4917:     my $formname = 'display';
 4918:     my ($datatable,$itemcount);
 4919:     if ($position eq 'top') {
 4920:         $itemcount = 1;
 4921:         my (%choices,%defaultchecked,@toggles);
 4922:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4923:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4924:                                      &mt('LON-CAPA bug tracker'),600,500));
 4925:         %defaultchecked = ('submitbugs' => 'on');
 4926:         @toggles = ('submitbugs');
 4927:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4928:                                                      \%choices,$itemcount);
 4929:         $$rowtotal ++;
 4930:     } else {
 4931:         my $css_class;
 4932:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4933:         my (%customroles,%ordered,%current);
 4934:         if (ref($settings) eq 'HASH') {
 4935:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4936:                 %current = %{$settings->{'adhoc'}};
 4937:             }
 4938:         }
 4939:         my $count = 0;
 4940:         foreach my $key (sort(keys(%existing))) {
 4941:             if ($key=~/^rolesdef\_(\w+)$/) {
 4942:                 my $rolename = $1;
 4943:                 my (%privs,$order);
 4944:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4945:                 $customroles{$rolename} = \%privs;
 4946:                 if (ref($current{$rolename}) eq 'HASH') {
 4947:                     $order = $current{$rolename}{'order'};
 4948:                 }
 4949:                 if ($order eq '') {
 4950:                     $order = $count;
 4951:                 }
 4952:                 $ordered{$order} = $rolename;
 4953:                 $count++;
 4954:             }
 4955:         }
 4956:         my $maxnum = scalar(keys(%ordered));
 4957:         my @roles_by_num = ();
 4958:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4959:             push(@roles_by_num,$item);
 4960:         }
 4961:         my $context = 'domprefs';
 4962:         my $crstype = 'Course';
 4963:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4964:         my @accesstypes = ('all','dh','da','none');
 4965:         my ($numstatustypes,@jsarray);
 4966:         if (ref($types) eq 'ARRAY') {
 4967:             if (@{$types} > 0) {
 4968:                 $numstatustypes = scalar(@{$types});
 4969:                 push(@accesstypes,'status');
 4970:                 @jsarray = ('bystatus');
 4971:             }
 4972:         }
 4973:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4974:         if (keys(%domhelpdesk)) {
 4975:             push(@accesstypes,('inc','exc'));
 4976:             push(@jsarray,('notinc','notexc'));
 4977:         }
 4978:         my $hiddenstr = join("','",@jsarray);
 4979:         my $context = 'domprefs';
 4980:         my $crstype = 'Course';
 4981:         my $prefix = 'helproles_';
 4982:         my $add_class = 'LC_hidden';
 4983:         foreach my $num (@roles_by_num) {
 4984:             my $role = $ordered{$num};
 4985:             my ($desc,$access,@statuses);
 4986:             if (ref($current{$role}) eq 'HASH') {
 4987:                 $desc = $current{$role}{'desc'};
 4988:                 $access = $current{$role}{'access'};
 4989:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4990:                     @statuses = @{$current{$role}{'insttypes'}};
 4991:                 }
 4992:             }
 4993:             if ($desc eq '') {
 4994:                 $desc = $role;
 4995:             }
 4996:             my $identifier = 'custhelp'.$num;
 4997:             my %full=();
 4998:             my %levels= (
 4999:                          course => {},
 5000:                          domain => {},
 5001:                          system => {},
 5002:                         );
 5003:             my %levelscurrent=(
 5004:                                course => {},
 5005:                                domain => {},
 5006:                                system => {},
 5007:                               );
 5008:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 5009:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5010:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5011:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 5012:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 5013:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 5014:             for (my $k=0; $k<=$maxnum; $k++) {
 5015:                 my $vpos = $k+1;
 5016:                 my $selstr;
 5017:                 if ($k == $num) {
 5018:                     $selstr = ' selected="selected" ';
 5019:                 }
 5020:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5021:             }
 5022:             $datatable .= '</select>'.('&nbsp;'x2).
 5023:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 5024:                           '</td>'.
 5025:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5026:                           &mt('Name shown to users:').
 5027:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 5028:                           '</fieldset>'.
 5029:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 5030:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 5031:                           '<fieldset>'.
 5032:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 5033:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 5034:                                                                    \%levelscurrent,$identifier,
 5035:                                                                    'LC_hidden',$prefix.$num.'_privs').
 5036:                           '</fieldset></td>';
 5037:             $itemcount ++;
 5038:         }
 5039:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5040:         my $newcust = 'custhelp'.$count;
 5041:         my (%privs,%levelscurrent);
 5042:         my %full=();
 5043:         my %levels= (
 5044:                      course => {},
 5045:                      domain => {},
 5046:                      system => {},
 5047:                     );
 5048:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 5049:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5050:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 5051:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 5052:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5053:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5054:         for (my $k=0; $k<$maxnum+1; $k++) {
 5055:             my $vpos = $k+1;
 5056:             my $selstr;
 5057:             if ($k == $maxnum) {
 5058:                 $selstr = ' selected="selected" ';
 5059:             }
 5060:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5061:         }
 5062:         $datatable .= '</select>&nbsp;'."\n".
 5063:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5064:                       '</label></span></td>'.
 5065:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5066:                       '<span class="LC_nobreak">'.
 5067:                       &mt('Internal name:').
 5068:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5069:                       '</span>'.('&nbsp;'x4).
 5070:                       '<span class="LC_nobreak">'.
 5071:                       &mt('Name shown to users:').
 5072:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5073:                       '</span></fieldset>'.
 5074:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5075:                                              $usertypes,$types,\%domhelpdesk).
 5076:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5077:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5078:                                                                 \@templateroles,$newcust).
 5079:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5080:                                                                \%levelscurrent,$newcust).
 5081:                       '</fieldset>'.
 5082:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5083:                       '</td></tr>';
 5084:         $count ++;
 5085:         $$rowtotal += $count;
 5086:     }
 5087:     return $datatable;
 5088: }
 5089: 
 5090: sub adhocbutton {
 5091:     my ($prefix,$num,$field,$visibility) = @_;
 5092:     my %lt = &Apache::lonlocal::texthash(
 5093:                                           show => 'Show details',
 5094:                                           hide => 'Hide details',
 5095:                                         );
 5096:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5097:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5098:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5099:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5100: }
 5101: 
 5102: sub helpsettings_javascript {
 5103:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5104:     return unless(ref($roles_by_num) eq 'ARRAY');
 5105:     my %html_js_lt = &Apache::lonlocal::texthash(
 5106:                                           show => 'Show details',
 5107:                                           hide => 'Hide details',
 5108:                                         );
 5109:     &html_escape(\%html_js_lt);
 5110:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5111:     return <<"ENDSCRIPT";
 5112: <script type="text/javascript">
 5113: // <![CDATA[
 5114: 
 5115: function reorderHelpRoles(form,item) {
 5116:     var changedVal;
 5117: $jstext
 5118:     var newpos = 'helproles_${total}_pos';
 5119:     var maxh = 1 + $total;
 5120:     var current = new Array();
 5121:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5122:     if (item == newpos) {
 5123:         changedVal = newitemVal;
 5124:     } else {
 5125:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5126:         current[newitemVal] = newpos;
 5127:     }
 5128:     for (var i=0; i<helproles.length; i++) {
 5129:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5130:         if (elementName != item) {
 5131:             if (form.elements[elementName]) {
 5132:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5133:                 current[currVal] = elementName;
 5134:             }
 5135:         }
 5136:     }
 5137:     var oldVal;
 5138:     for (var j=0; j<maxh; j++) {
 5139:         if (current[j] == undefined) {
 5140:             oldVal = j;
 5141:         }
 5142:     }
 5143:     if (oldVal < changedVal) {
 5144:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5145:            var elementName = current[k];
 5146:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5147:         }
 5148:     } else {
 5149:         for (var k=changedVal; k<oldVal; k++) {
 5150:             var elementName = current[k];
 5151:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5152:         }
 5153:     }
 5154:     return;
 5155: }
 5156: 
 5157: function helpdeskAccess(num) {
 5158:     var curraccess = null;
 5159:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5160:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5161:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5162:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5163:             }
 5164:         }
 5165:     }
 5166:     var shown = Array();
 5167:     var hidden = Array();
 5168:     if (curraccess == 'none') {
 5169:         hidden = Array('$hiddenstr');
 5170:     } else {
 5171:         if (curraccess == 'status') {
 5172:             shown = Array('bystatus');
 5173:             hidden = Array('notinc','notexc');
 5174:         } else {
 5175:             if (curraccess == 'exc') {
 5176:                 shown = Array('notexc');
 5177:                 hidden = Array('notinc','bystatus');
 5178:             }
 5179:             if (curraccess == 'inc') {
 5180:                 shown = Array('notinc');
 5181:                 hidden = Array('notexc','bystatus');
 5182:             }
 5183:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5184:                 hidden = Array('notinc','notexc','bystatus');
 5185:             }
 5186:         }
 5187:     }
 5188:     if (hidden.length > 0) {
 5189:         for (var i=0; i<hidden.length; i++) {
 5190:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5191:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5192:             }
 5193:         }
 5194:     }
 5195:     if (shown.length > 0) {
 5196:         for (var i=0; i<shown.length; i++) {
 5197:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5198:                 if (shown[i] == 'privs') {
 5199:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5200:                 } else {
 5201:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5202:                 }
 5203:             }
 5204:         }
 5205:     }
 5206:     return;
 5207: }
 5208: 
 5209: function toggleHelpdeskItem(num,field) {
 5210:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5211:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5212:             document.getElementById('helproles_'+num+'_'+field).className =
 5213:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5214:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5215:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5216:             }
 5217:         } else {
 5218:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5219:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5220:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5221:             }
 5222:         }
 5223:     }
 5224:     return;
 5225: }
 5226: 
 5227: // ]]>
 5228: </script>
 5229: 
 5230: ENDSCRIPT
 5231: }
 5232: 
 5233: sub helpdeskroles_access {
 5234:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5235:         $usertypes,$types,$domhelpdesk) = @_;
 5236:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5237:     my %lt = &Apache::lonlocal::texthash(
 5238:                     'rou'    => 'Role usage',
 5239:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5240:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5241:                     'dh'     => 'All with domain helpdesk role',
 5242:                     'da'     => 'All with domain helpdesk assistant role',
 5243:                     'none'   => 'None',
 5244:                     'status' => 'Determined based on institutional status',
 5245:                     'inc'    => 'Include all, but exclude specific personnel',
 5246:                     'exc'    => 'Exclude all, but include specific personnel',
 5247:                   );
 5248:     my %usecheck = (
 5249:                      all => ' checked="checked"',
 5250:                    );
 5251:     my %displaydiv = (
 5252:                       status => 'none',
 5253:                       inc    => 'none',
 5254:                       exc    => 'none',
 5255:                       priv   => 'block',
 5256:                      );
 5257:     my $output;
 5258:     if (ref($current) eq 'HASH') {
 5259:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5260:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5261:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5262:                 delete($usecheck{'all'});
 5263:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5264:                     my $access = $1;
 5265:                     $displaydiv{$access} = 'inline';
 5266:                 } elsif ($current->{access} eq 'none') {
 5267:                     $displaydiv{'priv'} = 'none';
 5268:                 }
 5269:             }
 5270:         }
 5271:     }
 5272:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5273:               '<p>'.$lt{'whi'}.'</p>';
 5274:     foreach my $access (@{$accesstypes}) {
 5275:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5276:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5277:                    $lt{$access}.'</label>';
 5278:         if ($access eq 'status') {
 5279:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5280:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5281:                                                                  $othertitle,$usertypes,$types).
 5282:                        '</div>';
 5283:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5284:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5285:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5286:                        '</div>';
 5287:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5288:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5289:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5290:                        '</div>';
 5291:         }
 5292:         $output .= '</p>';
 5293:     }
 5294:     $output .= '</fieldset>';
 5295:     return $output;
 5296: }
 5297: 
 5298: sub radiobutton_prefs {
 5299:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5300:         $additional,$align,$firstval) = @_;
 5301:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5302:                    (ref($choices) eq 'HASH'));
 5303: 
 5304:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5305: 
 5306:     foreach my $item (@{$toggles}) {
 5307:         if ($defaultchecked->{$item} eq 'on') {
 5308:             $checkedon{$item} = ' checked="checked" ';
 5309:             $checkedoff{$item} = ' ';
 5310:         } elsif ($defaultchecked->{$item} eq 'off') {
 5311:             $checkedoff{$item} = ' checked="checked" ';
 5312:             $checkedon{$item} = ' ';
 5313:         }
 5314:     }
 5315:     if (ref($settings) eq 'HASH') {
 5316:         foreach my $item (@{$toggles}) {
 5317:             if ($settings->{$item} eq '1') {
 5318:                 $checkedon{$item} =  ' checked="checked" ';
 5319:                 $checkedoff{$item} = ' ';
 5320:             } elsif ($settings->{$item} eq '0') {
 5321:                 $checkedoff{$item} =  ' checked="checked" ';
 5322:                 $checkedon{$item} = ' ';
 5323:             }
 5324:         }
 5325:     }
 5326:     if ($onclick) {
 5327:         $onclick = ' onclick="'.$onclick.'"';
 5328:     }
 5329:     foreach my $item (@{$toggles}) {
 5330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5331:         $datatable .=
 5332:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5333:             '<span class="LC_nobreak">'.$choices->{$item}.
 5334:             '</span></td>';
 5335:         if ($align eq 'left') {
 5336:             $datatable .= '<td class="LC_left_item">';
 5337:         } else {
 5338:             $datatable .= '<td class="LC_right_item">';
 5339:         }
 5340:         $datatable .= '<span class="LC_nobreak">';
 5341:         if ($firstval eq 'no') {
 5342:             $datatable .=
 5343:                 '<label><input type="radio" name="'.
 5344:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5345:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5346:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5347:         } else {
 5348:             $datatable .=
 5349:                 '<label><input type="radio" name="'.
 5350:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5351:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5352:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5353:         }
 5354:         $datatable .= '</span>'.$additional.'</td></tr>';
 5355:         $itemcount ++;
 5356:     }
 5357:     return ($datatable,$itemcount);
 5358: }
 5359: 
 5360: sub print_ltitools {
 5361:     my ($position,$dom,$settings,$rowtotal) = @_;
 5362:     my (%rules,%encrypt,%privkeys,%linkprot);
 5363:     if (ref($settings) eq 'HASH') {
 5364:         if ($position eq 'top') {
 5365:             if (exists($settings->{'encrypt'})) {
 5366:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5367:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5368:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5369:                     }
 5370:                 }
 5371:             }
 5372:             if (exists($settings->{'private'})) {
 5373:                 if (ref($settings->{'private'}) eq 'HASH') {
 5374:                     if (ref($settings->{'private'}) eq 'HASH') {
 5375:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5376:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5377:                         }
 5378:                     }
 5379:                 }
 5380:             }
 5381:         } elsif ($position eq 'middle') {
 5382:             if (exists($settings->{'rules'})) {
 5383:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5384:                     %rules = %{$settings->{'rules'}};
 5385:                 }
 5386:             }
 5387:         } else {
 5388:             foreach my $key ('encrypt','private','rules') {
 5389:                 if (exists($settings->{$key})) {
 5390:                     delete($settings->{$key});
 5391:                 }
 5392:             }
 5393:         }
 5394:     }
 5395:     my $datatable;
 5396:     my $itemcount = 1;
 5397:     if ($position eq 'top') {
 5398:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5399:     } elsif ($position eq 'middle') {
 5400:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5401:         $$rowtotal += $itemcount;
 5402:     } else {
 5403:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5404:     }
 5405:     return $datatable;
 5406: }
 5407: 
 5408: sub ltitools_names {
 5409:     my %lt = &Apache::lonlocal::texthash(
 5410:                                           'title'          => 'Title',
 5411:                                           'version'        => 'Version',
 5412:                                           'msgtype'        => 'Message Type',
 5413:                                           'sigmethod'      => 'Signature Method',
 5414:                                           'url'            => 'URL',
 5415:                                           'key'            => 'Key',
 5416:                                           'lifetime'       => 'Nonce lifetime (s)',
 5417:                                           'secret'         => 'Secret',
 5418:                                           'icon'           => 'Icon',
 5419:                                           'user'           => 'User',
 5420:                                           'fullname'       => 'Full Name',
 5421:                                           'firstname'      => 'First Name',
 5422:                                           'lastname'       => 'Last Name',
 5423:                                           'email'          => 'E-mail',
 5424:                                           'roles'          => 'Role',
 5425:                                           'window'         => 'Window',
 5426:                                           'tab'            => 'Tab',
 5427:                                           'iframe'         => 'iFrame',
 5428:                                           'height'         => 'Height',
 5429:                                           'width'          => 'Width',
 5430:                                           'linktext'       => 'Default Link Text',
 5431:                                           'explanation'    => 'Default Explanation',
 5432:                                           'passback'       => 'Tool can return grades:',
 5433:                                           'roster'         => 'Tool can retrieve roster:',
 5434:                                           'crstarget'      => 'Display target',
 5435:                                           'crslabel'       => 'Course label',
 5436:                                           'crstitle'       => 'Course title',
 5437:                                           'crslinktext'    => 'Link Text',
 5438:                                           'crsexplanation' => 'Explanation',
 5439:                                           'crsappend'      => 'Provider URL',
 5440:                                         );
 5441:     return %lt;
 5442: }
 5443: 
 5444: sub secrets_form {
 5445:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5446:     my @ids=&Apache::lonnet::current_machine_ids();
 5447:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5448:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5449:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5450:     $itemcount = 0;
 5451:     foreach my $hostid (sort(keys(%servers))) {
 5452:         my ($showextra,$divsty,$switch);
 5453:         if ($hostid eq $primary) {
 5454:             if ($context eq 'ltisec') {
 5455:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5456:                     $showextra = 1;
 5457:                 }
 5458:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5459:                     $showextra = 1;
 5460:                 }
 5461:             } else {
 5462:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5463:                     $showextra = 1;
 5464:                 }
 5465:             }
 5466:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5467:                 $switch = 1;
 5468:             }
 5469:             if ($showextra) {
 5470:                 $numshown ++;
 5471:                 $divsty = 'display:inline-block';
 5472:             } else {
 5473:                 $divsty = 'display:none';
 5474:             }
 5475:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5476:                       '<legend>'.$hostid.'</legend>';
 5477:             if ($switch) {
 5478:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5479:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5480:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5481:                 if (exists($privkeys->{$hostid})) {
 5482:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5483:                               '<span class="LC_nobreak">'.
 5484:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5485:                               '<span class="LC_nobreak">'.&mt('Change?').
 5486:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5487:                               ('&nbsp;'x2).
 5488:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5489:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5490:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5491:                               '</span></div>';
 5492:                 } else {
 5493:                     $extra .= '<span class="LC_nobreak">'.
 5494:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5495:                               '</span>'."\n";
 5496:                 }
 5497:             } elsif (exists($privkeys->{$hostid})) {
 5498:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5499:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5500:                           '<span class="LC_nobreak">'.&mt('Change?').
 5501:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5502:                           ('&nbsp;'x2).
 5503:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5504:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5505:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5506:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5507:                           '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.'.$context.'_privkey_'.$hostid.'.type='."'text'".' } else { this.form.'.$context.'_privkey_'.$hostid.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 5508:                           '</span></div>';
 5509:             } else {
 5510:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5511:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5512:                           '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.'.$context.'_privkey_'.$hostid.'.type='."'text'".' } else { this.form.'.$context.'_privkey_'.$hostid.'.type='."'password'".' }" />'.&mt('Visible input').'</label>';
 5513:             }
 5514:             $extra .= '</fieldset>';
 5515:         }
 5516:     }
 5517:     my (%choices,@toggles,%defaultchecked);
 5518:     if ($context eq 'ltisec') {
 5519:         %choices = &Apache::lonlocal::texthash (
 5520:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5521:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5522:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5523:                                                );
 5524:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5525:         %defaultchecked = (
 5526:                            'ltisec_crslinkprot' => 'off',
 5527:                            'ltisec_domlinkprot' => 'off',
 5528:                            'ltisec_consumers'   => 'off',
 5529:                           );
 5530:     } else {
 5531:         %choices = &Apache::lonlocal::texthash (
 5532:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5533:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5534:                                                );
 5535:         @toggles = qw(toolsec_crs toolsec_dom);
 5536:         %defaultchecked = (
 5537:                            'toolsec_crs' => 'off',
 5538:                            'toolsec_dom' => 'off',
 5539:                           );
 5540:     }
 5541:     my ($onclick,$itemcount);
 5542:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5543:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5544:                                               \%choices,$itemcount,$onclick,'','left','no');
 5545: 
 5546:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5547:     my $noprivkeysty = 'display:inline-block';
 5548:     if ($numshown) {
 5549:         $noprivkeysty = 'display:none';
 5550:     }
 5551:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5552:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5553:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5554:                $extra.
 5555:                '</td></tr>';
 5556:     $itemcount ++;
 5557:     $$rowtotal += $itemcount;
 5558:     return $output;
 5559: }
 5560: 
 5561: sub print_proctoring {
 5562:     my ($dom,$settings,$rowtotal) = @_;
 5563:     my $itemcount = 1;
 5564:     my (%ordered,%providernames,%current,%currentdef);
 5565:     my $confname = $dom.'-domainconfig';
 5566:     my $switchserver = &check_switchserver($dom,$confname);
 5567:     if (ref($settings) eq 'HASH') {
 5568:         foreach my $item (keys(%{$settings})) {
 5569:             if (ref($settings->{$item}) eq 'HASH') {
 5570:                 my $num = $settings->{$item}{'order'};
 5571:                 $ordered{$num} = $item;
 5572:             }
 5573:         }
 5574:     } else {
 5575:         %ordered = (
 5576:                      1 => 'proctorio',
 5577:                      2 => 'examity',
 5578:                    );
 5579:     }
 5580:     %providernames = &proctoring_providernames();
 5581:     my $maxnum = scalar(keys(%ordered));
 5582:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5583:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5584:     if (ref($requref) eq 'HASH') {
 5585:         %requserfields = %{$requref};
 5586:     }
 5587:     if (ref($opturef) eq 'HASH') {
 5588:         %optuserfields = %{$opturef};
 5589:     }
 5590:     if (ref($defref) eq 'HASH') {
 5591:         %defaults = %{$defref};
 5592:     }
 5593:     if (ref($extref) eq 'HASH') {
 5594:         %extended = %{$extref};
 5595:     }
 5596:     if (ref($crsref) eq 'HASH') {
 5597:         %crsconf = %{$crsref};
 5598:     }
 5599:     if (ref($rolesref) eq 'ARRAY') {
 5600:         @courseroles = @{$rolesref};
 5601:     }
 5602:     if (ref($ltiref) eq 'ARRAY') {
 5603:         @ltiroles = @{$ltiref};
 5604:     }
 5605:     my $datatable;
 5606:     my $css_class;
 5607:     if (keys(%ordered)) {
 5608:         my @items = sort { $a <=> $b } keys(%ordered);
 5609:         for (my $i=0; $i<@items; $i++) {
 5610:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5611:             my $provider = $ordered{$items[$i]};
 5612:             my $optionsty = 'none';
 5613:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5614:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5615:             if (ref($settings) eq 'HASH') {
 5616:                 if (ref($settings->{$provider}) eq 'HASH') {
 5617:                     %current = %{$settings->{$provider}};
 5618:                     if ($current{'available'}) {
 5619:                         $optionsty = 'block';
 5620:                         $available = 1;
 5621:                     }
 5622:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5623:                         $lifetime = $current{'lifetime'};
 5624:                     }
 5625:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5626:                         $version = $current{'version'};
 5627:                     }
 5628:                     if ($current{'image'} ne '') {
 5629:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5630:                     }
 5631:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5632:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5633:                     }
 5634:                     $userincdom = $current{'incdom'};
 5635:                     if (ref($current{'roles'}) eq 'HASH') {
 5636:                         %rolemaps = %{$current{'roles'}};
 5637:                         $checkedfields{'roles'} = 1;
 5638:                     }
 5639:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5640:                         foreach my $val (@{$current{'defaults'}}) {
 5641:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5642:                                 $inuse{$val} = 1;
 5643:                             } else {
 5644:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5645:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5646:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5647:                                             $inuse{$poss} = $val;
 5648:                                             last;
 5649:                                         }
 5650:                                     }
 5651:                                 }
 5652:                             }
 5653:                         }
 5654:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5655:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5656:                             my $currval = $current{'defaults'}{$key}; 
 5657:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5658:                                 $inuse{$key} = 1;
 5659:                             } else {
 5660:                                 my $match;
 5661:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5662:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5663:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5664:                                             $inuse{$poss} = $key;
 5665:                                             last;
 5666:                                         }
 5667:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5668:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5669:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5670:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5671:                                                     $currentdef{$inner} = $currval;
 5672:                                                     $match = 1;
 5673:                                                     last;
 5674:                                                 }
 5675:                                             } elsif ($inner eq $key) {
 5676:                                                 $currentdef{$key} = $currval;
 5677:                                                 $match = 1;
 5678:                                                 last;
 5679:                                             }
 5680:                                         }
 5681:                                     }
 5682:                                     last if ($match);
 5683:                                 }
 5684:                             }
 5685:                         }
 5686:                     }
 5687:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5688:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5689:                     }
 5690:                 }
 5691:             }
 5692:             my %lt = &proctoring_titles($provider);
 5693:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5694:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5695:             my %checkedavailable = (
 5696:                                    yes => '',
 5697:                                    no  => ' checked="checked"',
 5698:                                  );
 5699:             if ($available) {
 5700:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5701:                 $checkedavailable{'no'} = '';
 5702:             }
 5703:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5704:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5705:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5706:             for (my $k=0; $k<$maxnum; $k++) {
 5707:                 my $vpos = $k+1;
 5708:                 my $selstr;
 5709:                 if ($k == $i) {
 5710:                     $selstr = ' selected="selected" ';
 5711:                 }
 5712:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5713:             }
 5714:             if ($version eq '') {
 5715:                 if ($provider eq 'proctorio') {
 5716:                     $version = '1.0';
 5717:                 } elsif ($provider eq 'examity') {
 5718:                     $version = '1.1';
 5719:                 }
 5720:             }
 5721:             if ($lifetime eq '') {
 5722:                 $lifetime = '300';
 5723:             }
 5724:             $datatable .=
 5725:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5726:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5727:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5728:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5729:                 '</td>'.
 5730:                 '<td colspan="2">'.
 5731:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5732:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5733:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5734:                 ('&nbsp;'x2).
 5735:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5736:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5737:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5738:                 ('&nbsp;'x2).
 5739:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5740:                 '<br />'.
 5741:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5742:                 '<br />'.
 5743:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5744:                 ('&nbsp;'x2).
 5745:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5746:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.proctoring_'.$provider.'_secret.type='."'text'".' } else { this.form.proctoring_'.$provider.'_secret.type='."'password'".' }" />'.$lt{'visible'}.'</label></span><br />'."\n";
 5747:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5748:             if ($imgsrc) {
 5749:                 $datatable .= $imgsrc.
 5750:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5751:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5752:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5753:             }
 5754:             $datatable .= '&nbsp;';
 5755:             if ($switchserver) {
 5756:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5757:             } else {
 5758:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5759:             }
 5760:             unless ($imgsrc) {
 5761:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5762:             }
 5763:             $datatable .= '</fieldset>'."\n";
 5764:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5765:                 if (@{$requserfields{$provider}} > 0) {
 5766:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5767:                     foreach my $field (@{$requserfields{$provider}}) {
 5768:                         $datatable .= '<span class="LC_nobreak">'.
 5769:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5770:                                       $lt{$field}.'</label>';
 5771:                         if ($field eq 'user') {
 5772:                             my $seluserdom = '';
 5773:                             my $unseluserdom = ' selected="selected"';
 5774:                             if ($userincdom) {
 5775:                                 $seluserdom = $unseluserdom;
 5776:                                 $unseluserdom = '';
 5777:                             }
 5778:                             $datatable .= ':&nbsp;'.
 5779:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5780:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5781:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5782:                                 '</select>&nbsp;';
 5783:                         } else {
 5784:                             $datatable .= '&nbsp;';
 5785:                             if ($field eq 'roles') {
 5786:                                 $showroles = 1; 
 5787:                             } 
 5788:                         }
 5789:                         $datatable .= '</span> ';
 5790:                     }
 5791:                 }
 5792:                 $datatable .= '</fieldset>'."\n";
 5793:             }
 5794:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5795:                 if (@{$optuserfields{$provider}} > 0) {
 5796:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5797:                     foreach my $field (@{$optuserfields{$provider}}) {
 5798:                         my $checked;
 5799:                         if ($checkedfields{$field}) {
 5800:                             $checked = ' checked="checked"';
 5801:                         }
 5802:                         $datatable .= '<span class="LC_nobreak">'.
 5803:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5804:                     }
 5805:                     $datatable .= '</fieldset>'."\n";
 5806:                 }
 5807:             }
 5808:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5809:                 if (@{$defaults{$provider}}) {
 5810:                     my (%options,@selectboxes);
 5811:                     if (ref($extended{$provider}) eq 'HASH') {
 5812:                         %options = %{$extended{$provider}};
 5813:                     }
 5814:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5815:                     my ($rem,$numinrow,$dropdowns);
 5816:                     if ($provider eq 'proctorio') { 
 5817:                         $datatable .= '<table>';
 5818:                         $numinrow = 4;
 5819:                     }
 5820:                     my $i = 0;
 5821:                     foreach my $field (@{$defaults{$provider}}) {
 5822:                         my $checked;
 5823:                         if ($inuse{$field}) {
 5824:                             $checked = ' checked="checked"';
 5825:                         }
 5826:                         if ($provider eq 'examity') {
 5827:                             if ($field eq 'display') {
 5828:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5829:                                 foreach my $option ('iframe','tab','window') {
 5830:                                     my $checkdisp;
 5831:                                     if ($currentdef{'target'} eq $option) {
 5832:                                         $checkdisp = ' checked="checked"';
 5833:                                     }
 5834:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5835:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5836:                                 }
 5837:                                 $datatable .= ('&nbsp;'x4);
 5838:                                 foreach my $dimen ('width','height') {
 5839:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5840:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5841:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5842:                                                   ('&nbsp;'x2);
 5843:                                 }
 5844:                                 $datatable .= '</span><br />'.
 5845:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5846:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5847:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5848:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5849:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5850:                                               $currentdef{'explanation'}.
 5851:                                               '</textarea></div><div style=""></div><br />';
 5852:                             }
 5853:                         } else {
 5854:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5855:                                 my ($output,$selnone);
 5856:                                 unless ($checked) {
 5857:                                     $selnone = ' selected="selected"';
 5858:                                 }
 5859:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5860:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5861:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5862:                                 foreach my $option (@{$options{$field}}) {
 5863:                                     my $sel; 
 5864:                                     if ($inuse{$field} eq $option) {
 5865:                                         $sel = ' selected="selected"';
 5866:                                     }
 5867:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5868:                                 }
 5869:                                 $output .= '</select></span>';
 5870:                                 push(@selectboxes,$output);
 5871:                             } else {
 5872:                                 $rem = $i%($numinrow);
 5873:                                 if ($rem == 0) {
 5874:                                     if ($i > 0) {
 5875:                                         $datatable .= '</tr>';
 5876:                                     }
 5877:                                     $datatable .= '<tr>';
 5878:                                 }
 5879:                                 $datatable .= '<td class="LC_left_item">'.
 5880:                                               '<span class="LC_nobreak">'.
 5881:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5882:                                               $fieldtitles{$field}.'</label></span></td>';
 5883:                                 $i++;
 5884:                             }
 5885:                         }
 5886:                     }
 5887:                     if ($provider eq 'proctorio') {
 5888:                         if ($numinrow) {
 5889:                             $rem = $i%$numinrow;
 5890:                         }
 5891:                         my $colsleft = $numinrow - $rem;
 5892:                         if ($colsleft > 1) {
 5893:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5894:                         } else {
 5895:                             $datatable .= '<td class="LC_left_item">';
 5896:                         }
 5897:                         $datatable .= '&nbsp;'.
 5898:                                       '</td></tr></table>';
 5899:                         if (@selectboxes) {
 5900:                             $datatable .= '<hr /><table>';
 5901:                             $numinrow = 2;
 5902:                             for (my $i=0; $i<@selectboxes; $i++) {
 5903:                                 $rem = $i%($numinrow);
 5904:                                 if ($rem == 0) {
 5905:                                     if ($i > 0) {
 5906:                                         $datatable .= '</tr>';
 5907:                                     }
 5908:                                     $datatable .= '<tr>';
 5909:                                 }
 5910:                                 $datatable .= '<td class="LC_left_item">'.
 5911:                                               $selectboxes[$i].'</td>';
 5912:                             }
 5913:                             if ($numinrow) {
 5914:                                 $rem = $i%$numinrow;
 5915:                             }
 5916:                             $colsleft = $numinrow - $rem;
 5917:                             if ($colsleft > 1) {
 5918:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5919:                             } else {
 5920:                                 $datatable .= '<td class="LC_left_item">';
 5921:                             }
 5922:                             $datatable .= '&nbsp;'.
 5923:                                           '</td></tr></table>';
 5924:                         }
 5925:                     }
 5926:                     $datatable .= '</fieldset>';
 5927:                 }
 5928:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5929:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5930:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5931:                     my ($rem,$numinrow);
 5932:                     if ($provider eq 'proctorio') {
 5933:                         $datatable .= '<table>';
 5934:                         $numinrow = 4;
 5935:                     }
 5936:                     my $i = 0;
 5937:                     foreach my $item (@{$crsconf{$provider}}) {
 5938:                         my $name;
 5939:                         if ($provider eq 'examity') {
 5940:                             $name = $lt{'crs'.$item};
 5941:                         } elsif ($provider eq 'proctorio') {
 5942:                             $name = $fieldtitles{$item};
 5943:                             $rem = $i%($numinrow);
 5944:                             if ($rem == 0) {
 5945:                                 if ($i > 0) {
 5946:                                     $datatable .= '</tr>';
 5947:                                 }
 5948:                                 $datatable .= '<tr>';
 5949:                             }
 5950:                             $datatable .= '<td class="LC_left_item>';
 5951:                         }
 5952:                         my $checked;
 5953:                         if ($crsconfig{$item}) {
 5954:                             $checked = ' checked="checked"';
 5955:                         }
 5956:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5957:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5958:                                       $name.'</label></span>';
 5959:                         if ($provider eq 'examity') {
 5960:                             $datatable .= '&nbsp; ';
 5961:                         }
 5962:                         $datatable .= "\n";
 5963:                         $i++;
 5964:                     }
 5965:                     if ($provider eq 'proctorio') {
 5966:                         if ($numinrow) {
 5967:                             $rem = $i%$numinrow;
 5968:                         }
 5969:                         my $colsleft = $numinrow - $rem;
 5970:                         if ($colsleft > 1) {
 5971:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5972:                         } else {
 5973:                             $datatable .= '<td class="LC_left_item">';
 5974:                         }
 5975:                         $datatable .= '&nbsp;'.
 5976:                                       '</td></tr></table>';
 5977:                     }
 5978:                     $datatable .= '</fieldset>';
 5979:                 }
 5980:                 if ($showroles) {
 5981:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5982:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5983:                     foreach my $role (@courseroles) {
 5984:                         my ($selected,$selectnone);
 5985:                         if (!$rolemaps{$role}) {
 5986:                             $selectnone = ' selected="selected"';
 5987:                         }
 5988:                         $datatable .= '<td style="text-align: center">'.
 5989:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5990:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5991:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5992:                         foreach my $ltirole (@ltiroles) {
 5993:                             unless ($selectnone) {
 5994:                                 if ($rolemaps{$role} eq $ltirole) {
 5995:                                     $selected = ' selected="selected"';
 5996:                                 } else {
 5997:                                     $selected = '';
 5998:                                 }
 5999:                             }
 6000:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 6001:                         }
 6002:                         $datatable .= '</select></td>';
 6003:                     }
 6004:                     $datatable .= '</tr></table></fieldset>'.
 6005:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6006:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 6007:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 6008:                                   '<tr><td></td><td>lms</td>'.
 6009:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 6010:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 6011:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 6012:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 6013:                         my %custom = %{$settings->{$provider}->{'custom'}};
 6014:                         if (keys(%custom) > 0) {
 6015:                             foreach my $key (sort(keys(%custom))) {
 6016:                                 next if ($key eq 'lms');
 6017:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 6018:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 6019:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6020:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6021:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6022:                             }
 6023:                         }
 6024:                     }
 6025:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6026:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6027:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6028:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6029:                                   '</table></fieldset></td></tr>'."\n";
 6030:                 }
 6031:                 $datatable .= '</td></tr>';
 6032:             }
 6033:             $itemcount ++;
 6034:         }
 6035:     }
 6036:     return $datatable;
 6037: }
 6038: 
 6039: sub proctoring_data {
 6040:     my $requserfields = {
 6041:                       proctorio => ['user'],
 6042:                       examity   => ['roles','user'],
 6043:                      };
 6044:     my $optuserfields = {
 6045:                         proctorio => ['fullname'],
 6046:                         examity   => ['fullname','firstname','lastname','email'],
 6047:                      };
 6048:     my $defaults = {
 6049:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6050:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6051:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6052:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6053:                                 'reentry','calculator','whiteboard'],
 6054:                   examity   => ['display'],
 6055:                 };
 6056:     my $extended = { 
 6057:                   proctorio => {
 6058:                                  verifyid => ['verifyidauto','verifyidlive'],
 6059:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6060:                                  tabslinks => ['notabs','linksonly'],
 6061:                                  reentry => ['noreentry','agentreentry'],
 6062:                                  calculator => ['calculatorbasic','calculatorsci'],
 6063:                                },
 6064:                   examity => {
 6065:                                display => {
 6066:                                            target      => ['iframe','tab','window'],
 6067:                                            width       => '',
 6068:                                            height      => '',
 6069:                                            linktext    => '',
 6070:                                            explanation => '',
 6071:                                           },
 6072:                              },
 6073:                 };
 6074:     my $crsconf = {
 6075:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6076:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6077:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6078:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6079:                                'reentry','calculator','whiteboard'],
 6080:                  examity => ['label','title','target','linktext','explanation','append'],
 6081:                };
 6082:     my $courseroles = ['cc','in','ta','ep','st'];
 6083:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6084:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6085: }
 6086: 
 6087: sub proctoring_titles {
 6088:     my ($item) = @_;
 6089:     my (%common_lt,%custom_lt);
 6090:     %common_lt = &Apache::lonlocal::texthash (
 6091:                      'avai'      => 'Available?',
 6092:                      'base'      => 'Basic Settings',
 6093:                      'requ'      => 'User data required to be sent on launch',
 6094:                      'optu'      => 'User data optionally sent on launch',
 6095:                      'udsl'      => 'User data sent on launch',
 6096:                      'defa'      => 'Defaults for items configurable in course',
 6097:                      'sigmethod' => 'Signature Method',
 6098:                      'key'       => 'Key',
 6099:                      'lifetime'  => 'Nonce lifetime (s)',
 6100:                      'secret'    => 'Secret',
 6101:                      'icon'      => 'Icon',
 6102:                      'fullname'  => 'Full Name',
 6103:                      'visible'   => 'Visible input',
 6104:                      'username'  => 'username',
 6105:                      'user'      => 'User',
 6106:                  );
 6107:     if ($item eq 'proctorio') {
 6108:         %custom_lt = &Apache::lonlocal::texthash (
 6109:                          'version'        => 'OAuth version',
 6110:                          'url'            => 'API URL',
 6111:                          'uname:dom'      => 'username-domain',
 6112:         );
 6113:     } elsif ($item eq 'examity') {
 6114:         %custom_lt = &Apache::lonlocal::texthash (
 6115:                          'version'        => 'LTI Version',
 6116:                          'url'            => 'URL',
 6117:                          'uname:dom'      => 'username:domain',
 6118:                          'msgtype'        => 'Message Type',
 6119:                          'firstname'      => 'First Name',
 6120:                          'lastname'       => 'Last Name',
 6121:                          'email'          => 'E-mail',
 6122:                          'roles'          => 'Role',
 6123:                          'crstarget'      => 'Display target',
 6124:                          'crslabel'       => 'Course label',
 6125:                          'crstitle'       => 'Course title', 
 6126:                          'crslinktext'    => 'Link Text',
 6127:                          'crsexplanation' => 'Explanation',
 6128:                          'crsappend'      => 'Provider URL',
 6129:         );
 6130:     }
 6131:     my %lt = (%common_lt,%custom_lt);
 6132:     return %lt;
 6133: }
 6134: 
 6135: sub proctoring_fieldtitles {
 6136:     my ($item) = @_;
 6137:     if ($item eq 'proctorio') {
 6138:         return &Apache::lonlocal::texthash (
 6139:                   'recordvideo' => 'Record video',
 6140:                   'recordaudio' => 'Record audio',
 6141:                   'recordscreen' => 'Record screen',
 6142:                   'recordwebtraffic' => 'Record web traffic',
 6143:                   'recordroomstart' => 'Record room scan',
 6144:                   'verifyvideo' => 'Verify webcam',
 6145:                   'verifyaudio' => 'Verify microphone',
 6146:                   'verifydesktop' => 'Verify desktop recording',
 6147:                   'verifyid' => 'Photo ID verification',
 6148:                   'verifysignature' => 'Require signature',
 6149:                   'fullscreen' => 'Fullscreen',
 6150:                   'clipboard' => 'Disable copy/paste',
 6151:                   'tabslinks' => 'New tabs/windows',
 6152:                   'closetabs' => 'Close other tabs',
 6153:                   'onescreen' => 'Limit to single screen',
 6154:                   'print' => 'Disable Printing',
 6155:                   'downloads' => 'Disable Downloads',
 6156:                   'cache' => 'Empty cache after exam',
 6157:                   'rightclick' => 'Disable right click',
 6158:                   'reentry' => 'Re-entry to exam',
 6159:                   'calculator' => 'Onscreen calculator',
 6160:                   'whiteboard' => 'Onscreen whiteboard',
 6161:                   'verifyidauto' => 'Automated verification',
 6162:                   'verifyidlive' => 'Live agent verification',
 6163:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6164:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6165:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6166:                   'notabs' => 'Disaallowed',
 6167:                   'linksonly' => 'Allowed from links in exam',
 6168:                   'noreentry' => 'Disallowed',
 6169:                   'agentreentry' => 'Agent required for re-entry',
 6170:                   'calculatorbasic' => 'Basic',
 6171:                   'calculatorsci' => 'Scientific',
 6172:         );
 6173:     } elsif ($item eq 'examity') {
 6174:         return &Apache::lonlocal::texthash (
 6175:                 'target'         => 'Display target',   
 6176:                 'window'         => 'Window',
 6177:                 'tab'            => 'Tab',
 6178:                 'iframe'         => 'iFrame',
 6179:                 'height'         => 'Height (pixels)',
 6180:                 'width'          => 'Width (pixels)',
 6181:                 'linktext'       => 'Default Link Text',
 6182:                 'explanation'    => 'Default Explanation',
 6183:                 'append'         => 'Provider URL',
 6184:         );
 6185:     }
 6186: }
 6187: 
 6188: sub proctoring_providernames {
 6189:     return (
 6190:              proctorio => 'Proctorio',
 6191:              examity   => 'Examity',
 6192:            );
 6193: }
 6194: 
 6195: sub print_lti {
 6196:     my ($position,$dom,$settings,$rowtotal) = @_;
 6197:     my $itemcount = 1;
 6198:     my ($datatable,$css_class);
 6199:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 6200:     if (ref($settings) eq 'HASH') {
 6201:         if ($position eq 'top') {
 6202:             if (exists($settings->{'encrypt'})) {
 6203:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6204:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6205:                         if ($key eq 'consumers') {
 6206:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6207:                         } else {
 6208:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6209:                         }
 6210:                     }
 6211:                 }
 6212:             }
 6213:             if (exists($settings->{'private'})) {
 6214:                 if (ref($settings->{'private'}) eq 'HASH') {
 6215:                     if (ref($settings->{'private'}) eq 'HASH') {
 6216:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6217:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6218:                         }
 6219:                     }
 6220:                 }
 6221:             }
 6222:         } elsif ($position eq 'upper') {
 6223:             if (exists($settings->{'rules'})) {
 6224:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6225:                     %rules = %{$settings->{'rules'}};
 6226:                 }
 6227:             }
 6228:         } elsif ($position eq 'middle') {
 6229:             if (exists($settings->{'suggested'})) {
 6230:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 6231:                     %suggestions = %{$settings->{'suggested'}};
 6232:                 }
 6233:             }
 6234:         } elsif ($position eq 'lower') {
 6235:             if (exists($settings->{'linkprot'})) {
 6236:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6237:                     %linkprot = %{$settings->{'linkprot'}};
 6238:                     if ($linkprot{'lock'}) {
 6239:                         delete($linkprot{'lock'});
 6240:                     }
 6241:                 }
 6242:             }
 6243:         } else {
 6244:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 6245:                 if (exists($settings->{$key})) {
 6246:                     delete($settings->{$key});
 6247:                 }
 6248:             }
 6249:         }
 6250:     }
 6251:     if ($position eq 'top') {
 6252:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6253:     } elsif ($position eq 'upper') {
 6254:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6255:         $$rowtotal += $itemcount;
 6256:     } elsif ($position eq 'middle') {
 6257:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 6258:         $$rowtotal += $itemcount;
 6259:     } elsif ($position eq 'lower') {
 6260:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6261:     } else {
 6262:         my ($switchserver,$switchmessage);
 6263:         $switchserver = &check_switchserver($dom);
 6264:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6265:         my $maxnum = 0;
 6266:         my %ordered;
 6267:         if (ref($settings) eq 'HASH') {
 6268:             foreach my $item (keys(%{$settings})) {
 6269:                 if (ref($settings->{$item}) eq 'HASH') {
 6270:                     my $num = $settings->{$item}{'order'};
 6271:                     if ($num eq '') {
 6272:                         $num = scalar(keys(%{$settings}));
 6273:                     }
 6274:                     $ordered{$num} = $item;
 6275:                 }
 6276:             }
 6277:         }
 6278:         $maxnum = scalar(keys(%ordered));
 6279:         my %lt = &lti_names();
 6280:         if (keys(%ordered)) {
 6281:             my @items = sort { $a <=> $b } keys(%ordered);
 6282:             for (my $i=0; $i<@items; $i++) {
 6283:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6284:                 my $item = $ordered{$items[$i]};
 6285:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6286:                 if (ref($settings->{$item}) eq 'HASH') {
 6287:                     $key = $settings->{$item}->{'key'};
 6288:                     $usable = $settings->{$item}->{'usable'};
 6289:                     $lifetime = $settings->{$item}->{'lifetime'};
 6290:                     $consumer = $settings->{$item}->{'consumer'};
 6291:                     $requser = $settings->{$item}->{'requser'};
 6292:                     $crsinc = $settings->{$item}->{'crsinc'};
 6293:                     $current = $settings->{$item};
 6294:                 }
 6295:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6296:                 my %checkedrequser = (
 6297:                                        yes => ' checked="checked"',
 6298:                                        no  => '',
 6299:                                      );
 6300:                 if (!$requser) {
 6301:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6302:                     $checkedrequser{'yes'} = '';
 6303:                 }
 6304:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6305:                 my %checkedcrsinc = (
 6306:                                       yes => ' checked="checked"',
 6307:                                       no  => '',
 6308:                                     );
 6309:                 if (!$crsinc) {
 6310:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6311:                     $checkedcrsinc{'yes'} = '';
 6312:                 }
 6313:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6314:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6315:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6316:                 for (my $k=0; $k<=$maxnum; $k++) {
 6317:                     my $vpos = $k+1;
 6318:                     my $selstr;
 6319:                     if ($k == $i) {
 6320:                         $selstr = ' selected="selected" ';
 6321:                     }
 6322:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6323:                 }
 6324:                 $datatable .= '</select>'.('&nbsp;'x2).
 6325:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6326:                     &mt('Delete?').'</label></span></td>'.
 6327:                     '<td colspan="2">'.
 6328:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6329:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6330:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6331:                     ('&nbsp;'x2).
 6332:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6333:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6334:                     ('&nbsp;'x2).
 6335:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6336:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6337:                 if ($key ne '') {
 6338:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6339:                     if ($switchserver) {
 6340:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6341:                     } else {
 6342:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6343:                     }
 6344:                     $datatable .= '</span> '.('&nbsp;'x2);
 6345:                 } elsif (!$switchserver) {
 6346:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6347:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6348:                                   '</span> '.('&nbsp;'x2);
 6349:                 }
 6350:                 if ($switchserver) {
 6351:                     if ($usable ne '') {
 6352:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6353:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6354:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6355:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6356:                                       ('&nbsp;'x2).
 6357:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6358:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6359:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6360:                                       '</div>';
 6361:                     } elsif ($key eq '') {
 6362:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6363:                     } else {
 6364:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6365:                     }
 6366:                 } else {
 6367:                     if ($usable ne '') {
 6368:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6369:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6370:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6371:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6372:                                       ('&nbsp;'x2).
 6373:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6374:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6375:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6376:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6377:                                       '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label></span></div>';
 6378:                     } else {
 6379:                         $datatable .=
 6380:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6381:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6382:                             '<label><input type="checkbox" name="lti_visible_'.$i.'" id="lti_visible_'.$i.'" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>';
 6383:                     }
 6384:                 }
 6385:                 $datatable .= '<br /><br />'.
 6386:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6387:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6388:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6389:                     '<br /><br />'.
 6390:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6391:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6392:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6393:                     ('&nbsp;'x4).
 6394:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6395:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6396:                 $itemcount ++;
 6397:             }
 6398:         }
 6399:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6400:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6401:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6402:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6403:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6404:         for (my $k=0; $k<$maxnum+1; $k++) {
 6405:             my $vpos = $k+1;
 6406:             my $selstr;
 6407:             if ($k == $maxnum) {
 6408:                 $selstr = ' selected="selected" ';
 6409:             }
 6410:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6411:         }
 6412:         $datatable .= '</select>&nbsp;'."\n".
 6413:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6414:                       '<td colspan="2">'.
 6415:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6416:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6417:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6418:                       ('&nbsp;'x2).
 6419:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6420:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6421:                       ('&nbsp;'x2).
 6422:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6423:         if ($switchserver) {
 6424:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6425:         } else {
 6426:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6427:                           ('&nbsp;'x2).
 6428:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6429:                           '<label><input type="checkbox" name="lti_add_visible" id="lti_add_visible" onclick="if (this.checked) { this.form.lti_secret_add.type='."'text'".' } else { this.form.lti_secret_add.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n";
 6430:         }
 6431:         $datatable .= '<br /><br />'.
 6432:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6433:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6434:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6435:                       '<br /><br />'.
 6436:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6437:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6438:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6439:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6440:                       '</td>'."\n".
 6441:                       '</tr>'."\n";
 6442:         $itemcount ++;
 6443:     }
 6444:     $$rowtotal += $itemcount;
 6445:     return $datatable;
 6446: }
 6447: 
 6448: sub lti_names {
 6449:     my %lt = &Apache::lonlocal::texthash(
 6450:                                           'version'   => 'LTI Version',
 6451:                                           'url'       => 'URL',
 6452:                                           'key'       => 'Key',
 6453:                                           'lifetime'  => 'Nonce lifetime (s)',
 6454:                                           'consumer'  => 'Consumer',
 6455:                                           'secret'    => 'Secret',
 6456:                                           'requser'   => "User's identity sent",
 6457:                                           'crsinc'    => "Course's identity sent",
 6458:                                           'email'     => 'Email address',
 6459:                                           'sourcedid' => 'User ID',
 6460:                                           'other'     => 'Other',
 6461:                                           'passback'  => 'Can return grades to Consumer:',
 6462:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6463:                                           'topmenu'   => 'Display LON-CAPA page header',
 6464:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6465:                                         );
 6466:     return %lt;
 6467: }
 6468: 
 6469: sub lti_options {
 6470:     my ($num,$current,$itemcount,%lt) = @_;
 6471:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6472:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6473:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6474:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6475:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6476:     $checked{'mapcrstype'} = {};
 6477:     $checked{'makeuser'} = {};
 6478:     $checked{'selfenroll'} = {};
 6479:     $checked{'crssec'} = {};
 6480:     $checked{'crssecsrc'} = {};
 6481:     $checked{'lcauth'} = {};
 6482:     $checked{'menuitem'} = {};
 6483:     if ($num eq 'add') {
 6484:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6485:     }
 6486:     my $userfieldsty = 'none';
 6487:     my $crsfieldsty = 'none';
 6488:     my $crssecfieldsty = 'none';
 6489:     my $secsrcfieldsty = 'none';
 6490:     my $callbacksty = 'none';
 6491:     my $passbacksty = 'none';
 6492:     my $optionsty = 'block';
 6493:     my $crssty = 'block';
 6494:     my $lcauthparm;
 6495:     my $lcauthparmstyle = 'display:none';
 6496:     my $lcauthparmtext;
 6497:     my $menusty;
 6498:     my $numinrow = 4;
 6499:     my %menutitles = &ltimenu_titles();
 6500: 
 6501:     if (ref($current) eq 'HASH') {
 6502:         if (!$current->{'requser'}) {
 6503:             $optionsty = 'none';
 6504:             $crssty = 'none';
 6505:         } elsif (!$current->{'crsinc'}) {
 6506:             $crssty = 'none';
 6507:         }
 6508:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6509:             $checked{'mapuser'}{'sourcedid'} = '';
 6510:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6511:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6512:             } else {
 6513:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6514:                 $userfield = $current->{'mapuser'};
 6515:                 $userfieldsty = 'inline-block';
 6516:             }
 6517:         }
 6518:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6519:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6520:             if ($current->{'mapcrs'} eq 'context_id') {
 6521:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6522:             } else {
 6523:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6524:                 $cidfield = $current->{'mapcrs'};
 6525:                 $crsfieldsty = 'inline-block';
 6526:             }
 6527:         }
 6528:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6529:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6530:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6531:             }
 6532:         }
 6533:         if (!$current->{'storecrs'}) {
 6534:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6535:             $checked{'storecrs'}{'Y'} = '';
 6536:         }
 6537:         if ($current->{'makecrs'}) {
 6538:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6539:         }
 6540:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6541:             foreach my $role (@{$current->{'makeuser'}}) {
 6542:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6543:             }
 6544:         }
 6545:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6546:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6547:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6548:                 $lcauthparm = $current->{'lcauthparm'};
 6549:                 $lcauthparmstyle = 'display:table-row';
 6550:                 if ($current->{'lcauth'} eq 'localauth') {
 6551:                     $lcauthparmtext = &mt('Local auth argument');
 6552:                 } else {
 6553:                     $lcauthparmtext = &mt('Kerberos domain');
 6554:                 }
 6555:             }
 6556:         }
 6557:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6558:             foreach my $role (@{$current->{'selfenroll'}}) {
 6559:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6560:             }
 6561:         }
 6562:         if (ref($current->{'maproles'}) eq 'HASH') {
 6563:             %rolemaps = %{$current->{'maproles'}};
 6564:         }
 6565:         if ($current->{'section'} ne '') {
 6566:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6567:             $crssecfieldsty = 'inline-block';
 6568:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6569:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6570:             } else {
 6571:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6572:                 $crssecsrc = $current->{'section'};
 6573:                 $secsrcfieldsty = 'inline-block';
 6574:             }
 6575:         } else {
 6576:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6577:         }
 6578:         if ($current->{'callback'} ne '') {
 6579:             $callback = $current->{'callback'};
 6580:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6581:             $callbacksty = 'inline-block';
 6582:         } else {
 6583:             $checked{'callback'}{'N'} = ' checked="checked"';
 6584:         }
 6585:         if ($current->{'topmenu'}) {
 6586:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6587:         } else {
 6588:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6589:         }
 6590:         if ($current->{'inlinemenu'}) {
 6591:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6592:         } else {
 6593:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6594:         }
 6595:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6596:             $menusty = 'inline-block';
 6597:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6598:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6599:                     if (exists($menutitles{$item})) {
 6600:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6601:                     }
 6602:                 }
 6603:             }
 6604:         } else {
 6605:             $menusty = 'none';
 6606:         }
 6607:     } else {
 6608:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6609:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6610:         $checked{'callback'}{'N'} = ' checked="checked"';
 6611:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6612:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6613:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6614:         $menusty = 'inline-block';
 6615:     }
 6616:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6617:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6618:                                official   => 'Official',
 6619:                                unofficial => 'Unofficial',
 6620:                                community  => 'Community',
 6621:                                textbook   => 'Textbook',
 6622:                                placement  => 'Placement Test',
 6623:                                lti        => 'LTI Provider',
 6624:     );
 6625:     my @authtypes = ('internal','krb4','krb5','localauth');
 6626:     my %shortauth = (
 6627:                      internal => 'int',
 6628:                      krb4 => 'krb4',
 6629:                      krb5 => 'krb5',
 6630:                      localauth  => 'loc'
 6631:                     );
 6632:     my %authnames = &authtype_names();
 6633:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6634:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6635:     my @courseroles = ('cc','in','ta','ep','st');
 6636:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6637:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6638:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6639:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6640:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6641:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6642:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6643:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6644:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6645:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6646:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6647:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6648:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6649:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6650:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6651:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6652:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6653:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6654:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6655:     foreach my $option ('sourcedid','email','other') {
 6656:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6657:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6658:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6659:     }
 6660:     $output .= '</span></div>'.
 6661:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6662:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6663:                'value="'.$userfield.'" /></div></fieldset>'.
 6664:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6665:     foreach my $ltirole (@ltiroles) {
 6666:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6667:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6668:     }
 6669:     $output .= '</fieldset>'.
 6670:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6671:                '<table>'.
 6672:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6673:                '</table>'.
 6674:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6675:                '<td class="LC_left_item">';
 6676:     foreach my $auth ('lti',@authtypes) {
 6677:         my $authtext;
 6678:         if ($auth eq 'lti') {
 6679:             $authtext = &mt('None');
 6680:         } else {
 6681:             $authtext = $authnames{$shortauth{$auth}};
 6682:         }
 6683:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6684:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6685:                    $authtext.'</label></span> &nbsp;';
 6686:     }
 6687:     $output .= '</td></tr>'.
 6688:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6689:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6690:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6691:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6692:                '</table></fieldset>'.
 6693:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6694:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6695:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6696:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6697:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6698:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6699:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6700:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6701:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6702:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6703:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6704:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6705:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6706:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6707:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6708:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6709:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6710:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6711:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6712:                    ('&nbsp;'x2);
 6713:     }
 6714:     $output .= '</span></div></fieldset>'.
 6715:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6716:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6717:                &mt('Unique course identifier').':&nbsp;';
 6718:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6719:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6720:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6721:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6722:     }
 6723:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6724:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6725:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6726:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6727:     foreach my $type (@coursetypes) {
 6728:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6729:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6730:                    ('&nbsp;'x2);
 6731:     }
 6732:     $output .= '</span><br /><br />'.
 6733:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6734:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6735:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6736:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6737:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6738:                '</fieldset>'.
 6739:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6740:     foreach my $ltirole (@lticourseroles) {
 6741:         my ($selected,$selectnone);
 6742:         if ($rolemaps{$ltirole} eq '') {
 6743:             $selectnone = ' selected="selected"';
 6744:         }
 6745:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6746:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6747:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6748:         foreach my $role (@courseroles) {
 6749:             unless ($selectnone) {
 6750:                 if ($rolemaps{$ltirole} eq $role) {
 6751:                     $selected = ' selected="selected"';
 6752:                 } else {
 6753:                     $selected = '';
 6754:                 }
 6755:             }
 6756:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6757:                        &Apache::lonnet::plaintext($role,'Course').
 6758:                        '</option>';
 6759:         }
 6760:         $output .= '</select></td>';
 6761:     }
 6762:     $output .= '</tr></table></fieldset>'.
 6763:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6764:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6765:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6766:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6767:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6768:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6769:                '</fieldset>'.
 6770:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6771:     foreach my $lticrsrole (@lticourseroles) {
 6772:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6773:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6774:     }
 6775:     $output .= '</fieldset>'.
 6776:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6777:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6778:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6779:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6780:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6781:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6782:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6783:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6784:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6785:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6786:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6787:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6788:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6789:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6790:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6791:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6792:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6793:     foreach my $extra ('roster','passback') {
 6794:         my $checkedon = '';
 6795:         my $checkedoff = ' checked="checked"';
 6796:         if ($extra eq 'passback') {
 6797:             $pb1p1chk = ' checked="checked"';
 6798:             $pb1p0chk = '';
 6799:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6800:         } else {
 6801:             $onclickpb = '';
 6802:         }
 6803:         if (ref($current) eq 'HASH') {
 6804:             if (($current->{$extra})) {
 6805:                 $checkedon = $checkedoff;
 6806:                 $checkedoff = '';
 6807:                 if ($extra eq 'passback') {
 6808:                     $passbacksty = 'inline-block';
 6809:                 }
 6810:                 if ($current->{'passbackformat'} eq '1.0') {
 6811:                     $pb1p0chk =  ' checked="checked"';
 6812:                     $pb1p1chk = '';
 6813:                 }
 6814:             }
 6815:         }
 6816:         $output .= $lt{$extra}.'&nbsp;'.
 6817:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6818:                    &mt('No').'</label>'.('&nbsp;'x2).
 6819:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6820:                    &mt('Yes').'</label><br />';
 6821:     }
 6822:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6823:                '<span class="LC_nobreak">'.&mt('Grade format').
 6824:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6825:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6826:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6827:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6828:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6829:     $output .= '</span></div></fieldset>';
 6830: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6831: #
 6832: #    $output .= '</fieldset>'.
 6833: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6834:     return $output;
 6835: }
 6836: 
 6837: sub ltimenu_titles {
 6838:     return &Apache::lonlocal::texthash(
 6839:                                         fullname    => 'Full name',
 6840:                                         coursetitle => 'Course title',
 6841:                                         role        => 'Role',
 6842:                                         logout      => 'Logout',
 6843:                                         grades      => 'Grades',
 6844:     );
 6845: }
 6846: 
 6847: sub linkprot_suggestions {
 6848:     my ($suggested,$itemcount) = @_;
 6849:     my $count = 0;
 6850:     my $next = 1;
 6851:     my %lt = &Apache::lonlocal::texthash(
 6852:                                           'name' => 'Suggested Launcher',
 6853:                                           'info' => 'Recommendations',
 6854:                                         );
 6855:     my ($datatable,$css_class,$dest);
 6856:     if (ref($suggested) eq 'HASH') {
 6857:         my @current = sort { $a <=> $b } keys(%{$suggested});
 6858:         $next += $current[-1];
 6859:         for (my $i=0; $i<@current; $i++) {
 6860:             my $num = $current[$i];
 6861:             my %values;
 6862:             if (ref($suggested->{$num}) eq 'HASH') {
 6863:                 %values = %{$suggested->{$num}};
 6864:             } else {
 6865:                 next;
 6866:             }
 6867:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6868:             $datatable .=
 6869:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6870:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 6871:                 &mt('Delete?').'</label></span></td><td>'."\n".
 6872:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6873:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 6874:                 '</fieldset></div>'.
 6875:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6876:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 6877:                 '</fieldset></div>'.
 6878:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6879:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 6880:             $$itemcount ++;
 6881:         }
 6882:     }
 6883:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6884:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6885:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 6886:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6887:                   '<td>'."\n".
 6888:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6889:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 6890:                   '</fieldset></div>'.
 6891:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6892:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 6893:                   '</fieldset></div>'.
 6894:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6895:                   '</td></tr>'."\n";
 6896:     return $datatable;
 6897: }
 6898: 
 6899: sub print_coursedefaults {
 6900:     my ($position,$dom,$settings,$rowtotal) = @_;
 6901:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6902:     my $itemcount = 1;
 6903:     my %choices =  &Apache::lonlocal::texthash (
 6904:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6905:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6906:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course (MB)',
 6907:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6908:         coursecredits        => 'Credits can be specified for courses',
 6909:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6910:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6911:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6912:         texengine            => 'Default method to display mathematics',
 6913:         postsubmit           => 'Disable submit button/keypress following student submission',
 6914:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6915:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6916:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6917:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6918:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6919:         crsauthor            => 'Standard LON-CAPA problems can be created within a course/community (by type)',
 6920:         crseditors           => 'Available editors for web pages and/or problems created in a course/community',
 6921:     );
 6922:     my %staticdefaults = (
 6923:                            anonsurvey_threshold => 10,
 6924:                            uploadquota          => 500,
 6925:                            coursequota          => 20,
 6926:                            postsubmit           => 60,
 6927:                            mysqltables          => 172800,
 6928:                            domexttool           => 1,
 6929:                            exttool              => 0,
 6930:                            crsauthor            => 1,
 6931:                            crseditors           => ['edit','xml'],  
 6932:                          );
 6933:     if ($position eq 'top') {
 6934:         %defaultchecked = (
 6935:                             'canuse_pdfforms' => 'off',
 6936:                             'uselcmath'       => 'on',
 6937:                             'usejsme'         => 'on',
 6938:                             'inline_chem'     => 'on',
 6939:                             'canclone'        => 'none',
 6940:                           );
 6941:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6942:         my $deftex = $Apache::lonnet::deftex;
 6943:         if (ref($settings) eq 'HASH') {
 6944:             if ($settings->{'texengine'}) {
 6945:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6946:                     $deftex = $settings->{'texengine'};
 6947:                 }
 6948:             }
 6949:         }
 6950:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6951:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6952:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6953:                        '</span></td><td class="LC_right_item">'.
 6954:                        '<select name="texengine">'."\n";
 6955:         my %texoptions = (
 6956:                             MathJax  => 'MathJax',
 6957:                             mimetex  => &mt('Convert to Images'),
 6958:                             tth      => &mt('TeX to HTML'),
 6959:                          );
 6960:         foreach my $renderer ('MathJax','mimetex','tth') {
 6961:             my $selected = '';
 6962:             if ($renderer eq $deftex) {
 6963:                 $selected = ' selected="selected"';
 6964:             }
 6965:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6966:         }
 6967:         $mathdisp .= '</select></td></tr>'."\n";
 6968:         $itemcount ++;
 6969:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6970:                                                      \%choices,$itemcount);
 6971:         $datatable = $mathdisp.$datatable;
 6972:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6973:         $datatable .=
 6974:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6975:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6976:             '</span></td><td class="LC_left_item">';
 6977:         my $currcanclone = 'none';
 6978:         my $onclick;
 6979:         my @cloneoptions = ('none','domain');
 6980:         my %clonetitles = &Apache::lonlocal::texthash (
 6981:                              none     => 'No additional course requesters',
 6982:                              domain   => "Any course requester in course's domain",
 6983:                              instcode => 'Course requests for official courses ...',
 6984:                           );
 6985:         my (%codedefaults,@code_order,@posscodes);
 6986:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6987:                                                     \@code_order) eq 'ok') {
 6988:             if (@code_order > 0) {
 6989:                 push(@cloneoptions,'instcode');
 6990:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6991:             }
 6992:         }
 6993:         if (ref($settings) eq 'HASH') {
 6994:             if ($settings->{'canclone'}) {
 6995:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6996:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6997:                         if (@code_order > 0) {
 6998:                             $currcanclone = 'instcode';
 6999:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 7000:                         }
 7001:                     }
 7002:                 } elsif ($settings->{'canclone'} eq 'domain') {
 7003:                     $currcanclone = $settings->{'canclone'};
 7004:                 }
 7005:             }
 7006:         }
 7007:         foreach my $option (@cloneoptions) {
 7008:             my ($checked,$additional);
 7009:             if ($currcanclone eq $option) {
 7010:                 $checked = ' checked="checked"';
 7011:             }
 7012:             if ($option eq 'instcode') {
 7013:                 if (@code_order) {
 7014:                     my $show = 'none';
 7015:                     if ($checked) {
 7016:                         $show = 'block';
 7017:                     }
 7018:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 7019:                                   &mt('Institutional codes for new and cloned course have identical:').
 7020:                                   '<br />';
 7021:                     foreach my $item (@code_order) {
 7022:                         my $codechk;
 7023:                         if ($checked) {
 7024:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 7025:                                 $codechk = ' checked="checked"';
 7026:                             }
 7027:                         }
 7028:                         $additional .= '<label>'.
 7029:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 7030:                                        $item.'</label>';
 7031:                     }
 7032:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 7033:                 }
 7034:             }
 7035:             $datatable .=
 7036:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 7037:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 7038:                 '</label>&nbsp;'.$additional.'</span><br />';
 7039:         }
 7040:         $datatable .= '</td>'.
 7041:                       '</tr>';
 7042:         $itemcount ++;
 7043:     } else {
 7044:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7045:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 7046:             %deftimeout,%currmysql);
 7047:         my $currusecredits = 0;
 7048:         my $postsubmitclient = 1;
 7049:         my $ltiauth = 0;
 7050:         my %domexttool;
 7051:         my %exttool;
 7052:         my %crsauthor;
 7053:         my %crseditors;
 7054:         my @types = ('official','unofficial','community','textbook','placement');
 7055:         if (ref($settings) eq 'HASH') {
 7056:             if ($settings->{'ltiauth'}) {
 7057:                 $ltiauth = 1;
 7058:             }
 7059:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 7060:                 foreach my $type (@types) {
 7061:                     if ($settings->{'domexttool'}->{$type}) {
 7062:                         $domexttool{$type} = ' checked="checked"';
 7063:                     }
 7064:                 }
 7065:             } else {
 7066:                 foreach my $type (@types) {
 7067:                     if ($staticdefaults{'domexttool'}) {
 7068:                         $domexttool{$type} = ' checked="checked"';
 7069:                     }
 7070:                 }
 7071:             }
 7072:             if (ref($settings->{'exttool'}) eq 'HASH') {
 7073:                 foreach my $type (@types) {
 7074:                     if ($settings->{'exttool'}->{$type}) {
 7075:                         $exttool{$type} = ' checked="checked"';
 7076:                     }
 7077:                 }
 7078:             }
 7079:             if (ref($settings->{'crsauthor'}) eq 'HASH') {
 7080:                 foreach my $type (@types) {
 7081:                     if ($settings->{'crsauthor'}->{$type}) {
 7082:                         $crsauthor{$type} = ' checked="checked"';
 7083:                     }
 7084:                 }
 7085:             } else {
 7086:                 foreach my $type (@types) {
 7087:                     if ($staticdefaults{'crsauthor'}) {
 7088:                         $crsauthor{$type} = ' checked="checked"';
 7089:                     }
 7090:                 }
 7091:             }
 7092:             if (ref($settings->{'crseditors'}) eq 'ARRAY') {
 7093:                 foreach my $editor (@{$settings->{'crseditors'}}) {
 7094:                     $crseditors{$editor} = ' checked="checked"';
 7095:                 }
 7096:             } else {
 7097:                 foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7098:                     $crseditors{$editor} = ' checked="checked"';
 7099:                 }
 7100:             }
 7101:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 7102:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 7103:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 7104:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 7105:                 }
 7106:             }
 7107:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 7108:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 7109:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 7110:                 }
 7111:             }
 7112:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 7113:                 foreach my $type (@types) {
 7114:                     next if ($type eq 'community');
 7115:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 7116:                     if ($defcredits{$type} ne '') {
 7117:                         $currusecredits = 1;
 7118:                     }
 7119:                 }
 7120:             }
 7121:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 7122:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 7123:                     $postsubmitclient = 0;
 7124:                     foreach my $type (@types) {
 7125:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7126:                     }
 7127:                 } else {
 7128:                     foreach my $type (@types) {
 7129:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 7130:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 7131:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 7132:                             } else {
 7133:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7134:                             }
 7135:                         } else {
 7136:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7137:                         }
 7138:                     }
 7139:                 }
 7140:             } else {
 7141:                 foreach my $type (@types) {
 7142:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7143:                 }
 7144:             }
 7145:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 7146:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 7147:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 7148:                 }
 7149:             } else {
 7150:                 foreach my $type (@types) {
 7151:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7152:                 }
 7153:             }
 7154:         } else {
 7155:             foreach my $type (@types) {
 7156:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7157:                 if ($staticdefaults{'domexttool'}) {
 7158:                     $domexttool{$type} = ' checked="checked"';
 7159:                 }
 7160:                 if ($staticdefaults{'crsauthor'}) {
 7161:                     $crsauthor{$type} = ' checked="checked"';
 7162:                 }
 7163:             }
 7164:             foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7165:                 $crseditors{$editor} = ' checked="checked"';
 7166:             }
 7167:         }
 7168:         if (!$currdefresponder) {
 7169:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7170:         } elsif ($currdefresponder < 1) {
 7171:             $currdefresponder = 1;
 7172:         }
 7173:         foreach my $type (@types) {
 7174:             if ($curruploadquota{$type} eq '') {
 7175:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7176:             }
 7177:             if ($currcoursequota{$type} eq '') {
 7178:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 7179:             }
 7180:         }
 7181:         $datatable .=
 7182:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7183:                 $choices{'anonsurvey_threshold'}.
 7184:                 '</span></td>'.
 7185:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7186:                 '<input type="text" name="anonsurvey_threshold"'.
 7187:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7188:                 '</td></tr>'."\n";
 7189:         $itemcount ++;
 7190:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7191:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7192:                       $choices{'uploadquota'}.
 7193:                       '</span></td>'.
 7194:                       '<td style="text-align: right" class="LC_right_item">'.
 7195:                       '<table><tr>';
 7196:         foreach my $type (@types) {
 7197:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7198:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7199:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7200:         }
 7201:         $datatable .= '</tr></table></td></tr>'."\n";
 7202:         $itemcount ++;
 7203:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7204:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7205:                       $choices{'coursequota'}.
 7206:                       '</span></td>'.
 7207:                       '<td style="text-align: right" class="LC_right_item">'.
 7208:                       '<table><tr>';
 7209:         foreach my $type (@types) {
 7210:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7211:                            '<input type="text" name="coursequota_'.$type.'"'.
 7212:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 7213:         }
 7214:         $datatable .= '</tr></table></td></tr>'."\n";
 7215:         $itemcount ++;
 7216:         my $onclick = "toggleDisplay(this.form,'credits');";
 7217:         my $display = 'none';
 7218:         if ($currusecredits) {
 7219:             $display = 'block';
 7220:         }
 7221:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7222:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7223:         foreach my $type (@types) {
 7224:             next if ($type eq 'community');
 7225:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7226:                            '<input type="text" name="'.$type.'_credits"'.
 7227:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7228:         }
 7229:         $additional .= '</tr></table></div>'."\n";
 7230:         %defaultchecked = ('coursecredits' => 'off');
 7231:         @toggles = ('coursecredits');
 7232:         my $current = {
 7233:                         'coursecredits' => $currusecredits,
 7234:                       };
 7235:         (my $table,$itemcount) =
 7236:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7237:                                \%choices,$itemcount,$onclick,$additional,'left');
 7238:         $datatable .= $table;
 7239:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7240:         my $display = 'none';
 7241:         if ($postsubmitclient) {
 7242:             $display = 'block';
 7243:         }
 7244:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7245:                       &mt('Number of seconds submit is disabled').'<br />'.
 7246:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7247:                       '<table><tr>';
 7248:         foreach my $type (@types) {
 7249:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7250:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7251:                            $deftimeout{$type}.'" size="5" /></td>';
 7252:         }
 7253:         $additional .= '</tr></table></div>'."\n";
 7254:         %defaultchecked = ('postsubmit' => 'on');
 7255:         @toggles = ('postsubmit');
 7256:         $current = {
 7257:                        'postsubmit' => $postsubmitclient,
 7258:                    };
 7259:         ($table,$itemcount) =
 7260:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7261:                                \%choices,$itemcount,$onclick,$additional,'left');
 7262:         $datatable .= $table;
 7263:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7264:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7265:                       $choices{'mysqltables'}.
 7266:                       '</span></td>'.
 7267:                       '<td style="text-align: right" class="LC_right_item">'.
 7268:                       '<table><tr>';
 7269:         foreach my $type (@types) {
 7270:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7271:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7272:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7273:         }
 7274:         $datatable .= '</tr></table></td></tr>'."\n";
 7275:         $itemcount ++;
 7276:         %defaultchecked = ('ltiauth' => 'off');
 7277:         @toggles = ('ltiauth');
 7278:         $current = {
 7279:                        'ltiauth' => $ltiauth,
 7280:                    };
 7281:         ($table,$itemcount) =
 7282:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7283:                                \%choices,$itemcount,undef,undef,'left');
 7284:         $datatable .= $table;
 7285:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7286:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7287:                       $choices{'domexttool'}.
 7288:                       '</span></td>'.
 7289:                       '<td style="text-align: right" class="LC_right_item">'.
 7290:                       '<table><tr>';
 7291:         foreach my $type (@types) {
 7292:             $datatable .= '<td style="text-align: left">'.
 7293:                           '<span class="LC_nobreak">'.
 7294:                           '<label><input type="checkbox" name="domexttool"'.
 7295:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7296:                           &mt($type).'</label></span></td>'."\n";
 7297:         }
 7298:         $datatable .= '</tr></table></td></tr>'."\n";
 7299:         $itemcount ++;
 7300:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7301:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7302:                       $choices{'exttool'}.
 7303:                       '</span></td>'.
 7304:                       '<td style="text-align: right" class="LC_right_item">'.
 7305:                       '<table><tr>';
 7306:         foreach my $type (@types) {
 7307:             $datatable .= '<td style="text-align: left">'.
 7308:                           '<span class="LC_nobreak">'.
 7309:                           '<label><input type="checkbox" name="exttool"'.
 7310:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7311:                           &mt($type).'</label></span></td>'."\n";
 7312:         }
 7313:         $datatable .= '</tr></table></td></tr>'."\n";
 7314:         $itemcount ++;
 7315:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7316:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7317:                       $choices{'crsauthor'}.
 7318:                       '</span></td>'.
 7319:                       '<td style="text-align: right" class="LC_right_item">'.
 7320:                       '<table><tr>';
 7321:         foreach my $type (@types) {
 7322:             $datatable .= '<td style="text-align: left">'.
 7323:                           '<span class="LC_nobreak">'.
 7324:                           '<label><input type="checkbox" name="crsauthor"'.
 7325:                           ' value="'.$type.'"'.$crsauthor{$type}.' />'.
 7326:                           &mt($type).'</label></span></td>'."\n";
 7327:         }
 7328:         $datatable .= '</tr></table></td></tr>'."\n";
 7329:         $itemcount ++;
 7330:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7331:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7332:                       $choices{'crseditors'}.
 7333:                       '</span></td>'.
 7334:                       '<td style="text-align: right" class="LC_right_item">'.
 7335:                       '<table><tr>';
 7336:         my @editors = ('edit','xml','daxe');
 7337:         my %editornames = &crseditor_titles();
 7338:         foreach my $editor (@editors) {
 7339:             $datatable .= '<td style="text-align: left">'.
 7340:                           '<span class="LC_nobreak">'.
 7341:                           '<label><input type="checkbox" name="crseditors"'.
 7342:                           ' value="'.$editor.'"'.$crseditors{$editor}.' />'.
 7343:                           $editornames{$editor}.'</label></span></td>'."\n";
 7344:         }
 7345:         $datatable .= '</tr></table></td></tr>'."\n";
 7346:     }
 7347:     $$rowtotal += $itemcount;
 7348:     return $datatable;
 7349: }
 7350: 
 7351: sub crseditor_titles {
 7352:     return &Apache::lonlocal::texthash(
 7353:                edit  => 'Standard editor (Edit)',
 7354:                xml   => 'Text editor (EditXML)',
 7355:                daxe  => 'Daxe editor (Daxe)',
 7356:            );
 7357: }
 7358: 
 7359: sub print_authordefaults {
 7360:     my ($position,$dom,$settings,$rowtotal) = @_;
 7361:     my ($css_class,$datatable,%checkedon,%checkedoff);
 7362:     my $itemcount = 1;
 7363:     my %titles = &authordefaults_titles();
 7364:     if ($position eq 'top') {
 7365:         my %defaultchecked = (
 7366:                             'nocodemirror' => 'off',
 7367:                             'daxecollapse' => 'off',
 7368:                             'domcoordacc'  => 'on',
 7369:                           );
 7370:         my @toggles = ('nocodemirror','daxecollapse','domcoordacc');
 7371:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 7372:                                                      \%titles,$itemcount);
 7373:         my %staticdefaults = (
 7374:                                 'copyright'    => 'default',
 7375:                                 'sourceavail'  => 'closed',
 7376:                              );
 7377:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7378:         my %currrights;
 7379:         foreach my $item ('copyright','sourceavail') {
 7380:             $currrights{$item} = $staticdefaults{$item};
 7381:             if (ref($settings) eq 'HASH') {
 7382:                 if (exists($settings->{$item})) {
 7383:                     $currrights{$item} = $settings->{$item};
 7384:                 }
 7385:             }
 7386:         }
 7387:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7388:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 7389:                       '</span></td><td class="LC_right_item">'.
 7390:                       &selectbox('copyright',$currrights{'copyright'},'',
 7391:                                  \&Apache::loncommon::copyrightdescription,
 7392:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 7393:                       '</td></tr>'."\n";
 7394:         $itemcount ++;
 7395:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7396:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7397:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 7398:                       '</span></td><td class="LC_right_item">'.
 7399:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 7400:                                  \&Apache::loncommon::source_copyrightdescription,
 7401:                                  (&Apache::loncommon::source_copyrightids)).
 7402:                       '</td></tr>'."\n";        
 7403:     } else {
 7404:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7405:         my $curreditors;
 7406:         my %staticdefaults = (
 7407:                                 editors => ['edit','xml'],
 7408:                                 authorquota => 500,
 7409:                                 webdav => 0,
 7410:                              );
 7411:         my $curreditors = $staticdefaults{'editors'};
 7412:         if ((ref($settings) eq 'HASH') &&
 7413:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 7414:             $curreditors = $settings->{'editors'};
 7415:         } else {
 7416:             $curreditors = $staticdefaults{'editors'};
 7417:         }
 7418:         my @editors = ('edit','xml','daxe');
 7419:         $datatable = '<tr'.$css_class.'>'."\n".
 7420:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 7421:                      '<td class="LC_left_item">'."\n".
 7422:                      '<span class="LC_nobreak">';
 7423:         foreach my $editor (@editors) {
 7424:             my $checked;
 7425:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 7426:                 $checked = ' checked="checked"';
 7427:             }
 7428:             $datatable .= '<label>'.
 7429:                           '<input type="checkbox" name="author_editors" '.
 7430:                           $checked.' value="'.$editor.'" '.
 7431:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 7432:                           $titles{$editor}.'</label>&nbsp;';
 7433:         }
 7434:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 7435:         $itemcount ++;
 7436:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7437:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7438:         my @insttypes;
 7439:         if (ref($types) eq 'ARRAY') {
 7440:             @insttypes = @{$types};
 7441:         }
 7442:         my $typecount = 0;
 7443:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 7444:         my @items = ('webdav','authorquota');
 7445:         my %quotas;
 7446:         if (ref($domconf{'quotas'}) eq 'HASH') {
 7447:             %quotas = %{$domconf{'quotas'}};
 7448:             foreach my $item (@items) {
 7449:                 if (ref($quotas{$item}) eq 'HASH') {
 7450:                     foreach my $type (@insttypes,'default') {
 7451:                         if ($item eq 'authorquota') {
 7452:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 7453:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7454:                             }
 7455:                         } elsif ($item eq 'webdav') {
 7456:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 7457:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7458:                             }
 7459:                         }
 7460:                     }
 7461:                 } else {
 7462:                     foreach my $type (@insttypes,'default') {
 7463:                         $quotas{$item}{$type} = $staticdefaults{$item};
 7464:                     }
 7465:                 }
 7466:             }
 7467:         } else {
 7468:             foreach my $item (@items) {
 7469:                 foreach my $type (@insttypes,'default') {
 7470:                     $quotas{$item}{$type} = $staticdefaults{$item};
 7471:                 }
 7472:             }
 7473:         }
 7474:         if (ref($usertypes) eq 'HASH') {
 7475:             my $numinrow = 4;
 7476:             my $onclick = '';
 7477:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7478:                                          $numinrow,$othertitle,'authorquota',
 7479:                                          \$itemcount,$onclick);
 7480:             $itemcount ++;
 7481:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7482:                                          $numinrow,$othertitle,'webdav',
 7483:                                          \$itemcount);
 7484:             $itemcount ++;
 7485:         }
 7486:         my $checkedno = ' checked="checked"';
 7487:         my ($checkedon,$checkedoff);
 7488:         if (ref($quotas{'webdav'}) eq 'HASH') {
 7489:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) {
 7490:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 7491:                     $checkedon = $checkedno;
 7492:                 } else {
 7493:                     $checkedoff = $checkedno;
 7494:                 }
 7495:                 undef($checkedno);
 7496:             }
 7497:         }
 7498:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7499:         $datatable .= '<tr'.$css_class.'>'.
 7500:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 7501:                              $titles{'webdav_LC_adv_over'}.
 7502:                       '</td>'.
 7503:                       '<td class="LC_left_item">';
 7504:         foreach my $option ('none','off','on') {
 7505:             my ($text,$val,$checked);
 7506:             if ($option eq 'none') {
 7507:                 $text = $titles{'none'};
 7508:                 $val = '';
 7509:                 $checked = $checkedno;
 7510:             } elsif ($option eq 'off') {
 7511:                 $text = $titles{'overoff'};
 7512:                 $val = 0;
 7513:                 $checked = $checkedoff;
 7514:             } elsif ($option eq 'on') {
 7515:                 $text = $titles{'overon'};
 7516:                 $val = 1;
 7517:                 $checked = $checkedon;
 7518:             }
 7519:             $datatable .= '<span class="LC_nobreak"><label>'.
 7520:                           '<input type="radio" name="webdav_LC_adv"'.
 7521:                           ' value="'.$val.'"'.$checked.' />'.
 7522:                           $text.'</label></span>&nbsp; ';
 7523:         }
 7524:         $datatable .= '</td></tr>';
 7525:         $itemcount ++;
 7526:     }
 7527:     $$rowtotal += $itemcount;
 7528:     return $datatable;
 7529: }
 7530: 
 7531: sub authordefaults_titles {
 7532:     return &Apache::lonlocal::texthash(
 7533:                copyright => 'Copyright/Distribution',
 7534:                sourceavail => ' Source Available',
 7535:                editors => 'Available Editors',
 7536:                webdav => 'WebDAV',
 7537:                authorquota => 'Authoring Space quotas (MB)',
 7538:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 7539:                daxecollapse => 'Daxe editor: LON-CAPA standard menus start collapsed',
 7540:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 7541:                edit  => 'Standard editor (Edit)',
 7542:                xml   => 'Text editor (EditXML)',
 7543:                daxe  => 'Daxe editor (Daxe)',
 7544:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 7545:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 7546:                none => 'No override set',
 7547:                overon => 'Override -- webDAV on',
 7548:                overoff => 'Override -- webDAV off',
 7549:     );
 7550: }
 7551: 
 7552: sub selectbox {
 7553:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 7554:     my $selout = '<select name="'.$name.'">';
 7555:     foreach my $id (@idlist) {
 7556:         $selout.='<option value="'.$id.'"';
 7557:         if ($id eq $value) {
 7558:             $selout.=' selected="selected"';
 7559:         }
 7560:         if ($readonly) {
 7561:             $selout .= ' disabled="disabled"';
 7562:         }
 7563:         $selout.='>'.&{$functionref}($id).'</option>';
 7564:     }
 7565:     $selout.='</select>';
 7566:     return $selout;
 7567: }
 7568: 
 7569: sub print_selfenrollment {
 7570:     my ($position,$dom,$settings,$rowtotal) = @_;
 7571:     my ($css_class,$datatable);
 7572:     my $itemcount = 1;
 7573:     my @types = ('official','unofficial','community','textbook','placement');
 7574:     if (($position eq 'top') || ($position eq 'middle')) {
 7575:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7576:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7577:         my @rows;
 7578:         my $key;
 7579:         if ($position eq 'top') {
 7580:             $key = 'admin'; 
 7581:             if (ref($rowsref) eq 'ARRAY') {
 7582:                 @rows = @{$rowsref};
 7583:             }
 7584:         } elsif ($position eq 'middle') {
 7585:             $key = 'default';
 7586:             @rows = ('types','registered','approval','limit');
 7587:         }
 7588:         foreach my $row (@rows) {
 7589:             if (defined($titlesref->{$row})) {
 7590:                 $itemcount ++;
 7591:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7592:                 $datatable .= '<tr'.$css_class.'>'.
 7593:                               '<td>'.$titlesref->{$row}.'</td>'.
 7594:                               '<td class="LC_left_item">'.
 7595:                               '<table><tr>';
 7596:                 my (%current,%currentcap);
 7597:                 if (ref($settings) eq 'HASH') {
 7598:                     if (ref($settings->{$key}) eq 'HASH') {
 7599:                         foreach my $type (@types) {
 7600:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7601:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7602:                             }
 7603:                             if (($row eq 'limit') && ($key eq 'default')) {
 7604:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7605:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7606:                                 }
 7607:                             }
 7608:                         }
 7609:                     }
 7610:                 }
 7611:                 my %roles = (
 7612:                              '0' => &Apache::lonnet::plaintext('dc'),
 7613:                             ); 
 7614:             
 7615:                 foreach my $type (@types) {
 7616:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7617:                         $datatable .= '<th>'.&mt($type).'</th>';
 7618:                     }
 7619:                 }
 7620:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7621:                     $datatable .= '</tr><tr>';
 7622:                 }
 7623:                 foreach my $type (@types) {
 7624:                     if ($type eq 'community') {
 7625:                         $roles{'1'} = &mt('Community personnel');
 7626:                     } else {
 7627:                         $roles{'1'} = &mt('Course personnel');
 7628:                     }
 7629:                     $datatable .= '<td style="vertical-align: top">';
 7630:                     if ($position eq 'top') {
 7631:                         my %checked;
 7632:                         if ($current{$type} eq '0') {
 7633:                             $checked{'0'} = ' checked="checked"';
 7634:                         } else {
 7635:                             $checked{'1'} = ' checked="checked"';
 7636:                         }
 7637:                         foreach my $role ('1','0') {
 7638:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7639:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7640:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7641:                                           $roles{$role}.'</label></span> ';
 7642:                         }
 7643:                     } else {
 7644:                         if ($row eq 'types') {
 7645:                             my %checked;
 7646:                             if ($current{$type} =~ /^(all|dom)$/) {
 7647:                                 $checked{$1} = ' checked="checked"';
 7648:                             } else {
 7649:                                 $checked{''} = ' checked="checked"';
 7650:                             }
 7651:                             foreach my $val ('','dom','all') {
 7652:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7653:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7654:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7655:                             }
 7656:                         } elsif ($row eq 'registered') {
 7657:                             my %checked;
 7658:                             if ($current{$type} eq '1') {
 7659:                                 $checked{'1'} = ' checked="checked"';
 7660:                             } else {
 7661:                                 $checked{'0'} = ' checked="checked"';
 7662:                             }
 7663:                             foreach my $val ('0','1') {
 7664:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7665:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7666:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7667:                             }
 7668:                         } elsif ($row eq 'approval') {
 7669:                             my %checked;
 7670:                             if ($current{$type} =~ /^([12])$/) {
 7671:                                 $checked{$1} = ' checked="checked"';
 7672:                             } else {
 7673:                                 $checked{'0'} = ' checked="checked"';
 7674:                             }
 7675:                             for my $val (0..2) {
 7676:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7677:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7678:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7679:                             }
 7680:                         } elsif ($row eq 'limit') {
 7681:                             my %checked;
 7682:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7683:                                 $checked{$1} = ' checked="checked"';
 7684:                             } else {
 7685:                                 $checked{'none'} = ' checked="checked"';
 7686:                             }
 7687:                             my $cap;
 7688:                             if ($currentcap{$type} =~ /^\d+$/) {
 7689:                                 $cap = $currentcap{$type};
 7690:                             }
 7691:                             foreach my $val ('none','allstudents','selfenrolled') {
 7692:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7693:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7694:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7695:                             }
 7696:                             $datatable .= '<br />'.
 7697:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7698:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7699:                                           '</span>'; 
 7700:                         }
 7701:                     }
 7702:                     $datatable .= '</td>';
 7703:                 }
 7704:                 $datatable .= '</tr>';
 7705:             }
 7706:             $datatable .= '</table></td></tr>';
 7707:         }
 7708:     } elsif ($position eq 'bottom') {
 7709:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7710:     }
 7711:     $$rowtotal += $itemcount;
 7712:     return $datatable;
 7713: }
 7714: 
 7715: sub print_validation_rows {
 7716:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7717:     my ($itemsref,$namesref,$fieldsref);
 7718:     if ($caller eq 'selfenroll') { 
 7719:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7720:     } elsif ($caller eq 'requestcourses') {
 7721:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7722:     }
 7723:     my %currvalidation;
 7724:     if (ref($settings) eq 'HASH') {
 7725:         if (ref($settings->{'validation'}) eq 'HASH') {
 7726:             %currvalidation = %{$settings->{'validation'}};
 7727:         }
 7728:     }
 7729:     my $datatable;
 7730:     my $itemcount = 0;
 7731:     foreach my $item (@{$itemsref}) {
 7732:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7733:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7734:                       $namesref->{$item}.
 7735:                       '</span></td>'.
 7736:                       '<td class="LC_left_item">';
 7737:         if (($item eq 'url') || ($item eq 'button')) {
 7738:             $datatable .= '<span class="LC_nobreak">'.
 7739:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7740:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7741:         } elsif ($item eq 'fields') {
 7742:             my @currfields;
 7743:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7744:                 @currfields = @{$currvalidation{$item}};
 7745:             }
 7746:             foreach my $field (@{$fieldsref}) {
 7747:                 my $check = '';
 7748:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7749:                     $check = ' checked="checked"';
 7750:                 }
 7751:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7752:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7753:                               ' value="'.$field.'"'.$check.' />'.$field.
 7754:                               '</label></span> ';
 7755:             }
 7756:         } elsif ($item eq 'markup') {
 7757:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7758:                            $currvalidation{$item}.
 7759:                               '</textarea>';
 7760:         }
 7761:         $datatable .= '</td></tr>'."\n";
 7762:         if (ref($rowtotal)) {
 7763:             $itemcount ++;
 7764:         }
 7765:     }
 7766:     if ($caller eq 'requestcourses') {
 7767:         my %currhash;
 7768:         if (ref($settings) eq 'HASH') {
 7769:             if (ref($settings->{'validation'}) eq 'HASH') {
 7770:                 if ($settings->{'validation'}{'dc'} ne '') {
 7771:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7772:                 }
 7773:             }
 7774:         }
 7775:         my $numinrow = 2;
 7776:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7777:                                                        'validationdc',%currhash);
 7778:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7779:         $datatable .= '<tr'.$css_class.'><td>';
 7780:         if ($numdc > 1) {
 7781:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7782:         } else {
 7783:             $datatable .=  &mt('Course creation processed as: ');
 7784:         }
 7785:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7786:         $itemcount ++;
 7787:     }
 7788:     if (ref($rowtotal)) {
 7789:         $$rowtotal += $itemcount;
 7790:     }
 7791:     return $datatable;
 7792: }
 7793: 
 7794: sub print_privacy {
 7795:     my ($position,$dom,$settings,$rowtotal) = @_;
 7796:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7797:     my $itemcount = 0;
 7798:     if ($position eq 'top') {
 7799:         $numinrow = 2;
 7800:     } else {
 7801:         @items = ('domain','author','course','community');
 7802:         %names = &Apache::lonlocal::texthash (
 7803:                      domain => 'Assigned domain role(s)',
 7804:                      author => 'Assigned co-author role(s)',
 7805:                      course => 'Assigned course role(s)',
 7806:                      community => 'Assigned community role(s)',
 7807:                  );
 7808:         $numinrow = 4;
 7809:         ($othertitle,$usertypes,$types) =
 7810:             &Apache::loncommon::sorted_inst_types($dom);
 7811:     }
 7812:     if (($position eq 'top') || ($position eq 'middle')) {
 7813:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7814:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7815:         if ($position eq 'top') {
 7816:             my %curr;
 7817:             my @options = ('none','user','domain','auto');
 7818:             my %titles = &Apache::lonlocal::texthash (
 7819:                 none   => 'Not allowed',
 7820:                 user   => 'User authorizes',
 7821:                 domain => 'DC authorizes',
 7822:                 auto   => 'Unrestricted',
 7823:                 instdom => 'Other domain shares institution/provider',
 7824:                 extdom => 'Other domain has different institution/provider',
 7825:                 notify => 'Notify when role needs authorization',
 7826:             );
 7827:             my %names = &Apache::lonlocal::texthash (
 7828:                 domain => 'Domain role',
 7829:                 author => 'Co-author role',
 7830:                 course => 'Course role',
 7831:                 community => 'Community role',
 7832:             );
 7833:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7834:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7835:             foreach my $domtype ('instdom','extdom') {
 7836:                 my (%checked,$skip);
 7837:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7838:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7839:                               '<td class="LC_left_item">';
 7840:                 if ($domtype eq 'instdom') {
 7841:                     unless (@instdoms > 1) {
 7842:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7843:                         $skip = 1;
 7844:                     }
 7845:                 } elsif ($domtype eq 'extdom') {
 7846:                     if (keys(%by_location) == 0) {
 7847:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7848:                         $skip = 1;
 7849:                     }
 7850:                 }
 7851:                 unless ($skip) {
 7852:                     foreach my $roletype ('domain','author','course','community') {
 7853:                         $checked{'auto'} = ' checked="checked"';
 7854:                         if (ref($settings) eq 'HASH') {
 7855:                             if (ref($settings->{approval}) eq 'HASH') {
 7856:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7857:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7858:                                         $checked{$1} = ' checked="checked"';
 7859:                                         $checked{'auto'} = '';
 7860:                                     }
 7861:                                 }
 7862:                             }
 7863:                         }
 7864:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7865:                         foreach my $option (@options) {
 7866:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7867:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7868:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7869:                                           '</label></span>&nbsp; ';
 7870:                         }
 7871:                         $datatable .= '</fieldset>';
 7872:                     }
 7873:                 }
 7874:                 $datatable .= '</td></tr>';
 7875:                 $itemcount ++;
 7876:             }
 7877:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7878:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7879:                           '<td class="LC_left_item">';
 7880:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7881:                 my %curr;
 7882:                 if (ref($settings) eq 'HASH') {
 7883:                     if ($settings->{'notify'} ne '') {
 7884:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7885:                     }
 7886:                 }
 7887:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7888:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7889:                                                              'privacy_notify',%curr);
 7890:                 if ($numdc > 0) {
 7891:                     $datatable .= $table;
 7892:                 } else {
 7893:                     $datatable .= &mt('There are no active Domain Coordinators');
 7894:                 }
 7895:             } else {
 7896:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7897:             }
 7898:             $datatable .='</td></tr>';
 7899:         } elsif ($position eq 'middle') {
 7900:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7901:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7902:                     foreach my $item (@{$types}) {
 7903:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7904:                                                                $numinrow,$itemcount,'','','','','',
 7905:                                                                '',$usertypes->{$item});
 7906:                         $itemcount ++;
 7907:                     }
 7908:                 }
 7909:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7910:                                                        $numinrow,$itemcount,'','','','','',
 7911:                                                        '',$othertitle);
 7912:                 $itemcount ++;
 7913:             } else {
 7914:                 my (@insttypes,%insttitles);
 7915:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7916:                     @insttypes = @{$types};
 7917:                     %insttitles = %{$usertypes};
 7918:                 }
 7919:                 foreach my $item (@insttypes,'default') {
 7920:                     my $title;
 7921:                     if ($item eq 'default') {
 7922:                         $title = $othertitle;
 7923:                     } else {
 7924:                         $title = $insttitles{$item};
 7925:                     }
 7926:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7927:                     $datatable .= '<tr'.$css_class.'>'.
 7928:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7929:                                   '<td class="LC_left_item">'.
 7930:                                   &mt('Nothing to set here, as there are no other domains').
 7931:                                   '</td></tr>';
 7932:                     $itemcount ++;
 7933:                 }
 7934:             }
 7935:         }
 7936:     } else {
 7937:         my $prefix;
 7938:         if ($position eq 'lower') {
 7939:             $prefix = 'priv';
 7940:         } else {
 7941:             $prefix = 'unpriv';
 7942:         }
 7943:         foreach my $item (@items) {
 7944:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7945:                                                    $numinrow,$itemcount,'','','','','',
 7946:                                                    '',$names{$item});
 7947:             $itemcount ++;
 7948:         }
 7949:     }
 7950:     if (ref($rowtotal)) {
 7951:         $$rowtotal += $itemcount;
 7952:     }
 7953:     return $datatable;
 7954: }
 7955: 
 7956: sub print_passwords {
 7957:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7958:     my ($datatable,$css_class);
 7959:     my $itemcount = 0;
 7960:     my %titles = &Apache::lonlocal::texthash (
 7961:         captcha        => '"Forgot Password" CAPTCHA validation',
 7962:         link           => 'Reset link expiration (hours)',
 7963:         case           => 'Case-sensitive usernames/e-mail',
 7964:         prelink        => 'Information required (form 1)',
 7965:         postlink       => 'Information required (form 2)',
 7966:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7967:         customtext     => 'Domain specific text (HTML)',
 7968:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7969:         intauth_check  => 'Check bcrypt cost if authenticated',
 7970:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7971:         permanent      => 'Permanent e-mail address',
 7972:         critical       => 'Critical notification address',
 7973:         notify         => 'Notification address',
 7974:         min            => 'Minimum password length',
 7975:         max            => 'Maximum password length',
 7976:         chars          => 'Required characters',
 7977:         expire         => 'Password expiration (days)',
 7978:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7979:     );
 7980:     if ($position eq 'top') {
 7981:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7982:         my $shownlinklife = 2;
 7983:         my $prelink = 'both';
 7984:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7985:         if (ref($settings) eq 'HASH') {
 7986:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7987:                 $shownlinklife = $settings->{resetlink};
 7988:             }
 7989:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7990:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7991:             }
 7992:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7993:                 $prelink = $settings->{resetprelink};
 7994:             }
 7995:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7996:                 %postlink = %{$settings->{resetpostlink}};
 7997:             }
 7998:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7999:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 8000:             }
 8001:             if ($settings->{resetremove}) {
 8002:                 $nostdtext = 1;
 8003:             }
 8004:             if ($settings->{resetcustom}) {
 8005:                 $customurl = $settings->{resetcustom};
 8006:             }
 8007:         } else {
 8008:             if (ref($types) eq 'ARRAY') {
 8009:                 foreach my $item (@{$types}) {
 8010:                     $casesens{$item} = 1;
 8011:                     $postlink{$item} = ['username','email'];
 8012:                 }
 8013:             }
 8014:             $casesens{'default'} = 1;
 8015:             $postlink{'default'} = ['username','email'];
 8016:             $prelink = 'both';
 8017:             %emailsrc = (
 8018:                           permanent => 1,
 8019:                           critical  => 1,
 8020:                           notify    => 1,
 8021:             );
 8022:         }
 8023:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 8024:         $itemcount ++;
 8025:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8026:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 8027:                       '<td class="LC_left_item">'.
 8028:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 8029:                       'name="passwords_link" size="3" /></td></tr>';
 8030:         $itemcount ++;
 8031:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8032:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 8033:                       '<td class="LC_left_item">';
 8034:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8035:             foreach my $item (@{$types}) {
 8036:                 my $checkedcase;
 8037:                 if ($casesens{$item}) {
 8038:                     $checkedcase = ' checked="checked"';
 8039:                 }
 8040:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8041:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 8042:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 8043:                               '</span>&nbsp;&nbsp; ';
 8044:             }
 8045:         }
 8046:         my $checkedcase;
 8047:         if ($casesens{'default'}) {
 8048:             $checkedcase = ' checked="checked"';
 8049:         }
 8050:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8051:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 8052:                       $othertitle.'</label></span></td>';
 8053:         $itemcount ++;
 8054:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8055:         my %checkedpre = (
 8056:                              both => ' checked="checked"',
 8057:                              either => '',
 8058:                          );
 8059:         if ($prelink eq 'either') {
 8060:             $checkedpre{either} = ' checked="checked"';
 8061:             $checkedpre{both} = '';
 8062:         }
 8063:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 8064:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8065:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 8066:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 8067:                       '<span class="LC_nobreak"><label>'.
 8068:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 8069:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 8070:         $itemcount ++;
 8071:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8072:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 8073:                       '<td class="LC_left_item">';
 8074:         my %postlinked;
 8075:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8076:             foreach my $item (@{$types}) {
 8077:                 undef(%postlinked);
 8078:                 $datatable .= '<fieldset style="display: inline-block;">'.
 8079:                               '<legend>'.$usertypes->{$item}.'</legend>';
 8080:                 if (ref($postlink{$item}) eq 'ARRAY') {
 8081:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 8082:                 }
 8083:                 foreach my $field ('email','username') {
 8084:                     my $checked;
 8085:                     if ($postlinked{$field}) {
 8086:                         $checked = ' checked="checked"';
 8087:                     }
 8088:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8089:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 8090:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 8091:                                   '<span>&nbsp;&nbsp; ';
 8092:                 }
 8093:                 $datatable .= '</fieldset>';
 8094:             }
 8095:         }
 8096:         if (ref($postlink{'default'}) eq 'ARRAY') {
 8097:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 8098:         }
 8099:         $datatable .= '<fieldset style="display: inline-block;">'.
 8100:                       '<legend>'.$othertitle.'</legend>';
 8101:         foreach my $field ('email','username') {
 8102:             my $checked;
 8103:             if ($postlinked{$field}) {
 8104:                 $checked = ' checked="checked"';
 8105:             }
 8106:             $datatable .= '<span class="LC_nobreak"><label>'.
 8107:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 8108:                           $field.'"'.$checked.' />'.$field.'</label>'.
 8109:                           '<span>&nbsp;&nbsp; ';
 8110:         }
 8111:         $datatable .= '</fieldset></td></tr>';
 8112:         $itemcount ++;
 8113:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8114:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 8115:                       '<td class="LC_left_item">';
 8116:         foreach my $type ('permanent','critical','notify') {
 8117:             my $checkedemail;
 8118:             if ($emailsrc{$type}) {
 8119:                 $checkedemail = ' checked="checked"';
 8120:             }
 8121:             $datatable .= '<span class="LC_nobreak"><label>'.
 8122:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 8123:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 8124:                           '<span>&nbsp;&nbsp; ';
 8125:         }
 8126:         $datatable .= '</td></tr>';
 8127:         $itemcount ++;
 8128:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8129:         my $switchserver = &check_switchserver($dom,$confname);
 8130:         my ($showstd,$noshowstd);
 8131:         if ($nostdtext) {
 8132:             $noshowstd = ' checked="checked"';
 8133:         } else {
 8134:             $showstd = ' checked="checked"';
 8135:         }
 8136:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 8137:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8138:                       &mt('Retain standard text:').
 8139:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 8140:                       &mt('Yes').'</label>'.'&nbsp;'.
 8141:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 8142:                       &mt('No').'</label></span><br />'.
 8143:                       '<span class="LC_fontsize_small">'.
 8144:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 8145:                       &mt('Include custom text:');
 8146:         if ($customurl) {
 8147:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 8148:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8149:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 8150:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 8151:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 8152:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 8153:         }
 8154:         if ($switchserver) {
 8155:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 8156:         } else {
 8157:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8158:                          '<input type="file" name="passwords_customfile" /></span>';
 8159:         }
 8160:         $datatable .= '</td></tr>';
 8161:     } elsif ($position eq 'middle') {
 8162:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 8163:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 8164:         my %defaults;
 8165:         if (ref($domconf{'defaults'}) eq 'HASH') {
 8166:             %defaults = %{$domconf{'defaults'}};
 8167:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 8168:                 $defaults{'intauth_cost'} = 10;
 8169:             }
 8170:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 8171:                 $defaults{'intauth_check'} = 0;
 8172:             }
 8173:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 8174:                 $defaults{'intauth_switch'} = 0;
 8175:             }
 8176:         } else {
 8177:             %defaults = (
 8178:                           'intauth_cost'   => 10,
 8179:                           'intauth_check'  => 0,
 8180:                           'intauth_switch' => 0,
 8181:                         );
 8182:         }
 8183:         foreach my $item (@items) {
 8184:             if ($itemcount%2) {
 8185:                 $css_class = '';
 8186:             } else {
 8187:                 $css_class = ' class="LC_odd_row" ';
 8188:             }
 8189:             $datatable .= '<tr'.$css_class.'>'.
 8190:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 8191:                           '</span></td><td class="LC_left_item" colspan="3">';
 8192:             if ($item eq 'intauth_switch') {
 8193:                 my @options = (0,1,2);
 8194:                 my %optiondesc = &Apache::lonlocal::texthash (
 8195:                                    0 => 'No',
 8196:                                    1 => 'Yes',
 8197:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 8198:                                  );
 8199:                 $datatable .= '<table width="100%">';
 8200:                 foreach my $option (@options) {
 8201:                     my $checked = ' ';
 8202:                     if ($defaults{$item} eq $option) {
 8203:                         $checked = ' checked="checked"';
 8204:                     }
 8205:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8206:                                   '<label><input type="radio" name="'.$item.
 8207:                                   '" value="'.$option.'"'.$checked.' />'.
 8208:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8209:                 }
 8210:                 $datatable .= '</table>';
 8211:             } elsif ($item eq 'intauth_check') {
 8212:                 my @options = (0,1,2);
 8213:                 my %optiondesc = &Apache::lonlocal::texthash (
 8214:                                    0 => 'No',
 8215:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 8216:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 8217:                                  );
 8218:                 $datatable .= '<table width="100%">';
 8219:                 foreach my $option (@options) {
 8220:                     my $checked = ' ';
 8221:                     my $onclick;
 8222:                     if ($defaults{$item} eq $option) {
 8223:                         $checked = ' checked="checked"';
 8224:                     }
 8225:                     if ($option == 2) {
 8226:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 8227:                     }
 8228:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8229:                                   '<label><input type="radio" name="'.$item.
 8230:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 8231:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8232:                 }
 8233:                 $datatable .= '</table>';
 8234:             } else {
 8235:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8236:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 8237:             }
 8238:             $datatable .= '</td></tr>';
 8239:             $itemcount ++;
 8240:         }
 8241:     } elsif ($position eq 'lower') {
 8242:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 8243:     } else {
 8244:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8245:         my %ownerchg = (
 8246:                           by  => {},
 8247:                           for => {},
 8248:                        );
 8249:         my %ownertitles = &Apache::lonlocal::texthash (
 8250:                             by  => 'Course owner status(es) allowed',
 8251:                             for => 'Student status(es) allowed',
 8252:                           );
 8253:         if (ref($settings) eq 'HASH') {
 8254:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 8255:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 8256:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 8257:                 }
 8258:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 8259:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 8260:                 }
 8261:             }
 8262:         }
 8263:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8264:         $datatable .= '<tr '.$css_class.'>'.
 8265:                       '<td>'.
 8266:                       &mt('Requirements').'<ul>'.
 8267:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 8268:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 8269:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 8270:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 8271:                       '</ul>'.
 8272:                       '</td>'.
 8273:                       '<td class="LC_left_item">';
 8274:         foreach my $item ('by','for') {
 8275:             $datatable .= '<fieldset style="display: inline-block;">'.
 8276:                           '<legend>'.$ownertitles{$item}.'</legend>';
 8277:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8278:                 foreach my $type (@{$types}) {
 8279:                     my $checked;
 8280:                     if ($ownerchg{$item}{$type}) {
 8281:                         $checked = ' checked="checked"';
 8282:                     }
 8283:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8284:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 8285:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 8286:                                   '</span>&nbsp;&nbsp; ';
 8287:                 }
 8288:             }
 8289:             my $checked;
 8290:             if ($ownerchg{$item}{'default'}) {
 8291:                 $checked = ' checked="checked"';
 8292:             }
 8293:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8294:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 8295:                           $othertitle.'</label></span></fieldset>';
 8296:         }
 8297:         $datatable .= '</td></tr>';
 8298:     }
 8299:     return $datatable;
 8300: }
 8301: 
 8302: sub password_rules {
 8303:     my ($prefix,$itemcountref,$settings) = @_;
 8304:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 8305:     my %titles;
 8306:     if ($prefix eq 'passwords') {
 8307:         %titles = &Apache::lonlocal::texthash (
 8308:             min            => 'Minimum password length',
 8309:             max            => 'Maximum password length',
 8310:             chars          => 'Required characters',
 8311:         );
 8312:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 8313:         %titles = &Apache::lonlocal::texthash (
 8314:             min            => 'Minimum secret length',
 8315:             max            => 'Maximum secret length',
 8316:             chars          => 'Required characters',
 8317:         );
 8318:     }
 8319:     $min = $Apache::lonnet::passwdmin;
 8320:     my $datatable;
 8321:     my $itemcount;
 8322:     if (ref($itemcountref)) {
 8323:         $itemcount = $$itemcountref;
 8324:     }
 8325:     if (ref($settings) eq 'HASH') {
 8326:         if ($settings->{min}) {
 8327:             $min = $settings->{min};
 8328:         }
 8329:         if ($settings->{max}) {
 8330:             $max = $settings->{max};
 8331:         }
 8332:         if (ref($settings->{chars}) eq 'ARRAY') {
 8333:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 8334:         }
 8335:         if ($prefix eq 'passwords') {
 8336:             if ($settings->{expire}) {
 8337:                 $expire = $settings->{expire};
 8338:             }
 8339:             if ($settings->{numsaved}) {
 8340:                 $numsaved = $settings->{numsaved};
 8341:             }
 8342:         }
 8343:     }
 8344:     my %rulenames = &Apache::lonlocal::texthash(
 8345:                                                  uc => 'At least one upper case letter',
 8346:                                                  lc => 'At least one lower case letter',
 8347:                                                  num => 'At least one number',
 8348:                                                  spec => 'At least one non-alphanumeric',
 8349:                                                );
 8350:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8351:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 8352:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8353:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 8354:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8355:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 8356:                   '</span></td></tr>';
 8357:     $itemcount ++;
 8358:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8359:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 8360:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8361:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 8362:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8363:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 8364:                   '</span></td></tr>';
 8365:     $itemcount ++;
 8366:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8367:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 8368:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 8369:                   '</span></td>';
 8370:     my $numinrow = 2;
 8371:     my @possrules = ('uc','lc','num','spec');
 8372:     $datatable .= '<td class="LC_left_item"><table>';
 8373:     for (my $i=0; $i<@possrules; $i++) {
 8374:         my ($rem,$checked);
 8375:         if ($chars{$possrules[$i]}) {
 8376:             $checked = ' checked="checked"';
 8377:         }
 8378:         $rem = $i%($numinrow);
 8379:         if ($rem == 0) {
 8380:             if ($i > 0) {
 8381:                 $datatable .= '</tr>';
 8382:             }
 8383:             $datatable .= '<tr>';
 8384:         }
 8385:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 8386:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 8387:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 8388:     }
 8389:     my $rem = @possrules%($numinrow);
 8390:     my $colsleft = $numinrow - $rem;
 8391:     if ($colsleft > 1 ) {
 8392:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8393:                       '&nbsp;</td>';
 8394:     } elsif ($colsleft == 1) {
 8395:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8396:     }
 8397:     $datatable .='</table></td></tr>';
 8398:     $itemcount ++;
 8399:     if ($prefix eq 'passwords') {
 8400:         $titles{'expire'} = &mt('Password expiration (days)');
 8401:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 8402:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8403:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 8404:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8405:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 8406:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8407:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 8408:                       '</span></td></tr>';
 8409:         $itemcount ++;
 8410:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8411:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 8412:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8413:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 8414:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8415:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 8416:                       '</span></td></tr>';
 8417:         $itemcount ++;
 8418:     }
 8419:     if (ref($itemcountref)) {
 8420:         $$itemcountref += $itemcount;
 8421:     }
 8422:     return $datatable;
 8423: }
 8424: 
 8425: sub print_wafproxy {
 8426:     my ($position,$dom,$settings,$rowtotal) = @_;
 8427:     my $css_class;
 8428:     my $itemcount = 0;
 8429:     my $datatable;
 8430:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8431:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8432:     my %lt = &wafproxy_titles();
 8433:     foreach my $server (sort(keys(%servers))) {
 8434:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8435:         next if ($serverhome eq '');
 8436:         my $serverdom;
 8437:         if ($serverhome ne $server) {
 8438:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8439:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8440:                 $othercontrol{$server} = $serverdom;
 8441:             }
 8442:         } else {
 8443:             $serverdom = &Apache::lonnet::host_domain($server);
 8444:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8445:             if ($serverdom ne $dom) {
 8446:                 $othercontrol{$server} = $serverdom;
 8447:             } else {
 8448:                 $setdom = 1;
 8449:                 if (ref($settings) eq 'HASH') {
 8450:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8451:                         $aliases{$dom} = $settings->{'alias'};
 8452:                         if ($aliases{$dom} ne '') {
 8453:                             $showdom = 1;
 8454:                         }
 8455:                     }
 8456:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8457:                         $saml{$dom} = $settings->{'saml'};
 8458:                     }
 8459:                 }
 8460:             }
 8461:         }
 8462:     }
 8463:     if ($setdom) {
 8464:         %{$values{$dom}} = ();
 8465:         if (ref($settings) eq 'HASH') {
 8466:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8467:                 $values{$dom}{$item} = $settings->{$item};
 8468:             }
 8469:         }
 8470:     }
 8471:     if (keys(%othercontrol)) {
 8472:         %otherdoms = reverse(%othercontrol);
 8473:         foreach my $domain (keys(%otherdoms)) {
 8474:             %{$values{$domain}} = ();
 8475:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8476:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8477:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8478:                 if (exists($config{'wafproxy'}{'saml'})) {
 8479:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8480:                 }
 8481:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8482:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8483:                 }
 8484:             }
 8485:         }
 8486:     }
 8487:     if ($position eq 'top') {
 8488:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8489:         my %aliasinfo;
 8490:         foreach my $server (sort(keys(%servers))) {
 8491:             $itemcount ++;
 8492:             my $dom_in_effect;
 8493:             my $aliasrows = '<tr>'.
 8494:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8495:                             &mt('Hostname').': '.
 8496:                             '<span class="LC_nobreak LC_cusr_emph">'.
 8497:                             &Apache::lonnet::hostname($server).
 8498:                             '</span></td><td>&nbsp;</td>';
 8499:             if ($othercontrol{$server}) {
 8500:                 $dom_in_effect = $othercontrol{$server};
 8501:                 my ($current,$forsaml);
 8502:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8503:                     $current = $aliases{$dom_in_effect}{$server};
 8504:                 }
 8505:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8506:                     if ($saml{$dom_in_effect}{$server}) {
 8507:                         $forsaml = 1;
 8508:                     }
 8509:                 }
 8510:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8511:                               &mt('Alias').':&nbsp';
 8512:                 if ($current) {
 8513:                     $aliasrows .= $current;
 8514:                     if ($forsaml) {
 8515:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8516:                     }
 8517:                 } else {
 8518:                     $aliasrows .= &mt('None');
 8519:                 }
 8520:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8521:                               &mt('controlled by domain: [_1]',
 8522:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8523:             } else {
 8524:                 $dom_in_effect = $dom;
 8525:                 my ($current,$samlon,$samloff);
 8526:                 $samloff = ' checked="checked"';
 8527:                 if (ref($aliases{$dom}) eq 'HASH') {
 8528:                     if ($aliases{$dom}{$server}) {
 8529:                         $current = $aliases{$dom}{$server};
 8530:                     }
 8531:                 }
 8532:                 if (ref($saml{$dom}) eq 'HASH') {
 8533:                     if ($saml{$dom}{$server}) {
 8534:                         $samlon = $samloff;
 8535:                         undef($samloff);
 8536:                     }
 8537:                 }
 8538:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8539:                               &mt('Alias').':&nbsp;'.
 8540:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8541:                               'value="'.$current.'" size="30" />'.
 8542:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8543:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8544:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8545:                               &mt('No').'</label>&nbsp;<label>'.
 8546:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8547:                               &mt('Yes').'</label></span>'.
 8548:                               '</td>';
 8549:             }
 8550:             $aliasrows .= '</tr>';
 8551:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8552:         }
 8553:         if ($aliasinfo{$dom}) {
 8554:             my ($onclick,$wafon,$wafoff,$showtable);
 8555:             $onclick = ' onclick="javascript:toggleWAF();"';
 8556:             $wafoff = ' checked="checked"';
 8557:             $showtable = ' style="display:none";';
 8558:             if ($showdom) {
 8559:                 $wafon = $wafoff;
 8560:                 $wafoff = '';
 8561:                 $showtable = ' style="display:inline;"';
 8562:             }
 8563:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8564:             $datatable = '<tr'.$css_class.'>'.
 8565:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8566:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8567:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8568:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8569:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8570:                          &mt('No').'</label></span></td>'.
 8571:                          '<td class="LC_left_item">'.
 8572:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8573:                          '</table></td></tr>';
 8574:             $itemcount++;
 8575:         }
 8576:         if (keys(%otherdoms)) {
 8577:             foreach my $key (sort(keys(%otherdoms))) {
 8578:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8579:                 $datatable .= '<tr'.$css_class.'>'.
 8580:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8581:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8582:                               '</table></td></tr>';
 8583:                 $itemcount++;
 8584:             }
 8585:         }
 8586:     } else {
 8587:         my %ip_methods = &remoteip_methods();
 8588:         if ($setdom) {
 8589:             $itemcount ++;
 8590:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8591:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8592:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8593:             $wafstyle = ' style="display:none;"';
 8594:             $nowafstyle = ' style="display:table-row;"';
 8595:             $currwafdisplay = ' style="display: none"';
 8596:             $wafrangestyle = ' style="display: none"';
 8597:             $curr_remotip = 'n';
 8598:             $ssltossl = ' checked="checked"';
 8599:             if ($showdom) {
 8600:                 $wafstyle = ' style="display:table-row;"';
 8601:                 $nowafstyle =  ' style="display:none;"';
 8602:                 if (keys(%{$values{$dom}})) {
 8603:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8604:                         $curr_remotip = $values{$dom}{remoteip};
 8605:                     }
 8606:                     if ($curr_remotip eq 'h') {
 8607:                         $currwafdisplay = ' style="display:table-row"';
 8608:                         $wafrangestyle = ' style="display:inline-block;"';
 8609:                     }
 8610:                     if ($values{$dom}{'sslopt'}) {
 8611:                         $alltossl = ' checked="checked"';
 8612:                         $ssltossl = '';
 8613:                     }
 8614:                 }
 8615:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8616:                     $vpndircheck = ' checked="checked"';
 8617:                     $currwafvpn = ' style="display:table-row;"';
 8618:                     $wafrangestyle = ' style="display:inline-block;"';
 8619:                 } else {
 8620:                     $vpnaliascheck = ' checked="checked"';
 8621:                     $currwafvpn = ' style="display:none;"';
 8622:                 }
 8623:             }
 8624:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8625:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8626:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8627:                           '</tr>'.
 8628:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8629:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8630:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8631:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8632:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8633:                           '<td class="LC_left_item"><table>'.
 8634:                           '<tr>'.
 8635:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8636:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8637:             foreach my $option ('m','h','n') {
 8638:                 my $sel;
 8639:                 if ($option eq $curr_remotip) {
 8640:                    $sel = ' selected="selected"';
 8641:                 }
 8642:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8643:                               $ip_methods{$option}.'</option>';
 8644:             }
 8645:             $datatable .= '</select></td></tr>'."\n".
 8646:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8647:                           $lt{'ipheader'}.':&nbsp;'.
 8648:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8649:                           'name="wafproxy_ipheader" />'.
 8650:                           '</td></tr>'."\n".
 8651:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8652:                           $lt{'trusted'}.':<br />'.
 8653:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8654:                           $values{$dom}{'trusted'}.'</textarea>'.
 8655:                           '</td></tr>'."\n".
 8656:                           '<tr><td><hr /></td></tr>'."\n".
 8657:                           '<tr>'.
 8658:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8659:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8660:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8661:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8662:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8663:             foreach my $item ('vpnint','vpnext') {
 8664:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8665:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8666:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8667:                               $values{$dom}{$item}.'</textarea>'.
 8668:                               '</td></tr>'."\n";
 8669:             }
 8670:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8671:                           '<tr>'.
 8672:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8673:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8674:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8675:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8676:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8677:                           '</table></td></tr>';
 8678:         }
 8679:         if (keys(%otherdoms)) {
 8680:             foreach my $domain (sort(keys(%otherdoms))) {
 8681:                 $itemcount ++;
 8682:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8683:                 $datatable .= '<tr'.$css_class.'>'.
 8684:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8685:                               '<td class="LC_left_item"><table>';
 8686:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8687:                     my $showval = &mt('None');
 8688:                     if ($item eq 'ssl') {
 8689:                         $showval = $lt{'ssltossl'};
 8690:                     }
 8691:                     if ($values{$domain}{$item}) {
 8692:                         $showval = $values{$domain}{$item};
 8693:                         if ($item eq 'ssl') {
 8694:                             $showval = $lt{'alltossl'};
 8695:                         } elsif ($item eq 'remoteip') {
 8696:                             $showval = $ip_methods{$values{$domain}{$item}};
 8697:                         }
 8698:                     }
 8699:                     $datatable .= '<tr>'.
 8700:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8701:                 }
 8702:                 $datatable .= '</table></td></tr>';
 8703:             }
 8704:         }
 8705:     }
 8706:     $$rowtotal += $itemcount;
 8707:     return $datatable;
 8708: }
 8709: 
 8710: sub wafproxy_titles {
 8711:     return &Apache::lonlocal::texthash(
 8712:                remoteip   => "Method for determining user's IP",
 8713:                ipheader   => 'Request header containing remote IP',
 8714:                trusted    => 'Trusted IP range(s)',
 8715:                vpnaccess  => 'Access from institutional VPN',
 8716:                vpndirect  => 'via regular hostname (no WAF)',
 8717:                vpnaliased => 'via aliased hostname (WAF)',
 8718:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8719:                vpnext     => 'IP Range(s) for backend WAF connections',
 8720:                sslopt     => 'Forwarding http/https',
 8721:                alltossl   => 'WAF forwards both http and https requests to https',
 8722:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8723:            );
 8724: }
 8725: 
 8726: sub remoteip_methods {
 8727:     return &Apache::lonlocal::texthash(
 8728:               m => 'Use Apache mod_remoteip',
 8729:               h => 'Use headers parsed by LON-CAPA',
 8730:               n => 'Not in use',
 8731:            );
 8732: }
 8733: 
 8734: sub print_usersessions {
 8735:     my ($position,$dom,$settings,$rowtotal) = @_;
 8736:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8737:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8738:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8739: 
 8740:     my @alldoms = &Apache::lonnet::all_domains();
 8741:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8742:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8743:     my %altids = &id_for_thisdom(%servers);
 8744:     if ($position eq 'top') {
 8745:         if (keys(%serverhomes) > 1) {
 8746:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8747:             my ($curroffloadnow,$curroffloadoth);
 8748:             if (ref($settings) eq 'HASH') {
 8749:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8750:                     $curroffloadnow = $settings->{'offloadnow'};
 8751:                 }
 8752:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8753:                     $curroffloadoth = $settings->{'offloadoth'};
 8754:                 }
 8755:             }
 8756:             my $other_insts = scalar(keys(%by_location));
 8757:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8758:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8759:         } else {
 8760:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8761:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8762:                           '</td></tr>';
 8763:         }
 8764:     } else {
 8765:         my %titles = &usersession_titles();
 8766:         my ($prefix,@types);
 8767:         if ($position eq 'bottom') {
 8768:             $prefix = 'remote';
 8769:             @types = ('version','excludedomain','includedomain');
 8770:         } else {
 8771:             $prefix = 'hosted';
 8772:             @types = ('excludedomain','includedomain');
 8773:         }
 8774:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8775:     }
 8776:     $$rowtotal += $itemcount;
 8777:     return $datatable;
 8778: }
 8779: 
 8780: sub rules_by_location {
 8781:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8782:     my ($datatable,$itemcount,$css_class);
 8783:     if (keys(%{$by_location}) == 0) {
 8784:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8785:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8786:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8787:                      '</td></tr>';
 8788:         $itemcount = 1;
 8789:     } else {
 8790:         $itemcount = 0;
 8791:         my $numinrow = 5;
 8792:         my (%current,%checkedon,%checkedoff);
 8793:         my @locations = sort(keys(%{$by_location}));
 8794:         foreach my $type (@{$types}) {
 8795:             $checkedon{$type} = '';
 8796:             $checkedoff{$type} = ' checked="checked"';
 8797:         }
 8798:         if (ref($settings) eq 'HASH') {
 8799:             if (ref($settings->{$prefix}) eq 'HASH') {
 8800:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8801:                     $current{$key} = $settings->{$prefix}{$key};
 8802:                     if ($key eq 'version') {
 8803:                         if ($current{$key} ne '') {
 8804:                             $checkedon{$key} = ' checked="checked"';
 8805:                             $checkedoff{$key} = '';
 8806:                         }
 8807:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8808:                         $checkedon{$key} = ' checked="checked"';
 8809:                         $checkedoff{$key} = '';
 8810:                     }
 8811:                 }
 8812:             }
 8813:         }
 8814:         foreach my $type (@{$types}) {
 8815:             next if ($type ne 'version' && !@locations);
 8816:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8817:             $datatable .= '<tr'.$css_class.'>
 8818:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8819:                            <span class="LC_nobreak">&nbsp;
 8820:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8821:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8822:             if ($type eq 'version') {
 8823:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8824:                 my $selector = '<select name="'.$prefix.'_version">';
 8825:                 foreach my $version (@lcversions) {
 8826:                     my $selected = '';
 8827:                     if ($current{'version'} eq $version) {
 8828:                         $selected = ' selected="selected"';
 8829:                     }
 8830:                     $selector .= ' <option value="'.$version.'"'.
 8831:                                  $selected.'>'.$version.'</option>';
 8832:                 }
 8833:                 $selector .= '</select> ';
 8834:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8835:             } else {
 8836:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8837:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8838:                              ' />'.('&nbsp;'x2).
 8839:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8840:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8841:                              "\n".
 8842:                              '</div><div><table>';
 8843:                 my $rem;
 8844:                 for (my $i=0; $i<@locations; $i++) {
 8845:                     my ($showloc,$value,$checkedtype);
 8846:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8847:                         my $ip = $by_location->{$locations[$i]}->[0];
 8848:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8849:                             $value = join(':',@{$by_ip->{$ip}});
 8850:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8851:                             if (ref($current{$type}) eq 'ARRAY') {
 8852:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8853:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8854:                                         $checkedtype = ' checked="checked"';
 8855:                                         last;
 8856:                                     }
 8857:                                 }
 8858:                             }
 8859:                         }
 8860:                     }
 8861:                     $rem = $i%($numinrow);
 8862:                     if ($rem == 0) {
 8863:                         if ($i > 0) {
 8864:                             $datatable .= '</tr>';
 8865:                         }
 8866:                         $datatable .= '<tr>';
 8867:                     }
 8868:                     $datatable .= '<td class="LC_left_item">'.
 8869:                                   '<span class="LC_nobreak"><label>'.
 8870:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8871:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8872:                                   '</label></span></td>';
 8873:                 }
 8874:                 $rem = @locations%($numinrow);
 8875:                 my $colsleft = $numinrow - $rem;
 8876:                 if ($colsleft > 1 ) {
 8877:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8878:                                   '&nbsp;</td>';
 8879:                 } elsif ($colsleft == 1) {
 8880:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8881:                 }
 8882:                 $datatable .= '</tr></table>';
 8883:             }
 8884:             $datatable .= '</td></tr>';
 8885:             $itemcount ++;
 8886:         }
 8887:     }
 8888:     return ($datatable,$itemcount);
 8889: }
 8890: 
 8891: sub print_ssl {
 8892:     my ($position,$dom,$settings,$rowtotal) = @_;
 8893:     my ($css_class,$datatable);
 8894:     my $itemcount = 1;
 8895:     if ($position eq 'top') {
 8896:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8897:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8898:         my $same_institution;
 8899:         if ($intdom ne '') {
 8900:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8901:             if (ref($internet_names) eq 'ARRAY') {
 8902:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8903:                     $same_institution = 1;
 8904:                 }
 8905:             }
 8906:         }
 8907:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8908:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8909:         if ($same_institution) {
 8910:             my %domservers = &Apache::lonnet::get_servers($dom);
 8911:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8912:         } else {
 8913:             $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.");
 8914:         }
 8915:         $datatable .= '</td></tr>';
 8916:         $itemcount ++;
 8917:     } else {
 8918:         my %titles = &ssl_titles();
 8919:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8920:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8921:         my @alldoms = &Apache::lonnet::all_domains();
 8922:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8923:         my @domservers = &Apache::lonnet::get_servers($dom);
 8924:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8925:         my %altids = &id_for_thisdom(%servers);
 8926:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8927:             my $legacy;
 8928:             unless (ref($settings) eq 'HASH') {
 8929:                 my $name;
 8930:                 if ($position eq 'connto') {
 8931:                     $name = 'loncAllowInsecure';
 8932:                 } else {
 8933:                     $name = 'londAllowInsecure';
 8934:                 }
 8935:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8936:                 my @ids=&Apache::lonnet::current_machine_ids();
 8937:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8938:                     my %what = (
 8939:                                    $name => 1,
 8940:                                );
 8941:                     my ($result,$returnhash) =
 8942:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8943:                     if ($result eq 'ok') {
 8944:                         if (ref($returnhash) eq 'HASH') {
 8945:                             $legacy = $returnhash->{$name};
 8946:                         }
 8947:                     }
 8948:                 } else {
 8949:                     $legacy = $Apache::lonnet::perlvar{$name};
 8950:                 }
 8951:             }
 8952:             foreach my $type ('dom','intdom','other') {
 8953:                 my %checked;
 8954:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8955:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8956:                               '<td class="LC_right_item">';
 8957:                 my $skip; 
 8958:                 if ($type eq 'dom') {
 8959:                     unless (keys(%servers) > 1) {
 8960:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8961:                         $skip = 1;
 8962:                     }
 8963:                 }
 8964:                 if ($type eq 'intdom') {
 8965:                     unless (@instdoms > 1) {
 8966:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8967:                         $skip = 1;
 8968:                     } 
 8969:                 } elsif ($type eq 'other') {
 8970:                     if (keys(%by_location) == 0) {
 8971:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8972:                         $skip = 1;
 8973:                     }
 8974:                 }
 8975:                 unless ($skip) {
 8976:                     $checked{'yes'} = ' checked="checked"'; 
 8977:                     if (ref($settings) eq 'HASH') {
 8978:                         if (ref($settings->{$position}) eq 'HASH') {
 8979:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8980:                                 $checked{$1} = $checked{'yes'};
 8981:                                 delete($checked{'yes'}); 
 8982:                             }
 8983:                         }
 8984:                     } else {
 8985:                         if ($legacy == 0) {
 8986:                             $checked{'req'} = $checked{'yes'};
 8987:                             delete($checked{'yes'});    
 8988:                         }
 8989:                     }
 8990:                     foreach my $option ('no','yes','req') {
 8991:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8992:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8993:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8994:                                       '</label></span>'.('&nbsp;'x2);
 8995:                     }
 8996:                 }
 8997:                 $datatable .= '</td></tr>';
 8998:                 $itemcount ++; 
 8999:             }
 9000:         } else {
 9001:             my $prefix = 'replication';
 9002:             my @types = ('certreq','nocertreq');
 9003:             if (keys(%by_location) == 0) {
 9004:                 $datatable .= '<tr'.$css_class.'><td>'.
 9005:                               &mt('Nothing to set here, as there are no other institutions').
 9006:                               '</td></tr>';
 9007:                 $itemcount ++;
 9008:             } else {
 9009:                 ($datatable,$itemcount) = 
 9010:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9011:             }
 9012:         }
 9013:     }
 9014:     $$rowtotal += $itemcount;
 9015:     return $datatable;
 9016: }
 9017: 
 9018: sub ssl_titles {
 9019:     return &Apache::lonlocal::texthash (
 9020:                dom           => 'LON-CAPA servers/VMs from same domain',
 9021:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 9022:                other         => 'External LON-CAPA servers/VMs',
 9023:                connto        => 'Connections to other servers',
 9024:                connfrom      => 'Connections from other servers',
 9025:                replication   => 'Replicating content to other institutions',
 9026:                certreq       => 'Client certificate required, but specific domains exempt',
 9027:                nocertreq     => 'No client certificate required, except for specific domains',
 9028:                no            => 'SSL not used',
 9029:                yes           => 'SSL Optional (used if available)',
 9030:                req           => 'SSL Required',
 9031:     );
 9032: }
 9033: 
 9034: sub print_trust {
 9035:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 9036:     my ($css_class,$datatable,%checked,%choices);
 9037:     my (%by_ip,%by_location,@intdoms,@instdoms);
 9038:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 9039:     my $itemcount = 1;
 9040:     my %titles = &trust_titles();
 9041:     my @types = ('exc','inc');
 9042:     if ($prefix eq 'top') {
 9043:         $prefix = 'content';
 9044:     } elsif ($prefix eq 'bottom') {
 9045:         $prefix = 'msg';
 9046:     }
 9047:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9048:     $$rowtotal += $itemcount;
 9049:     return $datatable;
 9050: }
 9051: 
 9052: sub trust_titles {
 9053:     return &Apache::lonlocal::texthash(
 9054:                content  => "Access to this domain's content by others",
 9055:                shared   => "Access to other domain's content by this domain",
 9056:                enroll   => "Enrollment in this domain's courses by others", 
 9057:                othcoau  => "Co-author roles in this domain for others",
 9058:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 9059:                domroles => "Domain roles in this domain assignable to others",
 9060:                catalog  => "Course Catalog for this domain displayed elsewhere",
 9061:                reqcrs   => "Requests for creation of courses in this domain by others",
 9062:                msg      => "Users in other domains can send messages to this domain",
 9063:                exc      => "Allow all, but exclude specific domains",
 9064:                inc      => "Deny all, but include specific domains",
 9065:            );
 9066: } 
 9067: 
 9068: sub build_location_hashes {
 9069:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 9070:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 9071:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 9072:     my %iphost = &Apache::lonnet::get_iphost();
 9073:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 9074:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 9075:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 9076:         foreach my $id (@{$iphost{$primary_ip}}) {
 9077:             my $intdom = &Apache::lonnet::internet_dom($id);
 9078:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 9079:                 push(@{$intdoms},$intdom);
 9080:             }
 9081:         }
 9082:     }
 9083:     foreach my $ip (keys(%iphost)) {
 9084:         if (ref($iphost{$ip}) eq 'ARRAY') {
 9085:             foreach my $id (@{$iphost{$ip}}) {
 9086:                 my $location = &Apache::lonnet::internet_dom($id);
 9087:                 if ($location) {
 9088:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 9089:                         my $dom = &Apache::lonnet::host_domain($id);
 9090:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 9091:                             push(@{$instdoms},$dom);
 9092:                         }
 9093:                         next;
 9094:                     }
 9095:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9096:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 9097:                             push(@{$by_ip->{$ip}},$location);
 9098:                         }
 9099:                     } else {
 9100:                         $by_ip->{$ip} = [$location];
 9101:                     }
 9102:                 }
 9103:             }
 9104:         }
 9105:     }
 9106:     foreach my $ip (sort(keys(%{$by_ip}))) {
 9107:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9108:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 9109:             my $first = $by_ip->{$ip}->[0];
 9110:             if (ref($by_location->{$first}) eq 'ARRAY') {
 9111:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 9112:                     push(@{$by_location->{$first}},$ip);
 9113:                 }
 9114:             } else {
 9115:                 $by_location->{$first} = [$ip];
 9116:             }
 9117:         }
 9118:     }
 9119:     return;
 9120: }
 9121: 
 9122: sub current_offloads_to {
 9123:     my ($dom,$settings,$servers) = @_;
 9124:     my (%spareid,%otherdomconfigs);
 9125:     if (ref($servers) eq 'HASH') {
 9126:         foreach my $lonhost (sort(keys(%{$servers}))) {
 9127:             my $gotspares;
 9128:             if (ref($settings) eq 'HASH') {
 9129:                 if (ref($settings->{'spares'}) eq 'HASH') {
 9130:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 9131:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 9132:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 9133:                         $gotspares = 1;
 9134:                     }
 9135:                 }
 9136:             }
 9137:             unless ($gotspares) {
 9138:                 my $gotspares;
 9139:                 my $serverhomeID =
 9140:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 9141:                 my $serverhomedom =
 9142:                     &Apache::lonnet::host_domain($serverhomeID);
 9143:                 if ($serverhomedom ne $dom) {
 9144:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 9145:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9146:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9147:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9148:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9149:                                 $gotspares = 1;
 9150:                             }
 9151:                         }
 9152:                     } else {
 9153:                         $otherdomconfigs{$serverhomedom} =
 9154:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 9155:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 9156:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9157:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9158:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 9159:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9160:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9161:                                         $gotspares = 1;
 9162:                                     }
 9163:                                 }
 9164:                             }
 9165:                         }
 9166:                     }
 9167:                 }
 9168:             }
 9169:             unless ($gotspares) {
 9170:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9171:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9172:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9173:                } else {
 9174:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 9175:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 9176:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9177:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9178:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9179:                     } else {
 9180:                         my %what = (
 9181:                              spareid => 1,
 9182:                         );
 9183:                         my ($result,$returnhash) = 
 9184:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 9185:                         if ($result eq 'ok') { 
 9186:                             if (ref($returnhash) eq 'HASH') {
 9187:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 9188:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 9189:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 9190:                                 }
 9191:                             }
 9192:                         }
 9193:                     }
 9194:                 }
 9195:             }
 9196:         }
 9197:     }
 9198:     return %spareid;
 9199: }
 9200: 
 9201: sub spares_row {
 9202:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 9203:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 9204:     my $css_class;
 9205:     my $numinrow = 4;
 9206:     my $itemcount = 1;
 9207:     my $datatable;
 9208:     my %typetitles = &sparestype_titles();
 9209:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9210:         foreach my $server (sort(keys(%{$servers}))) {
 9211:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 9212:             my ($othercontrol,$serverdom);
 9213:             if ($serverhome ne $server) {
 9214:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 9215:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9216:             } else {
 9217:                 $serverdom = &Apache::lonnet::host_domain($server);
 9218:                 if ($serverdom ne $dom) {
 9219:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9220:                 }
 9221:             }
 9222:             next unless (ref($spareid->{$server}) eq 'HASH');
 9223:             my ($checkednow,$checkedoth);
 9224:             if (ref($curroffloadnow) eq 'HASH') {
 9225:                 if ($curroffloadnow->{$server}) {
 9226:                     $checkednow = ' checked="checked"';
 9227:                 }
 9228:             }
 9229:             if (ref($curroffloadoth) eq 'HASH') {
 9230:                 if ($curroffloadoth->{$server}) {
 9231:                     $checkedoth = ' checked="checked"';
 9232:                 }
 9233:             }
 9234:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 9235:             $datatable .= '<tr'.$css_class.'>
 9236:                            <td rowspan="2">
 9237:                             <span class="LC_nobreak">'.
 9238:                           &mt('[_1] when busy, offloads to:'
 9239:                               ,'<b>'.$server.'</b>').'</span><br />'.
 9240:                           '<span class="LC_nobreak">'."\n".
 9241:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 9242:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 9243:                           "\n";
 9244:             if ($other_insts) {
 9245:                 $datatable .= '<br />'.
 9246:                               '<span class="LC_nobreak">'."\n".
 9247:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 9248:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 9249:                           "\n";
 9250:             }
 9251:             my (%current,%canselect);
 9252:             my @choices = 
 9253:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 9254:             foreach my $type ('primary','default') {
 9255:                 if (ref($spareid->{$server}) eq 'HASH') {
 9256:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 9257:                         my @spares = @{$spareid->{$server}{$type}};
 9258:                         if (@spares > 0) {
 9259:                             if ($othercontrol) {
 9260:                                 $current{$type} = join(', ',@spares);
 9261:                             } else {
 9262:                                 $current{$type} .= '<table>';
 9263:                                 my $numspares = scalar(@spares);
 9264:                                 for (my $i=0;  $i<@spares; $i++) {
 9265:                                     my $rem = $i%($numinrow);
 9266:                                     if ($rem == 0) {
 9267:                                         if ($i > 0) {
 9268:                                             $current{$type} .= '</tr>';
 9269:                                         }
 9270:                                         $current{$type} .= '<tr>';
 9271:                                     }
 9272:                                     $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;'.
 9273:                                                        $spareid->{$server}{$type}[$i].
 9274:                                                        '</label></td>'."\n";
 9275:                                 }
 9276:                                 my $rem = @spares%($numinrow);
 9277:                                 my $colsleft = $numinrow - $rem;
 9278:                                 if ($colsleft > 1 ) {
 9279:                                     $current{$type} .= '<td colspan="'.$colsleft.
 9280:                                                        '" class="LC_left_item">'.
 9281:                                                        '&nbsp;</td>';
 9282:                                 } elsif ($colsleft == 1) {
 9283:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 9284:                                 }
 9285:                                 $current{$type} .= '</tr></table>';
 9286:                             }
 9287:                         }
 9288:                     }
 9289:                     if ($current{$type} eq '') {
 9290:                         $current{$type} = &mt('None specified');
 9291:                     }
 9292:                     if ($othercontrol) {
 9293:                         if ($type eq 'primary') {
 9294:                             $canselect{$type} = $othercontrol;
 9295:                         }
 9296:                     } else {
 9297:                         $canselect{$type} = 
 9298:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 9299:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 9300:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 9301:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 9302:                         if (@choices > 0) {
 9303:                             foreach my $lonhost (@choices) {
 9304:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 9305:                             }
 9306:                         }
 9307:                         $canselect{$type} .= '</select>'."\n";
 9308:                     }
 9309:                 } else {
 9310:                     $current{$type} = &mt('Could not be determined');
 9311:                     if ($type eq 'primary') {
 9312:                         $canselect{$type} =  $othercontrol;
 9313:                     }
 9314:                 }
 9315:                 if ($type eq 'default') {
 9316:                     $datatable .= '<tr'.$css_class.'>';
 9317:                 }
 9318:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 9319:                               '<td>'.$current{$type}.'</td>'."\n".
 9320:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 9321:             }
 9322:             $itemcount ++;
 9323:         }
 9324:     }
 9325:     $$rowtotal += $itemcount;
 9326:     return $datatable;
 9327: }
 9328: 
 9329: sub possible_newspares {
 9330:     my ($server,$currspares,$serverhomes,$altids) = @_;
 9331:     my $serverhostname = &Apache::lonnet::hostname($server);
 9332:     my %excluded;
 9333:     if ($serverhostname ne '') {
 9334:         %excluded = (
 9335:                        $serverhostname => 1,
 9336:                     );
 9337:     }
 9338:     if (ref($currspares) eq 'HASH') {
 9339:         foreach my $type (keys(%{$currspares})) {
 9340:             if (ref($currspares->{$type}) eq 'ARRAY') {
 9341:                 if (@{$currspares->{$type}} > 0) {
 9342:                     foreach my $curr (@{$currspares->{$type}}) {
 9343:                         my $hostname = &Apache::lonnet::hostname($curr);
 9344:                         $excluded{$hostname} = 1;
 9345:                     }
 9346:                 }
 9347:             }
 9348:         }
 9349:     }
 9350:     my @choices;
 9351:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9352:         if (keys(%{$serverhomes}) > 1) {
 9353:             foreach my $name (sort(keys(%{$serverhomes}))) {
 9354:                 unless ($excluded{$name}) {
 9355:                     if (exists($altids->{$serverhomes->{$name}})) {
 9356:                         push(@choices,$altids->{$serverhomes->{$name}});
 9357:                     } else {
 9358:                         push(@choices,$serverhomes->{$name});
 9359:                     }
 9360:                 }
 9361:             }
 9362:         }
 9363:     }
 9364:     return sort(@choices);
 9365: }
 9366: 
 9367: sub print_loadbalancing {
 9368:     my ($dom,$settings,$rowtotal) = @_;
 9369:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9370:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9371:     my $numinrow = 1;
 9372:     my $datatable;
 9373:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9374:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 9375:     if (ref($settings) eq 'HASH') {
 9376:         %existing = %{$settings};
 9377:     }
 9378:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 9379:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9380:                                   \%currtargets,\%currrules,\%currcookies);
 9381:     } else {
 9382:         return;
 9383:     }
 9384:     my ($othertitle,$usertypes,$types) =
 9385:         &Apache::loncommon::sorted_inst_types($dom);
 9386:     my $rownum = 8;
 9387:     if (ref($types) eq 'ARRAY') {
 9388:         $rownum += scalar(@{$types});
 9389:     }
 9390:     my @css_class = ('LC_odd_row','LC_even_row');
 9391:     my $balnum = 0;
 9392:     my $islast;
 9393:     my (@toshow,$disabledtext);
 9394:     if (keys(%currbalancer) > 0) {
 9395:         @toshow = sort(keys(%currbalancer));
 9396:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 9397:             push(@toshow,'');
 9398:         }
 9399:     } else {
 9400:         @toshow = ('');
 9401:         $disabledtext = &mt('No existing load balancer');
 9402:     }
 9403:     foreach my $lonhost (@toshow) {
 9404:         if ($balnum == scalar(@toshow)-1) {
 9405:             $islast = 1;
 9406:         } else {
 9407:             $islast = 0;
 9408:         }
 9409:         my $cssidx = $balnum%2;
 9410:         my $targets_div_style = 'display: none';
 9411:         my $disabled_div_style = 'display: block';
 9412:         my $homedom_div_style = 'display: none';
 9413:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 9414:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9415:                       '<p>';
 9416:         if ($lonhost eq '') {
 9417:             $datatable .= '<span class="LC_nobreak">';
 9418:             if (keys(%currbalancer) > 0) {
 9419:                 $datatable .= &mt('Add balancer:');
 9420:             } else {
 9421:                 $datatable .= &mt('Enable balancer:');
 9422:             }
 9423:             $datatable .= '&nbsp;'.
 9424:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9425:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9426:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9427:                           '<option value="" selected="selected">'.&mt('None').
 9428:                           '</option>'."\n";
 9429:             foreach my $server (sort(keys(%servers))) {
 9430:                 next if ($currbalancer{$server});
 9431:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9432:             }
 9433:             $datatable .=
 9434:                 '</select>'."\n".
 9435:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9436:         } else {
 9437:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9438:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9439:                            &mt('Stop balancing').'</label>'.
 9440:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9441:             $targets_div_style = 'display: block';
 9442:             $disabled_div_style = 'display: none';
 9443:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9444:                 $homedom_div_style = 'display: block';
 9445:             }
 9446:         }
 9447:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9448:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9449:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9450:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9451:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9452:         my @sparestypes = ('primary','default');
 9453:         my %typetitles = &sparestype_titles();
 9454:         my %hostherechecked = (
 9455:                                   no => ' checked="checked"',
 9456:                               );
 9457:         my %balcookiechecked = (
 9458:                                   no => ' checked="checked"',
 9459:                                );
 9460:         foreach my $sparetype (@sparestypes) {
 9461:             my $targettable;
 9462:             for (my $i=0; $i<$numspares; $i++) {
 9463:                 my $checked;
 9464:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9465:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9466:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9467:                             $checked = ' checked="checked"';
 9468:                         }
 9469:                     }
 9470:                 }
 9471:                 my ($chkboxval,$disabled);
 9472:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9473:                     $chkboxval = $spares[$i];
 9474:                 }
 9475:                 if (exists($currbalancer{$spares[$i]})) {
 9476:                     $disabled = ' disabled="disabled"';
 9477:                 }
 9478:                 $targettable .=
 9479:                     '<td><span class="LC_nobreak"><label>'.
 9480:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9481:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9482:                     '</span></label></span></td>';
 9483:                 my $rem = $i%($numinrow);
 9484:                 if ($rem == 0) {
 9485:                     if (($i > 0) && ($i < $numspares-1)) {
 9486:                         $targettable .= '</tr>';
 9487:                     }
 9488:                     if ($i < $numspares-1) {
 9489:                         $targettable .= '<tr>';
 9490:                     }
 9491:                 }
 9492:             }
 9493:             if ($targettable ne '') {
 9494:                 my $rem = $numspares%($numinrow);
 9495:                 my $colsleft = $numinrow - $rem;
 9496:                 if ($colsleft > 1 ) {
 9497:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9498:                                     '&nbsp;</td>';
 9499:                 } elsif ($colsleft == 1) {
 9500:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9501:                 }
 9502:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9503:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9504:             }
 9505:             $hostherechecked{$sparetype} = '';
 9506:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9507:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9508:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9509:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9510:                         $hostherechecked{'no'} = '';
 9511:                     }
 9512:                 }
 9513:             }
 9514:         }
 9515:         if ($currcookies{$lonhost}) {
 9516:             %balcookiechecked = (
 9517:                                     yes => ' checked="checked"',
 9518:                                 );
 9519:         }
 9520:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9521:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9522:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9523:         foreach my $sparetype (@sparestypes) {
 9524:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9525:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9526:                           '</i></label><br />';
 9527:         }
 9528:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9529:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9530:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9531:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9532:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9533:                       '</div></td></tr>'.
 9534:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9535:                                            $othertitle,$usertypes,$types,\%servers,
 9536:                                            \%currbalancer,$lonhost,
 9537:                                            $targets_div_style,$homedom_div_style,
 9538:                                            $css_class[$cssidx],$balnum,$islast);
 9539:         $$rowtotal += $rownum;
 9540:         $balnum ++;
 9541:     }
 9542:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9543:     return $datatable;
 9544: }
 9545: 
 9546: sub get_loadbalancers_config {
 9547:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9548:     return unless ((ref($servers) eq 'HASH') &&
 9549:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9550:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9551:                    (ref($currcookies) eq 'HASH'));
 9552:     if (keys(%{$existing}) > 0) {
 9553:         my $oldlonhost;
 9554:         foreach my $key (sort(keys(%{$existing}))) {
 9555:             if ($key eq 'lonhost') {
 9556:                 $oldlonhost = $existing->{'lonhost'};
 9557:                 $currbalancer->{$oldlonhost} = 1;
 9558:             } elsif ($key eq 'targets') {
 9559:                 if ($oldlonhost) {
 9560:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9561:                 }
 9562:             } elsif ($key eq 'rules') {
 9563:                 if ($oldlonhost) {
 9564:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9565:                 }
 9566:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9567:                 $currbalancer->{$key} = 1;
 9568:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9569:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9570:                 if ($existing->{$key}{'cookie'}) {
 9571:                     $currcookies->{$key} = 1;
 9572:                 }
 9573:             }
 9574:         }
 9575:     } else {
 9576:         my ($balancerref,$targetsref) =
 9577:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9578:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9579:             foreach my $server (sort(keys(%{$balancerref}))) {
 9580:                 $currbalancer->{$server} = 1;
 9581:                 $currtargets->{$server} = $targetsref->{$server};
 9582:             }
 9583:         }
 9584:     }
 9585:     return;
 9586: }
 9587: 
 9588: sub loadbalancing_rules {
 9589:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9590:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9591:         $css_class,$balnum,$islast) = @_;
 9592:     my $output;
 9593:     my $num = 0;
 9594:     my ($alltypes,$othertypes,$titles) =
 9595:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9596:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9597:         foreach my $type (@{$alltypes}) {
 9598:             $num ++;
 9599:             my $current;
 9600:             if (ref($currrules) eq 'HASH') {
 9601:                 $current = $currrules->{$type};
 9602:             }
 9603:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9604:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9605:                     $current = '';
 9606:                 }
 9607:             }
 9608:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9609:                                              $servers,$currbalancer,$lonhost,$dom,
 9610:                                              $targets_div_style,$homedom_div_style,
 9611:                                              $css_class,$balnum,$num,$islast);
 9612:         }
 9613:     }
 9614:     return $output;
 9615: }
 9616: 
 9617: sub loadbalancing_titles {
 9618:     my ($dom,$intdom,$usertypes,$types) = @_;
 9619:     my %othertypes = (
 9620:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9621:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9622:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9623:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9624:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9625:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9626:                      );
 9627:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9628:     my @available;
 9629:     if (ref($types) eq 'ARRAY') {
 9630:         @available = @{$types};
 9631:     }
 9632:     unless (grep(/^default$/,@available)) {
 9633:         push(@available,'default');
 9634:     }
 9635:     unshift(@alltypes,@available);
 9636:     my %titles;
 9637:     foreach my $type (@alltypes) {
 9638:         if ($type =~ /^_LC_/) {
 9639:             $titles{$type} = $othertypes{$type};
 9640:         } elsif ($type eq 'default') {
 9641:             $titles{$type} = &mt('All users from [_1]',$dom);
 9642:             if (ref($types) eq 'ARRAY') {
 9643:                 if (@{$types} > 0) {
 9644:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9645:                 }
 9646:             }
 9647:         } elsif (ref($usertypes) eq 'HASH') {
 9648:             $titles{$type} = $usertypes->{$type};
 9649:         }
 9650:     }
 9651:     return (\@alltypes,\%othertypes,\%titles);
 9652: }
 9653: 
 9654: sub loadbalance_rule_row {
 9655:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9656:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9657:     my @rulenames;
 9658:     my %ruletitles = &offloadtype_text();
 9659:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9660:         @rulenames = ('balancer','offloadedto','specific');
 9661:     } else {
 9662:         @rulenames = ('default','homeserver');
 9663:         if ($type eq '_LC_external') {
 9664:             push(@rulenames,'externalbalancer');
 9665:         } else {
 9666:             push(@rulenames,'specific');
 9667:         }
 9668:         push(@rulenames,'none');
 9669:     }
 9670:     my $style = $targets_div_style;
 9671:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9672:         $style = $homedom_div_style;
 9673:     }
 9674:     my $space;
 9675:     if ($islast && $num == 1) {
 9676:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9677:     }
 9678:     my $output =
 9679:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9680:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9681:         '<td valaign="top">'.$space.
 9682:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9683:     for (my $i=0; $i<@rulenames; $i++) {
 9684:         my $rule = $rulenames[$i];
 9685:         my ($checked,$extra);
 9686:         if ($rulenames[$i] eq 'default') {
 9687:             $rule = '';
 9688:         }
 9689:         if ($rulenames[$i] eq 'specific') {
 9690:             if (ref($servers) eq 'HASH') {
 9691:                 my $default;
 9692:                 if (($current ne '') && (exists($servers->{$current}))) {
 9693:                     $checked = ' checked="checked"';
 9694:                 }
 9695:                 unless ($checked) {
 9696:                     $default = ' selected="selected"';
 9697:                 }
 9698:                 $extra =
 9699:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9700:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9701:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9702:                     '<option value=""'.$default.'></option>'."\n";
 9703:                 foreach my $server (sort(keys(%{$servers}))) {
 9704:                     if (ref($currbalancer) eq 'HASH') {
 9705:                         next if (exists($currbalancer->{$server}));
 9706:                     }
 9707:                     my $selected;
 9708:                     if ($server eq $current) {
 9709:                         $selected = ' selected="selected"';
 9710:                     }
 9711:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9712:                 }
 9713:                 $extra .= '</select>';
 9714:             }
 9715:         } elsif ($rule eq $current) {
 9716:             $checked = ' checked="checked"';
 9717:         }
 9718:         $output .= '<span class="LC_nobreak"><label>'.
 9719:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9720:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9721:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9722:                    ')"'.$checked.' />&nbsp;';
 9723:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9724:             $output .= $ruletitles{'particular'};
 9725:         } else {
 9726:             $output .= $ruletitles{$rulenames[$i]};
 9727:         }
 9728:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9729:     }
 9730:     $output .= '</div></td></tr>'."\n";
 9731:     return $output;
 9732: }
 9733: 
 9734: sub offloadtype_text {
 9735:     my %ruletitles = &Apache::lonlocal::texthash (
 9736:            'default'          => 'Offloads to default destinations',
 9737:            'homeserver'       => "Offloads to user's home server",
 9738:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9739:            'specific'         => 'Offloads to specific server',
 9740:            'none'             => 'No offload',
 9741:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9742:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9743:            'particular'       => 'Session hosted (after re-auth) on server:',
 9744:     );
 9745:     return %ruletitles;
 9746: }
 9747: 
 9748: sub sparestype_titles {
 9749:     my %typestitles = &Apache::lonlocal::texthash (
 9750:                           'primary' => 'primary',
 9751:                           'default' => 'default',
 9752:                       );
 9753:     return %typestitles;
 9754: }
 9755: 
 9756: sub contact_titles {
 9757:     my %titles = &Apache::lonlocal::texthash (
 9758:                    'supportemail'    => 'Support E-mail address',
 9759:                    'adminemail'      => 'Default Server Admin E-mail address',
 9760:                    'errormail'       => 'Error reports to be e-mailed to',
 9761:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9762:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9763:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9764:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9765:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9766:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9767:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9768:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9769:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9770:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9771:                    'errorweights'    => 'Weights used to compute error count',
 9772:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9773:                  );
 9774:     my %short_titles = &Apache::lonlocal::texthash (
 9775:                            adminemail   => 'Admin E-mail address',
 9776:                            supportemail => 'Support E-mail',
 9777:                        );   
 9778:     return (\%titles,\%short_titles);
 9779: }
 9780: 
 9781: sub helpform_fields {
 9782:     my %titles =  &Apache::lonlocal::texthash (
 9783:                        'username'   => 'Name',
 9784:                        'user'       => 'Username/domain',
 9785:                        'phone'      => 'Phone',
 9786:                        'cc'         => 'Cc e-mail',
 9787:                        'course'     => 'Course Details',
 9788:                        'section'    => 'Sections',
 9789:                        'screenshot' => 'File upload',
 9790:     );
 9791:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9792:     my %possoptions = (
 9793:                         username     => ['yes','no','req'],
 9794:                         phone        => ['yes','no','req'],
 9795:                         user         => ['yes','no'],
 9796:                         cc           => ['yes','no'],
 9797:                         course       => ['yes','no'],
 9798:                         section      => ['yes','no'],
 9799:                         screenshot   => ['yes','no'],
 9800:                       );
 9801:     my %fieldoptions = &Apache::lonlocal::texthash (
 9802:                          'yes'  => 'Optional',
 9803:                          'req'  => 'Required',
 9804:                          'no'   => "Not shown",
 9805:     );
 9806:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9807: }
 9808: 
 9809: sub tool_titles {
 9810:     my %titles = &Apache::lonlocal::texthash (
 9811:                      aboutme    => 'Personal web page',
 9812:                      blog       => 'Blog',
 9813:                      portfolio  => 'Portfolio',
 9814:                      portaccess => 'Share portfolio files',
 9815:                      timezone   => 'Can set time zone',
 9816:                      official   => 'Official courses (with institutional codes)',
 9817:                      unofficial => 'Unofficial courses',
 9818:                      community  => 'Communities',
 9819:                      textbook   => 'Textbook courses',
 9820:                      placement  => 'Placement tests',
 9821:                  );
 9822:     return %titles;
 9823: }
 9824: 
 9825: sub courserequest_titles {
 9826:     my %titles = &Apache::lonlocal::texthash (
 9827:                                    official   => 'Official',
 9828:                                    unofficial => 'Unofficial',
 9829:                                    community  => 'Communities',
 9830:                                    textbook   => 'Textbook',
 9831:                                    placement  => 'Placement tests',
 9832:                                    lti        => 'LTI Provider',
 9833:                                    norequest  => 'Not allowed',
 9834:                                    approval   => 'Approval by DC',
 9835:                                    validate   => 'With validation',
 9836:                                    autolimit  => 'Numerical limit',
 9837:                                    unlimited  => '(blank for unlimited)',
 9838:                  );
 9839:     return %titles;
 9840: }
 9841: 
 9842: sub authorrequest_titles {
 9843:     my %titles = &Apache::lonlocal::texthash (
 9844:                                    norequest  => 'Not allowed',
 9845:                                    approval   => 'Approval by Dom. Coord.',
 9846:                                    automatic  => 'Automatic approval',
 9847:                  );
 9848:     return %titles;
 9849: }
 9850: 
 9851: sub courserequest_conditions {
 9852:     my %conditions = &Apache::lonlocal::texthash (
 9853:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9854:        validate   => '(Processing of request subject to institutional validation).',
 9855:                  );
 9856:     return %conditions;
 9857: }
 9858: 
 9859: 
 9860: sub print_usercreation {
 9861:     my ($position,$dom,$settings,$rowtotal) = @_;
 9862:     my $numinrow = 4;
 9863:     my $datatable;
 9864:     if ($position eq 'top') {
 9865:         $$rowtotal ++;
 9866:         my $rowcount = 0;
 9867:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9868:         if (ref($rules) eq 'HASH') {
 9869:             if (keys(%{$rules}) > 0) {
 9870:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9871:                                                 $ruleorder,$numinrow,$rowcount);
 9872:                 $$rowtotal ++;
 9873:                 $rowcount ++;
 9874:             }
 9875:         }
 9876:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9877:         if (ref($idrules) eq 'HASH') {
 9878:             if (keys(%{$idrules}) > 0) {
 9879:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9880:                                                 $idruleorder,$numinrow,$rowcount);
 9881:                 $$rowtotal ++;
 9882:                 $rowcount ++;
 9883:             }
 9884:         }
 9885:         if ($rowcount == 0) {
 9886:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9887:             $$rowtotal ++;
 9888:             $rowcount ++;
 9889:         }
 9890:     } elsif ($position eq 'middle') {
 9891:         my @creators = ('author','course','requestcrs');
 9892:         my ($rules,$ruleorder) =
 9893:             &Apache::lonnet::inst_userrules($dom,'username');
 9894:         my %lt = &usercreation_types();
 9895:         my %checked;
 9896:         if (ref($settings) eq 'HASH') {
 9897:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9898:                 foreach my $item (@creators) {
 9899:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9900:                 }
 9901:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9902:                 foreach my $item (@creators) {
 9903:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9904:                         $checked{$item} = 'none';
 9905:                     }
 9906:                 }
 9907:             }
 9908:         }
 9909:         my $rownum = 0;
 9910:         foreach my $item (@creators) {
 9911:             $rownum ++;
 9912:             if ($checked{$item} eq '') {
 9913:                 $checked{$item} = 'any';
 9914:             }
 9915:             my $css_class;
 9916:             if ($rownum%2) {
 9917:                 $css_class = '';
 9918:             } else {
 9919:                 $css_class = ' class="LC_odd_row" ';
 9920:             }
 9921:             $datatable .= '<tr'.$css_class.'>'.
 9922:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9923:                          '</span></td><td style="text-align: right">';
 9924:             my @options = ('any');
 9925:             if (ref($rules) eq 'HASH') {
 9926:                 if (keys(%{$rules}) > 0) {
 9927:                     push(@options,('official','unofficial'));
 9928:                 }
 9929:             }
 9930:             push(@options,'none');
 9931:             foreach my $option (@options) {
 9932:                 my $type = 'radio';
 9933:                 my $check = ' ';
 9934:                 if ($checked{$item} eq $option) {
 9935:                     $check = ' checked="checked" ';
 9936:                 } 
 9937:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9938:                               '<input type="'.$type.'" name="can_createuser_'.
 9939:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9940:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9941:             }
 9942:             $datatable .= '</td></tr>';
 9943:         }
 9944:     } else {
 9945:         my @contexts = ('author','course','domain');
 9946:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9947:         my %checked;
 9948:         if (ref($settings) eq 'HASH') {
 9949:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9950:                 foreach my $item (@contexts) {
 9951:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9952:                         foreach my $auth (@authtypes) {
 9953:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9954:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9955:                             }
 9956:                         }
 9957:                     }
 9958:                 }
 9959:             }
 9960:         } else {
 9961:             foreach my $item (@contexts) {
 9962:                 foreach my $auth (@authtypes) {
 9963:                     $checked{$item}{$auth} = ' checked="checked" ';
 9964:                 }
 9965:             }
 9966:         }
 9967:         my %title = &context_names();
 9968:         my %authname = &authtype_names();
 9969:         my $rownum = 0;
 9970:         my $css_class; 
 9971:         foreach my $item (@contexts) {
 9972:             if ($rownum%2) {
 9973:                 $css_class = '';
 9974:             } else {
 9975:                 $css_class = ' class="LC_odd_row" ';
 9976:             }
 9977:             $datatable .=   '<tr'.$css_class.'>'.
 9978:                             '<td>'.$title{$item}.
 9979:                             '</td><td class="LC_left_item">'.
 9980:                             '<span class="LC_nobreak">';
 9981:             foreach my $auth (@authtypes) {
 9982:                 $datatable .= '<label>'. 
 9983:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9984:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9985:                               $authname{$auth}.'</label>&nbsp;';
 9986:             }
 9987:             $datatable .= '</span></td></tr>';
 9988:             $rownum ++;
 9989:         }
 9990:         $$rowtotal += $rownum;
 9991:     }
 9992:     return $datatable;
 9993: }
 9994: 
 9995: sub print_selfcreation {
 9996:     my ($position,$dom,$settings,$rowtotal) = @_;
 9997:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9998:         $emaildomain,$datatable);
 9999:     if (ref($settings) eq 'HASH') {
10000:         if (ref($settings->{'cancreate'}) eq 'HASH') {
10001:             $createsettings = $settings->{'cancreate'};
10002:             if (ref($createsettings) eq 'HASH') {
10003:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
10004:                     @selfcreate = @{$createsettings->{'selfcreate'}};
10005:                 } elsif ($createsettings->{'selfcreate'} ne '') {
10006:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
10007:                         @selfcreate = ('email','login','sso');
10008:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
10009:                         @selfcreate = ($createsettings->{'selfcreate'});
10010:                     }
10011:                 }
10012:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
10013:                     $processing = $createsettings->{'selfcreateprocessing'};
10014:                 }
10015:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
10016:                     $emailoptions = $createsettings->{'emailoptions'};
10017:                 }
10018:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
10019:                     $emailverified = $createsettings->{'emailverified'};
10020:                 }
10021:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
10022:                     $emaildomain = $createsettings->{'emaildomain'};
10023:                 }
10024:             }
10025:         }
10026:     }
10027:     my %radiohash;
10028:     my $numinrow = 4;
10029:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
10030:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10031:     if ($position eq 'top') {
10032:         my %choices = &Apache::lonlocal::texthash (
10033:                                                       cancreate_login      => 'Institutional Login',
10034:                                                       cancreate_sso        => 'Institutional Single Sign On',
10035:                                                   );
10036:         my @toggles = sort(keys(%choices));
10037:         my %defaultchecked = (
10038:                                'cancreate_login' => 'off',
10039:                                'cancreate_sso'   => 'off',
10040:                              );
10041:         my ($onclick,$itemcount);
10042:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10043:                                                      \%choices,$itemcount,$onclick);
10044:         $$rowtotal += $itemcount;
10045: 
10046:         if (ref($usertypes) eq 'HASH') {
10047:             if (keys(%{$usertypes}) > 0) {
10048:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
10049:                                              $dom,$numinrow,$othertitle,
10050:                                              'statustocreate',$rowtotal);
10051:                 $$rowtotal ++;
10052:             }
10053:         }
10054:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
10055:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10056:         $fieldtitles{'inststatus'} = &mt('Institutional status');
10057:         my $rem;
10058:         my $numperrow = 2;
10059:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
10060:         $datatable .= '<tr'.$css_class.'>'.
10061:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
10062:                      '<td class="LC_left_item">'."\n".
10063:                      '<table>'."\n";
10064:         for (my $i=0; $i<@fields; $i++) {
10065:             $rem = $i%($numperrow);
10066:             if ($rem == 0) {
10067:                 if ($i > 0) {
10068:                     $datatable .= '</tr>';
10069:                 }
10070:                 $datatable .= '<tr>';
10071:             }
10072:             my $currval;
10073:             if (ref($createsettings) eq 'HASH') {
10074:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
10075:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
10076:                 }
10077:             }
10078:             $datatable .= '<td class="LC_left_item">'.
10079:                           '<span class="LC_nobreak">'.
10080:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
10081:                           'value="'.$currval.'" size="10" />&nbsp;'.
10082:                           $fieldtitles{$fields[$i]}.'</span></td>';
10083:         }
10084:         my $colsleft = $numperrow - $rem;
10085:         if ($colsleft > 1 ) {
10086:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10087:                          '&nbsp;</td>';
10088:         } elsif ($colsleft == 1) {
10089:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
10090:         }
10091:         $datatable .= '</tr></table></td></tr>';
10092:         $$rowtotal ++;
10093:     } elsif ($position eq 'middle') {
10094:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
10095:         my @posstypes;
10096:         if (ref($types) eq 'ARRAY') {
10097:             @posstypes = @{$types};
10098:         }
10099:         unless (grep(/^default$/,@posstypes)) {
10100:             push(@posstypes,'default');
10101:         }
10102:         my %usertypeshash;
10103:         if (ref($usertypes) eq 'HASH') {
10104:             %usertypeshash = %{$usertypes};
10105:         }
10106:         $usertypeshash{'default'} = $othertitle;
10107:         foreach my $status (@posstypes) {
10108:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
10109:                                                    $numinrow,$$rowtotal,\%usertypeshash);
10110:             $$rowtotal ++;
10111:         }
10112:     } else {
10113:         my %choices = &Apache::lonlocal::texthash (
10114:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
10115:                                                   );
10116:         my @toggles = sort(keys(%choices));
10117:         my %defaultchecked = (
10118:                                'cancreate_email' => 'off',
10119:                              );
10120:         my $customclass = 'LC_selfcreate_email';
10121:         my $classprefix = 'LC_canmodify_emailusername_';
10122:         my $optionsprefix = 'LC_options_emailusername_';
10123:         my $display = 'none';
10124:         my $rowstyle = 'display:none';
10125:         if (grep(/^\Qemail\E$/,@selfcreate)) {
10126:             $display = 'block';
10127:             $rowstyle = 'display:table-row';
10128:         }
10129:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
10130:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10131:                                                      \%choices,$$rowtotal,$onclick);
10132:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
10133:                                          $rowstyle);
10134:         $$rowtotal ++;
10135:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
10136:                                       $rowstyle);
10137:         $$rowtotal ++;
10138:         my (@ordered,@posstypes,%usertypeshash);
10139:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10140:         my ($emailrules,$emailruleorder) =
10141:             &Apache::lonnet::inst_userrules($dom,'email');
10142:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
10143:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
10144:         if (ref($types) eq 'ARRAY') {
10145:             @posstypes = @{$types};
10146:         }
10147:         if (@posstypes) {
10148:             unless (grep(/^default$/,@posstypes)) {
10149:                 push(@posstypes,'default');
10150:             }
10151:             if (ref($usertypes) eq 'HASH') {
10152:                 %usertypeshash = %{$usertypes};
10153:             }
10154:             my $currassign;
10155:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
10156:                 $currassign = {
10157:                                   selfassign => $domdefaults{'inststatusguest'},
10158:                               };
10159:                 @ordered = @{$domdefaults{'inststatusguest'}};
10160:             } else {
10161:                 $currassign = { selfassign => [] };
10162:             }
10163:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
10164:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
10165:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
10166:                                          $numinrow,$othertitle,'selfassign',
10167:                                          $rowtotal,$onclicktypes,$customclass,
10168:                                          $rowstyle);
10169:             $$rowtotal ++;
10170:             $usertypeshash{'default'} = $othertitle;
10171:             foreach my $status (@posstypes) {
10172:                 my $css_class;
10173:                 if ($$rowtotal%2) {
10174:                     $css_class = 'LC_odd_row ';
10175:                 }
10176:                 $css_class .= $customclass;
10177:                 my $rowid = $optionsprefix.$status;
10178:                 my $hidden = 1;
10179:                 my $currstyle = 'display:none';
10180:                 if (grep(/^\Q$status\E$/,@ordered)) {
10181:                     $currstyle = $rowstyle;
10182:                     $hidden = 0;
10183:                 }
10184:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10185:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
10186:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
10187:                 unless ($hidden) {
10188:                     $$rowtotal ++;
10189:                 }
10190:             }
10191:         } else {
10192:             my $css_class;
10193:             if ($$rowtotal%2) {
10194:                 $css_class = 'LC_odd_row ';
10195:             }
10196:             $css_class .= $customclass;
10197:             $usertypeshash{'default'} = $othertitle;
10198:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10199:                                          $emailrules,$emailruleorder,$settings,'default','',
10200:                                          $othertitle,$css_class,$rowstyle,$intdom);
10201:             $$rowtotal ++;
10202:         }
10203:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10204:         $numinrow = 1;
10205:         if (@posstypes) {
10206:             foreach my $status (@posstypes) {
10207:                 my $rowid = $classprefix.$status;
10208:                 my $datarowstyle = 'display:none';
10209:                 if (grep(/^\Q$status\E$/,@ordered)) {
10210:                     $datarowstyle = $rowstyle;
10211:                 }
10212:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
10213:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10214:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
10215:                 unless ($datarowstyle eq 'display:none') {
10216:                     $$rowtotal ++;
10217:                 }
10218:             }
10219:         } else {
10220:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
10221:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10222:                                                    $infotitles,'',$customclass,$rowstyle);
10223:         }
10224:     }
10225:     return $datatable;
10226: }
10227: 
10228: sub selfcreate_javascript {
10229:     return <<"ENDSCRIPT";
10230: 
10231: <script type="text/javascript">
10232: // <![CDATA[
10233: 
10234: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
10235:     var x = document.getElementsByClassName(target);
10236:     var insttypes = 0;
10237:     var insttypeRegExp = new RegExp(prefix);
10238:     if ((x.length != undefined) && (x.length > 0)) {
10239:         if (form.elements[radio].length != undefined) {
10240:             for (var i=0; i<form.elements[radio].length; i++) {
10241:                 if (form.elements[radio][i].checked) {
10242:                     if (form.elements[radio][i].value == 1) {
10243:                         for (var j=0; j<x.length; j++) {
10244:                             if (x[j].id == 'undefined') {
10245:                                 x[j].style.display = 'table-row';
10246:                             } else if (insttypeRegExp.test(x[j].id)) {
10247:                                 insttypes ++;
10248:                             } else {
10249:                                 x[j].style.display = 'table-row';
10250:                             }
10251:                         }
10252:                     } else {
10253:                         for (var j=0; j<x.length; j++) {
10254:                             x[j].style.display = 'none';
10255:                         }
10256:                     }
10257:                     break;
10258:                 }
10259:             }
10260:             if (insttypes > 0) {
10261:                 toggleDataRow(form,checkbox,target,altprefix);
10262:                 toggleDataRow(form,checkbox,target,prefix,1);
10263:             }
10264:         }
10265:     }
10266:     return;
10267: }
10268: 
10269: function toggleDataRow(form,checkbox,target,prefix,docount) {
10270:     if (form.elements[checkbox].length != undefined) {
10271:         var count = 0;
10272:         if (docount) {
10273:             for (var i=0; i<form.elements[checkbox].length; i++) {
10274:                 if (form.elements[checkbox][i].checked) {
10275:                     count ++;
10276:                 }
10277:             }
10278:         }
10279:         for (var i=0; i<form.elements[checkbox].length; i++) {
10280:             var type = form.elements[checkbox][i].value;
10281:             if (document.getElementById(prefix+type)) {
10282:                 if (form.elements[checkbox][i].checked) {
10283:                     document.getElementById(prefix+type).style.display = 'table-row';
10284:                     if (count % 2 == 1) {
10285:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
10286:                     } else {
10287:                         document.getElementById(prefix+type).className = target;
10288:                     }
10289:                     count ++;
10290:                 } else {
10291:                     document.getElementById(prefix+type).style.display = 'none';
10292:                 }
10293:             }
10294:         }
10295:     }
10296:     return;
10297: }
10298: 
10299: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
10300:     var caller = radio+'_'+status;
10301:     if (form.elements[caller].length != undefined) {
10302:         for (var i=0; i<form.elements[caller].length; i++) {
10303:             if (form.elements[caller][i].checked) {
10304:                 if (document.getElementById(altprefix+'_inst_'+status)) {
10305:                     var curr = form.elements[caller][i].value;
10306:                     if (prefix) {
10307:                         document.getElementById(prefix+'_'+status).style.display = 'none';
10308:                     }
10309:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
10310:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
10311:                     if (curr == 'custom') {
10312:                         if (prefix) {
10313:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
10314:                         }
10315:                     } else if (curr == 'inst') {
10316:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
10317:                     } else if (curr == 'noninst') {
10318:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
10319:                     }
10320:                     break;
10321:                 }
10322:             }
10323:         }
10324:     }
10325: }
10326: 
10327: // ]]>
10328: </script>
10329: 
10330: ENDSCRIPT
10331: }
10332: 
10333: sub noninst_users {
10334:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
10335:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
10336:     my $class = 'LC_left_item';
10337:     if ($css_class) {
10338:         $css_class = ' class="'.$css_class.'"';
10339:     }
10340:     if ($rowid) {
10341:         $rowid = ' id="'.$rowid.'"';
10342:     }
10343:     if ($rowstyle) {
10344:         $rowstyle = ' style="'.$rowstyle.'"';
10345:     }
10346:     my ($output,$description);
10347:     if ($type eq 'default') {
10348:         $description = &mt('Requests for: [_1]',$typetitle);
10349:     } else {
10350:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
10351:     }
10352:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
10353:               "<td>$description</td>\n".
10354:               '<td class="'.$class.'" colspan="2">'.
10355:               '<table><tr>';
10356:     my %headers = &Apache::lonlocal::texthash(
10357:               approve  => 'Processing',
10358:               email    => 'E-mail',
10359:               username => 'Username',
10360:     );
10361:     foreach my $item ('approve','email','username') {
10362:         $output .= '<th>'.$headers{$item}.'</th>';
10363:     }
10364:     $output .= '</tr><tr>';
10365:     foreach my $item ('approve','email','username') {
10366:         $output .= '<td style="vertical-align: top">';
10367:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
10368:         if ($item eq 'approve') {
10369:             %choices = &Apache::lonlocal::texthash (
10370:                                                      automatic => 'Automatically approved',
10371:                                                      approval  => 'Queued for approval',
10372:                                                    );
10373:             @options = ('automatic','approval');
10374:             $hashref = $processing;
10375:             $defoption = 'automatic';
10376:             $name = 'cancreate_emailprocess_'.$type;
10377:         } elsif ($item eq 'email') {
10378:             %choices = &Apache::lonlocal::texthash (
10379:                                                      any     => 'Any e-mail',
10380:                                                      inst    => 'Institutional only',
10381:                                                      noninst => 'Non-institutional only',
10382:                                                      custom  => 'Custom restrictions',
10383:                                                    );
10384:             @options = ('any','inst','noninst');
10385:             my $showcustom;
10386:             if (ref($emailrules) eq 'HASH') {
10387:                 if (keys(%{$emailrules}) > 0) {
10388:                     push(@options,'custom');
10389:                     $showcustom = 'cancreate_emailrule';
10390:                     if (ref($settings) eq 'HASH') {
10391:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
10392:                             foreach my $rule (@{$settings->{'email_rule'}}) {
10393:                                 if (exists($emailrules->{$rule})) {
10394:                                     $hascustom ++;
10395:                                 }
10396:                             }
10397:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
10398:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
10399:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
10400:                                     if (exists($emailrules->{$rule})) {
10401:                                         $hascustom ++;
10402:                                     }
10403:                                 }
10404:                             }
10405:                         }
10406:                     }
10407:                 }
10408:             }
10409:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
10410:                                                      "'cancreate_emaildomain','$type'".');"';
10411:             $hashref = $emailoptions;
10412:             $defoption = 'any';
10413:             $name = 'cancreate_emailoptions_'.$type;
10414:         } elsif ($item eq 'username') {
10415:             %choices = &Apache::lonlocal::texthash (
10416:                                                      all    => 'Same as e-mail',
10417:                                                      first  => 'Omit @domain',
10418:                                                      free   => 'Free to choose',
10419:                                                    );
10420:             @options = ('all','first','free');
10421:             $hashref = $emailverified;
10422:             $defoption = 'all';
10423:             $name = 'cancreate_usernameoptions_'.$type;
10424:         }
10425:         foreach my $option (@options) {
10426:             my $checked;
10427:             if (ref($hashref) eq 'HASH') {
10428:                 if ($type eq '') {
10429:                     if (!exists($hashref->{'default'})) {
10430:                         if ($option eq $defoption) {
10431:                             $checked = ' checked="checked"';
10432:                         }
10433:                     } else {
10434:                         if ($hashref->{'default'} eq $option) {
10435:                             $checked = ' checked="checked"';
10436:                         }
10437:                     }
10438:                 } else {
10439:                     if (!exists($hashref->{$type})) {
10440:                         if ($option eq $defoption) {
10441:                             $checked = ' checked="checked"';
10442:                         }
10443:                     } else {
10444:                         if ($hashref->{$type} eq $option) {
10445:                             $checked = ' checked="checked"';
10446:                         }
10447:                     }
10448:                 }
10449:             } elsif (($item eq 'email') && ($hascustom)) {
10450:                 if ($option eq 'custom') {
10451:                     $checked = ' checked="checked"';
10452:                 }
10453:             } elsif ($option eq $defoption) {
10454:                 $checked = ' checked="checked"';
10455:             }
10456:             $output .= '<span class="LC_nobreak"><label>'.
10457:                        '<input type="radio" name="'.$name.'"'.
10458:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10459:                        $choices{$option}.'</label></span><br />';
10460:             if ($item eq 'email') {
10461:                 if ($option eq 'custom') {
10462:                     my $id = 'cancreate_emailrule_'.$type;
10463:                     my $display = 'none';
10464:                     if ($checked) {
10465:                         $display = 'inline';
10466:                     }
10467:                     my $numinrow = 2;
10468:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10469:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10470:                                &user_formats_row('email',$settings,$emailrules,
10471:                                                  $emailruleorder,$numinrow,'',$type);
10472:                               '</table></fieldset>';
10473:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10474:                     my %text = &Apache::lonlocal::texthash (
10475:                                                              inst    => 'must end:',
10476:                                                              noninst => 'cannot end:',
10477:                                                            );
10478:                     my $value;
10479:                     if (ref($emaildomain) eq 'HASH') {
10480:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10481:                             $value = $emaildomain->{$type}->{$option};
10482:                         }
10483:                     }
10484:                     if ($value eq '') {
10485:                         $value = '@'.$intdom;
10486:                     }
10487:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10488:                     my $display = 'none';
10489:                     if ($checked) {
10490:                         $display = 'inline';
10491:                     }
10492:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10493:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10494:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10495:                                '</div>';
10496:                 }
10497:             }
10498:         }
10499:         $output .= '</td>'."\n";
10500:     }
10501:     $output .= "</tr></table></td></tr>\n";
10502:     return $output;
10503: }
10504: 
10505: sub captcha_choice {
10506:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10507:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10508:         $vertext,$currver);
10509:     my %lt = &captcha_phrases();
10510:     $keyentry = 'hidden';
10511:     my $colspan=2;
10512:     if ($context eq 'cancreate') {
10513:         $rowname = &mt('CAPTCHA validation');
10514:     } elsif ($context eq 'login') {
10515:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10516:     } elsif ($context eq 'passwords') {
10517:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10518:         $colspan=1;
10519:     }
10520:     if (ref($settings) eq 'HASH') {
10521:         if ($settings->{'captcha'}) {
10522:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10523:         } else {
10524:             $checked{'original'} = ' checked="checked"';
10525:         }
10526:         if ($settings->{'captcha'} eq 'recaptcha') {
10527:             $pubtext = $lt{'pub'};
10528:             $privtext = $lt{'priv'};
10529:             $keyentry = 'text';
10530:             $vertext = $lt{'ver'};
10531:             $currver = $settings->{'recaptchaversion'};
10532:             if ($currver ne '2') {
10533:                 $currver = 1;
10534:             }
10535:         }
10536:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10537:             $currpub = $settings->{'recaptchakeys'}{'public'};
10538:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10539:         }
10540:     } else {
10541:         $checked{'original'} = ' checked="checked"';
10542:     }
10543:     my $css_class;
10544:     if ($itemcount%2) {
10545:         $css_class = 'LC_odd_row';
10546:     }
10547:     if ($customcss) {
10548:         $css_class .= " $customcss";
10549:     }
10550:     $css_class =~ s/^\s+//;
10551:     if ($css_class) {
10552:         $css_class = ' class="'.$css_class.'"';
10553:     }
10554:     if ($rowstyle) {
10555:         $css_class .= ' style="'.$rowstyle.'"';
10556:     }
10557:     my $output = '<tr'.$css_class.'>'.
10558:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10559:                  '<table><tr><td>'."\n";
10560:     foreach my $option ('original','recaptcha','notused') {
10561:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10562:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10563:                    $lt{$option}.'</label></span>';
10564:         unless ($option eq 'notused') {
10565:             $output .= ('&nbsp;'x2)."\n";
10566:         }
10567:     }
10568: #
10569: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10570: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10571: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10572: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10573: #
10574:     $output .= '</td></tr>'."\n".
10575:                '<tr><td class="LC_zero_height">'."\n".
10576:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10577:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10578:                $currpub.'" size="40" /></span><br />'."\n".
10579:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10580:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10581:                $currpriv.'" size="40" /></span><br />'.
10582:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10583:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10584:                $currver.'" size="3" /></span><br />'.
10585:                '</td></tr></table>'."\n".
10586:                '</td></tr>';
10587:     return $output;
10588: }
10589: 
10590: sub user_formats_row {
10591:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10592:     my $output;
10593:     my %text = (
10594:                    'username' => 'new usernames',
10595:                    'id'       => 'IDs',
10596:                );
10597:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10598:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10599:         $output = '<tr '.$css_class.'>'.
10600:                   '<td><span class="LC_nobreak">'.
10601:                   &mt("Format rules to check for $text{$type}: ").
10602:                   '</td><td class="LC_left_item" colspan="2"><table>';
10603:     }
10604:     my $rem;
10605:     if (ref($ruleorder) eq 'ARRAY') {
10606:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10607:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10608:                 my $rem = $i%($numinrow);
10609:                 if ($rem == 0) {
10610:                     if ($i > 0) {
10611:                         $output .= '</tr>';
10612:                     }
10613:                     $output .= '<tr>';
10614:                 }
10615:                 my $check = ' ';
10616:                 if (ref($settings) eq 'HASH') {
10617:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10618:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10619:                             $check = ' checked="checked" ';
10620:                         }
10621:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10622:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10623:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10624:                                 $check = ' checked="checked" ';
10625:                             }
10626:                         }
10627:                     }
10628:                 }
10629:                 my $name = $type.'_rule';
10630:                 if ($type eq 'email') {
10631:                     $name .= '_'.$status;
10632:                 }
10633:                 $output .= '<td class="LC_left_item">'.
10634:                            '<span class="LC_nobreak"><label>'.
10635:                            '<input type="checkbox" name="'.$name.'" '.
10636:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10637:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10638:             }
10639:         }
10640:         $rem = @{$ruleorder}%($numinrow);
10641:     }
10642:     my $colsleft;
10643:     if ($rem) {
10644:         $colsleft = $numinrow - $rem;
10645:     }
10646:     if ($colsleft > 1 ) {
10647:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10648:                    '&nbsp;</td>';
10649:     } elsif ($colsleft == 1) {
10650:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10651:     }
10652:     $output .= '</tr>';
10653:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10654:         $output .= '</table></td></tr>';
10655:     }
10656:     return $output;
10657: }
10658: 
10659: sub usercreation_types {
10660:     my %lt = &Apache::lonlocal::texthash (
10661:                     author     => 'When adding a co-author',
10662:                     course     => 'When adding a user to a course',
10663:                     requestcrs => 'When requesting a course',
10664:                     any        => 'Any',
10665:                     official   => 'Institutional only ',
10666:                     unofficial => 'Non-institutional only',
10667:                     none       => 'None',
10668:     );
10669:     return %lt;
10670: }
10671: 
10672: sub selfcreation_types {
10673:     my %lt = &Apache::lonlocal::texthash (
10674:                     selfcreate => 'User creates own account',
10675:                     any        => 'Any',
10676:                     official   => 'Institutional only ',
10677:                     unofficial => 'Non-institutional only',
10678:                     email      => 'E-mail address',
10679:                     login      => 'Institutional Login',
10680:                     sso        => 'SSO',
10681:              );
10682: }
10683: 
10684: sub authtype_names {
10685:     my %lt = &Apache::lonlocal::texthash(
10686:                       int    => 'Internal',
10687:                       krb4   => 'Kerberos 4',
10688:                       krb5   => 'Kerberos 5',
10689:                       loc    => 'Local',
10690:                       lti    => 'LTI',
10691:                   );
10692:     return %lt;
10693: }
10694: 
10695: sub context_names {
10696:     my %context_title = &Apache::lonlocal::texthash(
10697:        author => 'Creating users when an Author',
10698:        course => 'Creating users when in a course',
10699:        domain => 'Creating users when a Domain Coordinator',
10700:     );
10701:     return %context_title;
10702: }
10703: 
10704: sub print_usermodification {
10705:     my ($position,$dom,$settings,$rowtotal) = @_;
10706:     my $numinrow = 4;
10707:     my ($context,$datatable,$rowcount);
10708:     if ($position eq 'top') {
10709:         $rowcount = 0;
10710:         $context = 'author'; 
10711:         foreach my $role ('ca','aa') {
10712:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10713:                                                    $numinrow,$rowcount);
10714:             $$rowtotal ++;
10715:             $rowcount ++;
10716:         }
10717:     } elsif ($position eq 'bottom') {
10718:         $context = 'course';
10719:         $rowcount = 0;
10720:         foreach my $role ('st','ep','ta','in','cr') {
10721:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10722:                                                    $numinrow,$rowcount);
10723:             $$rowtotal ++;
10724:             $rowcount ++;
10725:         }
10726:     }
10727:     return $datatable;
10728: }
10729: 
10730: sub print_defaults {
10731:     my ($position,$dom,$settings,$rowtotal) = @_;
10732:     my $rownum = 0;
10733:     my ($datatable,$css_class,$titles);
10734:     unless ($position eq 'bottom') {
10735:         $titles = &defaults_titles($dom);
10736:     }
10737:     if ($position eq 'top') {
10738:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10739:                      'datelocale_def','portal_def');
10740:         my %defaults;
10741:         if (ref($settings) eq 'HASH') {
10742:             %defaults = %{$settings};
10743:         } else {
10744:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10745:             foreach my $item (@items) {
10746:                 $defaults{$item} = $domdefaults{$item};
10747:             }
10748:         }
10749:         foreach my $item (@items) {
10750:             if ($rownum%2) {
10751:                 $css_class = '';
10752:             } else {
10753:                 $css_class = ' class="LC_odd_row" ';
10754:             }
10755:             $datatable .= '<tr'.$css_class.'>'.
10756:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10757:                           '</span></td><td class="LC_right_item" colspan="3">';
10758:             if ($item eq 'auth_def') {
10759:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10760:                 my %shortauth = (
10761:                                  internal => 'int',
10762:                                  krb4 => 'krb4',
10763:                                  krb5 => 'krb5',
10764:                                  localauth  => 'loc',
10765:                                  lti => 'lti',
10766:                                 );
10767:                 my %authnames = &authtype_names();
10768:                 foreach my $auth (@authtypes) {
10769:                     my $checked = ' ';
10770:                     if ($defaults{$item} eq $auth) {
10771:                         $checked = ' checked="checked" ';
10772:                     }
10773:                     $datatable .= '<label><input type="radio" name="'.$item.
10774:                                   '" value="'.$auth.'"'.$checked.'/>'.
10775:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10776:                 }
10777:             } elsif ($item eq 'timezone_def') {
10778:                 my $includeempty = 1;
10779:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10780:             } elsif ($item eq 'datelocale_def') {
10781:                 my $includeempty = 1;
10782:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10783:             } elsif ($item eq 'lang_def') {
10784:                 my $includeempty = 1;
10785:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10786:             } elsif ($item eq 'portal_def') {
10787:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10788:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10789:                 my $portalsty = 'none';
10790:                 if ($defaults{$item}) {
10791:                     $portalsty = 'block';
10792:                 }
10793:                 foreach my $field ('email','web') {
10794:                     my $checkedoff = ' checked="checked"';
10795:                     my $checkedon;
10796:                     if ($defaults{$item.'_'.$field}) {
10797:                         $checkedon = $checkedoff;
10798:                         $checkedoff = '';
10799:                     }
10800:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10801:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10802:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10803:                               ('&nbsp;'x2).
10804:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10805:                               '</div>';
10806:                 }
10807:             } else {
10808:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10809:             }
10810:             $datatable .= '</td></tr>';
10811:             $rownum ++;
10812:         }
10813:     } elsif ($position eq 'middle') {
10814:         my %defaults;
10815:         if (ref($settings) eq 'HASH') {
10816:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10817:                 my $maxnum = @{$settings->{'inststatusorder'}};
10818:                 for (my $i=0; $i<$maxnum; $i++) {
10819:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10820:                     my $item = $settings->{'inststatusorder'}->[$i];
10821:                     my $title = $settings->{'inststatustypes'}->{$item};
10822:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10823:                     $datatable .= '<tr'.$css_class.'>'.
10824:                                   '<td><span class="LC_nobreak">'.
10825:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10826:                     for (my $k=0; $k<=$maxnum; $k++) {
10827:                         my $vpos = $k+1;
10828:                         my $selstr;
10829:                         if ($k == $i) {
10830:                             $selstr = ' selected="selected" ';
10831:                         }
10832:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10833:                     }
10834:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10835:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10836:                                   &mt('delete').'</span></td>'.
10837:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10838:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10839:                                   '</span></td></tr>';
10840:                 }
10841:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10842:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10843:                 $datatable .= '<tr '.$css_class.'>'.
10844:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10845:                 for (my $k=0; $k<=$maxnum; $k++) {
10846:                     my $vpos = $k+1;
10847:                     my $selstr;
10848:                     if ($k == $maxnum) {
10849:                         $selstr = ' selected="selected" ';
10850:                     }
10851:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10852:                 }
10853:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10854:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10855:                               '&nbsp;'.&mt('(new)').
10856:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10857:                               &mt('Name displayed').':'.
10858:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10859:                               '</tr>'."\n";
10860:                 $rownum ++;
10861:             }
10862:         }
10863:     } else {
10864:         my ($unamemaprules,$ruleorder) =
10865:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10866:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10867:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10868:             my $numinrow = 2;
10869:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10870:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10871:                                             $ruleorder,$numinrow).
10872:                           '</table></td></tr>';
10873:         }
10874:         if ($datatable eq '') {
10875:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10876:                           &mt('No rules set for domain in customized localenroll.pm').
10877:                           '</td></tr>';
10878:         }
10879:     }
10880:     $$rowtotal += $rownum;
10881:     return $datatable;
10882: }
10883: 
10884: sub get_languages_hash {
10885:     my %langchoices;
10886:     foreach my $id (&Apache::loncommon::languageids()) {
10887:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10888:         if ($code ne '') {
10889:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10890:         }
10891:     }
10892:     return %langchoices;
10893: }
10894: 
10895: sub defaults_titles {
10896:     my ($dom) = @_;
10897:     my %titles = &Apache::lonlocal::texthash (
10898:                    'auth_def'      => 'Default authentication type',
10899:                    'auth_arg_def'  => 'Default authentication argument',
10900:                    'lang_def'      => 'Default language',
10901:                    'timezone_def'  => 'Default timezone',
10902:                    'datelocale_def' => 'Default locale for dates',
10903:                    'portal_def'     => 'Portal/Default URL',
10904:                    'email'          => 'Email links use portal URL',
10905:                    'web'            => 'Public web links use portal URL',
10906:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10907:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10908:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10909:                  );
10910:     if ($dom) {
10911:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10912:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10913:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10914:         $protocol = 'http' if ($protocol ne 'https');
10915:         if ($uint_dom) {
10916:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10917:                                          $uint_dom);
10918:         }
10919:     }
10920:     return (\%titles);
10921: }
10922: 
10923: sub print_scantron {
10924:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10925:     if ($position eq 'top') {
10926:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10927:     } else {
10928:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10929:     }
10930: }
10931: 
10932: sub scantron_javascript {
10933:     return <<"ENDSCRIPT";
10934: 
10935: <script type="text/javascript">
10936: // <![CDATA[
10937: 
10938: function toggleScantron(form) {
10939:     var csvfieldset = new Array();
10940:     if (document.getElementById('scantroncsv_cols')) {
10941:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10942:     }
10943:     if (document.getElementById('scantroncsv_options')) {
10944:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10945:     }
10946:     if (csvfieldset.length) {
10947:         if (document.getElementById('scantronconfcsv')) {
10948:             var scantroncsv = document.getElementById('scantronconfcsv');
10949:             if (scantroncsv.checked) {
10950:                 for (var i=0; i<csvfieldset.length; i++) {
10951:                     csvfieldset[i].style.display = 'block';
10952:                 }
10953:             } else {
10954:                 for (var i=0; i<csvfieldset.length; i++) {
10955:                     csvfieldset[i].style.display = 'none';
10956:                 }
10957:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10958:                 if (csvselects.length) {
10959:                     for (var j=0; j<csvselects.length; j++) {
10960:                         csvselects[j].selectedIndex = 0;
10961:                     }
10962:                 }
10963:             }
10964:         }
10965:     }
10966:     return;
10967: }
10968: // ]]>
10969: </script>
10970: 
10971: ENDSCRIPT
10972: 
10973: }
10974: 
10975: sub print_scantronformat {
10976:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10977:     my $itemcount = 1;
10978:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10979:         %confhash);
10980:     my $switchserver = &check_switchserver($dom,$confname);
10981:     my %lt = &Apache::lonlocal::texthash (
10982:                 default => 'Default bubblesheet format file error',
10983:                 custom  => 'Custom bubblesheet format file error',
10984:              );
10985:     my %scantronfiles = (
10986:         default => 'default.tab',
10987:         custom => 'custom.tab',
10988:     );
10989:     foreach my $key (keys(%scantronfiles)) {
10990:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10991:                               .$scantronfiles{$key};
10992:     }
10993:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10994:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10995:         if (!$switchserver) {
10996:             my $servadm = $r->dir_config('lonAdmEMail');
10997:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10998:             if ($configuserok eq 'ok') {
10999:                 if ($author_ok eq 'ok') {
11000:                     my %legacyfile = (
11001:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
11002:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
11003:                     );
11004:                     my %md5chk;
11005:                     foreach my $type (keys(%legacyfile)) {
11006:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
11007:                         chomp($md5chk{$type});
11008:                     }
11009:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
11010:                         foreach my $type (keys(%legacyfile)) {
11011:                             ($scantronurls{$type},my $error) =
11012:                                 &legacy_scantronformat($r,$dom,$confname,
11013:                                                  $type,$legacyfile{$type},
11014:                                                  $scantronurls{$type},
11015:                                                  $scantronfiles{$type});
11016:                             if ($error ne '') {
11017:                                 $error{$type} = $error;
11018:                             }
11019:                         }
11020:                         if (keys(%error) == 0) {
11021:                             $is_custom = 1;
11022:                             $confhash{'scantron'}{'scantronformat'} =
11023:                                 $scantronurls{'custom'};
11024:                             my $putresult =
11025:                                 &Apache::lonnet::put_dom('configuration',
11026:                                                          \%confhash,$dom);
11027:                             if ($putresult ne 'ok') {
11028:                                 $error{'custom'} =
11029:                                     '<span class="LC_error">'.
11030:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11031:                             }
11032:                         }
11033:                     } else {
11034:                         ($scantronurls{'default'},my $error) =
11035:                             &legacy_scantronformat($r,$dom,$confname,
11036:                                           'default',$legacyfile{'default'},
11037:                                           $scantronurls{'default'},
11038:                                           $scantronfiles{'default'});
11039:                         if ($error eq '') {
11040:                             $confhash{'scantron'}{'scantronformat'} = ''; 
11041:                             my $putresult =
11042:                                 &Apache::lonnet::put_dom('configuration',
11043:                                                          \%confhash,$dom);
11044:                             if ($putresult ne 'ok') {
11045:                                 $error{'default'} =
11046:                                     '<span class="LC_error">'.
11047:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11048:                             }
11049:                         } else {
11050:                             $error{'default'} = $error;
11051:                         }
11052:                     }
11053:                 }
11054:             }
11055:         } else {
11056:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
11057:         }
11058:     }
11059:     if (ref($settings) eq 'HASH') {
11060:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
11061:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
11062:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
11063:                 $scantronurl = '';
11064:             } else {
11065:                 $scantronurl = $settings->{'scantronformat'};
11066:             }
11067:             $is_custom = 1;
11068:         } else {
11069:             $scantronurl = $scantronurls{'default'};
11070:         }
11071:     } else {
11072:         if ($is_custom) {
11073:             $scantronurl = $scantronurls{'custom'};
11074:         } else {
11075:             $scantronurl = $scantronurls{'default'};
11076:         }
11077:     }
11078:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11079:     $datatable .= '<tr'.$css_class.'>';
11080:     if (!$is_custom) {
11081:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
11082:                       '<span class="LC_nobreak">';
11083:         if ($scantronurl) {
11084:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
11085:                                                          undef,undef,undef,undef,'background-color:#ffffff');
11086:         } else {
11087:             $datatable = &mt('File unavailable for display');
11088:         }
11089:         $datatable .= '</span></td>';
11090:         if (keys(%error) == 0) { 
11091:             $datatable .= '<td style="vertical-align: bottom">';
11092:             if (!$switchserver) {
11093:                 $datatable .= &mt('Upload:').'<br />';
11094:             }
11095:         } else {
11096:             my $errorstr;
11097:             foreach my $key (sort(keys(%error))) {
11098:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11099:             }
11100:             $datatable .= '<td>'.$errorstr;
11101:         }
11102:     } else {
11103:         if (keys(%error) > 0) {
11104:             my $errorstr;
11105:             foreach my $key (sort(keys(%error))) {
11106:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11107:             } 
11108:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
11109:         } elsif ($scantronurl) {
11110:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
11111:                                                        undef,undef,undef,undef,'background-color:#ffffff');
11112:             $datatable .= '<td><span class="LC_nobreak">'.
11113:                           $link.
11114:                           '<label><input type="checkbox" name="scantronformat_del"'.
11115:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
11116:                           '<td><span class="LC_nobreak">&nbsp;'.
11117:                           &mt('Replace:').'</span><br />';
11118:         }
11119:     }
11120:     if (keys(%error) == 0) {
11121:         if ($switchserver) {
11122:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
11123:         } else {
11124:             $datatable .='<span class="LC_nobreak">&nbsp;'.
11125:                          '<input type="file" name="scantronformat" /></span>';
11126:         }
11127:     }
11128:     $datatable .= '</td></tr>';
11129:     $$rowtotal ++;
11130:     return $datatable;
11131: }
11132: 
11133: sub legacy_scantronformat {
11134:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
11135:     my ($url,$error);
11136:     my @statinfo = &Apache::lonnet::stat_file($newurl);
11137:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
11138:         my $modified = [];
11139:         (my $result,$url) =
11140:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
11141:                                                     'scantron','','',$newfile,$modified);
11142:         if ($result eq 'ok') {
11143:             &update_modify_urls($r,$modified);
11144:         } else {
11145:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
11146:         }
11147:     }
11148:     return ($url,$error);
11149: }
11150: 
11151: sub print_scantronconfig {
11152:     my ($dom,$settings,$rowtotal) = @_;
11153:     my $itemcount = 2;
11154:     my $is_checked = ' checked="checked"';
11155:     my %optionson = (
11156:                      hdr => ' checked="checked"',
11157:                      pad => ' checked="checked"',
11158:                      rem => ' checked="checked"',
11159:                     );
11160:     my %optionsoff = (
11161:                       hdr => '',
11162:                       pad => '',
11163:                       rem => '',
11164:                      );
11165:     my $currcsvsty = 'none';
11166:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
11167:     my @fields = &scantroncsv_fields();
11168:     my %titles = &scantronconfig_titles();
11169:     if (ref($settings) eq 'HASH') {
11170:         if (ref($settings->{config}) eq 'HASH') {
11171:             if ($settings->{config}->{dat}) {
11172:                 $checked{'dat'} = $is_checked;
11173:             }
11174:             if (ref($settings->{config}->{csv}) eq 'HASH') {
11175:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
11176:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
11177:                     if (keys(%csvfields) > 0) {
11178:                         $checked{'csv'} = $is_checked;
11179:                         $currcsvsty = 'block';
11180:                     }
11181:                 }
11182:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
11183:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
11184:                     foreach my $option (keys(%optionson)) {
11185:                         unless ($csvoptions{$option}) {
11186:                             $optionsoff{$option} = $optionson{$option};
11187:                             $optionson{$option} = '';
11188:                         }
11189:                     }
11190:                 }
11191:             }
11192:         } else {
11193:             $checked{'dat'} = $is_checked;
11194:         }
11195:     } else {
11196:         $checked{'dat'} = $is_checked;
11197:     }
11198:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
11199:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11200:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
11201:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
11202:     foreach my $item ('dat','csv') {
11203:         my $id;
11204:         if ($item eq 'csv') {
11205:             $id = 'id="scantronconfcsv" ';
11206:         }
11207:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
11208:                       $titles{$item}.'</label>'.('&nbsp;'x3);
11209:         if ($item eq 'csv') {
11210:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
11211:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
11212:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
11213:             foreach my $col (@fields) {
11214:                 my $selnone;
11215:                 if ($csvfields{$col} eq '') {
11216:                     $selnone = ' selected="selected"';
11217:                 }
11218:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
11219:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
11220:                               '<option value=""'.$selnone.'></option>';
11221:                 for (my $i=0; $i<20; $i++) {
11222:                     my $shown = $i+1;
11223:                     my $sel;
11224:                     unless ($selnone) {
11225:                         if (exists($csvfields{$col})) {
11226:                             if ($csvfields{$col} == $i) {
11227:                                 $sel = ' selected="selected"';
11228:                             }
11229:                         }
11230:                     }
11231:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
11232:                 }
11233:                 $datatable .= '</select></td></tr>';
11234:            }
11235:            $datatable .= '</table></fieldset>'.
11236:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
11237:                          '<legend>'.&mt('CSV Options').'</legend>';
11238:            foreach my $option ('hdr','pad','rem') {
11239:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
11240:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
11241:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
11242:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
11243:            }
11244:            $datatable .= '</fieldset>';
11245:            $itemcount ++;
11246:         }
11247:     }
11248:     $datatable .= '</td></tr>';
11249:     $$rowtotal ++;
11250:     return $datatable;
11251: }
11252: 
11253: sub scantronconfig_titles {
11254:     return &Apache::lonlocal::texthash(
11255:                                           dat => 'Standard format (.dat)',
11256:                                           csv => 'Comma separated values (.csv)',
11257:                                           hdr => 'Remove first line in file (contains column titles)',
11258:                                           pad => 'Prepend 0s to PaperID',
11259:                                           rem => 'Remove leading spaces (except Question Response columns)',
11260:                                           CODE => 'CODE',
11261:                                           ID   => 'Student ID',
11262:                                           PaperID => 'Paper ID',
11263:                                           FirstName => 'First Name',
11264:                                           LastName => 'Last Name',
11265:                                           FirstQuestion => 'First Question Response',
11266:                                           Section => 'Section',
11267:     );
11268: }
11269: 
11270: sub scantroncsv_fields {
11271:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
11272: }
11273: 
11274: sub print_coursecategories {
11275:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
11276:     my $datatable;
11277:     if ($position eq 'top') {
11278:         my (%checked);
11279:         my @catitems = ('unauth','auth');
11280:         my @cattypes = ('std','domonly','codesrch','none');
11281:         $checked{'unauth'} = 'std';
11282:         $checked{'auth'} = 'std';
11283:         if (ref($settings) eq 'HASH') {
11284:             foreach my $type (@cattypes) {
11285:                 if ($type eq $settings->{'unauth'}) {
11286:                     $checked{'unauth'} = $type;
11287:                 }
11288:                 if ($type eq $settings->{'auth'}) {
11289:                     $checked{'auth'} = $type;
11290:                 }
11291:             }
11292:         }
11293:         my %lt = &Apache::lonlocal::texthash (
11294:                                                unauth   => 'Catalog type for unauthenticated users',
11295:                                                auth     => 'Catalog type for authenticated users',
11296:                                                none     => 'No catalog',
11297:                                                std      => 'Standard catalog',
11298:                                                domonly  => 'Domain-only catalog',
11299:                                                codesrch => "Code search form",
11300:                                              );
11301:        my $itemcount = 0;
11302:        foreach my $item (@catitems) {
11303:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11304:            $datatable .= '<tr '.$css_class.'>'.
11305:                          '<td>'.$lt{$item}.'</td>'.
11306:                          '<td class="LC_right_item"><span class="LC_nobreak">';
11307:            foreach my $type (@cattypes) {
11308:                my $ischecked;
11309:                if ($checked{$item} eq $type) {
11310:                    $ischecked=' checked="checked"';
11311:                }
11312:                $datatable .= '<label>'.
11313:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
11314:                              ' />'.$lt{$type}.'</label>&nbsp;';
11315:            }
11316:            $datatable .= '</span></td></tr>';
11317:            $itemcount ++;
11318:         }
11319:         $$rowtotal += $itemcount;
11320:     } elsif ($position eq 'middle') {
11321:         my $toggle_cats_crs = ' ';
11322:         my $toggle_cats_dom = ' checked="checked" ';
11323:         my $can_cat_crs = ' ';
11324:         my $can_cat_dom = ' checked="checked" ';
11325:         my $toggle_catscomm_comm = ' ';
11326:         my $toggle_catscomm_dom = ' checked="checked" ';
11327:         my $can_catcomm_comm = ' ';
11328:         my $can_catcomm_dom = ' checked="checked" ';
11329:         my $toggle_catsplace_place = ' ';
11330:         my $toggle_catsplace_dom = ' checked="checked" ';
11331:         my $can_catplace_place = ' ';
11332:         my $can_catplace_dom = ' checked="checked" ';
11333: 
11334:         if (ref($settings) eq 'HASH') {
11335:             if ($settings->{'togglecats'} eq 'crs') {
11336:                 $toggle_cats_crs = $toggle_cats_dom;
11337:                 $toggle_cats_dom = ' ';
11338:             }
11339:             if ($settings->{'categorize'} eq 'crs') {
11340:                 $can_cat_crs = $can_cat_dom;
11341:                 $can_cat_dom = ' ';
11342:             }
11343:             if ($settings->{'togglecatscomm'} eq 'comm') {
11344:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
11345:                 $toggle_catscomm_dom = ' ';
11346:             }
11347:             if ($settings->{'categorizecomm'} eq 'comm') {
11348:                 $can_catcomm_comm = $can_catcomm_dom;
11349:                 $can_catcomm_dom = ' ';
11350:             }
11351:             if ($settings->{'togglecatsplace'} eq 'place') {
11352:                 $toggle_catsplace_place = $toggle_catsplace_dom;
11353:                 $toggle_catsplace_dom = ' ';
11354:             }
11355:             if ($settings->{'categorizeplace'} eq 'place') {
11356:                 $can_catplace_place = $can_catplace_dom;
11357:                 $can_catplace_dom = ' ';
11358:             }
11359:         }
11360:         my %title = &Apache::lonlocal::texthash (
11361:                      togglecats      => 'Show/Hide a course in catalog',
11362:                      togglecatscomm  => 'Show/Hide a community in catalog',
11363:                      togglecatsplace => 'Show/Hide a placement test in catalog',
11364:                      categorize      => 'Assign a category to a course',
11365:                      categorizecomm  => 'Assign a category to a community',
11366:                      categorizeplace => 'Assign a category to a placement test',
11367:                     );
11368:         my %level = &Apache::lonlocal::texthash (
11369:                      dom   => 'Set in Domain',
11370:                      crs   => 'Set in Course',
11371:                      comm  => 'Set in Community',
11372:                      place => 'Set in Placement Test',
11373:                     );
11374:         $datatable = '<tr class="LC_odd_row">'.
11375:                   '<td>'.$title{'togglecats'}.'</td>'.
11376:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11377:                   '<input type="radio" name="togglecats"'.
11378:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11379:                   '<label><input type="radio" name="togglecats"'.
11380:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
11381:                   '</tr><tr>'.
11382:                   '<td>'.$title{'categorize'}.'</td>'.
11383:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11384:                   '<label><input type="radio" name="categorize"'.
11385:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11386:                   '<label><input type="radio" name="categorize"'.
11387:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
11388:                   '</tr><tr class="LC_odd_row">'.
11389:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
11390:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11391:                   '<input type="radio" name="togglecatscomm"'.
11392:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11393:                   '<label><input type="radio" name="togglecatscomm"'.
11394:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
11395:                   '</tr><tr>'.
11396:                   '<td>'.$title{'categorizecomm'}.'</td>'.
11397:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11398:                   '<label><input type="radio" name="categorizecomm"'.
11399:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11400:                   '<label><input type="radio" name="categorizecomm"'.
11401:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
11402:                   '</tr><tr class="LC_odd_row">'.
11403:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
11404:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11405:                   '<input type="radio" name="togglecatsplace"'.
11406:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11407:                   '<label><input type="radio" name="togglecatscomm"'.
11408:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
11409:                   '</tr><tr>'.
11410:                   '<td>'.$title{'categorizeplace'}.'</td>'.
11411:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11412:                   '<label><input type="radio" name="categorizeplace"'.
11413:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11414:                   '<label><input type="radio" name="categorizeplace"'.
11415:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
11416:                   '</tr>';
11417:         $$rowtotal += 6;
11418:     } else {
11419:         my $css_class;
11420:         my $itemcount = 1;
11421:         my $cathash; 
11422:         if (ref($settings) eq 'HASH') {
11423:             $cathash = $settings->{'cats'};
11424:         }
11425:         if (ref($cathash) eq 'HASH') {
11426:             my (@cats,@trails,%allitems,%idx,@jsarray);
11427:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11428:                                                    \%allitems,\%idx,\@jsarray);
11429:             my $maxdepth = scalar(@cats);
11430:             my $colattrib = '';
11431:             if ($maxdepth > 2) {
11432:                 $colattrib = ' colspan="2" ';
11433:             }
11434:             my @path;
11435:             if (@cats > 0) {
11436:                 if (ref($cats[0]) eq 'ARRAY') {
11437:                     my $numtop = @{$cats[0]};
11438:                     my $maxnum = $numtop;
11439:                     my %default_names = (
11440:                           instcode    => &mt('Official courses'),
11441:                           communities => &mt('Communities'),
11442:                           placement   => &mt('Placement Tests'),
11443:                     );
11444: 
11445:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11446:                         ($cathash->{'instcode::0'} eq '') ||
11447:                         (!grep(/^communities$/,@{$cats[0]})) || 
11448:                         ($cathash->{'communities::0'} eq '') ||
11449:                         (!grep(/^placement$/,@{$cats[0]})) ||
11450:                         ($cathash->{'placement::0'} eq '')) {
11451:                         $maxnum ++;
11452:                     }
11453:                     my $lastidx;
11454:                     for (my $i=0; $i<$numtop; $i++) {
11455:                         my $parent = $cats[0][$i];
11456:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11457:                         my $item = &escape($parent).'::0';
11458:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11459:                         $lastidx = $idx{$item};
11460:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11461:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11462:                         for (my $k=0; $k<=$maxnum; $k++) {
11463:                             my $vpos = $k+1;
11464:                             my $selstr;
11465:                             if ($k == $i) {
11466:                                 $selstr = ' selected="selected" ';
11467:                             }
11468:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11469:                         }
11470:                         $datatable .= '</select></span></td><td>';
11471:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11472:                             $datatable .=  '<span class="LC_nobreak">'
11473:                                            .$default_names{$parent}.'</span>';
11474:                             if ($parent eq 'instcode') {
11475:                                 $datatable .= '<br /><span class="LC_nobreak">('
11476:                                               .&mt('with institutional codes')
11477:                                               .')</span></td><td'.$colattrib.'>';
11478:                             } else {
11479:                                 $datatable .= '<table><tr><td>';
11480:                             }
11481:                             $datatable .= '<span class="LC_nobreak">'
11482:                                           .'<label><input type="radio" name="'
11483:                                           .$parent.'" value="1" checked="checked" />'
11484:                                           .&mt('Display').'</label>';
11485:                             if ($parent eq 'instcode') {
11486:                                 $datatable .= '&nbsp;';
11487:                             } else {
11488:                                 $datatable .= '</span></td></tr><tr><td>'
11489:                                               .'<span class="LC_nobreak">';
11490:                             }
11491:                             $datatable .= '<label><input type="radio" name="'
11492:                                           .$parent.'" value="0" />'
11493:                                           .&mt('Do not display').'</label></span>';
11494:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11495:                                 $datatable .= '</td></tr></table>';
11496:                             }
11497:                             $datatable .= '</td>';
11498:                         } else {
11499:                             $datatable .= $parent
11500:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11501:                                           .'<input type="checkbox" name="deletecategory" '
11502:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11503:                         }
11504:                         my $depth = 1;
11505:                         push(@path,$parent);
11506:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11507:                         pop(@path);
11508:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11509:                         $itemcount ++;
11510:                     }
11511:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11512:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11513:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11514:                     for (my $k=0; $k<=$maxnum; $k++) {
11515:                         my $vpos = $k+1;
11516:                         my $selstr;
11517:                         if ($k == $numtop) {
11518:                             $selstr = ' selected="selected" ';
11519:                         }
11520:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11521:                     }
11522:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11523:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11524:                                   .'</tr>'."\n";
11525:                     $itemcount ++;
11526:                     foreach my $default ('instcode','communities','placement') {
11527:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11528:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11529:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11530:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11531:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11532:                             for (my $k=0; $k<=$maxnum; $k++) {
11533:                                 my $vpos = $k+1;
11534:                                 my $selstr;
11535:                                 if ($k == $maxnum) {
11536:                                     $selstr = ' selected="selected" ';
11537:                                 }
11538:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11539:                             }
11540:                             $datatable .= '</select></span></td>'.
11541:                                           '<td><span class="LC_nobreak">'.
11542:                                           $default_names{$default}.'</span>';
11543:                             if ($default eq 'instcode') {
11544:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11545:                                               .&mt('with institutional codes').')</span>';
11546:                             }
11547:                             $datatable .= '</td>'
11548:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11549:                                           .&mt('Display').'</label>&nbsp;'
11550:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11551:                                           .&mt('Do not display').'</label></span></td></tr>';
11552:                         }
11553:                     }
11554:                 }
11555:             } else {
11556:                 $datatable .= &initialize_categories($itemcount);
11557:             }
11558:         } else {
11559:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11560:                           .&initialize_categories($itemcount);
11561:         }
11562:         $$rowtotal += $itemcount;
11563:     }
11564:     return $datatable;
11565: }
11566: 
11567: sub print_serverstatuses {
11568:     my ($dom,$settings,$rowtotal) = @_;
11569:     my $datatable;
11570:     my @pages = &serverstatus_pages();
11571:     my (%namedaccess,%machineaccess);
11572:     foreach my $type (@pages) {
11573:         $namedaccess{$type} = '';
11574:         $machineaccess{$type}= '';
11575:     }
11576:     if (ref($settings) eq 'HASH') {
11577:         foreach my $type (@pages) {
11578:             if (exists($settings->{$type})) {
11579:                 if (ref($settings->{$type}) eq 'HASH') {
11580:                     foreach my $key (keys(%{$settings->{$type}})) {
11581:                         if ($key eq 'namedusers') {
11582:                             $namedaccess{$type} = $settings->{$type}->{$key};
11583:                         } elsif ($key eq 'machines') {
11584:                             $machineaccess{$type} = $settings->{$type}->{$key};
11585:                         }
11586:                     }
11587:                 }
11588:             }
11589:         }
11590:     }
11591:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11592:     my $rownum = 0;
11593:     my $css_class;
11594:     foreach my $type (@pages) {
11595:         $rownum ++;
11596:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11597:         $datatable .= '<tr'.$css_class.'>'.
11598:                       '<td><span class="LC_nobreak">'.
11599:                       $titles->{$type}.'</span></td>'.
11600:                       '<td class="LC_left_item">'.
11601:                       '<input type="text" name="'.$type.'_namedusers" '.
11602:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11603:                       '<td class="LC_right_item">'.
11604:                       '<span class="LC_nobreak">'.
11605:                       '<input type="text" name="'.$type.'_machines" '.
11606:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11607:                       '</span></td></tr>'."\n";
11608:     }
11609:     $$rowtotal += $rownum;
11610:     return $datatable;
11611: }
11612: 
11613: sub serverstatus_pages {
11614:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11615:             'checksums','clusterstatus','certstatus','metadata_keywords',
11616:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11617:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11618: }
11619: 
11620: sub defaults_javascript {
11621:     my ($settings) = @_;
11622:     return unless (ref($settings) eq 'HASH');
11623:     my $portal_js = <<"ENDPORTAL";
11624: 
11625: function portalExtras(caller) {
11626:     var x = caller.value;
11627:     var y = new Array('email','web');
11628:     for (var i=0; i<y.length; i++) {
11629:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11630:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11631:             if (x.length > 0) {
11632:                 z.style.display = 'block';
11633:             } else {
11634:                 z.style.display = 'none';
11635:             }
11636:         }
11637:     }
11638: }
11639: ENDPORTAL
11640:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11641:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11642:         if ($maxnum eq '') {
11643:             $maxnum = 0;
11644:         }
11645:         $maxnum ++;
11646:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11647:         return <<"ENDSCRIPT";
11648: <script type="text/javascript">
11649: // <![CDATA[
11650: function reorderTypes(form,caller) {
11651:     var changedVal;
11652: $jstext 
11653:     var newpos = 'addinststatus_pos';
11654:     var current = new Array;
11655:     var maxh = $maxnum;
11656:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11657:     var oldVal;
11658:     if (caller == newpos) {
11659:         changedVal = newitemVal;
11660:     } else {
11661:         var curritem = 'inststatus_pos_'+caller;
11662:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11663:         current[newitemVal] = newpos;
11664:     }
11665:     for (var i=0; i<inststatuses.length; i++) {
11666:         if (inststatuses[i] != caller) {
11667:             var elementName = 'inststatus_pos_'+inststatuses[i];
11668:             if (form.elements[elementName]) {
11669:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11670:                 current[currVal] = elementName;
11671:             }
11672:         }
11673:     }
11674:     for (var j=0; j<maxh; j++) {
11675:         if (current[j] == undefined) {
11676:             oldVal = j;
11677:         }
11678:     }
11679:     if (oldVal < changedVal) {
11680:         for (var k=oldVal+1; k<=changedVal ; k++) {
11681:            var elementName = current[k];
11682:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11683:         }
11684:     } else {
11685:         for (var k=changedVal; k<oldVal; k++) {
11686:             var elementName = current[k];
11687:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11688:         }
11689:     }
11690:     return;
11691: }
11692: 
11693: $portal_js
11694: 
11695: // ]]>
11696: </script>
11697: 
11698: ENDSCRIPT
11699:     } else {
11700: return <<"ENDSCRIPT";
11701: <script type="text/javascript">
11702: // <![CDATA[
11703: $portal_js
11704: // ]]>
11705: </script>
11706: 
11707: ENDSCRIPT
11708:     }
11709:     return;
11710: }
11711: 
11712: sub passwords_javascript {
11713:     my ($prefix) = @_;
11714:     my %intalert;
11715:     if ($prefix eq 'passwords') {
11716:         %intalert = &Apache::lonlocal::texthash (
11717:             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.',
11718:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11719:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11720:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11721:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11722:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11723:         );
11724:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11725:         %intalert = &Apache::lonlocal::texthash (
11726:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11727:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11728:         );
11729:     }
11730:     &js_escape(\%intalert);
11731:     my $defmin = $Apache::lonnet::passwdmin;
11732:     my $intauthjs;
11733:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11734: 
11735: function warnIntAuth(field) {
11736:     if (field.name == 'intauth_check') {
11737:         if (field.value == '2') {
11738:             alert('$intalert{authcheck}');
11739:         }
11740:     }
11741:     if (field.name == 'intauth_cost') {
11742:         field.value.replace(/\s/g,'');
11743:         if (field.value != '') {
11744:             var regexdigit=/^\\d+\$/;
11745:             if (!regexdigit.test(field.value)) {
11746:                 alert('$intalert{authcost}');
11747:             }
11748:         }
11749:     }
11750:     return;
11751: }
11752: 
11753: ENDSCRIPT
11754: 
11755:      }
11756: 
11757:      $intauthjs .= <<"ENDSCRIPT";
11758: 
11759: function warnInt$prefix(field) {
11760:     field.value.replace(/^\s+/,'');
11761:     field.value.replace(/\s+\$/,'');
11762:     var regexdigit=/^\\d+\$/;
11763:     if (field.name == '${prefix}_min') {
11764:         if (field.value == '') {
11765:             alert('$intalert{passmin}');
11766:             field.value = '$defmin';
11767:         } else {
11768:             if (!regexdigit.test(field.value)) {
11769:                 alert('$intalert{passmin}');
11770:                 field.value = '$defmin';
11771:             }
11772:             var minval = parseInt(field.value,10);
11773:             if (minval < $defmin) {
11774:                 alert('$intalert{passmin}');
11775:                 field.value = '$defmin';
11776:             }
11777:         }
11778:     } else {
11779:         if (field.value == '0') {
11780:             field.value = '';
11781:         }
11782:         if (field.value != '') {
11783:             if (field.name == '${prefix}_expire') {
11784:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11785:                 if (!regexpposnum.test(field.value)) {
11786:                     alert('$intalert{passexp}');
11787:                     field.value = '';
11788:                 } else {
11789:                     var expval = parseFloat(field.value);
11790:                     if (expval == 0) {
11791:                         alert('$intalert{passexp}');
11792:                         field.value = '';
11793:                     }
11794:                 }
11795:             } else {
11796:                 if (!regexdigit.test(field.value)) {
11797:                     if (field.name == '${prefix}_max') {
11798:                         alert('$intalert{passmax}');
11799:                     } else {
11800:                         if (field.name == '${prefix}_numsaved') {
11801:                             alert('$intalert{passnum}');
11802:                         }
11803:                     }
11804:                     field.value = '';
11805:                 }
11806:             }
11807:         }
11808:     }
11809:     return;
11810: }
11811: 
11812: ENDSCRIPT
11813:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11814: }
11815: 
11816: sub coursecategories_javascript {
11817:     my ($settings) = @_;
11818:     my ($output,$jstext,$cathash);
11819:     if (ref($settings) eq 'HASH') {
11820:         $cathash = $settings->{'cats'};
11821:     }
11822:     if (ref($cathash) eq 'HASH') {
11823:         my (@cats,@jsarray,%idx);
11824:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11825:         if (@jsarray > 0) {
11826:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11827:             for (my $i=0; $i<@jsarray; $i++) {
11828:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11829:                     my $catstr = join('","',@{$jsarray[$i]});
11830:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11831:                 }
11832:             }
11833:         }
11834:     } else {
11835:         $jstext  = '    var categories = Array(1);'."\n".
11836:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11837:     }
11838:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11839:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11840:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11841:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11842:     &js_escape(\$instcode_reserved);
11843:     &js_escape(\$communities_reserved);
11844:     &js_escape(\$placement_reserved);
11845:     &js_escape(\$choose_again);
11846:     $output = <<"ENDSCRIPT";
11847: <script type="text/javascript">
11848: // <![CDATA[
11849: function reorderCats(form,parent,item,idx) {
11850:     var changedVal;
11851: $jstext
11852:     var newpos = 'addcategory_pos';
11853:     if (parent == '') {
11854:         var has_instcode = 0;
11855:         var maxtop = categories[idx].length;
11856:         for (var j=0; j<maxtop; j++) {
11857:             if (categories[idx][j] == 'instcode::0') {
11858:                 has_instcode == 1;
11859:             }
11860:         }
11861:         if (has_instcode == 0) {
11862:             categories[idx][maxtop] = 'instcode_pos';
11863:         }
11864:     } else {
11865:         newpos += '_'+parent;
11866:     }
11867:     var maxh = 1 + categories[idx].length;
11868:     var current = new Array;
11869:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11870:     if (item == newpos) {
11871:         changedVal = newitemVal;
11872:     } else {
11873:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11874:         current[newitemVal] = newpos;
11875:     }
11876:     for (var i=0; i<categories[idx].length; i++) {
11877:         var elementName = categories[idx][i];
11878:         if (elementName != item) {
11879:             if (form.elements[elementName]) {
11880:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11881:                 current[currVal] = elementName;
11882:             }
11883:         }
11884:     }
11885:     var oldVal;
11886:     for (var j=0; j<maxh; j++) {
11887:         if (current[j] == undefined) {
11888:             oldVal = j;
11889:         }
11890:     }
11891:     if (oldVal < changedVal) {
11892:         for (var k=oldVal+1; k<=changedVal ; k++) {
11893:            var elementName = current[k];
11894:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11895:         }
11896:     } else {
11897:         for (var k=changedVal; k<oldVal; k++) {
11898:             var elementName = current[k];
11899:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11900:         }
11901:     }
11902:     return;
11903: }
11904: 
11905: function categoryCheck(form) {
11906:     if (form.elements['addcategory_name'].value == 'instcode') {
11907:         alert('$instcode_reserved\\n$choose_again');
11908:         return false;
11909:     }
11910:     if (form.elements['addcategory_name'].value == 'communities') {
11911:         alert('$communities_reserved\\n$choose_again');
11912:         return false;
11913:     }
11914:     if (form.elements['addcategory_name'].value == 'placement') {
11915:         alert('$placement_reserved\\n$choose_again');
11916:         return false;
11917:     }
11918:     return true;
11919: }
11920: 
11921: // ]]>
11922: </script>
11923: 
11924: ENDSCRIPT
11925:     return $output;
11926: }
11927: 
11928: sub initialize_categories {
11929:     my ($itemcount) = @_;
11930:     my ($datatable,$css_class,$chgstr);
11931:     my %default_names = &Apache::lonlocal::texthash (
11932:                       instcode    => 'Official courses (with institutional codes)',
11933:                       communities => 'Communities',
11934:                       placement   => 'Placement Tests',
11935:                         );
11936:     my %selnum = (
11937:                    instcode    => '0',
11938:                    communities => '1',
11939:                    placement   => '2',
11940:                  );
11941:     my %selected;
11942:     foreach my $default ('instcode','communities','placement') {
11943:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11944:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11945:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11946:         $selected{$selnum{$default}} = ' selected="selected"';
11947:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11948:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11949:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11950:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11951:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11952:                      .'<option value="3">4</option></select>&nbsp;'
11953:                      .$default_names{$default}
11954:                      .'</span></td><td><span class="LC_nobreak">'
11955:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11956:                      .&mt('Display').'</label>&nbsp;<label>'
11957:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11958:                  .'</label></span></td></tr>';
11959:         $itemcount ++;
11960:     }
11961:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11962:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11963:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11964:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11965:                   .'<option value="0">1</option>'
11966:                   .'<option value="1">2</option>'
11967:                   .'<option value="2">3</option>'
11968:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11969:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11970:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11971:                   .'</td></tr>';
11972:     return $datatable;
11973: }
11974: 
11975: sub build_category_rows {
11976:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11977:     my ($text,$name,$item,$chgstr);
11978:     if (ref($cats) eq 'ARRAY') {
11979:         my $maxdepth = scalar(@{$cats});
11980:         if (ref($cats->[$depth]) eq 'HASH') {
11981:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11982:                 my $numchildren = @{$cats->[$depth]{$parent}};
11983:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11984:                 $text .= '<td><table class="LC_data_table">';
11985:                 my ($idxnum,$parent_name,$parent_item);
11986:                 my $higher = $depth - 1;
11987:                 if ($higher == 0) {
11988:                     $parent_name = &escape($parent).'::'.$higher;
11989:                 } else {
11990:                     if (ref($path) eq 'ARRAY') {
11991:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11992:                     }
11993:                 }
11994:                 $parent_item = 'addcategory_pos_'.$parent_name;
11995:                 for (my $j=0; $j<=$numchildren; $j++) {
11996:                     if ($j < $numchildren) {
11997:                         $name = $cats->[$depth]{$parent}[$j];
11998:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11999:                         $idxnum = $idx->{$item};
12000:                     } else {
12001:                         $name = $parent_name;
12002:                         $item = $parent_item;
12003:                     }
12004:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
12005:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
12006:                     for (my $i=0; $i<=$numchildren; $i++) {
12007:                         my $vpos = $i+1;
12008:                         my $selstr;
12009:                         if ($j == $i) {
12010:                             $selstr = ' selected="selected" ';
12011:                         }
12012:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
12013:                     }
12014:                     $text .= '</select>&nbsp;';
12015:                     if ($j < $numchildren) {
12016:                         my $deeper = $depth+1;
12017:                         $text .= $name.'&nbsp;'
12018:                                  .'<label><input type="checkbox" name="deletecategory" value="'
12019:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
12020:                         if(ref($path) eq 'ARRAY') {
12021:                             push(@{$path},$name);
12022:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
12023:                             pop(@{$path});
12024:                         }
12025:                     } else {
12026:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
12027:                         if ($j == $numchildren) {
12028:                             $text .= $name;
12029:                         } else {
12030:                             $text .= $item;
12031:                         }
12032:                         $text .= '" value="" />';
12033:                     }
12034:                     $text .= '</td></tr>';
12035:                 }
12036:                 $text .= '</table></td>';
12037:             } else {
12038:                 my $higher = $depth-1;
12039:                 if ($higher == 0) {
12040:                     $name = &escape($parent).'::'.$higher;
12041:                 } else {
12042:                     if (ref($path) eq 'ARRAY') {
12043:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12044:                     }
12045:                 }
12046:                 my $colspan;
12047:                 if ($parent ne 'instcode') {
12048:                     $colspan = $maxdepth - $depth - 1;
12049:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
12050:                 }
12051:             }
12052:         }
12053:     }
12054:     return $text;
12055: }
12056: 
12057: sub modifiable_userdata_row {
12058:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
12059:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
12060:     my ($role,$rolename,$statustype);
12061:     $role = $item;
12062:     if ($context eq 'cancreate') {
12063:         if ($item =~ /^(emailusername)_(.+)$/) {
12064:             $role = $1;
12065:             $statustype = $2;
12066:             if (ref($usertypes) eq 'HASH') {
12067:                 if ($usertypes->{$statustype}) {
12068:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
12069:                 } else {
12070:                     $rolename = &mt('Data provided by user');
12071:                 }
12072:             }
12073:         }
12074:     } elsif ($context eq 'selfcreate') {
12075:         if (ref($usertypes) eq 'HASH') {
12076:             $rolename = $usertypes->{$role};
12077:         } else {
12078:             $rolename = $role;
12079:         }
12080:     } elsif ($context eq 'lti') {
12081:         $rolename = &mt('Institutional data used (if available)');
12082:     } elsif ($context eq 'privacy') {
12083:         $rolename = $itemdesc;
12084:     } else {
12085:         if ($role eq 'cr') {
12086:             $rolename = &mt('Custom role');
12087:         } else {
12088:             $rolename = &Apache::lonnet::plaintext($role);
12089:         }
12090:     }
12091:     my (@fields,%fieldtitles);
12092:     if (ref($fieldsref) eq 'ARRAY') {
12093:         @fields = @{$fieldsref};
12094:     } else {
12095:         @fields = ('lastname','firstname','middlename','generation',
12096:                    'permanentemail','id');
12097:     }
12098:     if ((ref($titlesref) eq 'HASH')) {
12099:         %fieldtitles = %{$titlesref};
12100:     } else {
12101:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12102:     }
12103:     my $output;
12104:     my $css_class;
12105:     if ($rowcount%2) {
12106:         $css_class = 'LC_odd_row';
12107:     }
12108:     if ($customcss) {
12109:         $css_class .= " $customcss";
12110:     }
12111:     $css_class =~ s/^\s+//;
12112:     if ($css_class) {
12113:         $css_class = ' class="'.$css_class.'"';
12114:     }
12115:     if ($rowstyle) {
12116:         $css_class .= ' style="'.$rowstyle.'"';
12117:     }
12118:     if ($rowid) {
12119:         $rowid = ' id="'.$rowid.'"';
12120:     }
12121:     $output = '<tr '.$css_class.$rowid.'>'.
12122:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
12123:               '<td class="LC_left_item" colspan="2"><table>';
12124:     my $rem;
12125:     my %checks;
12126:     if (ref($settings) eq 'HASH') {
12127:         my $hashref;
12128:         if ($context eq 'lti') {
12129:             if (ref($settings) eq 'HASH') {
12130:                 $hashref = $settings->{'instdata'};
12131:             }
12132:         } elsif ($context eq 'privacy') {
12133:             my ($key,$inner) = split(/_/,$role);
12134:             if (ref($settings) eq 'HASH') {
12135:                 if (ref($settings->{$key}) eq 'HASH') {
12136:                     $hashref = $settings->{$key}->{$inner};
12137:                 }
12138:             }
12139:         } elsif (ref($settings->{$context}) eq 'HASH') {
12140:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
12141:                 $hashref = $settings->{'lti_instdata'};
12142:             }
12143:             if ($role eq 'emailusername') {
12144:                 if ($statustype) {
12145:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
12146:                         $hashref = $settings->{$context}->{$role}->{$statustype};
12147:                     }
12148:                 }
12149:             }
12150:         }
12151:         if (ref($hashref) eq 'HASH') {
12152:             foreach my $field (@fields) {
12153:                 if ($hashref->{$field}) {
12154:                     if ($role eq 'emailusername') {
12155:                         $checks{$field} = $hashref->{$field};
12156:                     } else {
12157:                         $checks{$field} = ' checked="checked" ';
12158:                     }
12159:                 }
12160:             }
12161:         }
12162:     }
12163:     my $total = scalar(@fields);
12164:     for (my $i=0; $i<$total; $i++) {
12165:         $rem = $i%($numinrow);
12166:         if ($rem == 0) {
12167:             if ($i > 0) {
12168:                 $output .= '</tr>';
12169:             }
12170:             $output .= '<tr>';
12171:         }
12172:         my $check = ' ';
12173:         unless ($role eq 'emailusername') {
12174:             if (exists($checks{$fields[$i]})) {
12175:                 $check = $checks{$fields[$i]};
12176:             } elsif ($context eq 'privacy') {
12177:                 if ($role =~ /^priv_(domain|course)$/) {
12178:                     if (ref($settings) ne 'HASH') {
12179:                         $check = ' checked="checked" ';
12180:                     }
12181:                 } elsif ($role =~ /^priv_(author|community)$/) {
12182:                     if (ref($settings) ne 'HASH') {
12183:                         unless ($fields[$i] eq 'id') {
12184:                             $check = ' checked="checked" ';
12185:                         }
12186:                     }
12187:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
12188:                     if (ref($settings) ne 'HASH') {
12189:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
12190:                             $check = ' checked="checked" ';
12191:                         }
12192:                     }
12193:                 }
12194:             } elsif ($context ne 'lti') {
12195:                 if ($role eq 'st') {
12196:                     if (ref($settings) ne 'HASH') {
12197:                         $check = ' checked="checked" '; 
12198:                     }
12199:                 }
12200:             }
12201:         }
12202:         $output .= '<td class="LC_left_item">'.
12203:                    '<span class="LC_nobreak">';
12204:         my $prefix = 'canmodify';
12205:         if ($role eq 'emailusername') {
12206:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
12207:                 $checks{$fields[$i]} = 'omit';
12208:             }
12209:             foreach my $option ('required','optional','omit') {
12210:                 my $checked='';
12211:                 if ($checks{$fields[$i]} eq $option) {
12212:                     $checked='checked="checked" ';
12213:                 }
12214:                 $output .= '<label>'.
12215:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
12216:                            &mt($option).'</label>'.('&nbsp;' x2);
12217:             }
12218:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
12219:         } else {
12220:             if ($context eq 'lti') {
12221:                 $prefix = 'lti';
12222:             } elsif ($context eq 'privacy') {
12223:                 $prefix = 'privacy';
12224:             }
12225:             $output .= '<label>'.
12226:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
12227:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
12228:                        '</label>';
12229:         }
12230:         $output .= '</span></td>';
12231:     }
12232:     $rem = $total%$numinrow;
12233:     my $colsleft;
12234:     if ($rem) {
12235:         $colsleft = $numinrow - $rem;
12236:     }
12237:     if ($colsleft > 1) {
12238:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
12239:                    '&nbsp;</td>';
12240:     } elsif ($colsleft == 1) {
12241:         $output .= '<td class="LC_left_item">&nbsp;</td>';
12242:     }
12243:     $output .= '</tr></table></td></tr>';
12244:     return $output;
12245: }
12246: 
12247: sub insttypes_row {
12248:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
12249:         $customcss,$rowstyle) = @_;
12250:     my %lt = &Apache::lonlocal::texthash (
12251:                       cansearch      => 'Users allowed to search',
12252:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
12253:                       lockablenames  => 'User preference to lock name',
12254:                       selfassign     => 'Self-reportable affiliations',
12255:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
12256:                       webdav         => 'WebDAV access available',
12257:                       authorquota    => 'Authoring Space quota (MB)',
12258:              );
12259:     my ($showdom,$defaultquota);
12260:     if ($context eq 'cansearch') {
12261:         $showdom = ' ('.$dom.')';
12262:     } elsif ($context eq 'authorquota') {
12263:         $defaultquota = 500;
12264:     }
12265:     my $class = 'LC_left_item';
12266:     if ($context eq 'statustocreate') {
12267:         $class = 'LC_right_item';
12268:     }
12269:     my $css_class;
12270:     if ($$rowtotal%2) {
12271:         $css_class = 'LC_odd_row';
12272:     }
12273:     if ($customcss) {
12274:         $css_class .= ' '.$customcss;
12275:     }
12276:     $css_class =~ s/^\s+//;
12277:     if ($css_class) {
12278:         $css_class = ' class="'.$css_class.'"';
12279:     }
12280:     if ($rowstyle) {
12281:         $css_class .= ' style="'.$rowstyle.'"';
12282:     }
12283:     if ($onclick) {
12284:         $onclick = 'onclick="'.$onclick.'" ';
12285:     }
12286:     my $output = '<tr'.$css_class.'>'.
12287:                  '<td>'.$lt{$context}.$showdom.
12288:                  '</td><td class="'.$class.'" colspan="2"><table>';
12289:     my $rem;
12290:     if (ref($types) eq 'ARRAY') {
12291:         for (my $i=0; $i<@{$types}; $i++) {
12292:             if (defined($usertypes->{$types->[$i]})) {
12293:                 my $rem = $i%($numinrow);
12294:                 if ($rem == 0) {
12295:                     if ($i > 0) {
12296:                         $output .= '</tr>';
12297:                     }
12298:                     $output .= '<tr>';
12299:                 }
12300:                 if ($context eq 'authorquota') {
12301:                     my $currquota;
12302:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
12303:                         $currquota = $settings->{$context}->{$types->[$i]};
12304:                     } else {
12305:                         $currquota = $defaultquota;
12306:                     }
12307:                     $output .= '<td class="LC_left_item">'."\n".
12308:                                '<label><span class="LC_nobreak">'."\n".
12309:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
12310:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
12311:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12312:                                '</label></td>';
12313:                 } else {
12314:                     my $check = ' ';
12315:                     if (ref($settings) eq 'HASH') {
12316:                         if (ref($settings->{$context}) eq 'ARRAY') {
12317:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
12318:                                 $check = ' checked="checked" ';
12319:                             }
12320:                         } elsif (ref($settings->{$context}) eq 'HASH') {
12321:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
12322:                                 $check = ' checked="checked" ';
12323:                             } elsif ($context eq 'webdav') {
12324:                                 if ($settings->{$context}->{$types->[$i]}) {
12325:                                     $check = ' checked="checked" ';
12326:                                 }
12327:                             }
12328:                         } elsif ($context eq 'statustocreate') {
12329:                             $check = ' checked="checked" ';
12330:                         }
12331:                     }
12332:                     $output .= '<td class="LC_left_item">'.
12333:                                '<span class="LC_nobreak"><label>'.
12334:                                '<input type="checkbox" name="'.$context.'" '.
12335:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
12336:                                $usertypes->{$types->[$i]}.'</label></span></td>';
12337:                 }
12338:             }
12339:         }
12340:         $rem = @{$types}%($numinrow);
12341:     }
12342:     my $colsleft = $numinrow - $rem;
12343:     if ($context eq 'overrides') {
12344:         if ($colsleft > 1) {
12345:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12346:         } else {
12347:             $output .= '<td class="LC_left_item">';
12348:         }
12349:         $output .= '&nbsp;';
12350:     } else {
12351:         if ($rem == 0) {
12352:             $output .= '<tr>';
12353:         }
12354:         if ($colsleft > 1) {
12355:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12356:         } else {
12357:             $output .= '<td class="LC_left_item">';
12358:         }
12359:         if ($context eq 'authorquota') {
12360:             my $currquota = 500;
12361:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
12362:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
12363:                     $currquota = $settings->{$context}{'default'};
12364:                 }
12365:             }
12366:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
12367:                        '<input type="text" name="'.$context.'_default" '.
12368:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12369:                        '</label>';
12370:         } else {
12371:             my $defcheck = ' ';
12372:             if (ref($settings) eq 'HASH') {
12373:                 if (ref($settings->{$context}) eq 'ARRAY') {
12374:                     if (grep(/^default$/,@{$settings->{$context}})) {
12375:                         $defcheck = ' checked="checked" ';
12376:                     }
12377:                 } elsif (ref($settings->{$context}) eq 'HASH') {
12378:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
12379:                         $defcheck = ' checked="checked" ';
12380:                     } elsif ($context eq 'webdav') {
12381:                         if ($settings->{$context}->{'default'}) {
12382:                             $defcheck = ' checked="checked" ';
12383:                         }
12384:                     }
12385:                 } elsif ($context eq 'statustocreate') {
12386:                     $defcheck = ' checked="checked" ';
12387:                 }
12388:             }
12389:             $output .= '<span class="LC_nobreak"><label>'.
12390:                        '<input type="checkbox" name="'.$context.'" '.
12391:                        'value="default"'.$defcheck.$onclick.'/>'.
12392:                        $othertitle.'</label></span>';
12393:         }
12394:     }
12395:     $output .= '</td></tr></table></td></tr>';
12396:     return $output;
12397: }
12398: 
12399: sub sorted_searchtitles {
12400:     my %searchtitles = &Apache::lonlocal::texthash(
12401:                          'uname' => 'username',
12402:                          'lastname' => 'last name',
12403:                          'lastfirst' => 'last name, first name',
12404:                      );
12405:     my @titleorder = ('uname','lastname','lastfirst');
12406:     return (\%searchtitles,\@titleorder);
12407: }
12408: 
12409: sub sorted_searchtypes {
12410:     my %srchtypes_desc = (
12411:                            exact    => 'is exact match',
12412:                            contains => 'contains ..',
12413:                            begins   => 'begins with ..',
12414:                          );
12415:     my @srchtypeorder = ('exact','begins','contains');
12416:     return (\%srchtypes_desc,\@srchtypeorder);
12417: }
12418: 
12419: sub usertype_update_row {
12420:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
12421:     my $datatable;
12422:     my $numinrow = 4;
12423:     foreach my $type (@{$types}) {
12424:         if (defined($usertypes->{$type})) {
12425:             $$rownums ++;
12426:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
12427:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
12428:                           '</td><td class="LC_left_item"><table>';
12429:             for (my $i=0; $i<@{$fields}; $i++) {
12430:                 my $rem = $i%($numinrow);
12431:                 if ($rem == 0) {
12432:                     if ($i > 0) {
12433:                         $datatable .= '</tr>';
12434:                     }
12435:                     $datatable .= '<tr>';
12436:                 }
12437:                 my $check = ' ';
12438:                 if (ref($settings) eq 'HASH') {
12439:                     if (ref($settings->{'fields'}) eq 'HASH') {
12440:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
12441:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
12442:                                 $check = ' checked="checked" ';
12443:                             }
12444:                         }
12445:                     }
12446:                 }
12447: 
12448:                 if ($i == @{$fields}-1) {
12449:                     my $colsleft = $numinrow - $rem;
12450:                     if ($colsleft > 1) {
12451:                         $datatable .= '<td colspan="'.$colsleft.'">';
12452:                     } else {
12453:                         $datatable .= '<td>';
12454:                     }
12455:                 } else {
12456:                     $datatable .= '<td>';
12457:                 }
12458:                 $datatable .= '<span class="LC_nobreak"><label>'.
12459:                               '<input type="checkbox" name="updateable_'.$type.
12460:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
12461:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
12462:             }
12463:             $datatable .= '</tr></table></td></tr>';
12464:         }
12465:     }
12466:     return $datatable;
12467: }
12468: 
12469: sub modify_login {
12470:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12471:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12472:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12473:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12474:     %title = ( coursecatalog => 'Display course catalog',
12475:                adminmail => 'Display administrator E-mail address',
12476:                helpdesk  => 'Display "Contact Helpdesk" link',
12477:                newuser => 'Link for visitors to create a user account',
12478:                loginheader => 'Log-in box header',
12479:                saml => 'Dual SSO and non-SSO login');
12480:     @offon = ('off','on');
12481:     if (ref($domconfig{login}) eq 'HASH') {
12482:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12483:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12484:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12485:             }
12486:         }
12487:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12488:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12489:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12490:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12491:                     $saml{$lonhost} = 1;
12492:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12493:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12494:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12495:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12496:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12497:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12498:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12499:                 }
12500:             }
12501:         }
12502:     }
12503:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12504:                                            \%domconfig,\%loginhash);
12505:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12506:     foreach my $item (@toggles) {
12507:         $loginhash{login}{$item} = $env{'form.'.$item};
12508:     }
12509:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12510:     if (ref($colchanges{'login'}) eq 'HASH') {  
12511:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12512:                                          \%loginhash);
12513:     }
12514: 
12515:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12516:     my %domservers = &Apache::lonnet::get_servers($dom);
12517:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12518:     if (keys(%servers) > 1) {
12519:         foreach my $lonhost (keys(%servers)) {
12520:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12521:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12522:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12523:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12524:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12525:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12526:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12527:                         $changes{'loginvia'}{$lonhost} = 1;
12528:                     } else {
12529:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12530:                         $changes{'loginvia'}{$lonhost} = 1;
12531:                     }
12532:                 } else {
12533:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12534:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12535:                         $changes{'loginvia'}{$lonhost} = 1;
12536:                     }
12537:                 }
12538:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12539:                     foreach my $item (@loginvia_attribs) {
12540:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12541:                     }
12542:                 } else {
12543:                     foreach my $item (@loginvia_attribs) {
12544:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12545:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12546:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12547:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12548:                                 $new = '/';
12549:                             }
12550:                         }
12551:                         if (($item eq 'custompath') && 
12552:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12553:                             $new = '';
12554:                         }
12555:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12556:                             $changes{'loginvia'}{$lonhost} = 1;
12557:                         }
12558:                         if ($item eq 'exempt') {
12559:                             $new = &check_exempt_addresses($new);
12560:                         }
12561:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12562:                     }
12563:                 }
12564:             } else {
12565:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12566:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12567:                     $changes{'loginvia'}{$lonhost} = 1;
12568:                     foreach my $item (@loginvia_attribs) {
12569:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12570:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12571:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12572:                                 $new = '/';
12573:                             }
12574:                         }
12575:                         if (($item eq 'custompath') && 
12576:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12577:                             $new = '';
12578:                         }
12579:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12580:                     }
12581:                 }
12582:             }
12583:         }
12584:     }
12585: 
12586:     my $servadm = $r->dir_config('lonAdmEMail');
12587:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12588:     if (ref($domconfig{'login'}) eq 'HASH') {
12589:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12590:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12591:                 if ($lang eq 'nolang') {
12592:                     push(@currlangs,$lang);
12593:                 } elsif (defined($langchoices{$lang})) {
12594:                     push(@currlangs,$lang);
12595:                 } else {
12596:                     next;
12597:                 }
12598:             }
12599:         }
12600:     }
12601:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12602:     if (@currlangs > 0) {
12603:         foreach my $lang (@currlangs) {
12604:             if (grep(/^\Q$lang\E$/,@delurls)) {
12605:                 $changes{'helpurl'}{$lang} = 1;
12606:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12607:                 $changes{'helpurl'}{$lang} = 1;
12608:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12609:                 push(@newlangs,$lang);
12610:             } else {
12611:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12612:             }
12613:         }
12614:     }
12615:     unless (grep(/^nolang$/,@currlangs)) {
12616:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12617:             $changes{'helpurl'}{'nolang'} = 1;
12618:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12619:             push(@newlangs,'nolang');
12620:         }
12621:     }
12622:     if ($env{'form.loginhelpurl_add_lang'}) {
12623:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12624:             ($env{'form.loginhelpurl_add_file.filename'})) {
12625:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12626:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12627:         }
12628:     }
12629:     if ((@newlangs > 0) || ($addedfile)) {
12630:         my $error;
12631:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12632:         if ($configuserok eq 'ok') {
12633:             if ($switchserver) {
12634:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12635:             } elsif ($author_ok eq 'ok') {
12636:                 my @allnew = @newlangs;
12637:                 if ($addedfile ne '') {
12638:                     push(@allnew,$addedfile);
12639:                 }
12640:                 my $modified = [];
12641:                 foreach my $lang (@allnew) {
12642:                     my $formelem = 'loginhelpurl_'.$lang;
12643:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12644:                         $formelem = 'loginhelpurl_add_file';
12645:                     }
12646:                     (my $result,$newurl{$lang}) =
12647:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12648:                                                                 "help/$lang",'','',$newfile{$lang},
12649:                                                                 $modified);
12650:                     if ($result eq 'ok') {
12651:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12652:                         $changes{'helpurl'}{$lang} = 1;
12653:                     } else {
12654:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12655:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12656:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12657:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12658:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12659:                         }
12660:                     }
12661:                 }
12662:                 &update_modify_urls($r,$modified);
12663:             } else {
12664:                 $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);
12665:             }
12666:         } else {
12667:             $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);
12668:         }
12669:         if ($error) {
12670:             &Apache::lonnet::logthis($error);
12671:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12672:         }
12673:     }
12674: 
12675:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12676:     if (ref($domconfig{'login'}) eq 'HASH') {
12677:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12678:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12679:                 if ($domservers{$lonhost}) {
12680:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12681:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12682:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12683:                     }
12684:                 }
12685:             }
12686:         }
12687:     }
12688:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12689:     foreach my $lonhost (sort(keys(%domservers))) {
12690:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12691:             $changes{'headtag'}{$lonhost} = 1;
12692:         } else {
12693:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12694:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12695:             }
12696:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12697:                 push(@newhosts,$lonhost);
12698:             } elsif ($currheadtagurls{$lonhost}) {
12699:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12700:                 if ($currexempt{$lonhost}) {
12701:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12702:                         $changes{'headtag'}{$lonhost} = 1;
12703:                     }
12704:                 } elsif ($possexempt{$lonhost}) {
12705:                     $changes{'headtag'}{$lonhost} = 1;
12706:                 }
12707:                 if ($possexempt{$lonhost}) {
12708:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12709:                 }
12710:             }
12711:         }
12712:     }
12713:     if (@newhosts) {
12714:         my $error;
12715:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12716:         if ($configuserok eq 'ok') {
12717:             if ($switchserver) {
12718:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12719:             } elsif ($author_ok eq 'ok') {
12720:                 my $modified = [];
12721:                 foreach my $lonhost (@newhosts) {
12722:                     my $formelem = 'loginheadtag_'.$lonhost;
12723:                     (my $result,$newheadtagurls{$lonhost}) =
12724:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12725:                                                                 "login/headtag/$lonhost",'','',
12726:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12727:                                                                 $modified);
12728:                     if ($result eq 'ok') {
12729:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12730:                         $changes{'headtag'}{$lonhost} = 1;
12731:                         if ($possexempt{$lonhost}) {
12732:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12733:                         }
12734:                     } else {
12735:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12736:                                            $newheadtagurls{$lonhost},$result);
12737:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12738:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12739:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12740:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12741:                         }
12742:                     }
12743:                 }
12744:                 &update_modify_urls($r,$modified);
12745:             } else {
12746:                 $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);
12747:             }
12748:         } else {
12749:             $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);
12750:         }
12751:         if ($error) {
12752:             &Apache::lonnet::logthis($error);
12753:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12754:         }
12755:     }
12756:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12757:     my @newsamlimgs;
12758:     foreach my $lonhost (keys(%domservers)) {
12759:         if ($env{'form.saml_'.$lonhost}) {
12760:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12761:                 push(@newsamlimgs,$lonhost);
12762:             }
12763:             foreach my $item ('text','alt','url','title','window','notsso') {
12764:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12765:             }
12766:             if ($saml{$lonhost}) {
12767:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12768:                     $env{'form.saml_window_'.$lonhost} = '';
12769:                 }
12770:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12771: #FIXME Need to obsolete published image
12772:                     delete($currsaml{$lonhost}{'img'});
12773:                     $changes{'saml'}{$lonhost} = 1;
12774:                 }
12775:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12776:                     $changes{'saml'}{$lonhost} = 1;
12777:                 }
12778:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12779:                     $changes{'saml'}{$lonhost} = 1;
12780:                 }
12781:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12782:                     $changes{'saml'}{$lonhost} = 1;
12783:                 }
12784:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12785:                     $changes{'saml'}{$lonhost} = 1;
12786:                 }
12787:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12788:                     $changes{'saml'}{$lonhost} = 1;
12789:                 }
12790:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12791:                     $changes{'saml'}{$lonhost} = 1;
12792:                 }
12793:             } else {
12794:                 $changes{'saml'}{$lonhost} = 1;
12795:             }
12796:             foreach my $item ('text','alt','url','title','window','notsso') {
12797:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12798:             }
12799:         } else {
12800:             if ($saml{$lonhost}) {
12801:                 $changes{'saml'}{$lonhost} = 1;
12802:                 delete($currsaml{$lonhost});
12803:             }
12804:         }
12805:     }
12806:     foreach my $posshost (keys(%currsaml)) {
12807:         unless (exists($domservers{$posshost})) {
12808:             delete($currsaml{$posshost});
12809:         }
12810:     }
12811:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12812:     if (@newsamlimgs) {
12813:         my $error;
12814:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12815:         if ($configuserok eq 'ok') {
12816:             if ($switchserver) {
12817:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12818:             } elsif ($author_ok eq 'ok') {
12819:                 my $modified = [];
12820:                 foreach my $lonhost (@newsamlimgs) {
12821:                     my $formelem = 'saml_img_'.$lonhost;
12822:                     my ($result,$imgurl) =
12823:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12824:                                                                 "login/saml/$lonhost",'','',
12825:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12826:                                                                 $modified);
12827:                     if ($result eq 'ok') {
12828:                         $currsaml{$lonhost}{'img'} = $imgurl;
12829:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12830:                         $changes{'saml'}{$lonhost} = 1;
12831:                     } else {
12832:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12833:                                            $lonhost,$result);
12834:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12835:                     }
12836:                 }
12837:                 &update_modify_urls($r,$modified);
12838:             } else {
12839:                 $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);
12840:             }
12841:         } else {
12842:             $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);
12843:         }
12844:         if ($error) {
12845:             &Apache::lonnet::logthis($error);
12846:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12847:         }
12848:     }
12849:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12850: 
12851:     my $defaulthelpfile = '/adm/loginproblems.html';
12852:     my $defaulttext = &mt('Default in use');
12853: 
12854:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12855:                                              $dom);
12856:     if ($putresult eq 'ok') {
12857:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12858:         my %defaultchecked = (
12859:                     'coursecatalog' => 'on',
12860:                     'helpdesk'      => 'on',
12861:                     'adminmail'     => 'off',
12862:                     'newuser'       => 'off',
12863:         );
12864:         if (ref($domconfig{'login'}) eq 'HASH') {
12865:             foreach my $item (@toggles) {
12866:                 if ($defaultchecked{$item} eq 'on') { 
12867:                     if (($domconfig{'login'}{$item} eq '0') &&
12868:                         ($env{'form.'.$item} eq '1')) {
12869:                         $changes{$item} = 1;
12870:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12871:                               $domconfig{'login'}{$item} eq '1') &&
12872:                              ($env{'form.'.$item} eq '0')) {
12873:                         $changes{$item} = 1;
12874:                     }
12875:                 } elsif ($defaultchecked{$item} eq 'off') {
12876:                     if (($domconfig{'login'}{$item} eq '1') &&
12877:                         ($env{'form.'.$item} eq '0')) {
12878:                         $changes{$item} = 1;
12879:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12880:                               $domconfig{'login'}{$item} eq '0') &&
12881:                              ($env{'form.'.$item} eq '1')) {
12882:                         $changes{$item} = 1;
12883:                     }
12884:                 }
12885:             }
12886:         }
12887:         if (keys(%changes) > 0 || $colchgtext) {
12888:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12889:             if (exists($changes{'saml'})) {
12890:                 my $hostid_in_use;
12891:                 my @hosts = &Apache::lonnet::current_machine_ids();
12892:                 if (@hosts > 1) {
12893:                     foreach my $hostid (@hosts) {
12894:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12895:                             $hostid_in_use = $hostid;
12896:                             last;
12897:                         }
12898:                     }
12899:                 } else {
12900:                     $hostid_in_use = $r->dir_config('lonHostID');
12901:                 }
12902:                 if (($hostid_in_use) &&
12903:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12904:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12905:                 }
12906:                 if (ref($lastactref) eq 'HASH') {
12907:                     if (ref($changes{'saml'}) eq 'HASH') {
12908:                         my %updates;
12909:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12910:                         $lastactref->{'samllanding'} = \%updates;
12911:                     }
12912:                 }
12913:             }
12914:             if (ref($lastactref) eq 'HASH') {
12915:                 $lastactref->{'domainconfig'} = 1;
12916:             }
12917:             $resulttext = &mt('Changes made:').'<ul>';
12918:             foreach my $item (sort(keys(%changes))) {
12919:                 if ($item eq 'loginvia') {
12920:                     if (ref($changes{$item}) eq 'HASH') {
12921:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12922:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12923:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12924:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12925:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12926:                                     $protocol = 'http' if ($protocol ne 'https');
12927:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12928: 
12929:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12930:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12931:                                     } else {
12932:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12933:                                     }
12934:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12935:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12936:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12937:                                     }
12938:                                     $resulttext .= '</li>';
12939:                                 } else {
12940:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12941:                                 }
12942:                             } else {
12943:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12944:                             }
12945:                         }
12946:                         $resulttext .= '</ul></li>';
12947:                     }
12948:                 } elsif ($item eq 'helpurl') {
12949:                     if (ref($changes{$item}) eq 'HASH') {
12950:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12951:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12952:                                 my ($chg,$link);
12953:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12954:                                 if ($lang eq 'nolang') {
12955:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12956:                                 } else {
12957:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12958:                                 }
12959:                                 $resulttext .= '<li>'.$chg.'</li>';
12960:                             } else {
12961:                                 my $chg;
12962:                                 if ($lang eq 'nolang') {
12963:                                     $chg = &mt('custom log-in help file for no preferred language');
12964:                                 } else {
12965:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12966:                                 }
12967:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12968:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12969:                                                       '?inhibitmenu=yes',$chg,600,500).
12970:                                                '</li>';
12971:                             }
12972:                         }
12973:                     }
12974:                 } elsif ($item eq 'headtag') {
12975:                     if (ref($changes{$item}) eq 'HASH') {
12976:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12977:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12978:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12979:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12980:                                 $resulttext .= '<li><a href="'.
12981:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12982:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12983:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12984:                                 if ($possexempt{$lonhost}) {
12985:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12986:                                 } else {
12987:                                     $resulttext .= &mt('included for any client IP');
12988:                                 }
12989:                                 $resulttext .= '</li>';
12990:                             }
12991:                         }
12992:                     }
12993:                 } elsif ($item eq 'saml') {
12994:                     if (ref($changes{$item}) eq 'HASH') {
12995:                         my %notlt = (
12996:                                        text   => 'Text for log-in by SSO',
12997:                                        img    => 'SSO button image',
12998:                                        alt    => 'Alt text for button image',
12999:                                        url    => 'SSO URL',
13000:                                        title  => 'Tooltip for SSO link',
13001:                                        window => 'Pop-up window if iframe',
13002:                                        notsso => 'Text for non-SSO log-in',
13003:                                     );
13004:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
13005:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
13006:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
13007:                                                '<ul>';
13008:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
13009:                                     if ($currsaml{$lonhost}{$key} eq '') {
13010:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
13011:                                     } else {
13012:                                         my $value = "'$currsaml{$lonhost}{$key}'";
13013:                                         if ($key eq 'img') {
13014:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
13015:                                         } elsif ($key eq 'window') {
13016:                                             $value = 'On';
13017:                                         }
13018:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
13019:                                                                   $value).'</li>';
13020:                                     }
13021:                                 }
13022:                                 $resulttext .= '</ul></li>';
13023:                             } else {
13024:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
13025:                             }
13026:                         }
13027:                     }
13028:                 } elsif ($item eq 'captcha') {
13029:                     if (ref($loginhash{'login'}) eq 'HASH') {
13030:                         my $chgtxt;
13031:                         if ($loginhash{'login'}{$item} eq 'notused') {
13032:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
13033:                         } else {
13034:                             my %captchas = &captcha_phrases();
13035:                             if ($captchas{$loginhash{'login'}{$item}}) {
13036:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
13037:                             } else {
13038:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
13039:                             }
13040:                         }
13041:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13042:                     }
13043:                 } elsif ($item eq 'recaptchakeys') {
13044:                     if (ref($loginhash{'login'}) eq 'HASH') {
13045:                         my ($privkey,$pubkey);
13046:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
13047:                             $pubkey = $loginhash{'login'}{$item}{'public'};
13048:                             $privkey = $loginhash{'login'}{$item}{'private'};
13049:                         }
13050:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
13051:                         if (!$pubkey) {
13052:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
13053:                         } else {
13054:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13055:                         }
13056:                         if (!$privkey) {
13057:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
13058:                         } else {
13059:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
13060:                         }
13061:                         $chgtxt .= '</ul>';
13062:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13063:                     }
13064:                 } elsif ($item eq 'recaptchaversion') {
13065:                     if (ref($loginhash{'login'}) eq 'HASH') {
13066:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
13067:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
13068:                                            '</li>';
13069:                         }
13070:                     }
13071:                 } else {
13072:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
13073:                 }
13074:             }
13075:             $resulttext .= $colchgtext.'</ul>';
13076:         } else {
13077:             $resulttext = &mt('No changes made to log-in page settings');
13078:         }
13079:     } else {
13080:         $resulttext = '<span class="LC_error">'.
13081: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13082:     }
13083:     if ($errors) {
13084:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13085:                        $errors.'</ul>';
13086:     }
13087:     return $resulttext;
13088: }
13089: 
13090: sub check_exempt_addresses {
13091:     my ($iplist) = @_;
13092:     $iplist =~ s/^\s+//;
13093:     $iplist =~ s/\s+$//;
13094:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
13095:     my (@okips,$new);
13096:     foreach my $ip (@poss_ips) {
13097:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
13098:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
13099:                 push(@okips,$ip);
13100:             }
13101:         }
13102:     }
13103:     if (@okips > 0) {
13104:         $new = join(',',@okips);
13105:     } else {
13106:         $new = '';
13107:     }
13108:     return $new;
13109: }
13110: 
13111: sub color_font_choices {
13112:     my %choices =
13113:         &Apache::lonlocal::texthash (
13114:             img => "Header",
13115:             bgs => "Background colors",
13116:             links => "Link colors",
13117:             images => "Images",
13118:             font => "Font color",
13119:             fontmenu => "Font menu",
13120:             pgbg => "Page",
13121:             tabbg => "Header",
13122:             sidebg => "Border",
13123:             link => "Link",
13124:             alink => "Active link",
13125:             vlink => "Visited link",
13126:         );
13127:     return %choices;
13128: }
13129: 
13130: sub modify_ipaccess {
13131:     my ($dom,$lastactref,%domconfig) = @_;
13132:     my (@allpos,%changes,%confhash,$errors,$resulttext);
13133:     my (@items,%deletions,%itemids,@warnings);
13134:     my ($typeorder,$types) = &commblocktype_text();
13135:     if ($env{'form.ipaccess_add'}) {
13136:         my $name = $env{'form.ipaccess_name_add'};
13137:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
13138:         if ($newid) {
13139:             $itemids{'add'} = $newid;
13140:             push(@items,'add');
13141:             $changes{$newid} = 1;
13142:         } else {
13143:             $error = &mt('Failed to acquire unique ID for new IP access control item');
13144:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13145:         }
13146:     }
13147:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13148:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
13149:         if (@todelete) {
13150:             map { $deletions{$_} = 1; } @todelete;
13151:         }
13152:         my $maxnum = $env{'form.ipaccess_maxnum'};
13153:         for (my $i=0; $i<$maxnum; $i++) {
13154:             my $itemid = $env{'form.ipaccess_id_'.$i};
13155:             $itemid =~ s/\D+//g;
13156:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13157:                 if ($deletions{$itemid}) {
13158:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
13159:                 } else {
13160:                     push(@items,$i);
13161:                     $itemids{$i} = $itemid;
13162:                 }
13163:             }
13164:         }
13165:     }
13166:     foreach my $idx (@items) {
13167:         my $itemid = $itemids{$idx};
13168:         next unless ($itemid);
13169:         my %current;
13170:         unless ($idx eq 'add') {
13171:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13172:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
13173:             }
13174:         }
13175:         my $position = $env{'form.ipaccess_pos_'.$itemid};
13176:         $position =~ s/\D+//g;
13177:         if ($position ne '') {
13178:             $allpos[$position] = $itemid;
13179:         }
13180:         my $name = $env{'form.ipaccess_name_'.$idx};
13181:         $name =~ s/^\s+|\s+$//g;
13182:         $confhash{$itemid}{'name'} = $name;
13183:         my $possrange = $env{'form.ipaccess_range_'.$idx};
13184:         $possrange =~ s/^\s+|\s+$//g;
13185:         unless ($possrange eq '') {
13186:             $possrange =~ s/[\r\n]+/\s/g;
13187:             $possrange =~ s/\s*-\s*/-/g;
13188:             $possrange =~ s/\s+/,/g;
13189:             $possrange =~ s/,+/,/g;
13190:             if ($possrange ne '') {
13191:                 my (@ok,$count);
13192:                 $count = 0;
13193:                 foreach my $poss (split(/\,/,$possrange)) {
13194:                     $count ++;
13195:                     $poss = &validate_ip_pattern($poss);
13196:                     if ($poss ne '') {
13197:                         push(@ok,$poss);
13198:                     }
13199:                 }
13200:                 my $diff = $count - scalar(@ok);
13201:                 if ($diff) {
13202:                     $errors .= '<li><span class="LC_error">'.
13203:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
13204:                                    $diff,$name).
13205:                                '</span></li>';
13206:                 }
13207:                 if (@ok) {
13208:                     my @cidr_list;
13209:                     foreach my $item (@ok) {
13210:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
13211:                     }
13212:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
13213:                 }
13214:             }
13215:         }
13216:         foreach my $field ('name','ip') {
13217:             unless (($idx eq 'add') || ($changes{$itemid})) {
13218:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
13219:                     $changes{$itemid} = 1;
13220:                     last;
13221:                 }
13222:             }
13223:         }
13224:         $confhash{$itemid}{'commblocks'} = {};
13225: 
13226:         my %commblocks;
13227:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
13228:         foreach my $type (@{$typeorder}) {
13229:             if ($commblocks{$type}) {
13230:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
13231:             }
13232:             unless (($idx eq 'add') || ($changes{$itemid})) {
13233:                 if (ref($current{'commblocks'}) eq 'HASH') {
13234:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
13235:                         $changes{$itemid} = 1;
13236:                     }
13237:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
13238:                     $changes{$itemid} = 1;
13239:                 }
13240:             }
13241:         }
13242:         $confhash{$itemid}{'courses'} = {};
13243:         my %crsdeletions;
13244:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
13245:         if (@delcrs) {
13246:             map { $crsdeletions{$_} = 1; } @delcrs;
13247:         }
13248:         if (ref($current{'courses'}) eq 'HASH') {
13249:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
13250:                 if ($crsdeletions{$cid}) {
13251:                     $changes{$itemid} = 1;
13252:                 } else {
13253:                     $confhash{$itemid}{'courses'}{$cid} = 1;
13254:                 }
13255:             }
13256:         }
13257:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
13258:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
13259:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
13260:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
13261:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
13262:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
13263:                 $errors .= '<li><span class="LC_error">'.
13264:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
13265:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
13266:                            '</span></li>';
13267:             } else {
13268:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
13269:                 $changes{$itemid} = 1;
13270:             }
13271:         }
13272:     }
13273:     if (@allpos > 0) {
13274:         my $idx = 0;
13275:         foreach my $itemid (@allpos) {
13276:             if ($itemid ne '') {
13277:                 $confhash{$itemid}{'order'} = $idx;
13278:                 unless ($changes{$itemid}) {
13279:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13280:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13281:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
13282:                                 $changes{$itemid} = 1;
13283:                             }
13284:                         }
13285:                     }
13286:                 }
13287:                 $idx ++;
13288:             }
13289:         }
13290:     }
13291:     if (keys(%changes)) {
13292:         my %defaultshash = (
13293:                               ipaccess => \%confhash,
13294:                            );
13295:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13296:                                                  $dom);
13297:         if ($putresult eq 'ok') {
13298:             my $cachetime = 1800;
13299:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
13300:             if (ref($lastactref) eq 'HASH') {
13301:                 $lastactref->{'ipaccess'} = 1;
13302:             }
13303:             $resulttext = &mt('Changes made:').'<ul>';
13304:             my %bynum;
13305:             foreach my $itemid (sort(keys(%changes))) {
13306:                 if (ref($confhash{$itemid}) eq 'HASH') {
13307:                     my $position = $confhash{$itemid}{'order'};
13308:                     if ($position =~ /^\d+$/) {
13309:                         $bynum{$position} = $itemid;
13310:                     }
13311:                 }
13312:             }
13313:             if (keys(%deletions)) {
13314:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
13315:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13316:                 }
13317:             }
13318:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13319:                 my $itemid = $bynum{$pos};
13320:                 if (ref($confhash{$itemid}) eq 'HASH') {
13321:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
13322:                     my $position = $pos + 1;
13323:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13324:                     if ($confhash{$itemid}{'ip'} eq '') {
13325:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
13326:                     } else {
13327:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
13328:                     }
13329:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
13330:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
13331:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
13332:                                        '</li>';
13333:                     } else {
13334:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
13335:                     }
13336:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
13337:                         my @courses;
13338:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
13339:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
13340:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
13341:                         }
13342:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
13343:                                              join('</li><li>',@courses).'</li></ul>';
13344:                     } else {
13345:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
13346:                     }
13347:                     $resulttext .= '</ul></li>';
13348:                 }
13349:             }
13350:             $resulttext .= '</ul>';
13351:         } else {
13352:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13353:         }
13354:     } else {
13355:         $resulttext = &mt('No changes made');
13356:     }
13357:     if ($errors) {
13358:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13359:                        $errors.'</ul></p>';
13360:     }
13361:     return $resulttext;
13362: }
13363: 
13364: sub get_ipaccess_id {
13365:     my ($domain,$location) = @_;
13366:     # get lock on ipaccess db
13367:     my $lockhash = {
13368:                       lock => $env{'user.name'}.
13369:                               ':'.$env{'user.domain'},
13370:                    };
13371:     my $tries = 0;
13372:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13373:     my ($id,$error);
13374: 
13375:     while (($gotlock ne 'ok') && ($tries<10)) {
13376:         $tries ++;
13377:         sleep (0.1);
13378:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13379:     }
13380:     if ($gotlock eq 'ok') {
13381:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
13382:         if ($currids{'lock'}) {
13383:             delete($currids{'lock'});
13384:             if (keys(%currids)) {
13385:                 my @curr = sort { $a <=> $b } keys(%currids);
13386:                 if ($curr[-1] =~ /^\d+$/) {
13387:                     $id = 1 + $curr[-1];
13388:                 }
13389:             } else {
13390:                 $id = 1;
13391:             }
13392:             if ($id) {
13393:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
13394:                     $error = 'nostore';
13395:                 }
13396:             } else {
13397:                 $error = 'nonumber';
13398:             }
13399:         }
13400:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
13401:     } else {
13402:         $error = 'nolock';
13403:     }
13404:     return ($id,$error);
13405: }
13406: 
13407: sub modify_authordefaults {
13408:     my ($dom,$lastactref,%domconfig) = @_;
13409: #
13410: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
13411: #
13412:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
13413:     if (ref($domconfig{'quotas'}) eq 'HASH') {
13414:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
13415:             if ($key =~ /^webdav|authorquota$/) {
13416:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
13417:             } else {
13418:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
13419:             }
13420:         }
13421:     }
13422:     my %staticdefaults = (
13423:                            'copyright'    => 'default',
13424:                            'sourceavail'  => 'closed',
13425:                            'nocodemirror' => 'off',
13426:                            'daxecollapse' => 'off',
13427:                            'domcoordacc'  => 'on',
13428:                            'editors'      => ['edit','xml'],
13429:                            'authorquota'  => 500,
13430:                            'webdav'       => 0,
13431:                          );
13432:     my %titles = &authordefaults_titles();
13433:     foreach my $item ('nocodemirror','daxecollapse','domcoordacc') {
13434:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13435:             $confhash{$item} = $env{'form.'.$item};
13436:         }
13437:     }
13438:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
13439:         $confhash{'copyright'} = $1;
13440:     }
13441:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
13442:         $confhash{'sourceavail'} = $1;
13443:     }
13444:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
13445:     my @okeditors = ('edit','xml','daxe');
13446:     my @editors;
13447:     foreach my $item (@posseditors) {
13448:         if (grep(/^\Q$item\E$/,@okeditors)) {
13449:             push(@editors,$item);
13450:         }
13451:     }
13452:     $confhash{'editors'} = \@editors;
13453: 
13454:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13455:     my @insttypes;
13456:     if (ref($types) eq 'ARRAY') {
13457:         @insttypes = @{$types};
13458:     }
13459:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
13460:     my %webdav;
13461:     map { $webdav{$_} = 1; } @webdavon;
13462:     foreach my $type (@insttypes,'default') {
13463:         my $possquota = $env{'form.authorquota_'.$type};
13464:         if ($possquota =~ /^\d+$/) {
13465:             $save_quotas{'authorquota'}{$type} = $possquota;
13466:         }
13467:         if ($webdav{$type}) {
13468:             $save_quotas{'webdav'}{$type} = 1;
13469:         } else {
13470:             $save_quotas{'webdav'}{$type} = 0;
13471:         }
13472:     }
13473:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
13474:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
13475:     }
13476:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
13477:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail') {
13478:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
13479:                 $changes{$item} = 1;
13480:              }
13481:         }
13482:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
13483:             my @diffs =
13484:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13485:                                                    $domconfig{'authordefaults'}{'editors'});
13486:             unless (@diffs == 0) {
13487:                 $changes{'editors'} = 1;
13488:             }
13489:         } else {
13490:             my @diffs =
13491:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13492:                                                    $staticdefaults{'editors'});
13493:             unless (@diffs == 0) {
13494:                 $changes{'editors'} = 1;
13495:             }
13496:         }
13497:     } else {
13498:         my @offon = ('off','on');
13499:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc') {
13500:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) {
13501:                 $changes{$item} = 1; 
13502:             }
13503:         }
13504:         foreach my $item ('copyright','sourceavail') {
13505:             if ($confhash{$item} ne $staticdefaults{$item}) {
13506:                 $changes{$item} = 1;
13507:             }
13508:         }
13509:         my @diffs =
13510:             &Apache::loncommon::compare_arrays($confhash{'editors'},
13511:                                                $staticdefaults{'editors'});
13512:         unless (@diffs == 0) {
13513:             $changes{'editors'} = 1;
13514:         }
13515:     }
13516:     foreach my $key ('authorquota','webdav') {
13517:         if (ref($curr_quotas{$key}) eq 'HASH') {
13518:             foreach my $type (@insttypes,'default') {
13519:                 if (exists($save_quotas{$key}{$type})) {
13520:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
13521:                         $changes{$key}{$type} = 1;
13522:                     }
13523:                 } elsif (exists($curr_quotas{$key}{$type})) {
13524:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
13525:                 } else {
13526:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13527:                 }
13528:             }
13529:         } else {
13530:             foreach my $type (@insttypes,'default') {
13531:                 if (exists($save_quotas{$key}{$type})) {
13532:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
13533:                         $changes{$key}{$type} = 1;
13534:                     }
13535:                 } else {
13536:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13537:                 }
13538:             }
13539:         }
13540:     }
13541:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
13542:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13543:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) {
13544:                 $changes{'webdav_LC_adv'} = 1;
13545:             }
13546:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
13547:             $changes{'webdav_LC_adv'} = 1;
13548:         }
13549:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13550:         $changes{'webdav_LC_adv'} = 1;
13551:     }
13552:     my %confighash = (
13553:                         quotas  => \%save_quotas,
13554:                         authordefaults => \%confhash,
13555:                      );
13556:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
13557:                                              $dom);
13558:     my $resulttext;
13559:     if ($putresult eq 'ok') {
13560:         if (keys(%changes)) {
13561:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13562:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
13563:                 ($changes{'webdav_LC_adv'})) {
13564:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
13565:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
13566:                 }
13567:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
13568:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
13569:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
13570:                 }
13571:             }
13572:             $resulttext = &mt('Changes made:').'<ul>';
13573:             my $authoroverride;
13574:             foreach my $key ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail') {
13575:                 if (exists($changes{$key})) {
13576:                     $domdefaults{$key} = $confhash{$key};
13577:                     my $shown;
13578:                     unless ($authoroverride) {
13579:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
13580:                         $authoroverride = 1;
13581:                     }
13582:                     if (($key eq 'nocodemirror') || ($key eq 'daxecollapse') || ($key eq 'domcoordacc')) {
13583:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13584:                     } elsif ($key eq 'copyright') {
13585:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
13586:                     } elsif ($key eq 'sourceavail') {
13587:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
13588:                     }
13589:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13590:                 }
13591:             }
13592:             if ($authoroverride) {
13593:                 $resulttext .= '</ul></li>';
13594:             }
13595:             my $domcoordoverride;
13596:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv') {
13597:                 if (exists($changes{$key})) {
13598:                     my $shown;
13599:                     unless ($domcoordoverride) {
13600:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
13601:                         $domcoordoverride = 1;
13602:                     }
13603:                     if ($key eq 'editors') {
13604:                         if (ref($confhash{'editors'}) eq 'ARRAY') {
13605:                             $domdefaults{'editors'} = join(',',@{$confhash{'editors'}});
13606:                             if (@{$confhash{'editors'}}) {
13607:                                 $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
13608:                             } else {
13609:                                 $shown = &mt('None');
13610:                             }
13611:                         }
13612:                     } elsif ($key eq 'authorquota') {
13613:                         foreach my $type (@insttypes) {
13614:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
13615:                         }
13616:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
13617:                     } elsif ($key eq 'webdav') {
13618:                         foreach my $type (@insttypes) {
13619:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
13620:                         }
13621:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
13622:                     } elsif ($key eq 'webdav_LC_adv') {
13623:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13624:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
13625:                         } else {
13626:                             $shown = $titles{'none'};
13627:                         }
13628:                     }
13629:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13630:                 }
13631:             }
13632:             if ($domcoordoverride) {
13633:                 $resulttext .= '</ul></li>';
13634:             }
13635:             $resulttext .= '</ul>';
13636:             my $cachetime = 24*60*60;
13637:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13638:             if (ref($lastactref) eq 'HASH') {
13639:                 $lastactref->{'domdefaults'} = 1;
13640:             }
13641:         } else {
13642:             $resulttext = &mt('No changes made to Authoring Space defaults');
13643:         }
13644:     }
13645:     return $resulttext;
13646: }
13647: 
13648: sub modify_rolecolors {
13649:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
13650:     my ($resulttext,%rolehash);
13651:     $rolehash{'rolecolors'} = {};
13652:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
13653:         if ($domconfig{'rolecolors'} eq '') {
13654:             $domconfig{'rolecolors'} = {};
13655:         }
13656:     }
13657:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
13658:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
13659:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
13660:                                              $dom);
13661:     if ($putresult eq 'ok') {
13662:         if (keys(%changes) > 0) {
13663:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13664:             if (ref($lastactref) eq 'HASH') {
13665:                 $lastactref->{'domainconfig'} = 1;
13666:             }
13667:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
13668:                                              $rolehash{'rolecolors'});
13669:         } else {
13670:             $resulttext = &mt('No changes made to default color schemes');
13671:         }
13672:     } else {
13673:         $resulttext = '<span class="LC_error">'.
13674: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13675:     }
13676:     if ($errors) {
13677:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13678:                        $errors.'</ul>';
13679:     }
13680:     return $resulttext;
13681: }
13682: 
13683: sub modify_colors {
13684:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
13685:     my (%changes,%choices);
13686:     my @bgs;
13687:     my @links = ('link','alink','vlink');
13688:     my @logintext;
13689:     my @images;
13690:     my $servadm = $r->dir_config('lonAdmEMail');
13691:     my $errors;
13692:     my %defaults;
13693:     foreach my $role (@{$roles}) {
13694:         if ($role eq 'login') {
13695:             %choices = &login_choices();
13696:             @logintext = ('textcol','bgcol');
13697:         } else {
13698:             %choices = &color_font_choices();
13699:         }
13700:         if ($role eq 'login') {
13701:             @images = ('img','logo','domlogo','login');
13702:             @bgs = ('pgbg','mainbg','sidebg');
13703:         } else {
13704:             @images = ('img');
13705:             @bgs = ('pgbg','tabbg','sidebg');
13706:         }
13707:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
13708:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
13709:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13710:         }
13711:         if ($role eq 'login') {
13712:             foreach my $item (@logintext) {
13713:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13714:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13715:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13716:                 }
13717:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13718:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13719:                 }
13720:             }
13721:         } else {
13722:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13723:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13724:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13725:             }
13726:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13727:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13728:             }
13729:         }
13730:         foreach my $item (@bgs) {
13731:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13732:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13733:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13734:             }
13735:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13736:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13737:             }
13738:         }
13739:         foreach my $item (@links) {
13740:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13741:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13742:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13743:             }
13744:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13745:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13746:             }
13747:         }
13748:         my ($configuserok,$author_ok,$switchserver) = 
13749:             &config_check($dom,$confname,$servadm);
13750:         my ($width,$height) = &thumb_dimensions();
13751:         if (ref($domconfig->{$role}) ne 'HASH') {
13752:             $domconfig->{$role} = {};
13753:         }
13754:         foreach my $img (@images) {
13755:             if ($role eq 'login') {
13756:                 if (($img eq 'img') || ($img eq 'logo')) {  
13757:                     if (defined($env{'form.login_showlogo_'.$img})) {
13758:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13759:                     } else { 
13760:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13761:                     }
13762:                 }
13763:                 if ($env{'form.login_alt_'.$img} ne '') {
13764:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13765:                 }
13766:             }
13767: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13768: 		 && !defined($domconfig->{$role}{$img})
13769: 		 && !$env{'form.'.$role.'_del_'.$img}
13770: 		 && $env{'form.'.$role.'_import_'.$img}) {
13771: 		# import the old configured image from the .tab setting
13772: 		# if they haven't provided a new one 
13773: 		$domconfig->{$role}{$img} = 
13774: 		    $env{'form.'.$role.'_import_'.$img};
13775: 	    }
13776:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13777:                 my $error;
13778:                 if ($configuserok eq 'ok') {
13779:                     if ($switchserver) {
13780:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13781:                     } else {
13782:                         if ($author_ok eq 'ok') {
13783:                             my $modified = [];
13784:                             my ($result,$logourl) = 
13785:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13786:                                                                         $dom,$confname,$img,$width,$height,
13787:                                                                         '',$modified);
13788:                             if ($result eq 'ok') {
13789:                                 $confhash->{$role}{$img} = $logourl;
13790:                                 $changes{$role}{'images'}{$img} = 1;
13791:                                 &update_modify_urls($r,$modified);
13792:                             } else {
13793:                                 $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);
13794:                             }
13795:                         } else {
13796:                             $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);
13797:                         }
13798:                     }
13799:                 } else {
13800:                     $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);
13801:                 }
13802:                 if ($error) {
13803:                     &Apache::lonnet::logthis($error);
13804:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13805:                 }
13806:             } elsif ($domconfig->{$role}{$img} ne '') {
13807:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13808:                     my $error;
13809:                     if ($configuserok eq 'ok') {
13810: # is confname an author?
13811:                         if ($switchserver eq '') {
13812:                             if ($author_ok eq 'ok') {
13813:                                 my $modified = [];
13814:                                 my ($result,$logourl) = 
13815:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13816:                                                                             $dom,$confname,$img,$width,$height,
13817:                                                                             '',$modified);
13818:                                 if ($result eq 'ok') {
13819:                                     $confhash->{$role}{$img} = $logourl;
13820: 				    $changes{$role}{'images'}{$img} = 1;
13821:                                     &update_modify_urls($r,$modified);
13822:                                 }
13823:                             }
13824:                         }
13825:                     }
13826:                 }
13827:             }
13828:         }
13829:         if (ref($domconfig) eq 'HASH') {
13830:             if (ref($domconfig->{$role}) eq 'HASH') {
13831:                 foreach my $img (@images) {
13832:                     if ($domconfig->{$role}{$img} ne '') {
13833:                         if ($env{'form.'.$role.'_del_'.$img}) {
13834:                             $confhash->{$role}{$img} = '';
13835:                             $changes{$role}{'images'}{$img} = 1;
13836:                         } else {
13837:                             if ($confhash->{$role}{$img} eq '') {
13838:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13839:                             }
13840:                         }
13841:                     } else {
13842:                         if ($env{'form.'.$role.'_del_'.$img}) {
13843:                             $confhash->{$role}{$img} = '';
13844:                             $changes{$role}{'images'}{$img} = 1;
13845:                         } 
13846:                     }
13847:                     if ($role eq 'login') {
13848:                         if (($img eq 'logo') || ($img eq 'img')) {
13849:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13850:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13851:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13852:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13853:                                 }
13854:                             } else {
13855:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13856:                                     $changes{$role}{'showlogo'}{$img} = 1;
13857:                                 }
13858:                             }
13859:                         }
13860:                         if ($img ne 'login') {
13861:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13862:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13863:                                     $domconfig->{$role}{'alttext'}{$img}) {
13864:                                     $changes{$role}{'alttext'}{$img} = 1;
13865:                                 }
13866:                             } else {
13867:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13868:                                     $changes{$role}{'alttext'}{$img} = 1;
13869:                                 }
13870:                             }
13871:                         }
13872:                     }
13873:                 }
13874:                 if ($domconfig->{$role}{'font'} ne '') {
13875:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13876:                         $changes{$role}{'font'} = 1;
13877:                     }
13878:                 } else {
13879:                     if ($confhash->{$role}{'font'}) {
13880:                         $changes{$role}{'font'} = 1;
13881:                     }
13882:                 }
13883:                 if ($role ne 'login') {
13884:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13885:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13886:                             $changes{$role}{'fontmenu'} = 1;
13887:                         }
13888:                     } else {
13889:                         if ($confhash->{$role}{'fontmenu'}) {
13890:                             $changes{$role}{'fontmenu'} = 1;
13891:                         }
13892:                     }
13893:                 }
13894:                 foreach my $item (@bgs) {
13895:                     if ($domconfig->{$role}{$item} ne '') {
13896:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13897:                             $changes{$role}{'bgs'}{$item} = 1;
13898:                         } 
13899:                     } else {
13900:                         if ($confhash->{$role}{$item}) {
13901:                             $changes{$role}{'bgs'}{$item} = 1;
13902:                         }
13903:                     }
13904:                 }
13905:                 foreach my $item (@links) {
13906:                     if ($domconfig->{$role}{$item} ne '') {
13907:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13908:                             $changes{$role}{'links'}{$item} = 1;
13909:                         }
13910:                     } else {
13911:                         if ($confhash->{$role}{$item}) {
13912:                             $changes{$role}{'links'}{$item} = 1;
13913:                         }
13914:                     }
13915:                 }
13916:                 foreach my $item (@logintext) {
13917:                     if ($domconfig->{$role}{$item} ne '') {
13918:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13919:                             $changes{$role}{'logintext'}{$item} = 1;
13920:                         }
13921:                     } else {
13922:                         if ($confhash->{$role}{$item}) {
13923:                             $changes{$role}{'logintext'}{$item} = 1;
13924:                         }
13925:                     }
13926:                 }
13927:             } else {
13928:                 &default_change_checker($role,\@images,\@links,\@bgs,
13929:                                         \@logintext,$confhash,\%changes); 
13930:             }
13931:         } else {
13932:             &default_change_checker($role,\@images,\@links,\@bgs,
13933:                                     \@logintext,$confhash,\%changes); 
13934:         }
13935:     }
13936:     return ($errors,%changes);
13937: }
13938: 
13939: sub config_check {
13940:     my ($dom,$confname,$servadm) = @_;
13941:     my ($configuserok,$author_ok,$switchserver,%currroles);
13942:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13943:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13944:                                                    $confname,$servadm);
13945:     if ($configuserok eq 'ok') {
13946:         $switchserver = &check_switchserver($dom,$confname);
13947:         if ($switchserver eq '') {
13948:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13949:         }
13950:     }
13951:     return ($configuserok,$author_ok,$switchserver);
13952: }
13953: 
13954: sub default_change_checker {
13955:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13956:     foreach my $item (@{$links}) {
13957:         if ($confhash->{$role}{$item}) {
13958:             $changes->{$role}{'links'}{$item} = 1;
13959:         }
13960:     }
13961:     foreach my $item (@{$bgs}) {
13962:         if ($confhash->{$role}{$item}) {
13963:             $changes->{$role}{'bgs'}{$item} = 1;
13964:         }
13965:     }
13966:     foreach my $item (@{$logintext}) {
13967:         if ($confhash->{$role}{$item}) {
13968:             $changes->{$role}{'logintext'}{$item} = 1;
13969:         }
13970:     }
13971:     foreach my $img (@{$images}) {
13972:         if ($env{'form.'.$role.'_del_'.$img}) {
13973:             $confhash->{$role}{$img} = '';
13974:             $changes->{$role}{'images'}{$img} = 1;
13975:         }
13976:         if ($role eq 'login') {
13977:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13978:                 $changes->{$role}{'showlogo'}{$img} = 1;
13979:             }
13980:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13981:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13982:                     $changes->{$role}{'alttext'}{$img} = 1;
13983:                 }
13984:             }
13985:         }
13986:     }
13987:     if ($confhash->{$role}{'font'}) {
13988:         $changes->{$role}{'font'} = 1;
13989:     }
13990: }
13991: 
13992: sub display_colorchgs {
13993:     my ($dom,$changes,$roles,$confhash) = @_;
13994:     my (%choices,$resulttext);
13995:     if (!grep(/^login$/,@{$roles})) {
13996:         $resulttext = &mt('Changes made:').'<br />';
13997:     }
13998:     foreach my $role (@{$roles}) {
13999:         if ($role eq 'login') {
14000:             %choices = &login_choices();
14001:         } else {
14002:             %choices = &color_font_choices();
14003:         }
14004:         if (ref($changes->{$role}) eq 'HASH') {
14005:             if ($role ne 'login') {
14006:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
14007:             }
14008:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
14009:                 if ($role ne 'login') {
14010:                     $resulttext .= '<ul>';
14011:                 }
14012:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
14013:                     if ($role ne 'login') {
14014:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
14015:                     }
14016:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
14017:                         if (($role eq 'login') && ($key eq 'showlogo')) {
14018:                             if ($confhash->{$role}{$key}{$item}) {
14019:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
14020:                             } else {
14021:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
14022:                             }
14023:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
14024:                             if ($confhash->{$role}{$key}{$item} ne '') {
14025:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
14026:                                                $confhash->{$role}{$key}{$item}).'</li>';
14027:                             } else {
14028:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
14029:                             }
14030:                         } elsif ($confhash->{$role}{$item} eq '') {
14031:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
14032:                         } else {
14033:                             my $newitem = $confhash->{$role}{$item};
14034:                             if ($key eq 'images') {
14035:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
14036:                             }
14037:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
14038:                         }
14039:                     }
14040:                     if ($role ne 'login') {
14041:                         $resulttext .= '</ul></li>';
14042:                     }
14043:                 } else {
14044:                     if ($confhash->{$role}{$key} eq '') {
14045:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
14046:                     } else {
14047:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
14048:                     }
14049:                 }
14050:                 if ($role ne 'login') {
14051:                     $resulttext .= '</ul>';
14052:                 }
14053:             }
14054:         }
14055:     }
14056:     return $resulttext;
14057: }
14058: 
14059: sub thumb_dimensions {
14060:     return ('200','50');
14061: }
14062: 
14063: sub check_dimensions {
14064:     my ($inputfile) = @_;
14065:     my ($fullwidth,$fullheight);
14066:     if ($inputfile =~ m|^[/\w.\-]+$|) {
14067:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
14068:             my $imageinfo = <PIPE>;
14069:             if (!close(PIPE)) {
14070:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
14071:             }
14072:             chomp($imageinfo);
14073:             my ($fullsize) = 
14074:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
14075:             if ($fullsize) {
14076:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
14077:             }
14078:         }
14079:     }
14080:     return ($fullwidth,$fullheight);
14081: }
14082: 
14083: sub check_configuser {
14084:     my ($uhome,$dom,$confname,$servadm) = @_;
14085:     my ($configuserok,%currroles);
14086:     if ($uhome eq 'no_host') {
14087:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
14088:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
14089:         $configuserok = 
14090:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
14091:                              $configpass,'','','','','',undef,$servadm);
14092:     } else {
14093:         $configuserok = 'ok';
14094:         %currroles = 
14095:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
14096:     }
14097:     return ($configuserok,%currroles);
14098: }
14099: 
14100: sub check_authorstatus {
14101:     my ($dom,$confname,%currroles) = @_;
14102:     my $author_ok;
14103:     if (!$currroles{':'.$dom.':au'}) {
14104:         my $start = time;
14105:         my $end = 0;
14106:         $author_ok = 
14107:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
14108:                                         'au',$end,$start,'','','domconfig');
14109:     } else {
14110:         $author_ok = 'ok';
14111:     }
14112:     return $author_ok;
14113: }
14114: 
14115: sub update_modify_urls {
14116:     my ($r,$modified) = @_;
14117:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
14118:         push(@{$modified_urls},$modified);
14119:         unless ($registered_cleanup) {
14120:             my $handlers = $r->get_handlers('PerlCleanupHandler');
14121:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
14122:             $registered_cleanup=1;
14123:         }
14124:     }
14125: }
14126: 
14127: sub notifysubscribed {
14128:     foreach my $targetsource (@{$modified_urls}){
14129:         next unless (ref($targetsource) eq 'ARRAY');
14130:         my ($target,$source)=@{$targetsource};
14131:         if ($source ne '') {
14132:             if (open(my $logfh,">>",$source.'.log')) {
14133:                 print $logfh "\nCleanup phase: Notifications\n";
14134:                 my @subscribed=&subscribed_hosts($target);
14135:                 foreach my $subhost (@subscribed) {
14136:                     print $logfh "\nNotifying host ".$subhost.':';
14137:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
14138:                     print $logfh $reply;
14139:                 }
14140:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
14141:                 foreach my $subhost (@subscribedmeta) {
14142:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
14143:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
14144:                                                         $subhost);
14145:                     print $logfh $reply;
14146:                 }
14147:                 print $logfh "\n============ Done ============\n";
14148:                 close($logfh);
14149:             }
14150:         }
14151:     }
14152:     return OK;
14153: }
14154: 
14155: sub subscribed_hosts {
14156:     my ($target) = @_;
14157:     my @subscribed;
14158:     if (open(my $fh,"<","$target.subscription")) {
14159:         while (my $subline=<$fh>) {
14160:             if ($subline =~ /^($match_lonid):/) {
14161:                 my $host = $1;
14162:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
14163:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
14164:                         push(@subscribed,$host);
14165:                     }
14166:                 }
14167:             }
14168:         }
14169:     }
14170:     return @subscribed;
14171: }
14172: 
14173: sub check_switchserver {
14174:     my ($dom,$confname) = @_;
14175:     my ($allowed,$switchserver,$home);
14176:     if ($confname eq '') {
14177:         $home = &Apache::lonnet::domain($dom,'primary');
14178:     } else {
14179:         $home = &Apache::lonnet::homeserver($confname,$dom);
14180:         if ($home eq 'no_host') {
14181:             $home = &Apache::lonnet::domain($dom,'primary');
14182:         }
14183:     }
14184:     my @ids=&Apache::lonnet::current_machine_ids();
14185:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
14186:     if (!$allowed) {
14187: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
14188:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
14189:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
14190:     }
14191:     return $switchserver;
14192: }
14193: 
14194: sub modify_quotas {
14195:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14196:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
14197:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
14198:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
14199:         $validationfieldsref);
14200:     if ($action eq 'quotas') {
14201:         $context = 'tools';
14202:     } else {
14203:         $context = $action;
14204:     }
14205:     if ($context eq 'requestcourses') {
14206:         @usertools = ('official','unofficial','community','textbook','placement','lti');
14207:         @options =('norequest','approval','validate','autolimit');
14208:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
14209:         %titles = &courserequest_titles();
14210:         $toolregexp = join('|',@usertools);
14211:         %conditions = &courserequest_conditions();
14212:         $confname = $dom.'-domainconfig';
14213:         my $servadm = $r->dir_config('lonAdmEMail');
14214:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14215:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
14216:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
14217:     } elsif ($context eq 'requestauthor') {
14218:         @usertools = ('author');
14219:         %titles = &authorrequest_titles();
14220:     } else {
14221:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
14222:         %titles = &tool_titles();
14223:     }
14224:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14225:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14226:     foreach my $key (keys(%env)) {
14227:         if ($context eq 'requestcourses') {
14228:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
14229:                 my $item = $1;
14230:                 my $type = $2;
14231:                 if ($type =~ /^limit_(.+)/) {
14232:                     $limithash{$item}{$1} = $env{$key};
14233:                 } else {
14234:                     $confhash{$item}{$type} = $env{$key};
14235:                 }
14236:             }
14237:         } elsif ($context eq 'requestauthor') {
14238:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
14239:                 $confhash{$1} = $env{$key};
14240:             }
14241:         } else {
14242:             if ($key =~ /^form\.quota_(.+)$/) {
14243:                 $confhash{'defaultquota'}{$1} = $env{$key};
14244:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
14245:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
14246:             }
14247:         }
14248:     }
14249:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14250:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
14251:         @approvalnotify = sort(@approvalnotify);
14252:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
14253:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
14254:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
14255:         foreach my $type (@hasuniquecode) {
14256:             if (grep(/^\Q$type\E$/,@crstypes)) {
14257:                 $confhash{'uniquecode'}{$type} = 1;
14258:             }
14259:         }
14260:         my (%newbook,%allpos);
14261:         if ($context eq 'requestcourses') {
14262:             foreach my $type ('textbooks','templates') {
14263:                 @{$allpos{$type}} = (); 
14264:                 my $invalid;
14265:                 if ($type eq 'textbooks') {
14266:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
14267:                 } else {
14268:                     $invalid = &mt('Invalid LON-CAPA course for template');
14269:                 }
14270:                 if ($env{'form.'.$type.'_addbook'}) {
14271:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
14272:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
14273:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
14274:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
14275:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14276:                         } else {
14277:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
14278:                             my $position = $env{'form.'.$type.'_addbook_pos'};
14279:                             $position =~ s/\D+//g;
14280:                             if ($position ne '') {
14281:                                 $allpos{$type}[$position] = $newbook{$type};
14282:                             }
14283:                         }
14284:                     } else {
14285:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14286:                     }
14287:                 }
14288:             } 
14289:         }
14290:         if (ref($domconfig{$action}) eq 'HASH') {
14291:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
14292:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
14293:                     $changes{'notify'}{'approval'} = 1;
14294:                 }
14295:             } else {
14296:                 if ($confhash{'notify'}{'approval'}) {
14297:                     $changes{'notify'}{'approval'} = 1;
14298:                 }
14299:             }
14300:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
14301:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
14302:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
14303:                         unless ($confhash{'uniquecode'}{$crstype}) {
14304:                             $changes{'uniquecode'} = 1;
14305:                         }
14306:                     }
14307:                     unless ($changes{'uniquecode'}) {
14308:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
14309:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
14310:                                 $changes{'uniquecode'} = 1;
14311:                             }
14312:                         }
14313:                     }
14314:                } else {
14315:                    $changes{'uniquecode'} = 1;
14316:                }
14317:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
14318:                 $changes{'uniquecode'} = 1;
14319:             }
14320:             if ($context eq 'requestcourses') {
14321:                 foreach my $type ('textbooks','templates') {
14322:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
14323:                         my %deletions;
14324:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
14325:                         if (@todelete) {
14326:                             map { $deletions{$_} = 1; } @todelete;
14327:                         }
14328:                         my %imgdeletions;
14329:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
14330:                         if (@todeleteimages) {
14331:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
14332:                         }
14333:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
14334:                         for (my $i=0; $i<=$maxnum; $i++) {
14335:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
14336:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
14337:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
14338:                                 if ($deletions{$key}) {
14339:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
14340:                                         #FIXME need to obsolete item in RES space
14341:                                     }
14342:                                     next;
14343:                                 } else {
14344:                                     my $newpos = $env{'form.'.$itemid};
14345:                                     $newpos =~ s/\D+//g;
14346:                                     foreach my $item ('subject','title','publisher','author') {
14347:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14348:                                                  ($type eq 'templates'));
14349:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
14350:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
14351:                                             $changes{$type}{$key} = 1;
14352:                                         }
14353:                                     }
14354:                                     $allpos{$type}[$newpos] = $key;
14355:                                 }
14356:                                 if ($imgdeletions{$key}) {
14357:                                     $changes{$type}{$key} = 1;
14358:                                     #FIXME need to obsolete item in RES space
14359:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
14360:                                     my ($cdom,$cnum) = split(/_/,$key);
14361:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14362:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14363:                                     } else {
14364:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
14365:                                                                                       $cdom,$cnum,$type,$configuserok,
14366:                                                                                       $switchserver,$author_ok);
14367:                                         if ($imgurl) {
14368:                                             $confhash{$type}{$key}{'image'} = $imgurl;
14369:                                             $changes{$type}{$key} = 1; 
14370:                                         }
14371:                                         if ($error) {
14372:                                             &Apache::lonnet::logthis($error);
14373:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14374:                                         }
14375:                                     }
14376:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
14377:                                     $confhash{$type}{$key}{'image'} = 
14378:                                         $domconfig{$action}{$type}{$key}{'image'};
14379:                                 }
14380:                             }
14381:                         }
14382:                     }
14383:                 }
14384:             }
14385:         } else {
14386:             if ($confhash{'notify'}{'approval'}) {
14387:                 $changes{'notify'}{'approval'} = 1;
14388:             }
14389:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
14390:                 $changes{'uniquecode'} = 1;
14391:             }
14392:         }
14393:         if ($context eq 'requestcourses') {
14394:             foreach my $type ('textbooks','templates') {
14395:                 if ($newbook{$type}) {
14396:                     $changes{$type}{$newbook{$type}} = 1;
14397:                     foreach my $item ('subject','title','publisher','author') {
14398:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14399:                                  ($type eq 'template'));
14400:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
14401:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
14402:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
14403:                         }
14404:                     }
14405:                     if ($type eq 'textbooks') {
14406:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
14407:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
14408:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14409:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14410:                             } else {
14411:                                 my ($imageurl,$error) =
14412:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
14413:                                                             $configuserok,$switchserver,$author_ok);
14414:                                 if ($imageurl) {
14415:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
14416:                                 }
14417:                                 if ($error) {
14418:                                     &Apache::lonnet::logthis($error);
14419:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14420:                                 }
14421:                             }
14422:                         }
14423:                     }
14424:                 }
14425:                 if (@{$allpos{$type}} > 0) {
14426:                     my $idx = 0;
14427:                     foreach my $item (@{$allpos{$type}}) {
14428:                         if ($item ne '') {
14429:                             $confhash{$type}{$item}{'order'} = $idx;
14430:                             if (ref($domconfig{$action}) eq 'HASH') {
14431:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
14432:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
14433:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
14434:                                             $changes{$type}{$item} = 1;
14435:                                         }
14436:                                     }
14437:                                 }
14438:                             }
14439:                             $idx ++;
14440:                         }
14441:                     }
14442:                 }
14443:             }
14444:             if (ref($validationitemsref) eq 'ARRAY') {
14445:                 foreach my $item (@{$validationitemsref}) {
14446:                     if ($item eq 'fields') {
14447:                         my @changed;
14448:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
14449:                         if (@{$confhash{'validation'}{$item}} > 0) {
14450:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
14451:                         }
14452:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14453:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14454:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
14455:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
14456:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
14457:                                 } else {
14458:                                     @changed = @{$confhash{'validation'}{$item}};
14459:                                 }
14460:                             } else {
14461:                                 @changed = @{$confhash{'validation'}{$item}};
14462:                             }
14463:                         } else {
14464:                             @changed = @{$confhash{'validation'}{$item}};
14465:                         }
14466:                         if (@changed) {
14467:                             if ($confhash{'validation'}{$item}) {
14468:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
14469:                             } else {
14470:                                 $changes{'validation'}{$item} = &mt('None');
14471:                             }
14472:                         }
14473:                     } else {
14474:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
14475:                         if ($item eq 'markup') {
14476:                             if ($env{'form.requestcourses_validation_'.$item}) {
14477:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14478:                             }
14479:                         }
14480:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14481:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14482:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
14483:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14484:                                 }
14485:                             } else {
14486:                                 if ($confhash{'validation'}{$item} ne '') {
14487:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14488:                                 }
14489:                             }
14490:                         } else {
14491:                             if ($confhash{'validation'}{$item} ne '') {
14492:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14493:                             }
14494:                         }
14495:                     }
14496:                 }
14497:             }
14498:             if ($env{'form.validationdc'}) {
14499:                 my $newval = $env{'form.validationdc'};
14500:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14501:                 if (exists($domcoords{$newval})) {
14502:                     $confhash{'validation'}{'dc'} = $newval;
14503:                 }
14504:             }
14505:             if (ref($confhash{'validation'}) eq 'HASH') {
14506:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14507:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14508:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14509:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
14510:                                 if ($confhash{'validation'}{'dc'} eq '') {
14511:                                     $changes{'validation'}{'dc'} = &mt('None');
14512:                                 } else {
14513:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14514:                                 }
14515:                             }
14516:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
14517:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14518:                         }
14519:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
14520:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14521:                     }
14522:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
14523:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14524:                 }
14525:             } else {
14526:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14527:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14528:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14529:                             $changes{'validation'}{'dc'} = &mt('None');
14530:                         }
14531:                     }
14532:                 }
14533:             }
14534:         }
14535:     } else {
14536:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
14537:     }
14538:     foreach my $item (@usertools) {
14539:         foreach my $type (@{$types},'default','_LC_adv') {
14540:             my $unset; 
14541:             if ($context eq 'requestcourses') {
14542:                 $unset = '0';
14543:                 if ($type eq '_LC_adv') {
14544:                     $unset = '';
14545:                 }
14546:                 if ($confhash{$item}{$type} eq 'autolimit') {
14547:                     $confhash{$item}{$type} .= '=';
14548:                     unless ($limithash{$item}{$type} =~ /\D/) {
14549:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
14550:                     }
14551:                 }
14552:             } elsif ($context eq 'requestauthor') {
14553:                 $unset = '0';
14554:                 if ($type eq '_LC_adv') {
14555:                     $unset = '';
14556:                 }
14557:             } else {
14558:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
14559:                     $confhash{$item}{$type} = 1;
14560:                 } else {
14561:                     $confhash{$item}{$type} = 0;
14562:                 }
14563:             }
14564:             if (ref($domconfig{$action}) eq 'HASH') {
14565:                 if ($action eq 'requestauthor') {
14566:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
14567:                         $changes{$type} = 1;
14568:                     }
14569:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
14570:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
14571:                         $changes{$item}{$type} = 1;
14572:                     }
14573:                 } else {
14574:                     if ($context eq 'requestcourses') {
14575:                         if ($confhash{$item}{$type} ne $unset) {
14576:                             $changes{$item}{$type} = 1;
14577:                         }
14578:                     } else {
14579:                         if (!$confhash{$item}{$type}) {
14580:                             $changes{$item}{$type} = 1;
14581:                         }
14582:                     }
14583:                 }
14584:             } else {
14585:                 if ($context eq 'requestcourses') {
14586:                     if ($confhash{$item}{$type} ne $unset) {
14587:                         $changes{$item}{$type} = 1;
14588:                     }
14589:                 } elsif ($context eq 'requestauthor') {
14590:                     if ($confhash{$type} ne $unset) {
14591:                         $changes{$type} = 1;
14592:                     }
14593:                 } else {
14594:                     if (!$confhash{$item}{$type}) {
14595:                         $changes{$item}{$type} = 1;
14596:                     }
14597:                 }
14598:             }
14599:         }
14600:     }
14601:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14602:         if (ref($domconfig{'quotas'}) eq 'HASH') {
14603:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14604:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
14605:                     if (exists($confhash{'defaultquota'}{$key})) {
14606:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
14607:                             $changes{'defaultquota'}{$key} = 1;
14608:                         }
14609:                     } else {
14610:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
14611:                     }
14612:                 }
14613:             } else {
14614:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
14615:                     if (exists($confhash{'defaultquota'}{$key})) {
14616:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
14617:                             $changes{'defaultquota'}{$key} = 1;
14618:                         }
14619:                     } else {
14620:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
14621:                     }
14622:                 }
14623:             }
14624:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
14625:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
14626:             }
14627:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
14628:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
14629:             }
14630:         }
14631:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
14632:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
14633:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
14634:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14635:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
14636:                             $changes{'defaultquota'}{$key} = 1;
14637:                         }
14638:                     } else {
14639:                         if (!exists($domconfig{'quotas'}{$key})) {
14640:                             $changes{'defaultquota'}{$key} = 1;
14641:                         }
14642:                     }
14643:                 } else {
14644:                     $changes{'defaultquota'}{$key} = 1;
14645:                 }
14646:             }
14647:         }
14648:     }
14649: 
14650:     if ($context eq 'requestauthor') {
14651:         $domdefaults{'requestauthor'} = \%confhash;
14652:     } else {
14653:         foreach my $key (keys(%confhash)) {
14654:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
14655:                 $domdefaults{$key} = $confhash{$key};
14656:             }
14657:         }
14658:     }
14659: 
14660:     my %quotahash = (
14661:                       $action => { %confhash }
14662:                     );
14663:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
14664:                                              $dom);
14665:     if ($putresult eq 'ok') {
14666:         if (keys(%changes) > 0) {
14667:             my $cachetime = 24*60*60;
14668:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14669:             if (ref($lastactref) eq 'HASH') {
14670:                 $lastactref->{'domdefaults'} = 1;
14671:             }
14672:             $resulttext = &mt('Changes made:').'<ul>';
14673:             unless (($context eq 'requestcourses') ||
14674:                     ($context eq 'requestauthor')) {
14675:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
14676:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
14677:                     foreach my $type (@{$types},'default') {
14678:                         if (defined($changes{'defaultquota'}{$type})) {
14679:                             my $typetitle = $usertypes->{$type};
14680:                             if ($type eq 'default') {
14681:                                 $typetitle = $othertitle;
14682:                             }
14683:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
14684:                         }
14685:                     }
14686:                     $resulttext .= '</ul></li>';
14687:                 }
14688:             }
14689:             my %newenv;
14690:             foreach my $item (@usertools) {
14691:                 my (%haschgs,%inconf);
14692:                 if ($context eq 'requestauthor') {
14693:                     %haschgs = %changes;
14694:                     %inconf = %confhash;
14695:                 } else {
14696:                     if (ref($changes{$item}) eq 'HASH') {
14697:                         %haschgs = %{$changes{$item}};
14698:                     }
14699:                     if (ref($confhash{$item}) eq 'HASH') {
14700:                         %inconf = %{$confhash{$item}};
14701:                     }
14702:                 }
14703:                 if (keys(%haschgs) > 0) {
14704:                     my $newacc = 
14705:                         &Apache::lonnet::usertools_access($env{'user.name'},
14706:                                                           $env{'user.domain'},
14707:                                                           $item,'reload',$context);
14708:                     if (($context eq 'requestcourses') ||
14709:                         ($context eq 'requestauthor')) {
14710:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14711:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14712:                         }
14713:                     } else {
14714:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14715:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14716:                         }
14717:                     }
14718:                     unless ($context eq 'requestauthor') {
14719:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14720:                     }
14721:                     foreach my $type (@{$types},'default','_LC_adv') {
14722:                         if ($haschgs{$type}) {
14723:                             my $typetitle = $usertypes->{$type};
14724:                             if ($type eq 'default') {
14725:                                 $typetitle = $othertitle;
14726:                             } elsif ($type eq '_LC_adv') {
14727:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14728:                             }
14729:                             if ($inconf{$type}) {
14730:                                 if ($context eq 'requestcourses') {
14731:                                     my $cond;
14732:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14733:                                         if ($1 eq '') {
14734:                                             $cond = &mt('(Automatic processing of any request).');
14735:                                         } else {
14736:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14737:                                         }
14738:                                     } else { 
14739:                                         $cond = $conditions{$inconf{$type}};
14740:                                     }
14741:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14742:                                 } elsif ($context eq 'requestauthor') {
14743:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14744:                                                              $titles{$inconf{$type}},$typetitle);
14745: 
14746:                                 } else {
14747:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14748:                                 }
14749:                             } else {
14750:                                 if ($type eq '_LC_adv') {
14751:                                     if ($inconf{$type} eq '0') {
14752:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14753:                                     } else { 
14754:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14755:                                     }
14756:                                 } else {
14757:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14758:                                 }
14759:                             }
14760:                         }
14761:                     }
14762:                     unless ($context eq 'requestauthor') {
14763:                         $resulttext .= '</ul></li>';
14764:                     }
14765:                 }
14766:             }
14767:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14768:                 if (ref($changes{'notify'}) eq 'HASH') {
14769:                     if ($changes{'notify'}{'approval'}) {
14770:                         if (ref($confhash{'notify'}) eq 'HASH') {
14771:                             if ($confhash{'notify'}{'approval'}) {
14772:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14773:                             } else {
14774:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14775:                             }
14776:                         }
14777:                     }
14778:                 }
14779:             }
14780:             if ($action eq 'requestcourses') {
14781:                 my @offon = ('off','on');
14782:                 if ($changes{'uniquecode'}) {
14783:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14784:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14785:                         $resulttext .= '<li>'.
14786:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14787:                                        '</li>';
14788:                     } else {
14789:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14790:                                        '</li>';
14791:                     }
14792:                 }
14793:                 foreach my $type ('textbooks','templates') {
14794:                     if (ref($changes{$type}) eq 'HASH') {
14795:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14796:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14797:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14798:                             my $coursetitle = $coursehash{'description'};
14799:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14800:                             $resulttext .= '<li>';
14801:                             foreach my $item ('subject','title','publisher','author') {
14802:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14803:                                          ($type eq 'templates'));
14804:                                 my $name = $item.':';
14805:                                 $name =~ s/^(\w)/\U$1/;
14806:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14807:                             }
14808:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14809:                             if ($type eq 'textbooks') {
14810:                                 if ($confhash{$type}{$key}{'image'}) {
14811:                                     $resulttext .= ' '.&mt('Image: [_1]',
14812:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14813:                                                    ' alt="Textbook cover" />').'<br />';
14814:                                 }
14815:                             }
14816:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14817:                         }
14818:                         $resulttext .= '</ul></li>';
14819:                     }
14820:                 }
14821:                 if (ref($changes{'validation'}) eq 'HASH') {
14822:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14823:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14824:                         foreach my $item (@{$validationitemsref}) {
14825:                             if (exists($changes{'validation'}{$item})) {
14826:                                 if ($item eq 'markup') {
14827:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14828:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14829:                                 } else {
14830:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14831:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14832:                                 }
14833:                             }
14834:                         }
14835:                         if (exists($changes{'validation'}{'dc'})) {
14836:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14837:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14838:                         }
14839:                     }
14840:                 }
14841:             }
14842:             $resulttext .= '</ul>';
14843:             if (keys(%newenv)) {
14844:                 &Apache::lonnet::appenv(\%newenv);
14845:             }
14846:         } else {
14847:             if ($context eq 'requestcourses') {
14848:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14849:             } elsif ($context eq 'requestauthor') {
14850:                 $resulttext = &mt('No changes made to rights to request author space.');
14851:             } else {
14852:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14853:             }
14854:         }
14855:     } else {
14856:         $resulttext = '<span class="LC_error">'.
14857: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14858:     }
14859:     if ($errors) {
14860:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14861:                        '<ul>'.$errors.'</ul></p>';
14862:     }
14863:     return $resulttext;
14864: }
14865: 
14866: sub process_textbook_image {
14867:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14868:     my $filename = $env{'form.'.$caller.'.filename'};
14869:     my ($error,$url);
14870:     my ($width,$height) = (50,50);
14871:     if ($configuserok eq 'ok') {
14872:         if ($switchserver) {
14873:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14874:                          $switchserver);
14875:         } elsif ($author_ok eq 'ok') {
14876:             my $modified = [];
14877:             my ($result,$imageurl) =
14878:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14879:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14880:                                                         '',$modified);
14881:             if ($result eq 'ok') {
14882:                 $url = $imageurl;
14883:                 &update_modify_urls($r,$modified);
14884:             } else {
14885:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14886:             }
14887:         } else {
14888:             $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);
14889:         }
14890:     } else {
14891:         $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);
14892:     }
14893:     return ($url,$error);
14894: }
14895: 
14896: sub modify_ltitools {
14897:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14898:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14899:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14900: 
14901:     my $confname = $dom.'-domainconfig';
14902:     my $servadm = $r->dir_config('lonAdmEMail');
14903:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14904: 
14905:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14906:     my $toolserror =
14907:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14908:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14909: 
14910:     my $home = &Apache::lonnet::domain($dom,'primary');
14911:     unless (($home eq 'no_host') || ($home eq '')) {
14912:         my @ids=&Apache::lonnet::current_machine_ids();
14913:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14914:     }
14915: 
14916:     if (keys(%ltitoolschg)) {
14917:         foreach my $id (keys(%ltitoolschg)) {
14918:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14919:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14920:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14921:                         if ($is_home) {
14922:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14923:                         }
14924:                     }
14925:                 }
14926:             }
14927:         }
14928:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14929:         if (keys(%ltitoolschg)) {
14930:             %newltitools = %ltitoolschg;
14931:         }
14932:     }
14933:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14934:         foreach my $id (%{$domconfig{'ltitools'}}) {
14935:             next if ($id !~ /^\d+$/);
14936:             unless (exists($ltitoolschg{$id})) {
14937:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14938:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14939:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14940:                             if ($is_home) {
14941:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14942:                             }
14943:                         } else {
14944:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14945:                         }
14946:                     }
14947:                 } else {
14948:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14949:                 }
14950:             }
14951:         }
14952:     }
14953:     if ($toolserror) {
14954:         $errors = '<li>'.$toolserror.'</li>';
14955:     }
14956:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14957:         $resulttext = &mt('No changes made.');
14958:         if ($errors) {
14959:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14960:                                  $errors.'</ul>';
14961:         }
14962:         return $resulttext;
14963:     }
14964:     my %ltitoolshash = (
14965:                           $action => { %newltitools }
14966:                        );
14967:     if (keys(%secchanges)) {
14968:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14969:     }
14970:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14971:     if ($putresult eq 'ok') {
14972:         my %keystore;
14973:         if ($is_home) {
14974:             my %toolsenchash = (
14975:                                    $action => { %newtoolsenc }
14976:                                );
14977:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14978:             my $cachetime = 24*60*60;
14979:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
14980:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14981:         }
14982:         $resulttext = &mt('Changes made:').'<ul>';
14983:         if (keys(%secchanges) > 0) {
14984:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14985:         }
14986:         if (keys(%ltitoolschg) > 0) {
14987:             $resulttext .= $ltitoolsoutput;
14988:         }
14989:         my $cachetime = 24*60*60;
14990:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
14991:         if (ref($lastactref) eq 'HASH') {
14992:             $lastactref->{'ltitools'} = 1;
14993:         }
14994:     } else {
14995:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14996:     }
14997:     if ($errors) {
14998:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14999:                        $errors.'</ul></p>';
15000:     }
15001:     return $resulttext;
15002: }
15003: 
15004: sub fetch_secrets {
15005:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
15006:     my %keyset;
15007:     %{$currsec} = ();
15008:     $newsec->{'private'}{'keys'} = [];
15009:     $newsec->{'encrypt'} = {};
15010:     $newsec->{'rules'} = {};
15011:     if ($context eq 'ltisec') {
15012:         $newsec->{'linkprot'} = {};
15013:     }
15014:     if (ref($domconfig->{$context}) eq 'HASH') {
15015:         %{$currsec} = %{$domconfig->{$context}};
15016:         if ($context eq 'ltisec') {
15017:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
15018:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
15019:                     unless ($id =~ /^\d+$/) {
15020:                         delete($currsec->{'linkprot'}{$id});
15021:                     }
15022:                 }
15023:             }
15024:         }
15025:         if (ref($currsec->{'private'}) eq 'HASH') {
15026:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
15027:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
15028:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
15029:             }
15030:         }
15031:     }
15032:     my @items= ('crs','dom');
15033:     if ($context eq 'ltisec') {
15034:         push(@items,'consumers');
15035:     }
15036:     foreach my $item (@items) {
15037:         my $formelement;
15038:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
15039:             $formelement = 'form.'.$context.'_'.$item;
15040:         } else {
15041:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
15042:         }
15043:         if ($env{$formelement}) {
15044:             $newsec->{'encrypt'}{$item} = 1;
15045:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
15046:                 unless ($currsec->{'encrypt'}{$item}) {
15047:                     $secchanges->{'encrypt'} = 1;
15048:                 }
15049:             } else {
15050:                 $secchanges->{'encrypt'} = 1;
15051:             }
15052:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
15053:             if ($currsec->{'encrypt'}{$item}) {
15054:                 $secchanges->{'encrypt'} = 1;
15055:             }
15056:         }
15057:     }
15058:     my $secrets;
15059:     if ($context eq 'ltisec') {
15060:         $secrets = 'ltisecrets';
15061:     } else {
15062:         $secrets = 'toolsecrets';
15063:     }
15064:     unless (exists($currsec->{'rules'})) {
15065:         $currsec->{'rules'} = {};
15066:     }
15067:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
15068: 
15069:     my @ids=&Apache::lonnet::current_machine_ids();
15070:     my %servers = &Apache::lonnet::get_servers($dom,'library');
15071: 
15072:     foreach my $hostid (keys(%servers)) {
15073:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
15074:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
15075:             if (exists($env{$keyitem})) {
15076:                 $env{$keyitem} =~ s/(`)/'/g;
15077:                 if ($keyset{$hostid}) {
15078:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
15079:                         if ($env{$keyitem} ne '') {
15080:                             $secchanges->{'private'} = 1;
15081:                             $newkeyset->{$hostid} = $env{$keyitem};
15082:                         }
15083:                     }
15084:                 } elsif ($env{$keyitem} ne '') {
15085:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
15086:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
15087:                     }
15088:                     $secchanges->{'private'} = 1;
15089:                     $newkeyset->{$hostid} = $env{$keyitem};
15090:                 }
15091:             }
15092:         }
15093:     }
15094: }
15095: 
15096: sub store_security {
15097:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
15098:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
15099:                    (ref($keystore) eq 'HASH'));
15100:     if (keys(%{$secchanges})) {
15101:         if ($secchanges->{'private'}) {
15102:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
15103:             foreach my $hostid (keys(%{$newkeyset})) {
15104:                 my $storehash = {
15105:                                    key => $newkeyset->{$hostid},
15106:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
15107:                                 };
15108:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
15109:                                                                   $dom,$hostid);
15110:             }
15111:         }
15112:     }
15113: }
15114: 
15115: sub lti_security_results {
15116:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
15117:     my $output;
15118:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15119:     my $needs_update;
15120:     foreach my $item (keys(%{$secchanges})) {
15121:         if ($item eq 'encrypt') {
15122:             $needs_update = 1;
15123:             my %encrypted;
15124:             if ($context eq 'lti') {
15125:                 %encrypted = (
15126:                               crs  => {
15127:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
15128:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
15129:                                       },
15130:                               dom => {
15131:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
15132:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
15133:                                      },
15134:                               consumers => {
15135:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
15136:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
15137:                                            },
15138:                              );
15139:             } else {
15140:                 %encrypted = (
15141:                               crs  => {
15142:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
15143:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
15144:                                       },
15145:                               dom => {
15146:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
15147:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
15148:                                      },
15149:                              );
15150:             }
15151:             my @types= ('crs','dom');
15152:             if ($context eq 'lti') {
15153:                 foreach my $type (@types) {
15154:                     undef($domdefaults{'linkprotenc_'.$type});
15155:                 }
15156:                 push(@types,'consumers');
15157:                 undef($domdefaults{'ltienc_consumers'});
15158:             } elsif ($context eq 'ltitools') {
15159:                 foreach my $type (@types) {
15160:                     undef($domdefaults{'toolenc_'.$type});
15161:                 }
15162:             }
15163:             foreach my $type (@types) {
15164:                 my $shown = $encrypted{$type}{'off'};
15165:                 if (ref($newsec->{$item}) eq 'HASH') {
15166:                     if ($newsec->{$item}{$type}) {
15167:                         if ($context eq 'lti') {
15168:                             if ($type eq 'consumers') {
15169:                                 $domdefaults{'ltienc_consumers'} = 1;
15170:                             } else {
15171:                                 $domdefaults{'linkprotenc_'.$type} = 1;
15172:                             }
15173:                         } elsif ($context eq 'ltitools') {
15174:                             $domdefaults{'toolenc_'.$type} = 1;
15175:                         }
15176:                         $shown = $encrypted{$type}{'on'};
15177:                     }
15178:                 }
15179:                 $output .= '<li>'.$shown.'</li>';
15180:             }
15181:         } elsif ($item eq 'rules') {
15182:             my %titles = &Apache::lonlocal::texthash(
15183:                                       min   => 'Minimum password length',
15184:                                       max   => 'Maximum password length',
15185:                                       chars => 'Required characters',
15186:                          );
15187:             foreach my $rule ('min','max') {
15188:                 if ($newsec->{rules}{$rule} eq '') {
15189:                     if ($rule eq 'min') {
15190:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15191:                                    ' '.&mt('Default of [_1] will be used',
15192:                                            $Apache::lonnet::passwdmin).'</li>';
15193:                     } else {
15194:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15195:                     }
15196:                 } else {
15197:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
15198:                 }
15199:             }
15200:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
15201:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
15202:                     my %rulenames = &Apache::lonlocal::texthash(
15203:                                              uc => 'At least one upper case letter',
15204:                                              lc => 'At least one lower case letter',
15205:                                              num => 'At least one number',
15206:                                              spec => 'At least one non-alphanumeric',
15207:                                     );
15208:                     my $needed = '<ul><li>'.
15209:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
15210:                                  '</li></ul>';
15211:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15212:                 } else {
15213:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15214:                 }
15215:             } else {
15216:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15217:             }
15218:         } elsif ($item eq 'private') {
15219:             $needs_update = 1;
15220:             if ($context eq 'lti') {
15221:                 undef($domdefaults{'ltiprivhosts'});
15222:             } elsif ($context eq 'ltitools') {
15223:                 undef($domdefaults{'toolprivhosts'});
15224:             }
15225:             if (keys(%{$newkeyset})) {
15226:                 my @privhosts;
15227:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
15228:                     if ($keystore->{$hostid} eq 'ok') {
15229:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
15230:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
15231:                             push(@privhosts,$hostid);
15232:                         }
15233:                     }
15234:                 }
15235:                 if (@privhosts) {
15236:                     if ($context eq 'lti') {
15237:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
15238:                     } elsif ($context eq 'ltitools') {
15239:                         $domdefaults{'toolprivhosts'} = \@privhosts;
15240:                     }
15241:                 }
15242:             }
15243:         } elsif ($item eq 'linkprot') {
15244:             next;
15245:         } elsif ($item eq 'suggested') {
15246:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
15247:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
15248:                 my $suggestions;
15249:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
15250:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
15251:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
15252:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
15253:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
15254:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
15255:                                         '</li>';
15256:                     } else {
15257:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
15258:                                                    $newsec->{'suggested'}->{$id}).'</li>';
15259:                     }
15260:                 }
15261:                 if ($suggestions) {
15262:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
15263:                                '<ul>'.$suggestions.'</ul>'.
15264:                                '</li>';
15265:                 }
15266:             }
15267:         }
15268:     }
15269:     if ($needs_update) {
15270:         my $cachetime = 24*60*60;
15271:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15272:     }
15273:     return $output;
15274: }
15275: 
15276: sub modify_proctoring {
15277:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15278:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15279:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
15280:     my $confname = $dom.'-domainconfig';
15281:     my $servadm = $r->dir_config('lonAdmEMail');
15282:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
15283:     my %providernames = &proctoring_providernames();
15284:     my $maxnum = scalar(keys(%providernames));
15285: 
15286:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
15287:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
15288:     if (ref($requref) eq 'HASH') {
15289:         %requserfields = %{$requref};
15290:     }
15291:     if (ref($opturef) eq 'HASH') {
15292:         %optuserfields = %{$opturef};
15293:     }
15294:     if (ref($defref) eq 'HASH') {
15295:         %defaults = %{$defref};
15296:     }
15297:     if (ref($extref) eq 'HASH') {
15298:         %extended = %{$extref};
15299:     }
15300:     if (ref($crsref) eq 'HASH') {
15301:         %crsconf = %{$crsref};
15302:     }
15303:     if (ref($rolesref) eq 'ARRAY') {
15304:         @courseroles = @{$rolesref};
15305:     }
15306:     if (ref($ltiref) eq 'ARRAY') {
15307:         @ltiroles = @{$ltiref};
15308:     }
15309: 
15310:     if (ref($domconfig{$action}) eq 'HASH') {
15311:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
15312:         if (@todeleteimages) {
15313:             map { $imgdeletions{$_} = 1; } @todeleteimages;
15314:         }
15315:     }
15316:     my %customadds;
15317:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
15318:     if (@newcustom) {
15319:         map { $customadds{$_} = 1; } @newcustom;
15320:     }
15321:     foreach my $provider (sort(keys(%providernames))) {
15322:         $confhash{$provider} = {};
15323:         my $pos = $env{'form.proctoring_pos_'.$provider};
15324:         $pos =~ s/\D+//g;
15325:         $allpos[$pos] = $provider;
15326:         my (%current,%currentenc);
15327:         my $showroles = 0;
15328:         if (ref($domconfig{$action}) eq 'HASH') {
15329:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15330:                 %current = %{$domconfig{$action}{$provider}};
15331:                 foreach my $item ('key','secret') { 
15332:                     $currentenc{$item} = $current{$item};
15333:                     delete($current{$item});
15334:                 }
15335:             }
15336:         }
15337:         if ($env{'form.proctoring_available_'.$provider}) {
15338:             $confhash{$provider}{'available'} = 1;
15339:             unless ($current{'available'}) {
15340:                 $changes{$provider} = 1;
15341:             }
15342:         } else {
15343:             %{$confhash{$provider}} = %current;
15344:             %{$encconfhash{$provider}} = %currentenc;
15345:             $confhash{$provider}{'available'} = 0;
15346:             if ($current{'available'}) {
15347:                 $changes{$provider} = 1;
15348:             }
15349:         }
15350:         if ($confhash{$provider}{'available'}) {
15351:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
15352:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
15353:                 if ($field eq 'lifetime') {
15354:                     if ($possval =~ /^\d+$/) {
15355:                         $confhash{$provider}{$field} = $possval;
15356:                     }
15357:                 } elsif ($field eq 'version') {
15358:                     if ($possval =~ /^\d+\.\d+$/) {
15359:                         $confhash{$provider}{$field} = $possval;
15360:                     }
15361:                 } elsif ($field eq 'sigmethod') {
15362:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
15363:                         $confhash{$provider}{$field} = $possval;
15364:                     }
15365:                 } elsif ($field eq 'url') {
15366:                     $confhash{$provider}{$field} = $possval;
15367:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
15368:                     $encconfhash{$provider}{$field} = $possval;
15369:                     unless ($currentenc{$field} eq $possval) {
15370:                         $changes{$provider} = 1;
15371:                     }
15372:                 }
15373:                 unless (($field eq 'key') || ($field eq 'secret')) {
15374:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
15375:                         $changes{$provider} = 1;
15376:                     }
15377:                 }
15378:             }
15379:             if ($imgdeletions{$provider}) {
15380:                 $changes{$provider} = 1;
15381:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
15382:                 my ($imageurl,$error) =
15383:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
15384:                                               $configuserok,$switchserver,$author_ok);
15385:                 if ($imageurl) {
15386:                     $confhash{$provider}{'image'} = $imageurl;
15387:                     $changes{$provider} = 1; 
15388:                 }
15389:                 if ($error) {
15390:                     &Apache::lonnet::logthis($error);
15391:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15392:                 }
15393:             } elsif (exists($current{'image'})) {
15394:                 $confhash{$provider}{'image'} = $current{'image'};
15395:             }
15396:             if (ref($requserfields{$provider}) eq 'ARRAY') {
15397:                 if (@{$requserfields{$provider}} > 0) {
15398:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
15399:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
15400:                             $confhash{$provider}{'incdom'} = 1;
15401:                         }
15402:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
15403:                             $changes{$provider} = 1;
15404:                         }
15405:                     }
15406:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
15407:                         $showroles = 1;
15408:                     }
15409:                 }
15410:             }
15411:             $confhash{$provider}{'fields'} = [];
15412:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
15413:                 if (@{$optuserfields{$provider}} > 0) {
15414:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
15415:                     foreach my $field (@{$optuserfields{$provider}}) {
15416:                         if (grep(/^\Q$field\E$/,@optfields)) {
15417:                             push(@{$confhash{$provider}{'fields'}},$field);
15418:                         }
15419:                     }
15420:                 }
15421:                 if (ref($current{'fields'}) eq 'ARRAY') {
15422:                     unless ($changes{$provider}) {
15423:                         my @new = sort(@{$confhash{$provider}{'fields'}});
15424:                         my @old = sort(@{$current{'fields'}}); 
15425:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15426:                         if (@diffs) {
15427:                             $changes{$provider} = 1;
15428:                         }
15429:                     }
15430:                 } elsif (@{$confhash{$provider}{'fields'}}) {
15431:                     $changes{$provider} = 1;
15432:                 }
15433:             }
15434:             if (ref($defaults{$provider}) eq 'ARRAY') {  
15435:                 if (@{$defaults{$provider}} > 0) {
15436:                     my %options;
15437:                     if (ref($extended{$provider}) eq 'HASH') {
15438:                         %options = %{$extended{$provider}};
15439:                     }
15440:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
15441:                     foreach my $field (@{$defaults{$provider}}) {
15442:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
15443:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
15444:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
15445:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
15446:                             }
15447:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
15448:                             foreach my $inner (keys(%{$options{$field}})) {
15449:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
15450:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
15451:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
15452:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
15453:                                     }
15454:                                 } else {
15455:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
15456:                                 }
15457:                             }
15458:                         } else {
15459:                             if (grep(/^\Q$field\E$/,@checked)) {
15460:                                 push(@{$confhash{$provider}{'defaults'}},$field);
15461:                             }
15462:                         }
15463:                     }
15464:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15465:                         if (ref($current{'defaults'}) eq 'ARRAY') {
15466:                             unless ($changes{$provider}) {
15467:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
15468:                                 my @old = sort(@{$current{'defaults'}});
15469:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15470:                                 if (@diffs) {
15471:                                     $changes{$provider} = 1; 
15472:                                 }
15473:                             }
15474:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
15475:                             if (@{$current{'defaults'}}) {
15476:                                 $changes{$provider} = 1;
15477:                             }
15478:                         }
15479:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15480:                         if (ref($current{'defaults'}) eq 'HASH') {
15481:                             unless ($changes{$provider}) {
15482:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15483:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15484:                                         $changes{$provider} = 1;
15485:                                         last;
15486:                                     }
15487:                                 }
15488:                             }
15489:                             unless ($changes{$provider}) {
15490:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15491:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15492:                                         $changes{$provider} = 1;
15493:                                         last;
15494:                                     }
15495:                                 }
15496:                             }
15497:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15498:                             $changes{$provider} = 1;
15499:                         }
15500:                     }
15501:                 }
15502:             }
15503:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15504:                 if (@{$crsconf{$provider}} > 0) {
15505:                     $confhash{$provider}{'crsconf'} = [];
15506:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15507:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15508:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15509:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15510:                         }
15511:                     }
15512:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15513:                         unless ($changes{$provider}) {  
15514:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15515:                             my @old = sort(@{$current{'crsconf'}});
15516:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15517:                             if (@diffs) {
15518:                                 $changes{$provider} = 1;
15519:                             }
15520:                         }
15521:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15522:                         $changes{$provider} = 1;
15523:                     }
15524:                 }
15525:             }
15526:             if ($showroles) {
15527:                 $confhash{$provider}{'roles'} = {};
15528:                 foreach my $role (@courseroles) {
15529:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15530:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15531:                         $confhash{$provider}{'roles'}{$role} = $poss;
15532:                     }
15533:                 }
15534:                 unless ($changes{$provider}) {
15535:                     if (ref($current{'roles'}) eq 'HASH') {
15536:                         foreach my $role (keys(%{$current{'roles'}})) {
15537:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15538:                                 $changes{$provider} = 1;
15539:                                 last
15540:                             }
15541:                         }
15542:                         unless ($changes{$provider}) {
15543:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15544:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15545:                                     $changes{$provider} = 1;
15546:                                     last;
15547:                                 }
15548:                             }
15549:                         }
15550:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15551:                         $changes{$provider} = 1;
15552:                     }
15553:                 }
15554:             }
15555:             if (ref($current{'custom'}) eq 'HASH') {
15556:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15557:                 foreach my $key (keys(%{$current{'custom'}})) {
15558:                     if (grep(/^\Q$key\E$/,@customdels)) {
15559:                         $changes{$provider} = 1;
15560:                     } else {
15561:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15562:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15563:                             $changes{$provider} = 1;
15564:                         }
15565:                     }
15566:                 }
15567:             }
15568:             if ($customadds{$provider}) {
15569:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15570:                 $name =~ s/(`)/'/g;
15571:                 $name =~ s/^\s+//;
15572:                 $name =~ s/\s+$//;
15573:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15574:                 $value =~ s/(`)/'/g;
15575:                 $value =~ s/^\s+//;
15576:                 $value =~ s/\s+$//;
15577:                 if ($name ne '') {
15578:                     $confhash{$provider}{'custom'}{$name} = $value;
15579:                     $changes{$provider} = 1;
15580:                 }
15581:             }
15582:         }
15583:     }
15584:     if (@allpos > 0) {
15585:         my $idx = 0;
15586:         foreach my $provider (@allpos) {
15587:             if ($provider ne '') {
15588:                 $confhash{$provider}{'order'} = $idx;
15589:                 unless ($changes{$provider}) {
15590:                     if (ref($domconfig{$action}) eq 'HASH') {
15591:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15592:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15593:                                 $changes{$provider} = 1;
15594:                             }
15595:                         }
15596:                     }
15597:                 }
15598:                 $idx ++;
15599:             }
15600:         }
15601:     }
15602:     my %proc_hash = (
15603:                           $action => { %confhash }
15604:                        );
15605:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15606:                                              $dom);
15607:     if ($putresult eq 'ok') {
15608:         my %proc_enchash = (
15609:                              $action => { %encconfhash }
15610:                          );
15611:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15612:         if (keys(%changes) > 0) {
15613:             my $cachetime = 24*60*60;
15614:             my %procall = %confhash;
15615:             foreach my $provider (keys(%procall)) {
15616:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15617:                     foreach my $key ('key','secret') {
15618:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15619:                     }
15620:                 }
15621:             }
15622:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15623:             if (ref($lastactref) eq 'HASH') {
15624:                 $lastactref->{'proctoring'} = 1;
15625:             }
15626:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15627:             my %bynum;
15628:             foreach my $provider (sort(keys(%changes))) {
15629:                 my $position = $confhash{$provider}{'order'};
15630:                 $bynum{$position} = $provider;
15631:             }
15632:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15633:                 my $provider = $bynum{$pos};
15634:                 my %lt = &proctoring_titles($provider);
15635:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15636:                 if (!$confhash{$provider}{'available'}) {
15637:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15638:                 } else {
15639:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15640:                     if ($confhash{$provider}{'image'}) {
15641:                         $resulttext .= '&nbsp;'.
15642:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15643:                                        ' alt="'.&mt('Proctoring icon').'" />';
15644:                     }
15645:                     $resulttext .= '<ul>';
15646:                     my $position = $pos + 1;
15647:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15648:                     foreach my $key ('version','sigmethod','url','lifetime') {
15649:                         if ($confhash{$provider}{$key} ne '') {
15650:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15651:                         }
15652:                     }
15653:                     if ($encconfhash{$provider}{'key'} ne '') {
15654:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15655:                     }
15656:                     if ($encconfhash{$provider}{'secret'} ne '') {
15657:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15658:                         my $num = length($encconfhash{$provider}{'secret'});
15659:                         $resulttext .= ('*'x$num).'</li>';
15660:                     }
15661:                     my (@fields,$showroles);
15662:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15663:                         push(@fields,@{$requserfields{$provider}});
15664:                     }
15665:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15666:                         push(@fields,@{$confhash{$provider}{'fields'}});
15667:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15668:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15669:                     }
15670:                     if (@fields) {
15671:                         if (grep(/^roles$/,@fields)) {
15672:                             $showroles = 1;
15673:                         }
15674:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15675:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15676:                     }
15677:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15678:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15679:                             if ($confhash{$provider}{'incdom'}) {
15680:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15681:                             } else { 
15682:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15683:                             }
15684:                         }
15685:                     }
15686:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15687:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15688:                             $resulttext .= '<li>'.$lt{'defa'};
15689:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15690:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15691:                             }
15692:                             $resulttext =~ s/,$//;
15693:                             $resulttext .= '</li>';
15694:                         }
15695:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15696:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15697:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15698:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15699:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15700:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15701:                                 }
15702:                             }
15703:                             $resulttext .= '</ul></li>';
15704:                         }
15705:                     }
15706:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15707:                         if (@{$crsconf{$provider}} > 0) {
15708:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15709:                             my $numconfig = 0;
15710:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15711:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15712:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15713:                                         $numconfig ++;
15714:                                         if ($provider eq 'examity') {
15715:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15716:                                         } else {
15717:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15718:                                         }
15719:                                     }
15720:                                     $resulttext =~ s/,$//;
15721:                                 }
15722:                             }
15723:                             if (!$numconfig) {
15724:                                 $resulttext .= '&nbsp;'.&mt('None');
15725:                             }
15726:                             $resulttext .= '</li>';
15727:                         }
15728:                     }
15729:                     if ($showroles) {
15730:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15731:                             my $rolemaps;
15732:                             foreach my $role (@courseroles) {
15733:                                 if ($confhash{$provider}{'roles'}{$role}) {
15734:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15735:                                                  $confhash{$provider}{'roles'}{$role}.',';
15736:                                 }
15737:                             }
15738:                             if ($rolemaps) {
15739:                                 $rolemaps =~ s/,$//;
15740:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15741:                             }
15742:                         }
15743:                     }
15744:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15745:                         my $customlist;
15746:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15747:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15748:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15749:                             }
15750:                             $customlist =~ s/,$//;
15751:                         }
15752:                         if ($customlist) {
15753:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15754:                         }
15755:                     } 
15756:                     $resulttext .= '</ul></li>';
15757:                 }
15758:             }
15759:             $resulttext .= '</ul>';
15760:         } else {
15761:             $resulttext = &mt('No changes made.');
15762:         }
15763:     } else {
15764:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15765:     }
15766:     if ($errors) {
15767:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15768:                        $errors.'</ul>';
15769:     }
15770:     return $resulttext;
15771: }
15772: 
15773: sub process_proctoring_image {
15774:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15775:     my $filename = $env{'form.'.$caller.'.filename'};
15776:     my ($error,$url);
15777:     my ($width,$height) = (21,21);
15778:     if ($configuserok eq 'ok') {
15779:         if ($switchserver) {
15780:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15781:                          $switchserver);
15782:         } elsif ($author_ok eq 'ok') {
15783:             my $modified = [];
15784:             my ($result,$imageurl,$madethumb) =
15785:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15786:                                                         "proctoring/$provider/icon",$width,$height,
15787:                                                         '',$modified);
15788:             if ($result eq 'ok') {
15789:                 if ($madethumb) {
15790:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15791:                     my $imagethumb = "$path/tn-".$imagefile;
15792:                     $url = $imagethumb;
15793:                 } else {
15794:                     $url = $imageurl;
15795:                 }
15796:                 &update_modify_urls($r,$modified);
15797:             } else {
15798:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15799:             }
15800:         } else {
15801:             $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);
15802:         }
15803:     } else {
15804:         $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);
15805:     }
15806:     return ($url,$error);
15807: }
15808: 
15809: sub modify_lti {
15810:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15811:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15812:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15813:     my (%posslti,%posslticrs,%posscrstype);
15814:     my @courseroles = ('cc','in','ta','ep','st');
15815:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15816:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15817:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15818:     my %coursetypetitles = &Apache::lonlocal::texthash (
15819:                                official   => 'Official',
15820:                                unofficial => 'Unofficial',
15821:                                community  => 'Community',
15822:                                textbook   => 'Textbook',
15823:                                placement  => 'Placement Test',
15824:                                lti        => 'LTI Provider',
15825:     );
15826:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15827:     my %lt = &lti_names();
15828:     map { $posslti{$_} = 1; } @ltiroles;
15829:     map { $posslticrs{$_} = 1; } @lticourseroles;
15830:     map { $posscrstype{$_} = 1; } @coursetypes;
15831: 
15832:     my %menutitles = &ltimenu_titles();
15833:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15834: 
15835:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15836: 
15837:     my (%linkprotchg,$linkprotoutput,$is_home);
15838:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15839:                                                            \%linkprotchg,'domain');
15840:     my $home = &Apache::lonnet::domain($dom,'primary');
15841:     unless (($home eq 'no_host') || ($home eq '')) {
15842:         my @ids=&Apache::lonnet::current_machine_ids();
15843:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15844:     }
15845: 
15846:     if (keys(%linkprotchg)) {
15847:         $secchanges{'linkprot'} = 1;
15848:         my %oldlinkprot;
15849:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15850:             %oldlinkprot = %{$currltisec{'linkprot'}};
15851:         }
15852:         foreach my $id (keys(%linkprotchg)) {
15853:             if (ref($linkprotchg{$id}) eq 'HASH') {
15854:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15855:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15856:                         if ($is_home) {
15857:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15858:                         }
15859:                     }
15860:                 }
15861:             } else {
15862:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15863:             }
15864:         }
15865:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15866:         if (keys(%linkprotchg)) {
15867:             %{$newltisec{'linkprot'}} = %linkprotchg;
15868:         }
15869:     }
15870:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15871:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
15872:             next if ($id !~ /^\d+$/);
15873:             unless (exists($linkprotchg{$id})) {
15874:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15875:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15876:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15877:                             if ($is_home) {
15878:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15879:                             }
15880:                         } else {
15881:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15882:                         }
15883:                     }
15884:                 } else {
15885:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15886:                 }
15887:             }
15888:         }
15889:     }
15890:     if ($proterror) {
15891:         $errors .= '<li>'.$proterror.'</li>';
15892:     }
15893: 
15894:     my (%delsuggested,%suggids,@suggested);;
15895:     if (ref($currltisec{'suggested'}) eq 'HASH') {
15896:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
15897:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
15898:         for (my $i=0; $i<$maxnum; $i++) {
15899:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
15900:             $itemid =~ s/\D+//g;
15901:             if ($itemid) {
15902:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
15903:                     push(@suggested,$i);
15904:                     $suggids{$i} = $itemid;
15905:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
15906:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
15907:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15908:                         }
15909:                     } else {
15910:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
15911:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15912:                         } else {
15913:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
15914:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
15915:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
15916:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
15917:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
15918:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
15919:                                 $secchanges{'suggested'}{$itemid} = 1;
15920:                             }
15921:                         }
15922:                     }
15923:                 }
15924:             }
15925:         }
15926:     }
15927:     foreach my $key (keys(%delsuggested)) {
15928:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
15929:         $secchanges{'suggested'}{$key} = 1;
15930:     }
15931:     if (($env{'form.linkprot_suggested_add'}) &&
15932:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
15933:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
15934:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
15935:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
15936:         if ($newsuggid) {
15937:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
15938:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
15939:             $secchanges{'suggested'}{$newsuggid} = 1;
15940:         } else {
15941:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
15942:             if ($errormsg) {
15943:                 $error .= ' ('.$errormsg.')';
15944:             }
15945:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15946:         }
15947:     }
15948:     my (@items,%deletions,%itemids);
15949:     if ($env{'form.lti_add'}) {
15950:         my $consumer = $env{'form.lti_consumer_add'};
15951:         $consumer =~ s/(`)/'/g;
15952:         ($newid,my $errormsg) = &get_lti_id($dom,$consumer,'lti');
15953:         if ($newid) {
15954:             $itemids{'add'} = $newid;
15955:             push(@items,'add');
15956:             $changes{$newid} = 1;
15957:         } else {
15958:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15959:             if ($errormsg) {
15960:                 $error .= ' ('.$errormsg.')';
15961:             }
15962:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15963:         }
15964:     }
15965:     if (ref($domconfig{$action}) eq 'HASH') {
15966:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15967:         if (@todelete) {
15968:             map { $deletions{$_} = 1; } @todelete;
15969:         }
15970:         my $maxnum = $env{'form.lti_maxnum'};
15971:         for (my $i=0; $i<$maxnum; $i++) {
15972:             my $itemid = $env{'form.lti_id_'.$i};
15973:             $itemid =~ s/\D+//g;
15974:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15975:                 if ($deletions{$itemid}) {
15976:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15977:                 } else {
15978:                     push(@items,$i);
15979:                     $itemids{$i} = $itemid;
15980:                 }
15981:             }
15982:         }
15983:     }
15984:     my (%keystore,$secstored);
15985:     if ($is_home) {
15986:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
15987:     }
15988: 
15989:     my ($cipher,$privnum);
15990:     if ((@items > 0) && ($is_home)) {
15991:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
15992:                                              $newltisec{'encrypt'},$keystore{$home});
15993:     }
15994:     foreach my $idx (@items) {
15995:         my $itemid = $itemids{$idx};
15996:         next unless ($itemid);
15997:         my %currlti;
15998:         unless ($idx eq 'add') {
15999:             if (ref($domconfig{$action}) eq 'HASH') {
16000:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16001:                     %currlti = %{$domconfig{$action}{$itemid}};
16002:                 }
16003:             }
16004:         }
16005:         my $position = $env{'form.lti_pos_'.$itemid};
16006:         $position =~ s/\D+//g;
16007:         if ($position ne '') {
16008:             $allpos[$position] = $itemid;
16009:         }
16010:         foreach my $item ('consumer','lifetime','requser','crsinc') {
16011:             my $formitem = 'form.lti_'.$item.'_'.$idx;
16012:             $env{$formitem} =~ s/(`)/'/g;
16013:             if ($item eq 'lifetime') {
16014:                 $env{$formitem} =~ s/[^\d.]//g;
16015:             }
16016:             if ($env{$formitem} ne '') {
16017:                 $confhash{$itemid}{$item} = $env{$formitem};
16018:                 unless (($idx eq 'add') || ($changes{$itemid})) {
16019:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
16020:                         $changes{$itemid} = 1;
16021:                     }
16022:                 }
16023:             }
16024:         }
16025:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
16026:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
16027:         }
16028:         if ($confhash{$itemid}{'requser'}) {
16029:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
16030:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
16031:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
16032:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
16033:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
16034:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
16035:                 $mapuser =~ s/(`)/'/g;
16036:                 $mapuser =~ s/^\s+|\s+$//g;
16037:                 $confhash{$itemid}{'mapuser'} = $mapuser;
16038:             }
16039:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
16040:             my @makeuser;
16041:             foreach my $ltirole (sort(@possmakeuser)) {
16042:                 if ($posslti{$ltirole}) {
16043:                     push(@makeuser,$ltirole);
16044:                 }
16045:             }
16046:             $confhash{$itemid}{'makeuser'} = \@makeuser;
16047:             if (@makeuser) {
16048:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
16049:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
16050:                     $confhash{$itemid}{'lcauth'} = $lcauth;
16051:                     if ($lcauth ne 'internal') {
16052:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
16053:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
16054:                         $lcauthparm =~ s/`//g;
16055:                         if ($lcauthparm ne '') {
16056:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
16057:                         }
16058:                     }
16059:                 } else {
16060:                     $confhash{$itemid}{'lcauth'} = 'lti';
16061:                 }
16062:             }
16063:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
16064:             if (@possinstdata) {
16065:                 foreach my $field (@possinstdata) {
16066:                     if (exists($fieldtitles{$field})) {
16067:                         push(@{$confhash{$itemid}{'instdata'}});
16068:                     }
16069:                 }
16070:             }
16071:             if ($env{'form.lti_callback_'.$idx}) {
16072:                 if ($env{'form.lti_callbackparam_'.$idx}) {
16073:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
16074:                     $callback =~ s/^\s+|\s+$//g;
16075:                     $confhash{$itemid}{'callback'} = $callback;
16076:                 }
16077:             }
16078:             foreach my $field ('topmenu','inlinemenu') {
16079:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
16080:                     $confhash{$itemid}{$field} = 1;
16081:                 }
16082:             }
16083:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
16084:                 $confhash{$itemid}{lcmenu} = [];
16085:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
16086:                 foreach my $field (@possmenu) {
16087:                     if (exists($menutitles{$field})) {
16088:                         if ($field eq 'grades') {
16089:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
16090:                         }
16091:                         push(@{$confhash{$itemid}{lcmenu}},$field);
16092:                     }
16093:                 }
16094:             }
16095:             if ($confhash{$itemid}{'crsinc'}) {
16096:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
16097:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
16098:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
16099:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
16100:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
16101:                     $mapcrs =~ s/(`)/'/g;
16102:                     $mapcrs =~ s/^\s+|\s+$//g;
16103:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
16104:                 }
16105:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
16106:                 my @crstypes;
16107:                 foreach my $type (sort(@posstypes)) {
16108:                     if ($posscrstype{$type}) {
16109:                         push(@crstypes,$type);
16110:                     }
16111:                 }
16112:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
16113:                 if ($env{'form.lti_storecrs_'.$idx}) {
16114:                     $confhash{$itemid}{'storecrs'} = 1;
16115:                 }       
16116:                 if ($env{'form.lti_makecrs_'.$idx}) {
16117:                     $confhash{$itemid}{'makecrs'} = 1;
16118:                 }
16119:                 foreach my $ltirole (@lticourseroles) {
16120:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
16121:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
16122:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
16123:                     }
16124:                 }
16125:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
16126:                 my @selfenroll;
16127:                 foreach my $type (sort(@possenroll)) {
16128:                     if ($posslticrs{$type}) {
16129:                         push(@selfenroll,$type);
16130:                     }
16131:                 }
16132:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
16133:                 if ($env{'form.lti_crssec_'.$idx}) {
16134:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
16135:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
16136:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
16137:                         my $section = $env{'form.lti_customsection_'.$idx};
16138:                         $section =~ s/(`)/'/g;
16139:                         $section =~ s/^\s+|\s+$//g;
16140:                         if ($section ne '') {
16141:                             $confhash{$itemid}{'section'} = $section;
16142:                         }
16143:                     }
16144:                 }
16145:                 foreach my $field ('passback','roster') {
16146:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
16147:                         $confhash{$itemid}{$field} = 1;
16148:                     }
16149:                 }
16150:                 if ($env{'form.lti_passback_'.$idx}) {
16151:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
16152:                         $confhash{$itemid}{'passbackformat'} = '1.0';
16153:                     } else {
16154:                         $confhash{$itemid}{'passbackformat'} = '1.1';
16155:                     }
16156:                 }
16157:             }
16158:             unless (($idx eq 'add') || ($changes{$itemid})) {
16159:                 if ($confhash{$itemid}{'crsinc'}) {
16160:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
16161:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16162:                             $changes{$itemid} = 1;
16163:                         }
16164:                     }
16165:                     unless ($changes{$itemid}) {
16166:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
16167:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
16168:                                 $changes{$itemid} = 1;
16169:                             }
16170:                         }
16171:                     }
16172:                     foreach my $field ('mapcrstype','selfenroll') {
16173:                         unless ($changes{$itemid}) {
16174:                             if (ref($currlti{$field}) eq 'ARRAY') {
16175:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16176:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16177:                                                                                    $confhash{$itemid}{$field});
16178:                                     if (@diffs) {
16179:                                         $changes{$itemid} = 1;
16180:                                     }
16181:                                 } elsif (@{$currlti{$field}} > 0) {
16182:                                     $changes{$itemid} = 1;
16183:                                 }
16184:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16185:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16186:                                     $changes{$itemid} = 1;
16187:                                 }
16188:                             }
16189:                         }
16190:                     }
16191:                     unless ($changes{$itemid}) {
16192:                         if (ref($currlti{'maproles'}) eq 'HASH') {
16193:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16194:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
16195:                                     if ($currlti{'maproles'}{$ltirole} ne 
16196:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
16197:                                         $changes{$itemid} = 1;
16198:                                         last;
16199:                                     }
16200:                                 }
16201:                                 unless ($changes{$itemid}) {
16202:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
16203:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
16204:                                             $currlti{'maproles'}{$ltirole}) {
16205:                                             $changes{$itemid} = 1;
16206:                                             last;
16207:                                         }
16208:                                     }
16209:                                 }
16210:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
16211:                                 $changes{$itemid} = 1;
16212:                             }
16213:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16214:                             unless ($changes{$itemid}) {
16215:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
16216:                                     $changes{$itemid} = 1;
16217:                                 }
16218:                             }
16219:                         }
16220:                     }
16221:                 }
16222:                 unless ($changes{$itemid}) {
16223:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
16224:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16225:                             $changes{$itemid} = 1;
16226:                         }
16227:                     }
16228:                     unless ($changes{$itemid}) {
16229:                         foreach my $field ('makeuser','lcmenu') {
16230:                             if (ref($currlti{$field}) eq 'ARRAY') {
16231:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16232:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16233:                                                                                    $confhash{$itemid}{$field});
16234:                                     if (@diffs) {
16235:                                         $changes{$itemid} = 1;
16236:                                     }
16237:                                 } elsif (@{$currlti{$field}} > 0) {
16238:                                     $changes{$itemid} = 1;
16239:                                 }
16240:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16241:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16242:                                     $changes{$itemid} = 1;
16243:                                 }
16244:                             }
16245:                         }
16246:                     }
16247:                 }
16248:             }
16249:         }
16250:         if ($is_home) {
16251:             my $keyitem = 'form.lti_key_'.$idx;
16252:             $env{$keyitem} =~ s/(`)/'/g;
16253:             if ($env{$keyitem} ne '') {
16254:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
16255:                 unless ($changes{$itemid}) {
16256:                     if ($currlti{'key'} ne $env{$keyitem}) {
16257:                         $changes{$itemid} = 1;
16258:                     }
16259:                 }
16260:             }
16261:             my $secretitem = 'form.lti_secret_'.$idx;
16262:             $env{$secretitem} =~ s/(`)/'/g;
16263:             if ($currlti{'usable'}) {
16264:                 if ($env{'form.lti_changesecret_'.$idx}) {
16265:                     if ($env{$secretitem} ne '') {
16266:                         if ($privnum && $cipher) {
16267:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16268:                             $confhash{$itemid}{'cipher'} = $privnum;
16269:                         } else {
16270:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
16271:                         }
16272:                         $changes{$itemid} = 1;
16273:                     }
16274:                 } else {
16275:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
16276:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
16277:                 }
16278:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16279:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
16280:                         $confhash{$itemid}{'usable'} = 1;
16281:                     }
16282:                 }
16283:             } elsif ($env{$secretitem} ne '') {
16284:                 if ($privnum && $cipher) {
16285:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16286:                     $confhash{$itemid}{'cipher'} = $privnum;
16287:                 } else {
16288:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
16289:                 }
16290:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16291:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
16292:                         $confhash{$itemid}{'usable'} = 1;
16293:                     }
16294:                 }
16295:                 $changes{$itemid} = 1;
16296:             }
16297:         }
16298:         unless ($changes{$itemid}) {
16299:             foreach my $key (keys(%currlti)) {
16300:                 if (ref($currlti{$key}) eq 'HASH') {
16301:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
16302:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
16303:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
16304:                                 $changes{$itemid} = 1;
16305:                                 last;
16306:                             }
16307:                         }
16308:                     } elsif (keys(%{$currlti{$key}}) > 0) {
16309:                         $changes{$itemid} = 1;
16310:                     }
16311:                 }
16312:                 last if ($changes{$itemid});
16313:             }
16314:         }
16315:     }
16316:     if (@allpos > 0) {
16317:         my $idx = 0;
16318:         foreach my $itemid (@allpos) {
16319:             if ($itemid ne '') {
16320:                 $confhash{$itemid}{'order'} = $idx;
16321:                 if (ref($domconfig{$action}) eq 'HASH') {
16322:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16323:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
16324:                             $changes{$itemid} = 1;
16325:                         }
16326:                     }
16327:                 }
16328:                 $idx ++;
16329:             }
16330:         }
16331:     }
16332: 
16333:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
16334:         return &mt('No changes made.');
16335:     }
16336: 
16337:     my %ltihash = (
16338:                       $action => { %confhash }
16339:                   );
16340:     my %ltienchash;
16341: 
16342:     if ($is_home) {
16343:         %ltienchash = (
16344:                          $action => { %ltienc }
16345:                       );
16346:     }
16347:     if (keys(%secchanges)) {
16348:         $ltihash{'ltisec'} = \%newltisec;
16349:         if ($secchanges{'linkprot'}) {
16350:             if ($is_home) {
16351:                 $ltienchash{'linkprot'} = \%newltienc;
16352:             }
16353:         }
16354:     }
16355:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
16356:     if ($putresult eq 'ok') {
16357:         if (keys(%ltienchash)) {
16358:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
16359:         }
16360:         $resulttext = &mt('Changes made:').'<ul>';
16361:         if (keys(%secchanges) > 0) {
16362:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
16363:             if (exists($secchanges{'linkprot'})) {
16364:                 $resulttext .= $linkprotoutput;
16365:             }
16366:         }
16367:         if (keys(%changes) > 0) {
16368:             my $cachetime = 24*60*60;
16369:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
16370:             if (ref($lastactref) eq 'HASH') {
16371:                 $lastactref->{'lti'} = 1;
16372:             }
16373:             my %bynum;
16374:             foreach my $itemid (sort(keys(%changes))) {
16375:                 if (ref($confhash{$itemid}) eq 'HASH') {
16376:                     my $position = $confhash{$itemid}{'order'};
16377:                     $bynum{$position} = $itemid;
16378:                 }
16379:             }
16380:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
16381:                 my $itemid = $bynum{$pos};
16382:                 if (ref($confhash{$itemid}) eq 'HASH') {
16383:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
16384:                     my $position = $pos + 1;
16385:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
16386:                     foreach my $item ('version','lifetime') {
16387:                         if ($confhash{$itemid}{$item} ne '') {
16388:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
16389:                         }
16390:                     }
16391:                     if ($ltienc{$itemid}{'key'} ne '') {
16392:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
16393:                     }
16394:                     if ($ltienc{$itemid}{'secret'} ne '') {
16395:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
16396:                     }
16397:                     if ($confhash{$itemid}{'requser'}) {
16398:                         if ($confhash{$itemid}{'callback'}) {
16399:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
16400:                         } else {
16401:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
16402:                         }
16403:                         if ($confhash{$itemid}{'mapuser'}) {
16404:                             my $shownmapuser;
16405:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
16406:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
16407:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
16408:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
16409:                             } else {
16410:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
16411:                             }
16412:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
16413:                         }
16414:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
16415:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
16416:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
16417:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
16418:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
16419:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
16420:                                 } else {
16421:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
16422:                                                        $confhash{$itemid}{'lcauth'});
16423:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
16424:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
16425:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
16426:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
16427:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
16428:                                         }
16429:                                     } else {
16430:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
16431:                                     }
16432:                                 }
16433:                                 $resulttext .= '</li>';
16434:                             } else {
16435:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
16436:                             }
16437:                         }
16438:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
16439:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
16440:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
16441:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
16442:                             } else {
16443:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
16444:                             }
16445:                         }
16446:                         foreach my $item ('topmenu','inlinemenu') {
16447:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16448:                             if ($confhash{$itemid}{$item}) {
16449:                                 $resulttext .= &mt('Yes');
16450:                             } else {
16451:                                 $resulttext .= &mt('No');
16452:                             }
16453:                             $resulttext .= '</li>';
16454:                         }
16455:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16456:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16457:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
16458:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
16459:                             } else {
16460:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
16461:                             }
16462:                         }
16463:                         if ($confhash{$itemid}{'crsinc'}) {
16464:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16465:                                 my $rolemaps;
16466:                                 foreach my $role (@ltiroles) {
16467:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
16468:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
16469:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
16470:                                                                                 'Course').',';
16471:                                     }
16472:                                 }
16473:                                 if ($rolemaps) {
16474:                                     $rolemaps =~ s/,$//;
16475:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
16476:                                 }
16477:                             }
16478:                             if ($confhash{$itemid}{'mapcrs'}) {
16479:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
16480:                             }
16481:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
16482:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
16483:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
16484:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
16485:                                                    '</li>';
16486:                                 } else {
16487:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
16488:                                 }
16489:                             }
16490:                             if ($confhash{$itemid}{'storecrs'}) {
16491:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
16492:                             }
16493:                             if ($confhash{$itemid}{'makecrs'}) {
16494:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
16495:                             } else {
16496:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
16497:                             }
16498:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
16499:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
16500:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
16501:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
16502:                                                    '</li>';
16503:                                 } else {
16504:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
16505:                                 }
16506:                             }
16507:                             if ($confhash{$itemid}{'section'}) {
16508:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
16509:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
16510:                                                          ' (course_section_sourcedid)'.'</li>';  
16511:                                 } else {
16512:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
16513:                                                           $confhash{$itemid}{'section'}.'</li>';
16514:                                 }
16515:                             } else {
16516:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
16517:                             }
16518:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
16519:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16520:                                 if ($confhash{$itemid}{$item}) {
16521:                                     $resulttext .= &mt('Yes');
16522:                                     if ($item eq 'passback') {
16523:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
16524:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
16525:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
16526:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
16527:                                         }
16528:                                     }
16529:                                 } else {
16530:                                     $resulttext .= &mt('No');
16531:                                 }
16532:                                 $resulttext .= '</li>';
16533:                             }
16534:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16535:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16536:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
16537:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
16538:                                 } else {
16539:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
16540:                                 }
16541:                             }
16542:                         }
16543:                     }
16544:                     $resulttext .= '</ul></li>';
16545:                 }
16546:             }
16547:             if (keys(%deletions)) {
16548:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
16549:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
16550:                 }
16551:             }
16552:         }
16553:         $resulttext .= '</ul>';
16554:         if (ref($lastactref) eq 'HASH') {
16555:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
16556:                 &Apache::lonnet::get_domain_defaults($dom,1);
16557:                 $lastactref->{'domdefaults'} = 1;
16558:             }
16559:         }
16560:     } else {
16561:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
16562:     }
16563:     if ($errors) {
16564:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
16565:                        $errors.'</ul>';
16566:     }
16567:     return $resulttext;
16568: }
16569: 
16570: sub get_priv_creds {
16571:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
16572:     my ($needenc,$cipher,$privnum);
16573:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
16574:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
16575:         $needenc = $encrypt->{'consumers'} 
16576:     } else {
16577:         $needenc = $domdefs{'ltienc_consumers'};
16578:     }
16579:     if ($needenc) {
16580:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
16581:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
16582:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
16583:             my $privkey = $privhash{'key'};
16584:             $privnum = $privhash{'version'};
16585:             if (($privnum) && ($privkey ne '')) {
16586:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
16587:                                           'cipher'  => 'DES'});
16588:             }
16589:         }
16590:     }
16591:     return ($cipher,$privnum);
16592: }
16593: 
16594: sub get_lti_id {
16595:     my ($domain,$consumer,$dbname) = @_;
16596:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
16597:         return ('','invalid db');
16598:     }
16599:     # get lock on db
16600:     my $lockhash = {
16601:                       lock => $env{'user.name'}.
16602:                               ':'.$env{'user.domain'},
16603:                    };
16604:     my $tries = 0;
16605:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16606:     my ($id,$error);
16607: 
16608:     while (($gotlock ne 'ok') && ($tries<10)) {
16609:         $tries ++;
16610:         sleep (0.1);
16611:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16612:     }
16613:     if ($gotlock eq 'ok') {
16614:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
16615:         if ($currids{'lock'}) {
16616:             delete($currids{'lock'});
16617:             if (keys(%currids)) {
16618:                 my @curr = sort { $a <=> $b } keys(%currids);
16619:                 if ($curr[-1] =~ /^\d+$/) {
16620:                     $id = 1 + $curr[-1];
16621:                 }
16622:             } else {
16623:                 $id = 1;
16624:             }
16625:             if ($id) {
16626:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
16627:                     $error = 'nostore';
16628:                 }
16629:             } else {
16630:                 $error = 'nonumber';
16631:             }
16632:         }
16633:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
16634:     } else {
16635:         $error = 'nolock';
16636:     }
16637:     return ($id,$error);
16638: }
16639: 
16640: sub modify_autoenroll {
16641:     my ($dom,$lastactref,%domconfig) = @_;
16642:     my ($resulttext,%changes);
16643:     my %currautoenroll;
16644:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
16645:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
16646:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
16647:         }
16648:     }
16649:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
16650:     my %title = ( run => 'Auto-enrollment active',
16651:                   sender => 'Sender for notification messages',
16652:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
16653:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
16654:     my @offon = ('off','on');
16655:     my $sender_uname = $env{'form.sender_uname'};
16656:     my $sender_domain = $env{'form.sender_domain'};
16657:     if ($sender_domain eq '') {
16658:         $sender_uname = '';
16659:     } elsif ($sender_uname eq '') {
16660:         $sender_domain = '';
16661:     }
16662:     my $coowners = $env{'form.autoassign_coowners'};
16663:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
16664:     $autofailsafe =~ s{^\s+|\s+$}{}g;
16665:     if ($autofailsafe =~ /\D/) {
16666:         undef($autofailsafe);
16667:     }
16668:     my $failsafe = $env{'form.autoenroll_failsafe'};
16669:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
16670:         $failsafe = 'off';
16671:         undef($autofailsafe);
16672:     }
16673:     my %autoenrollhash =  (
16674:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
16675:                                        'sender_uname' => $sender_uname,
16676:                                        'sender_domain' => $sender_domain,
16677:                                        'co-owners' => $coowners,
16678:                                        'autofailsafe' => $autofailsafe,
16679:                                        'failsafe' => $failsafe,
16680:                                 }
16681:                      );
16682:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
16683:                                              $dom);
16684:     if ($putresult eq 'ok') {
16685:         if (exists($currautoenroll{'run'})) {
16686:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
16687:                  $changes{'run'} = 1;
16688:              }
16689:         } elsif ($autorun) {
16690:             if ($env{'form.autoenroll_run'} ne '1') {
16691:                  $changes{'run'} = 1;
16692:             }
16693:         }
16694:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
16695:             $changes{'sender'} = 1;
16696:         }
16697:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
16698:             $changes{'sender'} = 1;
16699:         }
16700:         if ($currautoenroll{'co-owners'} ne '') {
16701:             if ($currautoenroll{'co-owners'} ne $coowners) {
16702:                 $changes{'coowners'} = 1;
16703:             }
16704:         } elsif ($coowners) {
16705:             $changes{'coowners'} = 1;
16706:         }
16707:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
16708:             $changes{'autofailsafe'} = 1;
16709:         }
16710:         if ($currautoenroll{'failsafe'} ne $failsafe) {
16711:             $changes{'failsafe'} = 1;
16712:         }
16713:         if (keys(%changes) > 0) {
16714:             $resulttext = &mt('Changes made:').'<ul>';
16715:             if ($changes{'run'}) {
16716:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
16717:             }
16718:             if ($changes{'sender'}) {
16719:                 if ($sender_uname eq '' || $sender_domain eq '') {
16720:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
16721:                 } else {
16722:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
16723:                 }
16724:             }
16725:             if ($changes{'coowners'}) {
16726:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
16727:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16728:                 if (ref($lastactref) eq 'HASH') {
16729:                     $lastactref->{'domainconfig'} = 1;
16730:                 }
16731:             }
16732:             if ($changes{'autofailsafe'}) {
16733:                 if ($autofailsafe ne '') {
16734:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
16735:                 } else {
16736:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16737:                 }
16738:             }
16739:             if ($changes{'failsafe'}) {
16740:                 if ($failsafe eq 'off') {
16741:                     unless ($changes{'autofailsafe'}) {
16742:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16743:                     }
16744:                 } elsif ($failsafe eq 'zero') {
16745:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
16746:                 } else {
16747:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
16748:                 }
16749:             }
16750:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
16751:                 &Apache::lonnet::get_domain_defaults($dom,1);
16752:                 if (ref($lastactref) eq 'HASH') {
16753:                     $lastactref->{'domdefaults'} = 1;
16754:                 }
16755:             }
16756:             $resulttext .= '</ul>';
16757:         } else {
16758:             $resulttext = &mt('No changes made to auto-enrollment settings');
16759:         }
16760:     } else {
16761:         $resulttext = '<span class="LC_error">'.
16762: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16763:     }
16764:     return $resulttext;
16765: }
16766: 
16767: sub modify_autoupdate {
16768:     my ($dom,%domconfig) = @_;
16769:     my ($resulttext,%currautoupdate,%fields,%changes);
16770:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16771:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16772:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16773:         }
16774:     }
16775:     my @offon = ('off','on');
16776:     my %title = &Apache::lonlocal::texthash (
16777:                     run        => 'Auto-update:',
16778:                     classlists => 'Updates to user information in classlists?',
16779:                     unexpired  => 'Skip updates for users without active or future roles?',
16780:                     lastactive => 'Skip updates for inactive users?',
16781:                 );
16782:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16783:     my %fieldtitles = &Apache::lonlocal::texthash (
16784:                         id => 'Student/Employee ID',
16785:                         permanentemail => 'E-mail address',
16786:                         lastname => 'Last Name',
16787:                         firstname => 'First Name',
16788:                         middlename => 'Middle Name',
16789:                         generation => 'Generation',
16790:                       );
16791:     $othertitle = &mt('All users');
16792:     if (keys(%{$usertypes}) >  0) {
16793:         $othertitle = &mt('Other users');
16794:     }
16795:     foreach my $key (keys(%env)) {
16796:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16797:             my ($usertype,$item) = ($1,$2);
16798:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16799:                 if ($usertype eq 'default') {   
16800:                     push(@{$fields{$1}},$2);
16801:                 } elsif (ref($types) eq 'ARRAY') {
16802:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16803:                         push(@{$fields{$1}},$2);
16804:                     }
16805:                 }
16806:             }
16807:         }
16808:     }
16809:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16810:     @lockablenames = sort(@lockablenames);
16811:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16812:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16813:         if (@changed) {
16814:             $changes{'lockablenames'} = 1;
16815:         }
16816:     } else {
16817:         if (@lockablenames) {
16818:             $changes{'lockablenames'} = 1;
16819:         }
16820:     }
16821:     my %updatehash = (
16822:                       autoupdate => { run => $env{'form.autoupdate_run'},
16823:                                       classlists => $env{'form.classlists'},
16824:                                       unexpired  => $env{'form.unexpired'},
16825:                                       fields => {%fields},
16826:                                       lockablenames => \@lockablenames,
16827:                                     }
16828:                      );
16829:     my $lastactivedays;
16830:     if ($env{'form.lastactive'}) {
16831:         $lastactivedays = $env{'form.lastactivedays'};
16832:         $lastactivedays =~ s/^\s+|\s+$//g;
16833:         unless ($lastactivedays =~ /^\d+$/) {
16834:             undef($lastactivedays);
16835:             $env{'form.lastactive'} = 0;
16836:         }
16837:     }
16838:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16839:     foreach my $key (keys(%currautoupdate)) {
16840:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16841:             if (exists($updatehash{autoupdate}{$key})) {
16842:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16843:                     $changes{$key} = 1;
16844:                 }
16845:             }
16846:         } elsif ($key eq 'fields') {
16847:             if (ref($currautoupdate{$key}) eq 'HASH') {
16848:                 foreach my $item (@{$types},'default') {
16849:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16850:                         my $change = 0;
16851:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16852:                             if (!exists($fields{$item})) {
16853:                                 $change = 1;
16854:                                 last;
16855:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16856:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16857:                                     $change = 1;
16858:                                     last;
16859:                                 }
16860:                             }
16861:                         }
16862:                         if ($change) {
16863:                             push(@{$changes{$key}},$item);
16864:                         }
16865:                     } 
16866:                 }
16867:             }
16868:         } elsif ($key eq 'lockablenames') {
16869:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16870:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16871:                 if (@changed) {
16872:                     $changes{'lockablenames'} = 1;
16873:                 }
16874:             } else {
16875:                 if (@lockablenames) {
16876:                     $changes{'lockablenames'} = 1;
16877:                 }
16878:             }
16879:         }
16880:     }
16881:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16882:         if (@lockablenames) {
16883:             $changes{'lockablenames'} = 1;
16884:         }
16885:     }
16886:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16887:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16888:             $changes{'unexpired'} = 1;
16889:         }
16890:     }
16891:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16892:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16893:             $changes{'lastactive'} = 1;
16894:         }
16895:     }
16896:     foreach my $item (@{$types},'default') {
16897:         if (defined($fields{$item})) {
16898:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16899:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16900:                     my $change = 0;
16901:                     if (ref($fields{$item}) eq 'ARRAY') {
16902:                         foreach my $type (@{$fields{$item}}) {
16903:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16904:                                 $change = 1;
16905:                                 last;
16906:                             }
16907:                         }
16908:                     }
16909:                     if ($change) {
16910:                         push(@{$changes{'fields'}},$item);
16911:                     }
16912:                 } else {
16913:                     push(@{$changes{'fields'}},$item);
16914:                 }
16915:             } else {
16916:                 push(@{$changes{'fields'}},$item);
16917:             }
16918:         }
16919:     }
16920:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16921:                                              $dom);
16922:     if ($putresult eq 'ok') {
16923:         if (keys(%changes) > 0) {
16924:             $resulttext = &mt('Changes made:').'<ul>';
16925:             foreach my $key (sort(keys(%changes))) {
16926:                 if ($key eq 'lockablenames') {
16927:                     $resulttext .= '<li>';
16928:                     if (@lockablenames) {
16929:                         $usertypes->{'default'} = $othertitle;
16930:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16931:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16932:                     } else {
16933:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16934:                     }
16935:                     $resulttext .= '</li>';
16936:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16937:                     foreach my $item (@{$changes{$key}}) {
16938:                         my @newvalues;
16939:                         foreach my $type (@{$fields{$item}}) {
16940:                             push(@newvalues,$fieldtitles{$type});
16941:                         }
16942:                         my $newvaluestr;
16943:                         if (@newvalues > 0) {
16944:                             $newvaluestr = join(', ',@newvalues);
16945:                         } else {
16946:                             $newvaluestr = &mt('none');
16947:                         }
16948:                         if ($item eq 'default') {
16949:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16950:                         } else {
16951:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16952:                         }
16953:                     }
16954:                 } else {
16955:                     my $newvalue;
16956:                     if ($key eq 'run') {
16957:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16958:                     } elsif ($key eq 'lastactive') {
16959:                         $newvalue = $offon[$env{'form.lastactive'}];
16960:                         unless ($lastactivedays eq '') {
16961:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16962:                         }
16963:                     } else {
16964:                         $newvalue = $offon[$env{'form.'.$key}];
16965:                     }
16966:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16967:                 }
16968:             }
16969:             $resulttext .= '</ul>';
16970:         } else {
16971:             $resulttext = &mt('No changes made to autoupdates');
16972:         }
16973:     } else {
16974:         $resulttext = '<span class="LC_error">'.
16975: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16976:     }
16977:     return $resulttext;
16978: }
16979: 
16980: sub modify_autocreate {
16981:     my ($dom,%domconfig) = @_;
16982:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16983:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16984:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16985:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16986:         }
16987:     }
16988:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16989:                  req => 'Auto-creation of validated requests for official courses',
16990:                  xmldc => 'Identity of course creator of courses from XML files',
16991:                );
16992:     my @types = ('xml','req');
16993:     foreach my $item (@types) {
16994:         $newvals{$item} = $env{'form.autocreate_'.$item};
16995:         $newvals{$item} =~ s/\D//g;
16996:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16997:     }
16998:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16999:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17000:     unless (exists($domcoords{$newvals{'xmldc'}})) {
17001:         $newvals{'xmldc'} = '';
17002:     } 
17003:     %autocreatehash =  (
17004:                         autocreate => { xml => $newvals{'xml'},
17005:                                         req => $newvals{'req'},
17006:                                       }
17007:                        );
17008:     if ($newvals{'xmldc'} ne '') {
17009:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
17010:     }
17011:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
17012:                                              $dom);
17013:     if ($putresult eq 'ok') {
17014:         my @items = @types;
17015:         if ($newvals{'xml'}) {
17016:             push(@items,'xmldc');
17017:         }
17018:         foreach my $item (@items) {
17019:             if (exists($currautocreate{$item})) {
17020:                 if ($currautocreate{$item} ne $newvals{$item}) {
17021:                     $changes{$item} = 1;
17022:                 }
17023:             } elsif ($newvals{$item}) {
17024:                 $changes{$item} = 1;
17025:             }
17026:         }
17027:         if (keys(%changes) > 0) {
17028:             my @offon = ('off','on'); 
17029:             $resulttext = &mt('Changes made:').'<ul>';
17030:             foreach my $item (@types) {
17031:                 if ($changes{$item}) {
17032:                     my $newtxt = $offon[$newvals{$item}];
17033:                     $resulttext .= '<li>'.
17034:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
17035:                                        '<b>','</b>').
17036:                                    '</li>';
17037:                 }
17038:             }
17039:             if ($changes{'xmldc'}) {
17040:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
17041:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
17042:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
17043:             }
17044:             $resulttext .= '</ul>';
17045:         } else {
17046:             $resulttext = &mt('No changes made to auto-creation settings');
17047:         }
17048:     } else {
17049:         $resulttext = '<span class="LC_error">'.
17050:             &mt('An error occurred: [_1]',$putresult).'</span>';
17051:     }
17052:     return $resulttext;
17053: }
17054: 
17055: sub modify_directorysrch {
17056:     my ($dom,$lastactref,%domconfig) = @_;
17057:     my ($resulttext,%changes);
17058:     my %currdirsrch;
17059:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
17060:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
17061:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
17062:         }
17063:     }
17064:     my %title = ( available => 'Institutional directory search available',
17065:                   localonly => 'Other domains can search institution',
17066:                   lcavailable => 'LON-CAPA directory search available',
17067:                   lclocalonly => 'Other domains can search LON-CAPA domain',
17068:                   searchby => 'Search types',
17069:                   searchtypes => 'Search latitude');
17070:     my @offon = ('off','on');
17071:     my @otherdoms = ('Yes','No');
17072: 
17073:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
17074:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
17075:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
17076: 
17077:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17078:     if (keys(%{$usertypes}) == 0) {
17079:         @cansearch = ('default');
17080:     } else {
17081:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
17082:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
17083:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
17084:                     push(@{$changes{'cansearch'}},$type);
17085:                 }
17086:             }
17087:             foreach my $type (@cansearch) {
17088:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
17089:                     push(@{$changes{'cansearch'}},$type);
17090:                 }
17091:             }
17092:         } else {
17093:             push(@{$changes{'cansearch'}},@cansearch);
17094:         }
17095:     }
17096: 
17097:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
17098:         foreach my $by (@{$currdirsrch{'searchby'}}) {
17099:             if (!grep(/^\Q$by\E$/,@searchby)) {
17100:                 push(@{$changes{'searchby'}},$by);
17101:             }
17102:         }
17103:         foreach my $by (@searchby) {
17104:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
17105:                 push(@{$changes{'searchby'}},$by);
17106:             }
17107:         }
17108:     } else {
17109:         push(@{$changes{'searchby'}},@searchby);
17110:     }
17111: 
17112:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
17113:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
17114:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
17115:                 push(@{$changes{'searchtypes'}},$type);
17116:             }
17117:         }
17118:         foreach my $type (@searchtypes) {
17119:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
17120:                 push(@{$changes{'searchtypes'}},$type);
17121:             }
17122:         }
17123:     } else {
17124:         if (exists($currdirsrch{'searchtypes'})) {
17125:             foreach my $type (@searchtypes) {  
17126:                 if ($type ne $currdirsrch{'searchtypes'}) { 
17127:                     push(@{$changes{'searchtypes'}},$type);
17128:                 }
17129:             }
17130:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
17131:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
17132:             }   
17133:         } else {
17134:             push(@{$changes{'searchtypes'}},@searchtypes); 
17135:         }
17136:     }
17137: 
17138:     my %dirsrch_hash =  (
17139:             directorysrch => { available => $env{'form.dirsrch_available'},
17140:                                cansearch => \@cansearch,
17141:                                localonly => $env{'form.dirsrch_instlocalonly'},
17142:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
17143:                                lcavailable => $env{'form.dirsrch_domavailable'},
17144:                                searchby => \@searchby,
17145:                                searchtypes => \@searchtypes,
17146:                              }
17147:             );
17148:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
17149:                                              $dom);
17150:     if ($putresult eq 'ok') {
17151:         if (exists($currdirsrch{'available'})) {
17152:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
17153:                  $changes{'available'} = 1;
17154:              }
17155:         } else {
17156:             if ($env{'form.dirsrch_available'} eq '1') {
17157:                 $changes{'available'} = 1;
17158:             }
17159:         }
17160:         if (exists($currdirsrch{'lcavailable'})) {
17161:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
17162:                 $changes{'lcavailable'} = 1;
17163:             }
17164:         } else {
17165:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
17166:                 $changes{'lcavailable'} = 1;
17167:             }
17168:         }
17169:         if (exists($currdirsrch{'localonly'})) {
17170:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
17171:                 $changes{'localonly'} = 1;
17172:             }
17173:         } else {
17174:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
17175:                 $changes{'localonly'} = 1;
17176:             }
17177:         }
17178:         if (exists($currdirsrch{'lclocalonly'})) {
17179:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
17180:                 $changes{'lclocalonly'} = 1;
17181:             }
17182:         } else {
17183:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
17184:                 $changes{'lclocalonly'} = 1;
17185:             }
17186:         }
17187:         if (keys(%changes) > 0) {
17188:             $resulttext = &mt('Changes made:').'<ul>';
17189:             if ($changes{'available'}) {
17190:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
17191:             }
17192:             if ($changes{'lcavailable'}) {
17193:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
17194:             }
17195:             if ($changes{'localonly'}) {
17196:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
17197:             }
17198:             if ($changes{'lclocalonly'}) {
17199:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
17200:             }
17201:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
17202:                 my $chgtext;
17203:                 if (ref($usertypes) eq 'HASH') {
17204:                     if (keys(%{$usertypes}) > 0) {
17205:                         foreach my $type (@{$types}) {
17206:                             if (grep(/^\Q$type\E$/,@cansearch)) {
17207:                                 $chgtext .= $usertypes->{$type}.'; ';
17208:                             }
17209:                         }
17210:                         if (grep(/^default$/,@cansearch)) {
17211:                             $chgtext .= $othertitle;
17212:                         } else {
17213:                             $chgtext =~ s/\; $//;
17214:                         }
17215:                         $resulttext .=
17216:                             '<li>'.
17217:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
17218:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
17219:                             '</li>';
17220:                     }
17221:                 }
17222:             }
17223:             if (ref($changes{'searchby'}) eq 'ARRAY') {
17224:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
17225:                 my $chgtext;
17226:                 foreach my $type (@{$titleorder}) {
17227:                     if (grep(/^\Q$type\E$/,@searchby)) {
17228:                         if (defined($searchtitles->{$type})) {
17229:                             $chgtext .= $searchtitles->{$type}.'; ';
17230:                         }
17231:                     }
17232:                 }
17233:                 $chgtext =~ s/\; $//;
17234:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
17235:             }
17236:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
17237:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
17238:                 my $chgtext;
17239:                 foreach my $type (@{$srchtypeorder}) {
17240:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
17241:                         if (defined($srchtypes_desc->{$type})) {
17242:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
17243:                         }
17244:                     }
17245:                 }
17246:                 $chgtext =~ s/\; $//;
17247:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
17248:             }
17249:             $resulttext .= '</ul>';
17250:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
17251:             if (ref($lastactref) eq 'HASH') {
17252:                 $lastactref->{'directorysrch'} = 1;
17253:             }
17254:         } else {
17255:             $resulttext = &mt('No changes made to directory search settings');
17256:         }
17257:     } else {
17258:         $resulttext = '<span class="LC_error">'.
17259:                       &mt('An error occurred: [_1]',$putresult).'</span>';
17260:     }
17261:     return $resulttext;
17262: }
17263: 
17264: sub modify_contacts {
17265:     my ($dom,$lastactref,%domconfig) = @_;
17266:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
17267:     if (ref($domconfig{'contacts'}) eq 'HASH') {
17268:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
17269:             $currsetting{$key} = $domconfig{'contacts'}{$key};
17270:         }
17271:     }
17272:     my (%others,%to,%bcc,%includestr,%includeloc);
17273:     my @contacts = ('supportemail','adminemail');
17274:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
17275:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
17276:     my @toggles = ('reporterrors','reportupdates','reportstatus');
17277:     my @lonstatus = ('threshold','sysmail','weights','excluded');
17278:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
17279:     foreach my $type (@mailings) {
17280:         @{$newsetting{$type}} = 
17281:             &Apache::loncommon::get_env_multiple('form.'.$type);
17282:         foreach my $item (@contacts) {
17283:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
17284:                 $contacts_hash{contacts}{$type}{$item} = 1;
17285:             } else {
17286:                 $contacts_hash{contacts}{$type}{$item} = 0;
17287:             }
17288:         }
17289:         $others{$type} = $env{'form.'.$type.'_others'};
17290:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
17291:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17292:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
17293:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
17294:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
17295:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
17296:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
17297:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17298:             }
17299:         }
17300:     }
17301:     foreach my $item (@contacts) {
17302:         $to{$item} = $env{'form.'.$item};
17303:         $contacts_hash{'contacts'}{$item} = $to{$item};
17304:     }
17305:     foreach my $item (@toggles) {
17306:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
17307:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
17308:         }
17309:     }
17310:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
17311:     foreach my $item (@lonstatus) {
17312:         if ($item eq 'excluded') {
17313:             my (%serverhomes,@excluded);
17314:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
17315:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
17316:             if (@possexcluded) {
17317:                 foreach my $id (sort(@possexcluded)) {
17318:                     if ($serverhomes{$id}) {
17319:                         push(@excluded,$id);
17320:                     }
17321:                 }
17322:             }
17323:             if (@excluded) {
17324:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
17325:             }
17326:         } elsif ($item eq 'weights') {
17327:             foreach my $type ('E','W','N','U') {
17328:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
17329:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
17330:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
17331:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
17332:                             $env{'form.error'.$item.'_'.$type};
17333:                     }
17334:                 }
17335:             }
17336:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
17337:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
17338:             if ($env{'form.error'.$item} =~ /^\d+$/) {
17339:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
17340:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
17341:                 }
17342:             }
17343:         }
17344:     }
17345:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
17346:         foreach my $field (@{$fields}) {
17347:             if (ref($possoptions->{$field}) eq 'ARRAY') {
17348:                 my $value = $env{'form.helpform_'.$field};
17349:                 $value =~ s/^\s+|\s+$//g;
17350:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
17351:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
17352:                     if ($field eq 'screenshot') {
17353:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
17354:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
17355:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
17356:                         }
17357:                     }
17358:                 }
17359:             }
17360:         }
17361:     }
17362:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17363:     my (@statuses,%usertypeshash,@overrides);
17364:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
17365:         @statuses = @{$types};
17366:         if (ref($usertypes) eq 'HASH') {
17367:             %usertypeshash = %{$usertypes};
17368:         }
17369:     }
17370:     if (@statuses) {
17371:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
17372:         foreach my $type (@possoverrides) {
17373:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
17374:                 push(@overrides,$type);
17375:             }
17376:         }
17377:         if (@overrides) {
17378:             foreach my $type (@overrides) {
17379:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
17380:                 foreach my $item (@contacts) {
17381:                     if (grep(/^\Q$item\E$/,@standard)) {
17382:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
17383:                         $newsetting{'override_'.$type}{$item} = 1;
17384:                     } else {
17385:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
17386:                         $newsetting{'override_'.$type}{$item} = 0;
17387:                     }
17388:                 }
17389:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
17390:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17391:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
17392:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17393:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
17394:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
17395:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
17396:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17397:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
17398:                 }
17399:             }
17400:         }
17401:     }
17402:     if (keys(%currsetting) > 0) {
17403:         foreach my $item (@contacts) {
17404:             if ($to{$item} ne $currsetting{$item}) {
17405:                 $changes{$item} = 1;
17406:             }
17407:         }
17408:         foreach my $type (@mailings) {
17409:             foreach my $item (@contacts) {
17410:                 if (ref($currsetting{$type}) eq 'HASH') {
17411:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
17412:                         push(@{$changes{$type}},$item);
17413:                     }
17414:                 } else {
17415:                     push(@{$changes{$type}},@{$newsetting{$type}});
17416:                 }
17417:             }
17418:             if ($others{$type} ne $currsetting{$type}{'others'}) {
17419:                 push(@{$changes{$type}},'others');
17420:             }
17421:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17422:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
17423:                     push(@{$changes{$type}},'bcc'); 
17424:                 }
17425:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
17426:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
17427:                     push(@{$changes{$type}},'include');
17428:                 }
17429:             }
17430:         }
17431:         if (ref($fields) eq 'ARRAY') {
17432:             if (ref($currsetting{'helpform'}) eq 'HASH') {
17433:                 foreach my $field (@{$fields}) {
17434:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
17435:                         push(@{$changes{'helpform'}},$field);
17436:                     }
17437:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17438:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
17439:                             push(@{$changes{'helpform'}},'maxsize');
17440:                         }
17441:                     }
17442:                 }
17443:             } else {
17444:                 foreach my $field (@{$fields}) {
17445:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17446:                         push(@{$changes{'helpform'}},$field);
17447:                     }
17448:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17449:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17450:                             push(@{$changes{'helpform'}},'maxsize');
17451:                         }
17452:                     }
17453:                 }
17454:             }
17455:         }
17456:         if (@statuses) {
17457:             if (ref($currsetting{'overrides'}) eq 'HASH') {
17458:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
17459:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
17460:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
17461:                             foreach my $item (@contacts,'bcc','others','include') {
17462:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
17463:                                     push(@{$changes{'overrides'}},$key);
17464:                                     last;
17465:                                 }
17466:                             }
17467:                         } else {
17468:                             push(@{$changes{'overrides'}},$key);
17469:                         }
17470:                     }
17471:                 }
17472:                 foreach my $key (@overrides) {
17473:                     unless (exists($currsetting{'overrides'}{$key})) {
17474:                         push(@{$changes{'overrides'}},$key);
17475:                     }
17476:                 }
17477:             } else {
17478:                 foreach my $key (@overrides) {
17479:                     push(@{$changes{'overrides'}},$key);
17480:                 }
17481:             }
17482:         }
17483:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
17484:             foreach my $key ('excluded','weights','threshold','sysmail') {
17485:                 if ($key eq 'excluded') {
17486:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17487:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
17488:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17489:                             (@{$currsetting{'lonstatus'}{$key}})) {
17490:                             my @diffs =
17491:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
17492:                                                                    $currsetting{'lonstatus'}{$key});
17493:                             if (@diffs) {
17494:                                 push(@{$changes{'lonstatus'}},$key);
17495:                             }
17496:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
17497:                             push(@{$changes{'lonstatus'}},$key);
17498:                         }
17499:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17500:                              (@{$currsetting{'lonstatus'}{$key}})) {
17501:                         push(@{$changes{'lonstatus'}},$key);
17502:                     }
17503:                 } elsif ($key eq 'weights') {
17504:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17505:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
17506:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17507:                             foreach my $type ('E','W','N','U') {
17508:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
17509:                                         $currsetting{'lonstatus'}{$key}{$type}) {
17510:                                     push(@{$changes{'lonstatus'}},$key);
17511:                                     last;
17512:                                 }
17513:                             }
17514:                         } else {
17515:                             foreach my $type ('E','W','N','U') {
17516:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
17517:                                     push(@{$changes{'lonstatus'}},$key);
17518:                                     last;
17519:                                 }
17520:                             }
17521:                         }
17522:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17523:                         foreach my $type ('E','W','N','U') {
17524:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
17525:                                 push(@{$changes{'lonstatus'}},$key);
17526:                                 last;
17527:                             }
17528:                         }
17529:                     }
17530:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
17531:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17532:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17533:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
17534:                                 push(@{$changes{'lonstatus'}},$key);
17535:                             }
17536:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
17537:                             push(@{$changes{'lonstatus'}},$key);
17538:                         }
17539:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17540:                         push(@{$changes{'lonstatus'}},$key);
17541:                     }
17542:                 }
17543:             }
17544:         } else {
17545:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17546:                 foreach my $key ('excluded','weights','threshold','sysmail') {
17547:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17548:                         push(@{$changes{'lonstatus'}},$key);
17549:                     }
17550:                 }
17551:             }
17552:         }
17553:     } else {
17554:         my %default;
17555:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
17556:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
17557:         $default{'errormail'} = 'adminemail';
17558:         $default{'packagesmail'} = 'adminemail';
17559:         $default{'helpdeskmail'} = 'supportemail';
17560:         $default{'otherdomsmail'} = 'supportemail';
17561:         $default{'lonstatusmail'} = 'adminemail';
17562:         $default{'requestsmail'} = 'adminemail';
17563:         $default{'updatesmail'} = 'adminemail';
17564:         $default{'hostipmail'} = 'adminemail';
17565:         foreach my $item (@contacts) {
17566:            if ($to{$item} ne $default{$item}) {
17567:                $changes{$item} = 1;
17568:            }
17569:         }
17570:         foreach my $type (@mailings) {
17571:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
17572:                 push(@{$changes{$type}},@{$newsetting{$type}});
17573:             }
17574:             if ($others{$type} ne '') {
17575:                 push(@{$changes{$type}},'others');
17576:             }
17577:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17578:                 if ($bcc{$type} ne '') {
17579:                     push(@{$changes{$type}},'bcc');
17580:                 }
17581:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
17582:                     push(@{$changes{$type}},'include');
17583:                 }
17584:             }
17585:         }
17586:         if (ref($fields) eq 'ARRAY') {
17587:             foreach my $field (@{$fields}) {
17588:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17589:                     push(@{$changes{'helpform'}},$field);
17590:                 }
17591:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17592:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17593:                         push(@{$changes{'helpform'}},'maxsize');
17594:                     }
17595:                 }
17596:             }
17597:         }
17598:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17599:             foreach my $key ('excluded','weights','threshold','sysmail') {
17600:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17601:                     push(@{$changes{'lonstatus'}},$key);
17602:                 }
17603:             }
17604:         }
17605:     }
17606:     foreach my $item (@toggles) {
17607:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
17608:             $changes{$item} = 1;
17609:         } elsif ((!$env{'form.'.$item}) &&
17610:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
17611:             $changes{$item} = 1;
17612:         }
17613:     }
17614:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
17615:                                              $dom);
17616:     if ($putresult eq 'ok') {
17617:         if (keys(%changes) > 0) {
17618:             &Apache::loncommon::devalidate_domconfig_cache($dom);
17619:             if (ref($lastactref) eq 'HASH') {
17620:                 $lastactref->{'domainconfig'} = 1;
17621:             }
17622:             my ($titles,$short_titles)  = &contact_titles();
17623:             $resulttext = &mt('Changes made:').'<ul>';
17624:             foreach my $item (@contacts) {
17625:                 if ($changes{$item}) {
17626:                     $resulttext .= '<li>'.$titles->{$item}.
17627:                                     &mt(' set to: ').
17628:                                     '<span class="LC_cusr_emph">'.
17629:                                     $to{$item}.'</span></li>';
17630:                 }
17631:             }
17632:             foreach my $type (@mailings) {
17633:                 if (ref($changes{$type}) eq 'ARRAY') {
17634:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17635:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
17636:                     } else {
17637:                         $resulttext .= '<li>'.$titles->{$type}.': ';
17638:                     }
17639:                     my @text;
17640:                     foreach my $item (@{$newsetting{$type}}) {
17641:                         push(@text,$short_titles->{$item});
17642:                     }
17643:                     if ($others{$type} ne '') {
17644:                         push(@text,$others{$type});
17645:                     }
17646:                     if (@text) {
17647:                         $resulttext .= '<span class="LC_cusr_emph">'.
17648:                                        join(', ',@text).'</span>';
17649:                     }
17650:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17651:                         if ($bcc{$type} ne '') {
17652:                             my $bcctext;
17653:                             if (@text) {
17654:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
17655:                             } else {
17656:                                 $bcctext = '(Bcc)';
17657:                             }
17658:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
17659:                         } elsif (!@text) {
17660:                             $resulttext .= &mt('No one');
17661:                         }
17662:                         if ($includestr{$type} ne '') {
17663:                             if ($includeloc{$type} eq 'b') {
17664:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
17665:                             } elsif ($includeloc{$type} eq 's') {
17666:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
17667:                             }
17668:                         }
17669:                     } elsif (!@text) {
17670:                         $resulttext .= &mt('No recipients');
17671:                     }
17672:                     $resulttext .= '</li>';
17673:                 }
17674:             }
17675:             if (ref($changes{'overrides'}) eq 'ARRAY') {
17676:                 my @deletions;
17677:                 foreach my $type (@{$changes{'overrides'}}) {
17678:                     if ($usertypeshash{$type}) {
17679:                         if (grep(/^\Q$type\E/,@overrides)) {
17680:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
17681:                                                       $usertypeshash{$type}).'<ul><li>';
17682:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
17683:                                 my @text;
17684:                                 foreach my $item (@contacts) {
17685:                                     if ($newsetting{'override_'.$type}{$item}) {
17686:                                         push(@text,$short_titles->{$item});
17687:                                     }
17688:                                 }
17689:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
17690:                                     push(@text,$newsetting{'override_'.$type}{'others'});
17691:                                 }
17692: 
17693:                                 if (@text) {
17694:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
17695:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
17696:                                 }
17697:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
17698:                                     my $bcctext;
17699:                                     if (@text) {
17700:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
17701:                                     } else {
17702:                                         $bcctext = '(Bcc)';
17703:                                     }
17704:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
17705:                                 } elsif (!@text) {
17706:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
17707:                                 }
17708:                                 $resulttext .= '</li>';
17709:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
17710:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
17711:                                     if ($loc eq 'b') {
17712:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
17713:                                     } elsif ($loc eq 's') {
17714:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
17715:                                     }
17716:                                 }
17717:                             }
17718:                             $resulttext .= '</li></ul></li>';
17719:                         } else {
17720:                             push(@deletions,$usertypeshash{$type});
17721:                         }
17722:                     }
17723:                 }
17724:                 if (@deletions) {
17725:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
17726:                                               join(', ',@deletions)).'</li>';
17727:                 }
17728:             }
17729:             my @offon = ('off','on');
17730:             my $corelink = &core_link_msu();
17731:             if ($changes{'reporterrors'}) {
17732:                 $resulttext .= '<li>'.
17733:                                &mt('E-mail error reports to [_1] set to "'.
17734:                                    $offon[$env{'form.reporterrors'}].'".',
17735:                                    $corelink).
17736:                                '</li>';
17737:             }
17738:             if ($changes{'reportupdates'}) {
17739:                 $resulttext .= '<li>'.
17740:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
17741:                                     $offon[$env{'form.reportupdates'}].'".',
17742:                                     $corelink).
17743:                                 '</li>';
17744:             }
17745:             if ($changes{'reportstatus'}) {
17746:                 $resulttext .= '<li>'.
17747:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
17748:                                     $offon[$env{'form.reportstatus'}].'".',
17749:                                     $corelink).
17750:                                 '</li>';
17751:             }
17752:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
17753:                 $resulttext .= '<li>'.
17754:                                &mt('Nightly status check e-mail settings').':<ul>';
17755:                 my (%defval,%use_def,%shown);
17756:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17757:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17758:                 $defval{'weights'} =
17759:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17760:                 $defval{'excluded'} = &mt('None');
17761:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17762:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17763:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17764:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17765:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17766:                             } elsif ($item eq 'weights') {
17767:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17768:                                     foreach my $type ('E','W','N','U') {
17769:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17770:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17771:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17772:                                         } else {
17773:                                             $shown{$item} .= $lonstatus_defs->{$type};
17774:                                         }
17775:                                         $shown{$item} .= ', ';
17776:                                     }
17777:                                     $shown{$item} =~ s/, $//;
17778:                                 } else {
17779:                                     $shown{$item} = $defval{$item};
17780:                                 }
17781:                             } elsif ($item eq 'excluded') {
17782:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17783:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17784:                                 } else {
17785:                                     $shown{$item} = $defval{$item};
17786:                                 }
17787:                             }
17788:                         } else {
17789:                             $shown{$item} = $defval{$item};
17790:                         }
17791:                     }
17792:                 } else {
17793:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17794:                         $shown{$item} = $defval{$item};
17795:                     }
17796:                 }
17797:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17798:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17799:                                           $shown{$item}).'</li>';
17800:                 }
17801:                 $resulttext .= '</ul></li>';
17802:             }
17803:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17804:                 my (@optional,@required,@unused,$maxsizechg);
17805:                 foreach my $field (@{$changes{'helpform'}}) {
17806:                     if ($field eq 'maxsize') {
17807:                         $maxsizechg = 1;
17808:                         next;
17809:                     }
17810:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17811:                         push(@optional,$field);
17812:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17813:                         push(@unused,$field);
17814:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17815:                         push(@required,$field);
17816:                     }
17817:                 }
17818:                 if (@optional) {
17819:                     $resulttext .= '<li>'.
17820:                                    &mt('Help form fields changed to "Optional": [_1].',
17821:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17822:                                    '</li>';
17823:                 }
17824:                 if (@required) {
17825:                     $resulttext .= '<li>'.
17826:                                    &mt('Help form fields changed to "Required": [_1].',
17827:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17828:                                    '</li>';
17829:                 }
17830:                 if (@unused) {
17831:                     $resulttext .= '<li>'.
17832:                                    &mt('Help form fields changed to "Not shown": [_1].',
17833:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17834:                                    '</li>';
17835:                 }
17836:                 if ($maxsizechg) {
17837:                     $resulttext .= '<li>'.
17838:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17839:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17840:                                    '</li>';
17841:                 }
17842:             }
17843:             $resulttext .= '</ul>';
17844:         } else {
17845:             $resulttext = &mt('No changes made to contacts and form settings');
17846:         }
17847:     } else {
17848:         $resulttext = '<span class="LC_error">'.
17849:             &mt('An error occurred: [_1].',$putresult).'</span>';
17850:     }
17851:     return $resulttext;
17852: }
17853: 
17854: sub modify_privacy {
17855:     my ($dom,$lastactref,%domconfig) = @_;
17856:     my ($resulttext,%current,%changes);
17857:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17858:         %current = %{$domconfig{'privacy'}};
17859:     }
17860:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17861:     my @items = ('domain','author','course','community');
17862:     my %names = &Apache::lonlocal::texthash (
17863:                    domain => 'Assigned domain role(s)',
17864:                    author => 'Assigned co-author role(s)',
17865:                    course => 'Assigned course role(s)',
17866:                    community => 'Assigned community role(s)',
17867:                 );
17868:     my %roles = &Apache::lonlocal::texthash (
17869:                    domain => 'Domain role',
17870:                    author => 'Co-author role',
17871:                    course => 'Course role',
17872:                    community => 'Community role',
17873:                 );
17874:     my %titles = &Apache::lonlocal::texthash (
17875:                   approval => 'Approval for role in different domain',
17876:                   othdom   => 'User information available in other domain',
17877:                   priv     => 'Information viewable by privileged user in same domain',
17878:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17879:                   instdom  => 'Other domain shares institution/provider',
17880:                   extdom   => 'Other domain has different institution/provider',
17881:                   none     => 'Not allowed',
17882:                   user     => 'User authorizes',
17883:                   domain   => 'Domain Coordinator authorizes',
17884:                   auto     => 'Unrestricted',
17885:                   notify   => 'Notify when role needs authorization',
17886:     );
17887:     my %fieldnames = &Apache::lonlocal::texthash (
17888:                         id => 'Student/Employee ID',
17889:                         permanentemail => 'E-mail address',
17890:                         lastname => 'Last Name',
17891:                         firstname => 'First Name',
17892:                         middlename => 'Middle Name',
17893:                         generation => 'Generation',
17894:     );
17895:     my ($othertitle,$usertypes,$types) =
17896:         &Apache::loncommon::sorted_inst_types($dom);
17897:     my (%by_ip,%by_location,@intdoms,@instdoms);
17898:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17899: 
17900:     my %privacyhash = (
17901:                        'approval' => {
17902:                                        instdom => {},
17903:                                        extdom  => {},
17904:                                      },
17905:                        'othdom'   => {},
17906:                        'priv'     => {},
17907:                        'unpriv'   => {},
17908:                       );
17909:     foreach my $item (@items) {
17910:         if (@instdoms > 1) {
17911:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17912:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17913:             }
17914:             if (ref($current{'approval'}) eq 'HASH') {
17915:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17916:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17917:                         $changes{'approval'} = 1;
17918:                     }
17919:                 }
17920:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17921:                 $changes{'approval'} = 1;
17922:             }
17923:         }
17924:         if (keys(%by_location) > 0) {
17925:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17926:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17927:             }
17928:             if (ref($current{'approval'}) eq 'HASH') {
17929:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17930:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17931:                         $changes{'approval'} = 1;
17932:                     }
17933:                 }
17934:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17935:                 $changes{'approval'} = 1;
17936:             }
17937:         }
17938:         foreach my $status ('priv','unpriv') {
17939:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17940:             my @newvalues;
17941:             foreach my $field (@possibles) {
17942:                 if (grep(/^\Q$field\E$/,@fields)) {
17943:                     $privacyhash{$status}{$item}{$field} = 1;
17944:                     push(@newvalues,$field);
17945:                 }
17946:             }
17947:             @newvalues = sort(@newvalues);
17948:             if (ref($current{$status}) eq 'HASH') {
17949:                 if (ref($current{$status}{$item}) eq 'HASH') {
17950:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17951:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17952:                     if (@diffs > 0) {
17953:                         $changes{$status} = 1;
17954:                     }
17955:                 }
17956:             } else {
17957:                 my @stdfields;
17958:                 foreach my $field (@fields) {
17959:                     if ($field eq 'id') {
17960:                         next if ($status eq 'unpriv');
17961:                         next if (($status eq 'priv') && ($item eq 'community'));
17962:                     }
17963:                     push(@stdfields,$field);
17964:                 }
17965:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17966:                 if (@diffs > 0) {
17967:                     $changes{$status} = 1;
17968:                 }
17969:             }
17970:         }
17971:     }
17972:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17973:         my @statuses;
17974:         if (ref($types) eq 'ARRAY') {
17975:             @statuses = @{$types};
17976:         }
17977:         foreach my $type (@statuses,'default') {
17978:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17979:             my @newvalues;
17980:             foreach my $field (sort(@possfields)) {
17981:                 if (grep(/^\Q$field\E$/,@fields)) {
17982:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17983:                     push(@newvalues,$field);
17984:                 }
17985:             }
17986:             @newvalues = sort(@newvalues);
17987:             if (ref($current{'othdom'}) eq 'HASH') {
17988:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17989:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17990:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17991:                     if (@diffs > 0) {
17992:                         $changes{'othdom'} = 1;
17993:                     }
17994:                 }
17995:             } else {
17996:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17997:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17998:                 if (@diffs > 0) {
17999:                     $changes{'othdom'} = 1;
18000:                 }
18001:             }
18002:         }
18003:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
18004:         my %notify;
18005:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
18006:             if (exists($domcoords{$possdc})) {
18007:                 $notify{$possdc} = 1;
18008:             }
18009:         }
18010:         my $notify = join(',',sort(keys(%notify)));
18011:         if ($current{'notify'} ne $notify) {
18012:             $changes{'notify'} = 1;
18013:         }
18014:         $privacyhash{'notify'} = $notify;
18015:     }
18016:     my %confighash = (
18017:                         privacy => \%privacyhash,
18018:                      );
18019:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18020:     if ($putresult eq 'ok') {
18021:         if (keys(%changes) > 0) {
18022:             $resulttext = &mt('Changes made: ').'<ul>';
18023:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
18024:                 if ($changes{$key}) {
18025:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18026:                     if ($key eq 'approval') {
18027:                         if (keys(%{$privacyhash{$key}{instdom}})) {
18028:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
18029:                             foreach my $item (@items) {
18030:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
18031:                             }
18032:                             $resulttext .= '</ul></li>';
18033:                         }
18034:                         if (keys(%{$privacyhash{$key}{extdom}})) {
18035:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
18036:                             foreach my $item (@items) {
18037:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
18038:                             }
18039:                             $resulttext .= '</ul></li>';
18040:                         }
18041:                     } elsif ($key eq 'notify') {
18042:                         if ($privacyhash{$key}) {
18043:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
18044:                                 my ($dcname,$dcdom) = split(/:/,$dc);
18045:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
18046:                             }
18047:                         } else {
18048:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
18049:                         }
18050:                     } elsif ($key eq 'othdom') {
18051:                         my @statuses;
18052:                         if (ref($types) eq 'ARRAY') {
18053:                             @statuses = @{$types};
18054:                         }
18055:                         if (ref($privacyhash{$key}) eq 'HASH') {
18056:                             foreach my $status (@statuses,'default') {
18057:                                 if ($status eq 'default') {
18058:                                     $resulttext .= '<li>'.$othertitle.': ';
18059:                                 } elsif (ref($usertypes) eq 'HASH') {
18060:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
18061:                                 } else {
18062:                                     next;
18063:                                 }
18064:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
18065:                                     if (keys(%{$privacyhash{$key}{$status}})) {
18066:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
18067:                                     } else {
18068:                                         $resulttext .= &mt('none');
18069:                                     }
18070:                                 }
18071:                                 $resulttext .= '</li>';
18072:                             }
18073:                         }
18074:                     } else {
18075:                         foreach my $item (@items) {
18076:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
18077:                                 $resulttext .= '<li>'.$names{$item}.': ';
18078:                                 if (keys(%{$privacyhash{$key}{$item}})) {
18079:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
18080:                                 } else {
18081:                                     $resulttext .= &mt('none');
18082:                                 }
18083:                                 $resulttext .= '</li>';
18084:                             }
18085:                         }
18086:                     }
18087:                     $resulttext .= '</ul></li>';
18088:                 }
18089:             }
18090:             $resulttext .= '</ul>';
18091:             if ($changes{'approval'}) {
18092:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18093:                 delete($domdefaults{'userapprovals'});
18094:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
18095:                     foreach my $domtype ('instdom','extdom') {
18096:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
18097:                             foreach my $roletype ('domain','author','course','community') {
18098:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
18099:                                     $domdefaults{'userapprovals'} = 1;
18100:                                     last;
18101:                                 }
18102:                             }
18103:                         }
18104:                         last if ($domdefaults{'userapprovals'});               
18105:                     }
18106:                 }
18107:                 my $cachetime = 24*60*60;
18108:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18109:                 if (ref($lastactref) eq 'HASH') {
18110:                     $lastactref->{'domdefaults'} = 1;
18111:                 }
18112:             }
18113:         } else {
18114:             $resulttext = &mt('No changes made to user information settings');
18115:         }
18116:     } else {
18117:         $resulttext = '<span class="LC_error">'.
18118:             &mt('An error occurred: [_1]',$putresult).'</span>';
18119:     }
18120:     return $resulttext;
18121: }
18122: 
18123: sub modify_passwords {
18124:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18125:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
18126:         $updatedefaults,$updateconf);
18127:     my $customfn = 'resetpw.html';
18128:     if (ref($domconfig{'passwords'}) eq 'HASH') {
18129:         %current = %{$domconfig{'passwords'}};
18130:     }
18131:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18132:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18133:     if (ref($types) eq 'ARRAY') {
18134:         @oktypes = @{$types};
18135:     }
18136:     push(@oktypes,'default');
18137: 
18138:     my %titles = &Apache::lonlocal::texthash (
18139:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
18140:         intauth_check  => 'Check bcrypt cost if authenticated',
18141:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
18142:         permanent      => 'Permanent e-mail address',
18143:         critical       => 'Critical notification address',
18144:         notify         => 'Notification address',
18145:         min            => 'Minimum password length',
18146:         max            => 'Maximum password length',
18147:         chars          => 'Required characters',
18148:         expire         => 'Password expiration (days)',
18149:         numsaved       => 'Number of previous passwords to save',
18150:         reset          => 'Resetting Forgotten Password',
18151:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
18152:         rules          => 'Rules for LON-CAPA Passwords',
18153:         crsownerchg    => 'Course Owner Changing Student Passwords',
18154:         username       => 'Username',
18155:         email          => 'E-mail address',
18156:     );
18157: 
18158: #
18159: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
18160: #
18161:     my (%curr_defaults,%save_defaults);
18162:     if (ref($domconfig{'defaults'}) eq 'HASH') {
18163:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
18164:             if ($key =~ /^intauth_(cost|check|switch)$/) {
18165:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
18166:             } else {
18167:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
18168:             }
18169:         }
18170:     }
18171:     my %staticdefaults = (
18172:         'resetlink'      => 2,
18173:         'resetcase'      => \@oktypes,
18174:         'resetprelink'   => 'both',
18175:         'resetemail'     => ['critical','notify','permanent'],
18176:         'intauth_cost'   => 10,
18177:         'intauth_check'  => 0,
18178:         'intauth_switch' => 0,
18179:     );
18180:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18181:     foreach my $type (@oktypes) {
18182:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
18183:     }
18184:     my $linklife = $env{'form.passwords_link'};
18185:     $linklife =~ s/^\s+|\s+$//g;
18186:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
18187:         $newvalues{'resetlink'} = $linklife;
18188:         if ($current{'resetlink'}) {
18189:             if ($current{'resetlink'} ne $linklife) {
18190:                 $changes{'reset'} = 1;
18191:             }
18192:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18193:             if ($staticdefaults{'resetlink'} ne $linklife) {
18194:                 $changes{'reset'} = 1;
18195:             }
18196:         }
18197:     } elsif ($current{'resetlink'}) {
18198:         $changes{'reset'} = 1;
18199:     }
18200:     my @casesens;
18201:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
18202:     foreach my $case (sort(@posscase)) {
18203:         if (grep(/^\Q$case\E$/,@oktypes)) {
18204:             push(@casesens,$case);
18205:         }
18206:     }
18207:     $newvalues{'resetcase'} = \@casesens;
18208:     if (ref($current{'resetcase'}) eq 'ARRAY') {
18209:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
18210:         if (@diffs > 0) {
18211:             $changes{'reset'} = 1;
18212:         }
18213:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18214:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
18215:         if (@diffs > 0) {
18216:             $changes{'reset'} = 1;
18217:         }
18218:     }
18219:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
18220:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
18221:         if (exists($current{'resetprelink'})) {
18222:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
18223:                 $changes{'reset'} = 1;
18224:             }
18225:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18226:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
18227:                 $changes{'reset'} = 1;
18228:             }
18229:         }
18230:     } elsif ($current{'resetprelink'}) {
18231:         $changes{'reset'} = 1;
18232:     }
18233:     foreach my $type (@oktypes) {
18234:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
18235:         my @postlink;
18236:         foreach my $item (sort(@possplink)) {
18237:             if ($item =~ /^(email|username)$/) {
18238:                 push(@postlink,$item);
18239:             }
18240:         }
18241:         $newvalues{'resetpostlink'}{$type} = \@postlink;
18242:         unless ($changes{'reset'}) {
18243:             if (ref($current{'resetpostlink'}) eq 'HASH') {
18244:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
18245:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
18246:                     if (@diffs > 0) {
18247:                         $changes{'reset'} = 1;
18248:                     }
18249:                 } else {
18250:                     $changes{'reset'} = 1;
18251:                 }
18252:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18253:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
18254:                 if (@diffs > 0) {
18255:                     $changes{'reset'} = 1;
18256:                 }
18257:             }
18258:         }
18259:     }
18260:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
18261:     my @resetemail;
18262:     foreach my $item (sort(@possemailsrc)) {
18263:         if ($item =~ /^(permanent|critical|notify)$/) {
18264:             push(@resetemail,$item);
18265:         }
18266:     }
18267:     $newvalues{'resetemail'} = \@resetemail;
18268:     unless ($changes{'reset'}) {
18269:         if (ref($current{'resetemail'}) eq 'ARRAY') {
18270:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
18271:             if (@diffs > 0) {
18272:                 $changes{'reset'} = 1;
18273:             }
18274:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18275:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
18276:             if (@diffs > 0) {
18277:                 $changes{'reset'} = 1;
18278:             }
18279:         }
18280:     }
18281:     if ($env{'form.passwords_stdtext'} == 0) {
18282:         $newvalues{'resetremove'} = 1;
18283:         unless ($current{'resetremove'}) {
18284:             $changes{'reset'} = 1;
18285:         }
18286:     } elsif ($current{'resetremove'}) {
18287:         $changes{'reset'} = 1;
18288:     }
18289:     if ($env{'form.passwords_customfile.filename'} ne '') {
18290:         my $servadm = $r->dir_config('lonAdmEMail');
18291:         my ($configuserok,$author_ok,$switchserver) =
18292:             &config_check($dom,$confname,$servadm);
18293:         my $error;
18294:         if ($configuserok eq 'ok') {
18295:             if ($switchserver) {
18296:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
18297:             } else {
18298:                 if ($author_ok eq 'ok') {
18299:                     my $modified = [];
18300:                     my ($result,$customurl) =
18301:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
18302:                                                                 $confname,'customtext/resetpw','','',$customfn,
18303:                                                                 $modified);
18304:                     if ($result eq 'ok') {
18305:                         $newvalues{'resetcustom'} = $customurl;
18306:                         $changes{'reset'} = 1;
18307:                         &update_modify_urls($r,$modified);
18308:                     } else {
18309:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
18310:                     }
18311:                 } else {
18312:                     $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);
18313:                 }
18314:             }
18315:         } else {
18316:             $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);
18317:         }
18318:         if ($error) {
18319:             &Apache::lonnet::logthis($error);
18320:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18321:         }
18322:     } elsif ($current{'resetcustom'}) {
18323:         if ($env{'form.passwords_custom_del'}) {
18324:             $changes{'reset'} = 1;
18325:         } else {
18326:             $newvalues{'resetcustom'} = $current{'resetcustom'};
18327:         }
18328:     }
18329:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
18330:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
18331:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
18332:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
18333:             $changes{'intauth'} = 1;
18334:         }
18335:     } else {
18336:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
18337:     }
18338:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
18339:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
18340:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
18341:             $changes{'intauth'} = 1;
18342:         }
18343:     } else {
18344:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18345:     }
18346:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
18347:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
18348:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
18349:             $changes{'intauth'} = 1;
18350:         }
18351:     } else {
18352:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18353:     }
18354:     foreach my $item ('cost','check','switch') {
18355:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
18356:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
18357:             $updatedefaults = 1;
18358:         }
18359:     }
18360:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
18361:     my %crsownerchg = (
18362:                         by => [],
18363:                         for => [],
18364:                       );
18365:     foreach my $item ('by','for') {
18366:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
18367:         foreach my $type (sort(@posstypes)) {
18368:             if (grep(/^\Q$type\E$/,@oktypes)) {
18369:                 push(@{$crsownerchg{$item}},$type);
18370:             }
18371:         }
18372:     }
18373:     $newvalues{'crsownerchg'} = \%crsownerchg;
18374:     if (ref($current{'crsownerchg'}) eq 'HASH') {
18375:         foreach my $item ('by','for') {
18376:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
18377:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
18378:                 if (@diffs > 0) {
18379:                     $changes{'crsownerchg'} = 1;
18380:                     last;
18381:                 }
18382:             }
18383:         }
18384:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
18385:         foreach my $item ('by','for') {
18386:             if (@{$crsownerchg{$item}} > 0) {
18387:                 $changes{'crsownerchg'} = 1;
18388:                 last;
18389:             }
18390:         }
18391:     }
18392: 
18393:     my %confighash = (
18394:                         defaults  => \%save_defaults,
18395:                         passwords => \%newvalues,
18396:                      );
18397:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
18398: 
18399:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18400:     if ($putresult eq 'ok') {
18401:         if (keys(%changes) > 0) {
18402:             $resulttext = &mt('Changes made: ').'<ul>';
18403:             foreach my $key ('reset','intauth','rules','crsownerchg') {
18404:                 if ($changes{$key}) {
18405:                     unless ($key eq 'intauth') {
18406:                         $updateconf = 1;
18407:                     }
18408:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18409:                     if ($key eq 'reset') {
18410:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
18411:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
18412:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
18413:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
18414:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
18415:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
18416:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
18417:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
18418:                             }
18419:                         } else {
18420:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
18421:                         }
18422:                         if ($confighash{'passwords'}{'resetlink'}) {
18423:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
18424:                         } else {
18425:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
18426:                                                   &mt('Will default to 2 hours').'</li>';
18427:                         }
18428:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
18429:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
18430:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
18431:                             } else {
18432:                                 my $casesens;
18433:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
18434:                                     if ($type eq 'default') {
18435:                                         $casesens .= $othertitle.', ';
18436:                                     } elsif ($usertypes->{$type} ne '') {
18437:                                         $casesens .= $usertypes->{$type}.', ';
18438:                                     }
18439:                                 }
18440:                                 $casesens =~ s/\Q, \E$//;
18441:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
18442:                             }
18443:                         } else {
18444:                             $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>';
18445:                         }
18446:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
18447:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
18448:                         } else {
18449:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
18450:                         }
18451:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
18452:                             my $output;
18453:                             if (ref($types) eq 'ARRAY') {
18454:                                 foreach my $type (@{$types}) {
18455:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
18456:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
18457:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
18458:                                         } else {
18459:                                             $output .= $usertypes->{$type}.' -- '.
18460:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
18461:                                         }
18462:                                     }
18463:                                 }
18464:                             }
18465:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
18466:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
18467:                                     $output .= $othertitle.' -- '.&mt('none');
18468:                                 } else {
18469:                                     $output .= $othertitle.' -- '.
18470:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
18471:                                 }
18472:                             }
18473:                             if ($output) {
18474:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
18475:                             } else {
18476:                                 $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>';
18477:                             }
18478:                         } else {
18479:                             $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>';
18480:                         }
18481:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
18482:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
18483:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
18484:                             } else {
18485:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18486:                             }
18487:                         } else {
18488:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18489:                         }
18490:                         if ($confighash{'passwords'}{'resetremove'}) {
18491:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
18492:                         } else {
18493:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
18494:                         }
18495:                         if ($confighash{'passwords'}{'resetcustom'}) {
18496:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
18497:                                                                             &mt('custom text'),600,500,undef,undef,
18498:                                                                             undef,undef,'background-color:#ffffff');
18499:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
18500:                         } else {
18501:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
18502:                         }
18503:                     } elsif ($key eq 'intauth') {
18504:                         foreach my $item ('cost','switch','check') {
18505:                             my $value = $save_defaults{$key.'_'.$item};
18506:                             if ($item eq 'switch') {
18507:                                 my %optiondesc = &Apache::lonlocal::texthash (
18508:                                                      0 => 'No',
18509:                                                      1 => 'Yes',
18510:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
18511:                                                  );
18512:                                 if ($value =~ /^(0|1|2)$/) {
18513:                                     $value = $optiondesc{$value};
18514:                                 } else {
18515:                                     $value = &mt('none -- defaults to No');
18516:                                 }
18517:                             } elsif ($item eq 'check') {
18518:                                 my %optiondesc = &Apache::lonlocal::texthash (
18519:                                                      0 => 'No',
18520:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
18521:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
18522:                                                  );
18523:                                 if ($value =~ /^(0|1|2)$/) {
18524:                                     $value = $optiondesc{$value};
18525:                                 } else {
18526:                                     $value = &mt('none -- defaults to No');
18527:                                 }
18528:                             }
18529:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
18530:                         }
18531:                     } elsif ($key eq 'rules') {
18532:                         foreach my $rule ('min','max','expire','numsaved') {
18533:                             if ($confighash{'passwords'}{$rule} eq '') {
18534:                                 if ($rule eq 'min') {
18535:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
18536:                                                    ' '.&mt('Default of [_1] will be used',
18537:                                                            $Apache::lonnet::passwdmin).'</li>';
18538:                                 } else {
18539:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
18540:                                 }
18541:                             } else {
18542:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
18543:                             }
18544:                         }
18545:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
18546:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
18547:                                 my %rulenames = &Apache::lonlocal::texthash(
18548:                                                      uc => 'At least one upper case letter',
18549:                                                      lc => 'At least one lower case letter',
18550:                                                      num => 'At least one number',
18551:                                                      spec => 'At least one non-alphanumeric',
18552:                                                    );
18553:                                 my $needed = '<ul><li>'.
18554:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
18555:                                              '</li></ul>';
18556:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
18557:                             } else {
18558:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18559:                             }
18560:                         } else {
18561:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18562:                         }
18563:                     } elsif ($key eq 'crsownerchg') {
18564:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
18565:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
18566:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
18567:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18568:                             } else {
18569:                                 my %crsownerstr;
18570:                                 foreach my $item ('by','for') {
18571:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
18572:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
18573:                                             if ($type eq 'default') {
18574:                                                 $crsownerstr{$item} .= $othertitle.', ';
18575:                                             } elsif ($usertypes->{$type} ne '') {
18576:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
18577:                                             }
18578:                                         }
18579:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
18580:                                     }
18581:                                 }
18582:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
18583:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
18584:                             }
18585:                         } else {
18586:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18587:                         }
18588:                     }
18589:                     $resulttext .= '</ul></li>';
18590:                 }
18591:             }
18592:             $resulttext .= '</ul>';
18593:         } else {
18594:             $resulttext = &mt('No changes made to password settings');
18595:         }
18596:         my $cachetime = 24*60*60;
18597:         if ($updatedefaults) {
18598:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18599:             if (ref($lastactref) eq 'HASH') {
18600:                 $lastactref->{'domdefaults'} = 1;
18601:             }
18602:         }
18603:         if ($updateconf) {
18604:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
18605:             if (ref($lastactref) eq 'HASH') {
18606:                 $lastactref->{'passwdconf'} = 1;
18607:             }
18608:         }
18609:     } else {
18610:         $resulttext = '<span class="LC_error">'.
18611:             &mt('An error occurred: [_1]',$putresult).'</span>';
18612:     }
18613:     if ($errors) {
18614:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18615:                        $errors.'</ul></p>';
18616:     }
18617:     return $resulttext;
18618: }
18619: 
18620: sub password_rule_changes {
18621:     my ($prefix,$newvalues,$current,$changes) = @_;
18622:     return unless ((ref($newvalues) eq 'HASH') &&
18623:                    (ref($current) eq 'HASH') &&
18624:                    (ref($changes) eq 'HASH'));
18625:     my (@rules,%staticdefaults);
18626:     if ($prefix eq 'passwords') {
18627:         @rules = ('min','max','expire','numsaved');
18628:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
18629:         @rules = ('min','max');
18630:     }
18631:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18632:     foreach my $rule (@rules) {
18633:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
18634:         my $ruleok;
18635:         if ($rule eq 'expire') {
18636:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
18637:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18638:                 $ruleok = 1;
18639:             }
18640:         } elsif ($rule eq 'min') {
18641:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
18642:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
18643:                     $ruleok = 1;
18644:                 }
18645:             }
18646:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
18647:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18648:             $ruleok = 1;
18649:         }
18650:         if ($ruleok) {
18651:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
18652:             if (exists($current->{$rule})) {
18653:                 if ($newvalues->{$rule} ne $current->{$rule}) {
18654:                     $changes->{'rules'} = 1;
18655:                 }
18656:             } elsif ($rule eq 'min') {
18657:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
18658:                     $changes->{'rules'} = 1;
18659:                 }
18660:             } else {
18661:                 $changes->{'rules'} = 1;
18662:             }
18663:         } elsif (exists($current->{$rule})) {
18664:             $changes->{'rules'} = 1;
18665:         }
18666:     }
18667:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
18668:     my @chars;
18669:     foreach my $item (sort(@posschars)) {
18670:         if ($item =~ /^(uc|lc|num|spec)$/) {
18671:             push(@chars,$item);
18672:         }
18673:     }
18674:     $newvalues->{'chars'} = \@chars;
18675:     unless ($changes->{'rules'}) {
18676:         if (ref($current->{'chars'}) eq 'ARRAY') {
18677:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
18678:             if (@diffs > 0) {
18679:                 $changes->{'rules'} = 1;
18680:             }
18681:         } else {
18682:             if (@chars > 0) {
18683:                 $changes->{'rules'} = 1;
18684:             }
18685:         }
18686:     }
18687:     return;
18688: }
18689: 
18690: sub modify_usercreation {
18691:     my ($dom,%domconfig) = @_;
18692:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
18693:     my $warningmsg;
18694:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18695:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18696:             if ($key eq 'cancreate') {
18697:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18698:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18699:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
18700:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18701:                         } else {
18702:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18703:                         }
18704:                     }
18705:                 }
18706:             } elsif ($key eq 'email_rule') {
18707:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18708:             } else {
18709:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18710:             }
18711:         }
18712:     }
18713:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
18714:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
18715:     my @contexts = ('author','course','requestcrs');
18716:     foreach my $item(@contexts) {
18717:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
18718:     }
18719:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18720:         foreach my $item (@contexts) {
18721:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
18722:                 push(@{$changes{'cancreate'}},$item);
18723:             }
18724:         }
18725:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
18726:         foreach my $item (@contexts) {
18727:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
18728:                 if ($cancreate{$item} ne 'any') {
18729:                     push(@{$changes{'cancreate'}},$item);
18730:                 }
18731:             } else {
18732:                 if ($cancreate{$item} ne 'none') {
18733:                     push(@{$changes{'cancreate'}},$item);
18734:                 }
18735:             }
18736:         }
18737:     } else {
18738:         foreach my $item (@contexts)  {
18739:             push(@{$changes{'cancreate'}},$item);
18740:         }
18741:     }
18742: 
18743:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
18744:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
18745:             if (!grep(/^\Q$type\E$/,@username_rule)) {
18746:                 push(@{$changes{'username_rule'}},$type);
18747:             }
18748:         }
18749:         foreach my $type (@username_rule) {
18750:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
18751:                 push(@{$changes{'username_rule'}},$type);
18752:             }
18753:         }
18754:     } else {
18755:         push(@{$changes{'username_rule'}},@username_rule);
18756:     }
18757: 
18758:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18759:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18760:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18761:                 push(@{$changes{'id_rule'}},$type);
18762:             }
18763:         }
18764:         foreach my $type (@id_rule) {
18765:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18766:                 push(@{$changes{'id_rule'}},$type);
18767:             }
18768:         }
18769:     } else {
18770:         push(@{$changes{'id_rule'}},@id_rule);
18771:     }
18772: 
18773:     my @authen_contexts = ('author','course','domain');
18774:     my @authtypes = ('int','krb4','krb5','loc','lti');
18775:     my %authhash;
18776:     foreach my $item (@authen_contexts) {
18777:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18778:         foreach my $auth (@authtypes) {
18779:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18780:                 $authhash{$item}{$auth} = 1;
18781:             } else {
18782:                 $authhash{$item}{$auth} = 0;
18783:             }
18784:         }
18785:     }
18786:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18787:         foreach my $item (@authen_contexts) {
18788:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18789:                 foreach my $auth (@authtypes) {
18790:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18791:                         push(@{$changes{'authtypes'}},$item);
18792:                         last;
18793:                     }
18794:                 }
18795:             }
18796:         }
18797:     } else {
18798:         foreach my $item (@authen_contexts) {
18799:             push(@{$changes{'authtypes'}},$item);
18800:         }
18801:     }
18802: 
18803:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18804:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18805:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18806:     $save_usercreate{'id_rule'} = \@id_rule;
18807:     $save_usercreate{'username_rule'} = \@username_rule,
18808:     $save_usercreate{'authtypes'} = \%authhash;
18809: 
18810:     my %usercreation_hash =  (
18811:         usercreation     => \%save_usercreate,
18812:     );
18813: 
18814:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18815:                                              $dom);
18816: 
18817:     if ($putresult eq 'ok') {
18818:         if (keys(%changes) > 0) {
18819:             $resulttext = &mt('Changes made:').'<ul>';
18820:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18821:                 my %lt = &usercreation_types();
18822:                 foreach my $type (@{$changes{'cancreate'}}) {
18823:                     my $chgtext = $lt{$type}.', ';
18824:                     if ($cancreate{$type} eq 'none') {
18825:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18826:                     } elsif ($cancreate{$type} eq 'any') {
18827:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18828:                     } elsif ($cancreate{$type} eq 'official') {
18829:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18830:                     } elsif ($cancreate{$type} eq 'unofficial') {
18831:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18832:                     }
18833:                     $resulttext .= '<li>'.$chgtext.'</li>';
18834:                 }
18835:             }
18836:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18837:                 my ($rules,$ruleorder) = 
18838:                     &Apache::lonnet::inst_userrules($dom,'username');
18839:                 my $chgtext = '<ul>';
18840:                 foreach my $type (@username_rule) {
18841:                     if (ref($rules->{$type}) eq 'HASH') {
18842:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18843:                     }
18844:                 }
18845:                 $chgtext .= '</ul>';
18846:                 if (@username_rule > 0) {
18847:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18848:                 } else {
18849:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18850:                 }
18851:             }
18852:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18853:                 my ($idrules,$idruleorder) = 
18854:                     &Apache::lonnet::inst_userrules($dom,'id');
18855:                 my $chgtext = '<ul>';
18856:                 foreach my $type (@id_rule) {
18857:                     if (ref($idrules->{$type}) eq 'HASH') {
18858:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18859:                     }
18860:                 }
18861:                 $chgtext .= '</ul>';
18862:                 if (@id_rule > 0) {
18863:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18864:                 } else {
18865:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18866:                 }
18867:             }
18868:             my %authname = &authtype_names();
18869:             my %context_title = &context_names();
18870:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18871:                 my $chgtext = '<ul>';
18872:                 foreach my $type (@{$changes{'authtypes'}}) {
18873:                     my @allowed;
18874:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18875:                     foreach my $auth (@authtypes) {
18876:                         if ($authhash{$type}{$auth}) {
18877:                             push(@allowed,$authname{$auth});
18878:                         }
18879:                     }
18880:                     if (@allowed > 0) {
18881:                         $chgtext .= join(', ',@allowed).'</li>';
18882:                     } else {
18883:                         $chgtext .= &mt('none').'</li>';
18884:                     }
18885:                 }
18886:                 $chgtext .= '</ul>';
18887:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18888:                 $resulttext .= '</li>';
18889:             }
18890:             $resulttext .= '</ul>';
18891:         } else {
18892:             $resulttext = &mt('No changes made to user creation settings');
18893:         }
18894:     } else {
18895:         $resulttext = '<span class="LC_error">'.
18896:             &mt('An error occurred: [_1]',$putresult).'</span>';
18897:     }
18898:     if ($warningmsg ne '') {
18899:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18900:     }
18901:     return $resulttext;
18902: }
18903: 
18904: sub modify_selfcreation {
18905:     my ($dom,$lastactref,%domconfig) = @_;
18906:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18907:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18908:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18909:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18910:     if (ref($typesref) eq 'ARRAY') {
18911:         @types = @{$typesref};
18912:     }
18913:     if (ref($usertypesref) eq 'HASH') {
18914:         %usertypes = %{$usertypesref};
18915:     }
18916:     $usertypes{'default'} = $othertitle;
18917: #
18918: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18919: #
18920:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18921:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18922:             if ($key eq 'cancreate') {
18923:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18924:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18925:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18926:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18927:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18928:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18929:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18930:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18931:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18932:                         } else {
18933:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18934:                         }
18935:                     }
18936:                 }
18937:             } elsif ($key eq 'email_rule') {
18938:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18939:             } else {
18940:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18941:             }
18942:         }
18943:     }
18944: #
18945: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18946: #
18947:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18948:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18949:             if ($key eq 'selfcreate') {
18950:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18951:             } else {
18952:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18953:             }
18954:         }
18955:     }
18956: #
18957: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18958: #
18959:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18960:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18961:             if ($key eq 'inststatusguest') {
18962:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18963:             } else {
18964:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18965:             }
18966:         }
18967:     }
18968: 
18969:     my @contexts = ('selfcreate');
18970:     @{$cancreate{'selfcreate'}} = ();
18971:     %{$cancreate{'emailusername'}} = ();
18972:     if (@types) {
18973:         @{$cancreate{'statustocreate'}} = ();
18974:     }
18975:     %{$cancreate{'selfcreateprocessing'}} = ();
18976:     %{$cancreate{'shibenv'}} = ();
18977:     %{$cancreate{'emailverified'}} = ();
18978:     %{$cancreate{'emailoptions'}} = ();
18979:     %{$cancreate{'emaildomain'}} = ();
18980:     my %selfcreatetypes = (
18981:                              sso   => 'users authenticated by institutional single sign on',
18982:                              login => 'users authenticated by institutional log-in',
18983:                              email => 'users verified by e-mail',
18984:                           );
18985: #
18986: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18987: # is permitted.
18988: #
18989:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18990: 
18991:     my (@statuses,%email_rule);
18992:     foreach my $item ('login','sso','email') {
18993:         if ($item eq 'email') {
18994:             if ($env{'form.cancreate_email'}) {
18995:                 if (@types) {
18996:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18997:                     foreach my $status (@poss_statuses) {
18998:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18999:                             push(@statuses,$status);
19000:                         }
19001:                     }
19002:                     $save_inststatus{'inststatusguest'} = \@statuses;
19003:                 } else {
19004:                     push(@statuses,'default');
19005:                 }
19006:                 if (@statuses) {
19007:                     my %curr_rule;
19008:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
19009:                         foreach my $type (@statuses) {
19010:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
19011:                         }
19012:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
19013:                         foreach my $type (@statuses) {
19014:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
19015:                         }
19016:                     }
19017:                     push(@{$cancreate{'selfcreate'}},'email');
19018:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
19019:                     my %curremaildom;
19020:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
19021:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
19022:                     }
19023:                     foreach my $type (@statuses) {
19024:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
19025:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
19026:                         }
19027:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
19028:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
19029:                         }
19030:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
19031: #
19032: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
19033: #
19034:                             my $chosen = $1;
19035:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
19036:                                 my $emaildom;
19037:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
19038:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
19039:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
19040:                                     if (ref($curremaildom{$type}) eq 'HASH') {
19041:                                         if (exists($curremaildom{$type}{$chosen})) {
19042:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
19043:                                                 push(@{$changes{'cancreate'}},'emaildomain');
19044:                                             }
19045:                                         } elsif ($emaildom ne '') {
19046:                                             push(@{$changes{'cancreate'}},'emaildomain');
19047:                                         }
19048:                                     } elsif ($emaildom ne '') {
19049:                                         push(@{$changes{'cancreate'}},'emaildomain');
19050:                                     }
19051:                                 }
19052:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19053:                             } elsif ($chosen eq 'custom') {
19054:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
19055:                                 $email_rule{$type} = [];
19056:                                 if (ref($emailrules) eq 'HASH') {
19057:                                     foreach my $rule (@possemail_rules) {
19058:                                         if (exists($emailrules->{$rule})) {
19059:                                             push(@{$email_rule{$type}},$rule);
19060:                                         }
19061:                                     }
19062:                                 }
19063:                                 if (@{$email_rule{$type}}) {
19064:                                     $cancreate{'emailoptions'}{$type} = 'custom';
19065:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
19066:                                         if (@{$curr_rule{$type}} > 0) {
19067:                                             foreach my $rule (@{$curr_rule{$type}}) {
19068:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
19069:                                                     push(@{$changes{'email_rule'}},$type);
19070:                                                 }
19071:                                             }
19072:                                         }
19073:                                         foreach my $type (@{$email_rule{$type}}) {
19074:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
19075:                                                 push(@{$changes{'email_rule'}},$type);
19076:                                             }
19077:                                         }
19078:                                     } else {
19079:                                         push(@{$changes{'email_rule'}},$type);
19080:                                     }
19081:                                 }
19082:                             } else {
19083:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19084:                             }
19085:                         }
19086:                     }
19087:                     if (@types) {
19088:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19089:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
19090:                             if (@changed) {
19091:                                 push(@{$changes{'inststatus'}},'inststatusguest');
19092:                             }
19093:                         } else {
19094:                             push(@{$changes{'inststatus'}},'inststatusguest');
19095:                         }
19096:                     }
19097:                 } else {
19098:                     delete($env{'form.cancreate_email'});
19099:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19100:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19101:                             push(@{$changes{'inststatus'}},'inststatusguest');
19102:                         }
19103:                     }
19104:                 }
19105:             } else {
19106:                 $save_inststatus{'inststatusguest'} = [];
19107:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19108:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19109:                         push(@{$changes{'inststatus'}},'inststatusguest');
19110:                     }
19111:                 }
19112:             }
19113:         } else {
19114:             if ($env{'form.cancreate_'.$item}) {
19115:                 push(@{$cancreate{'selfcreate'}},$item);
19116:             }
19117:         }
19118:     }
19119:     my (%userinfo,%savecaptcha);
19120:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
19121: #
19122: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
19123: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
19124: #
19125: 
19126:     if ($env{'form.cancreate_email'}) {
19127:         push(@contexts,'emailusername');
19128:         if (@statuses) {
19129:             foreach my $type (@statuses) {
19130:                 if (ref($infofields) eq 'ARRAY') {
19131:                     foreach my $field (@{$infofields}) {
19132:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
19133:                             $cancreate{'emailusername'}{$type}{$field} = $1;
19134:                         }
19135:                     }
19136:                 }
19137:             }
19138:         }
19139: #
19140: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
19141: # queued requests for self-creation of account verified by e-mail.
19142: #
19143: 
19144:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
19145:         @approvalnotify = sort(@approvalnotify);
19146:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
19147:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19148:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
19149:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
19150:                     push(@{$changes{'cancreate'}},'notify');
19151:                 }
19152:             } else {
19153:                 if ($cancreate{'notify'}{'approval'}) {
19154:                     push(@{$changes{'cancreate'}},'notify');
19155:                 }
19156:             }
19157:         } elsif ($cancreate{'notify'}{'approval'}) {
19158:             push(@{$changes{'cancreate'}},'notify');
19159:         }
19160: 
19161:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
19162:     }
19163: #  
19164: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
19165: # institutional log-in.
19166: #
19167:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
19168:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
19169:                ($domdefaults{'auth_def'} eq 'localauth'))) {
19170:             $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.').' '.
19171:                           &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.');
19172:         }
19173:     }
19174:     my @fields = ('lastname','firstname','middlename','generation',
19175:                   'permanentemail','id');
19176:     my @shibfields = (@fields,'inststatus');
19177:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19178: #
19179: # Where usernames may created for institutional log-in and/or institutional single sign on:
19180: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
19181: # may self-create accounts 
19182: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
19183: # which the user may supply, if institutional data is unavailable.
19184: #
19185:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
19186:         if (@types) {
19187:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
19188:             push(@contexts,'statustocreate');
19189:             foreach my $type (@types) {
19190:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
19191:                 foreach my $field (@fields) {
19192:                     if (grep(/^\Q$field\E$/,@modifiable)) {
19193:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
19194:                     } else {
19195:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
19196:                     }
19197:                 }
19198:             }
19199:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
19200:                 foreach my $type (@types) {
19201:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
19202:                         foreach my $field (@fields) {
19203:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
19204:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
19205:                                 push(@{$changes{'selfcreate'}},$type);
19206:                                 last;
19207:                             }
19208:                         }
19209:                     }
19210:                 }
19211:             } else {
19212:                 foreach my $type (@types) {
19213:                     push(@{$changes{'selfcreate'}},$type);
19214:                 }
19215:             }
19216:         }
19217:         foreach my $field (@shibfields) {
19218:             if ($env{'form.shibenv_'.$field} ne '') {
19219:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
19220:             }
19221:         }
19222:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19223:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
19224:                 foreach my $field (@shibfields) {
19225:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
19226:                         push(@{$changes{'cancreate'}},'shibenv');
19227:                     }
19228:                 }
19229:             } else {
19230:                 foreach my $field (@shibfields) {
19231:                     if ($env{'form.shibenv_'.$field}) {
19232:                         push(@{$changes{'cancreate'}},'shibenv');
19233:                         last;
19234:                     }
19235:                 }
19236:             }
19237:         }
19238:     }
19239:     foreach my $item (@contexts) {
19240:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
19241:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
19242:                 if (ref($cancreate{$item}) eq 'ARRAY') {
19243:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
19244:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19245:                             push(@{$changes{'cancreate'}},$item);
19246:                         }
19247:                     }
19248:                 }
19249:             }
19250:             if (ref($cancreate{$item}) eq 'ARRAY') {
19251:                 foreach my $type (@{$cancreate{$item}}) {
19252:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
19253:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19254:                             push(@{$changes{'cancreate'}},$item);
19255:                         }
19256:                     }
19257:                 }
19258:             }
19259:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
19260:             if (ref($cancreate{$item}) eq 'HASH') {
19261:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
19262:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19263:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
19264:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19265:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19266:                                     push(@{$changes{'cancreate'}},$item);
19267:                                 }
19268:                             }
19269:                         }
19270:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19271:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19272:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19273:                                 push(@{$changes{'cancreate'}},$item);
19274:                             }
19275:                         }
19276:                     }
19277:                 }
19278:                 foreach my $type (keys(%{$cancreate{$item}})) {
19279:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19280:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19281:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19282:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19283:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19284:                                         push(@{$changes{'cancreate'}},$item);
19285:                                     }
19286:                                 }
19287:                             } else {
19288:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19289:                                     push(@{$changes{'cancreate'}},$item);
19290:                                 }
19291:                             }
19292:                         }
19293:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19294:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19295:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19296:                                 push(@{$changes{'cancreate'}},$item);
19297:                             }
19298:                         }
19299:                     }
19300:                 }
19301:             }
19302:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
19303:             if (ref($cancreate{$item}) eq 'ARRAY') {
19304:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
19305:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19306:                         push(@{$changes{'cancreate'}},$item);
19307:                     }
19308:                 }
19309:             }
19310:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19311:             if (ref($cancreate{$item}) eq 'HASH') {
19312:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19313:                     push(@{$changes{'cancreate'}},$item);
19314:                 }
19315:             }
19316:         } elsif ($item eq 'emailusername') {
19317:             if (ref($cancreate{$item}) eq 'HASH') {
19318:                 foreach my $type (keys(%{$cancreate{$item}})) {
19319:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19320:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19321:                             if ($cancreate{$item}{$type}{$field}) {
19322:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19323:                                     push(@{$changes{'cancreate'}},$item);
19324:                                 }
19325:                                 last;
19326:                             }
19327:                         }
19328:                     }
19329:                 }
19330:             }
19331:         }
19332:     }
19333: #
19334: # Populate %save_usercreate hash with updates to self-creation configuration.
19335: #
19336:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
19337:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
19338:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
19339:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
19340:     if (ref($cancreate{'notify'}) eq 'HASH') {
19341:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
19342:     }
19343:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
19344:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
19345:     }
19346:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
19347:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
19348:     }
19349:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
19350:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
19351:     }
19352:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
19353:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
19354:     }
19355:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19356:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
19357:     }
19358:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
19359:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
19360:     }
19361:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
19362:     $save_usercreate{'email_rule'} = \%email_rule;
19363: 
19364:     my %userconfig_hash = (
19365:             usercreation     => \%save_usercreate,
19366:             usermodification => \%save_usermodify,
19367:             inststatus       => \%save_inststatus,
19368:     );
19369: 
19370:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
19371:                                              $dom);
19372: #
19373: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
19374: #
19375:     if ($putresult eq 'ok') {
19376:         if (keys(%changes) > 0) {
19377:             $resulttext = &mt('Changes made:').'<ul>';
19378:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
19379:                 my %lt = &selfcreation_types();
19380:                 foreach my $type (@{$changes{'cancreate'}}) {
19381:                     my $chgtext = '';
19382:                     if ($type eq 'selfcreate') {
19383:                         if (@{$cancreate{$type}} == 0) {
19384:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
19385:                         } else {
19386:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
19387:                                         '<ul>';
19388:                             foreach my $case (@{$cancreate{$type}}) {
19389:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
19390:                             }
19391:                             $chgtext .= '</ul>';
19392:                             if (ref($cancreate{$type}) eq 'ARRAY') {
19393:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
19394:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19395:                                         if (@{$cancreate{'statustocreate'}} == 0) {
19396:                                             $chgtext .= '<span class="LC_warning">'.
19397:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
19398:                                                         '</span><br />';
19399:                                         }
19400:                                     }
19401:                                 }
19402:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
19403:                                     if (!@statuses) {
19404:                                         $chgtext .= '<span class="LC_warning">'.
19405:                                                     &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.").
19406:                                                     '</span><br />';
19407: 
19408:                                     }
19409:                                 }
19410:                             }
19411:                         }
19412:                     } elsif ($type eq 'shibenv') {
19413:                         if (keys(%{$cancreate{$type}}) == 0) {
19414:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
19415:                         } else {
19416:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
19417:                                         '<ul>';
19418:                             foreach my $field (@shibfields) {
19419:                                 next if ($cancreate{$type}{$field} eq '');
19420:                                 if ($field eq 'inststatus') {
19421:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
19422:                                 } else {
19423:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
19424:                                 }
19425:                             }
19426:                             $chgtext .= '</ul>';
19427:                         }
19428:                     } elsif ($type eq 'statustocreate') {
19429:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
19430:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
19431:                             if (@{$cancreate{'selfcreate'}} > 0) {
19432:                                 if (@{$cancreate{'statustocreate'}} == 0) {
19433:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
19434:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
19435:                                         $chgtext .= '<br />'.
19436:                                                     '<span class="LC_warning">'.
19437:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
19438:                                                     '</span>';
19439:                                     }
19440:                                 } elsif (keys(%usertypes) > 0) {
19441:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19442:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
19443:                                     } else {
19444:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
19445:                                     }
19446:                                     $chgtext .= '<ul>';
19447:                                     foreach my $case (@{$cancreate{$type}}) {
19448:                                         if ($case eq 'default') {
19449:                                             $chgtext .= '<li>'.$othertitle.'</li>';
19450:                                         } else {
19451:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
19452:                                         }
19453:                                     }
19454:                                     $chgtext .= '</ul>';
19455:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19456:                                         $chgtext .= '<span class="LC_warning">'.
19457:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
19458:                                                     '</span>';
19459:                                     }
19460:                                 }
19461:                             } else {
19462:                                 if (@{$cancreate{$type}} == 0) {
19463:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
19464:                                 } else {
19465:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
19466:                                 }
19467:                             }
19468:                             $chgtext .= '<br />';
19469:                         }
19470:                     } elsif ($type eq 'selfcreateprocessing') {
19471:                         my %choices = &Apache::lonlocal::texthash (
19472:                                                                     automatic => 'Automatic approval',
19473:                                                                     approval  => 'Queued for approval',
19474:                                                                   );
19475:                         if (@types) {
19476:                             if (@statuses) {
19477:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
19478:                                             '<ul>';
19479:                                 foreach my $status (@statuses) {
19480:                                     if ($status eq 'default') {
19481:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19482:                                     } else {
19483:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19484:                                     }
19485:                                 }
19486:                                 $chgtext .= '</ul>';
19487:                             }
19488:                         } else {
19489:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
19490:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
19491:                         }
19492:                     } elsif ($type eq 'emailverified') {
19493:                         my %options = &Apache::lonlocal::texthash (
19494:                                                                     all   => 'Same as e-mail',
19495:                                                                     first => 'Omit @domain',
19496:                                                                     free  => 'Free to choose',
19497:                                                                   );
19498:                         if (@types) {
19499:                             if (@statuses) {
19500:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
19501:                                             '<ul>';
19502:                                 foreach my $status (@statuses) {
19503:                                     if ($status eq 'default') {
19504:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19505:                                     } else {
19506:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19507:                                     }
19508:                                 }
19509:                                 $chgtext .= '</ul>';
19510:                             }
19511:                         } else {
19512:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
19513:                                             $options{$cancreate{'emailverified'}{'default'}});
19514:                         }
19515:                     } elsif ($type eq 'emailoptions') {
19516:                         my %options = &Apache::lonlocal::texthash (
19517:                                                                     any     => 'Any e-mail',
19518:                                                                     inst    => 'Institutional only',
19519:                                                                     noninst => 'Non-institutional only',
19520:                                                                     custom  => 'Custom restrictions',
19521:                                                                   );
19522:                         if (@types) {
19523:                             if (@statuses) {
19524:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
19525:                                             '<ul>';
19526:                                 foreach my $status (@statuses) {
19527:                                     if ($type eq 'default') {
19528:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19529:                                     } else {
19530:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19531:                                     }
19532:                                 }
19533:                                 $chgtext .= '</ul>';
19534:                             }
19535:                         } else {
19536:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
19537:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
19538:                             } else {
19539:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
19540:                                                 $options{$cancreate{'emailoptions'}{'default'}});
19541:                             }
19542:                         }
19543:                     } elsif ($type eq 'emaildomain') {
19544:                         my $output;
19545:                         if (@statuses) {
19546:                             foreach my $type (@statuses) {
19547:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
19548:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
19549:                                         if ($type eq 'default') {
19550:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19551:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19552:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19553:                                             } else {
19554:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
19555:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19556:                                             }
19557:                                         } else {
19558:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19559:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19560:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19561:                                             } else {
19562:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
19563:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19564:                                             }
19565:                                         }
19566:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
19567:                                         if ($type eq 'default') {
19568:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19569:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19570:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19571:                                             } else {
19572:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
19573:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19574:                                             }
19575:                                         } else {
19576:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19577:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19578:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19579:                                             } else {
19580:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
19581:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19582:                                             }
19583:                                         }
19584:                                     }
19585:                                 }
19586:                             }
19587:                         }
19588:                         if ($output ne '') {
19589:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
19590:                                         '<ul>'.$output.'</ul>';
19591:                         }
19592:                     } elsif ($type eq 'captcha') {
19593:                         if ($savecaptcha{$type} eq 'notused') {
19594:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
19595:                         } else {
19596:                             my %captchas = &captcha_phrases();
19597:                             if ($captchas{$savecaptcha{$type}}) {
19598:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
19599:                             } else {
19600:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
19601:                             }
19602:                         }
19603:                     } elsif ($type eq 'recaptchakeys') {
19604:                         my ($privkey,$pubkey);
19605:                         if (ref($savecaptcha{$type}) eq 'HASH') {
19606:                             $pubkey = $savecaptcha{$type}{'public'};
19607:                             $privkey = $savecaptcha{$type}{'private'};
19608:                         }
19609:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
19610:                         if (!$pubkey) {
19611:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
19612:                         } else {
19613:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
19614:                         }
19615:                         if (!$privkey) {
19616:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
19617:                         } else {
19618:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
19619:                         }
19620:                         $chgtext .= '</ul>';
19621:                     } elsif ($type eq 'recaptchaversion') {
19622:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
19623:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
19624:                         }
19625:                     } elsif ($type eq 'emailusername') {
19626:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
19627:                             if (@statuses) {
19628:                                 foreach my $type (@statuses) {
19629:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
19630:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
19631:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
19632:                                                     '<ul>';
19633:                                             foreach my $field (@{$infofields}) {
19634:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
19635:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
19636:                                                 }
19637:                                             }
19638:                                             $chgtext .= '</ul>';
19639:                                         } else {
19640:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19641:                                         }
19642:                                     } else {
19643:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19644:                                     }
19645:                                 }
19646:                             }
19647:                         }
19648:                     } elsif ($type eq 'notify') {
19649:                         my $numapprove = 0;
19650:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
19651:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
19652:                                 if ($cancreate{'notify'}{'approval'}) {
19653:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
19654:                                     $numapprove ++;
19655:                                 }
19656:                             }
19657:                         }
19658:                         unless ($numapprove) {
19659:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
19660:                         }
19661:                     }
19662:                     if ($chgtext) {
19663:                         $resulttext .= '<li>'.$chgtext.'</li>';
19664:                     }
19665:                 }
19666:             }
19667:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
19668:                 my ($emailrules,$emailruleorder) =
19669:                     &Apache::lonnet::inst_userrules($dom,'email');
19670:                 foreach my $type (@{$changes{'email_rule'}}) {
19671:                     if (ref($email_rule{$type}) eq 'ARRAY') {
19672:                         my $chgtext = '<ul>';
19673:                         foreach my $rule (@{$email_rule{$type}}) {
19674:                             if (ref($emailrules->{$rule}) eq 'HASH') {
19675:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
19676:                             }
19677:                         }
19678:                         $chgtext .= '</ul>';
19679:                         my $typename;
19680:                         if (@types) {
19681:                             if ($type eq 'default') {
19682:                                 $typename = $othertitle;
19683:                             } else {
19684:                                 $typename = $usertypes{$type};
19685:                             }
19686:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
19687:                         }
19688:                         if (@{$email_rule{$type}} > 0) {
19689:                             $resulttext .= '<li>'.
19690:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
19691:                                                $usertypes{$type}).
19692:                                            $chgtext.
19693:                                            '</li>';
19694:                         } else {
19695:                             $resulttext .= '<li>'.
19696:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
19697:                                            '</li>'.
19698:                                            &mt('(Affiliation: [_1])',$typename);
19699:                         }
19700:                     }
19701:                 }
19702:             }
19703:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
19704:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
19705:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
19706:                         my $chgtext = '<ul>';
19707:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
19708:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
19709:                         }
19710:                         $chgtext .= '</ul>';
19711:                         $resulttext .= '<li>'.
19712:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
19713:                                           $chgtext.
19714:                                        '</li>';
19715:                     } else {
19716:                         $resulttext .= '<li>'.
19717:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
19718:                                        '</li>';
19719:                     }
19720:                 }
19721:             }
19722:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
19723:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
19724:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19725:                 foreach my $type (@{$changes{'selfcreate'}}) {
19726:                     my $typename = $type;
19727:                     if (keys(%usertypes) > 0) {
19728:                         if ($usertypes{$type} ne '') {
19729:                             $typename = $usertypes{$type};
19730:                         }
19731:                     }
19732:                     my @modifiable;
19733:                     $resulttext .= '<li>'.
19734:                                     &mt('Self-creation of account by users with status: [_1]',
19735:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
19736:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
19737:                     foreach my $field (@fields) {
19738:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
19739:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
19740:                         }
19741:                     }
19742:                     if (@modifiable > 0) {
19743:                         $resulttext .= join(', ',@modifiable);
19744:                     } else {
19745:                         $resulttext .= &mt('none');
19746:                     }
19747:                     $resulttext .= '</li>';
19748:                 }
19749:                 $resulttext .= '</ul></li>';
19750:             }
19751:             $resulttext .= '</ul>';
19752:             my $cachetime = 24*60*60;
19753:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
19754:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19755:             if (ref($lastactref) eq 'HASH') {
19756:                 $lastactref->{'domdefaults'} = 1;
19757:             }
19758:         } else {
19759:             $resulttext = &mt('No changes made to self-creation settings');
19760:         }
19761:     } else {
19762:         $resulttext = '<span class="LC_error">'.
19763:             &mt('An error occurred: [_1]',$putresult).'</span>';
19764:     }
19765:     if ($warningmsg ne '') {
19766:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19767:     }
19768:     return $resulttext;
19769: }
19770: 
19771: sub process_captcha {
19772:     my ($container,$changes,$newsettings,$currsettings) = @_;
19773:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19774:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19775:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19776:         $newsettings->{'captcha'} = 'original';
19777:     }
19778:     my %current;
19779:     if (ref($currsettings) eq 'HASH') {
19780:         %current = %{$currsettings};
19781:     }
19782:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19783:         if ($container eq 'cancreate') {
19784:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19785:                 push(@{$changes->{'cancreate'}},'captcha');
19786:             } elsif (!defined($changes->{'cancreate'})) {
19787:                 $changes->{'cancreate'} = ['captcha'];
19788:             }
19789:         } elsif ($container eq 'passwords') {
19790:             $changes->{'reset'} = 1;
19791:         } else {
19792:             $changes->{'captcha'} = 1;
19793:         }
19794:     }
19795:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19796:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19797:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19798:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19799:         $newpub =~ s/[^\w\-]//g;
19800:         $newpriv =~ s/[^\w\-]//g;
19801:         $newsettings->{'recaptchakeys'} = {
19802:                                              public  => $newpub,
19803:                                              private => $newpriv,
19804:                                           };
19805:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19806:         $newversion =~ s/\D//g;
19807:         if ($newversion ne '2') {
19808:             $newversion = 1;
19809:         }
19810:         $newsettings->{'recaptchaversion'} = $newversion;
19811:     }
19812:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19813:         $currpub = $current{'recaptchakeys'}{'public'};
19814:         $currpriv = $current{'recaptchakeys'}{'private'};
19815:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19816:             $newsettings->{'recaptchakeys'} = {
19817:                                                  public  => '',
19818:                                                  private => '',
19819:                                               }
19820:         }
19821:     }
19822:     if ($current{'captcha'} eq 'recaptcha') {
19823:         $currversion = $current{'recaptchaversion'};
19824:         if ($currversion ne '2') {
19825:             $currversion = 1;
19826:         }
19827:     }
19828:     if ($currversion ne $newversion) {
19829:         if ($container eq 'cancreate') {
19830:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19831:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19832:             } elsif (!defined($changes->{'cancreate'})) {
19833:                 $changes->{'cancreate'} = ['recaptchaversion'];
19834:             }
19835:         } elsif ($container eq 'passwords') {
19836:             $changes->{'reset'} = 1;
19837:         } else {
19838:             $changes->{'recaptchaversion'} = 1;
19839:         }
19840:     }
19841:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19842:         if ($container eq 'cancreate') {
19843:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19844:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19845:             } elsif (!defined($changes->{'cancreate'})) {
19846:                 $changes->{'cancreate'} = ['recaptchakeys'];
19847:             }
19848:         } elsif ($container eq 'passwords') {
19849:             $changes->{'reset'} = 1;
19850:         } else {
19851:             $changes->{'recaptchakeys'} = 1;
19852:         }
19853:     }
19854:     return;
19855: }
19856: 
19857: sub modify_usermodification {
19858:     my ($dom,%domconfig) = @_;
19859:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19860:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19861:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19862:             if ($key eq 'selfcreate') {
19863:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19864:             } else {  
19865:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19866:             }
19867:         }
19868:     }
19869:     my @contexts = ('author','course');
19870:     my %context_title = (
19871:                            author => 'In author context',
19872:                            course => 'In course context',
19873:                         );
19874:     my @fields = ('lastname','firstname','middlename','generation',
19875:                   'permanentemail','id');
19876:     my %roles = (
19877:                   author => ['ca','aa'],
19878:                   course => ['st','ep','ta','in','cr'],
19879:                 );
19880:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19881:     foreach my $context (@contexts) {
19882:         foreach my $role (@{$roles{$context}}) {
19883:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19884:             foreach my $item (@fields) {
19885:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19886:                     $modifyhash{$context}{$role}{$item} = 1;
19887:                 } else {
19888:                     $modifyhash{$context}{$role}{$item} = 0;
19889:                 }
19890:             }
19891:         }
19892:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19893:             foreach my $role (@{$roles{$context}}) {
19894:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19895:                     foreach my $field (@fields) {
19896:                         if ($modifyhash{$context}{$role}{$field} ne 
19897:                                 $curr_usermodification{$context}{$role}{$field}) {
19898:                             push(@{$changes{$context}},$role);
19899:                             last;
19900:                         }
19901:                     }
19902:                 }
19903:             }
19904:         } else {
19905:             foreach my $context (@contexts) {
19906:                 foreach my $role (@{$roles{$context}}) {
19907:                     push(@{$changes{$context}},$role);
19908:                 }
19909:             }
19910:         }
19911:     }
19912:     my %usermodification_hash =  (
19913:                                    usermodification => \%modifyhash,
19914:                                  );
19915:     my $putresult = &Apache::lonnet::put_dom('configuration',
19916:                                              \%usermodification_hash,$dom);
19917:     if ($putresult eq 'ok') {
19918:         if (keys(%changes) > 0) {
19919:             $resulttext = &mt('Changes made: ').'<ul>';
19920:             foreach my $context (@contexts) {
19921:                 if (ref($changes{$context}) eq 'ARRAY') {
19922:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19923:                     if (ref($changes{$context}) eq 'ARRAY') {
19924:                         foreach my $role (@{$changes{$context}}) {
19925:                             my $rolename;
19926:                             if ($role eq 'cr') {
19927:                                 $rolename = &mt('Custom');
19928:                             } else {
19929:                                 $rolename = &Apache::lonnet::plaintext($role);
19930:                             }
19931:                             my @modifiable;
19932:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19933:                             foreach my $field (@fields) {
19934:                                 if ($modifyhash{$context}{$role}{$field}) {
19935:                                     push(@modifiable,$fieldtitles{$field});
19936:                                 }
19937:                             }
19938:                             if (@modifiable > 0) {
19939:                                 $resulttext .= join(', ',@modifiable);
19940:                             } else {
19941:                                 $resulttext .= &mt('none'); 
19942:                             }
19943:                             $resulttext .= '</li>';
19944:                         }
19945:                         $resulttext .= '</ul></li>';
19946:                     }
19947:                 }
19948:             }
19949:             $resulttext .= '</ul>';
19950:         } else {
19951:             $resulttext = &mt('No changes made to user modification settings');
19952:         }
19953:     } else {
19954:         $resulttext = '<span class="LC_error">'.
19955:             &mt('An error occurred: [_1]',$putresult).'</span>';
19956:     }
19957:     return $resulttext;
19958: }
19959: 
19960: sub modify_defaults {
19961:     my ($dom,$lastactref,%domconfig) = @_;
19962:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19963:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19964:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19965:                  'portal_def');
19966:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19967:     foreach my $item (@items) {
19968:         $newvalues{$item} = $env{'form.'.$item};
19969:         if ($item eq 'auth_def') {
19970:             if ($newvalues{$item} ne '') {
19971:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19972:                     push(@errors,$item);
19973:                 }
19974:             }
19975:         } elsif ($item eq 'lang_def') {
19976:             if ($newvalues{$item} ne '') {
19977:                 if ($newvalues{$item} =~ /^(\w+)/) {
19978:                     my $langcode = $1;
19979:                     if ($langcode ne 'x_chef') {
19980:                         if (code2language($langcode) eq '') {
19981:                             push(@errors,$item);
19982:                         }
19983:                     }
19984:                 } else {
19985:                     push(@errors,$item);
19986:                 }
19987:             }
19988:         } elsif ($item eq 'timezone_def') {
19989:             if ($newvalues{$item} ne '') {
19990:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19991:                     push(@errors,$item);   
19992:                 }
19993:             }
19994:         } elsif ($item eq 'datelocale_def') {
19995:             if ($newvalues{$item} ne '') {
19996:                 my @datelocale_ids = DateTime::Locale->ids();
19997:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19998:                     push(@errors,$item);
19999:                 }
20000:             }
20001:         } elsif ($item eq 'portal_def') {
20002:             if ($newvalues{$item} ne '') {
20003:                 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])\/?$/) {
20004:                     foreach my $field ('email','web') {
20005:                         if ($env{'form.'.$item.'_'.$field}) {
20006:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
20007:                         }
20008:                     }
20009:                 } else {
20010:                     push(@errors,$item);
20011:                 }
20012:             }
20013:         }
20014:         if (grep(/^\Q$item\E$/,@errors)) {
20015:             $newvalues{$item} = $domdefaults{$item};
20016:             if ($item eq 'portal_def') {
20017:                 if ($domdefaults{$item}) {
20018:                     foreach my $field ('email','web') {
20019:                         if (exists($domdefaults{$item.'_'.$field})) {
20020:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
20021:                         }
20022:                     }
20023:                 }
20024:             }
20025:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
20026:             $changes{$item} = 1;
20027:         }
20028:         if ($item eq 'portal_def') {
20029:             unless (grep(/^\Q$item\E$/,@errors)) {
20030:                 if ($newvalues{$item} eq '') {
20031:                     foreach my $field ('email','web') {
20032:                         if (exists($domdefaults{$item.'_'.$field})) {
20033:                             delete($domdefaults{$item.'_'.$field});
20034:                         }
20035:                     }
20036:                 } else {
20037:                     unless ($changes{$item}) {
20038:                         foreach my $field ('email','web') {
20039:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
20040:                                 $changes{$item} = 1;
20041:                                 last;
20042:                             }
20043:                         }
20044:                     }
20045:                     foreach my $field ('email','web') {
20046:                         if ($newvalues{$item.'_'.$field}) {
20047:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
20048:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
20049:                             delete($domdefaults{$item.'_'.$field});
20050:                         }
20051:                     }
20052:                 }
20053:             }
20054:         }
20055:         $domdefaults{$item} = $newvalues{$item};
20056:     }
20057:     my %staticdefaults = (
20058:                            'intauth_cost'   => 10,
20059:                            'intauth_check'  => 0,
20060:                            'intauth_switch' => 0,
20061:                          );
20062:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
20063:         if (exists($domdefaults{$item})) {
20064:             $newvalues{$item} = $domdefaults{$item};
20065:         } else {
20066:             $newvalues{$item} = $staticdefaults{$item};
20067:         }
20068:     }
20069:     my ($unamemaprules,$ruleorder);
20070:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
20071:     if (@possunamemaprules) {
20072:         ($unamemaprules,$ruleorder) =
20073:             &Apache::lonnet::inst_userrules($dom,'unamemap');
20074:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
20075:             if (@{$ruleorder} > 0) {
20076:                 my %possrules;
20077:                 map { $possrules{$_} = 1; } @possunamemaprules;
20078:                 foreach my $rule (@{$ruleorder}) {
20079:                     if ($possrules{$rule}) {
20080:                         push(@{$newvalues{'unamemap_rule'}},$rule);
20081:                     }
20082:                 }
20083:             }
20084:         }
20085:     }
20086:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
20087:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20088:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
20089:                                                                $newvalues{'unamemap_rule'});
20090:             if (@rulediffs) {
20091:                 $changes{'unamemap_rule'} = 1;
20092:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20093:             }
20094:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
20095:             $changes{'unamemap_rule'} = 1;
20096:             delete($domdefaults{'unamemap_rule'});
20097:         }
20098:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20099:         if (@{$newvalues{'unamemap_rule'}} > 0) {
20100:             $changes{'unamemap_rule'} = 1;
20101:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20102:         }
20103:     }
20104:     my %defaults_hash = (
20105:                          defaults => \%newvalues,
20106:                         );
20107:     my $title = &defaults_titles();
20108: 
20109:     my $currinststatus;
20110:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
20111:         $currinststatus = $domconfig{'inststatus'};
20112:     } else {
20113:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20114:         $currinststatus = {
20115:                              inststatustypes => $usertypes,
20116:                              inststatusorder => $types,
20117:                              inststatusguest => [],
20118:                           };
20119:     }
20120:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
20121:     my @allpos;
20122:     my %alltypes;
20123:     my @inststatusguest;
20124:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
20125:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
20126:             unless (grep(/^\Q$type\E$/,@todelete)) {
20127:                 push(@inststatusguest,$type);
20128:             }
20129:         }
20130:     }
20131:     my ($currtitles,$currorder);
20132:     if (ref($currinststatus) eq 'HASH') {
20133:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
20134:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
20135:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
20136:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
20137:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
20138:                     }
20139:                 }
20140:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
20141:                     my $position = $env{'form.inststatus_pos_'.$type};
20142:                     $position =~ s/\D+//g;
20143:                     $allpos[$position] = $type;
20144:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
20145:                     $alltypes{$type} =~ s/`//g;
20146:                 }
20147:             }
20148:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
20149:             $currtitles =~ s/,$//;
20150:         }
20151:     }
20152:     if ($env{'form.addinststatus'}) {
20153:         my $newtype = $env{'form.addinststatus'};
20154:         $newtype =~ s/\W//g;
20155:         unless (exists($alltypes{$newtype})) {
20156:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
20157:             $alltypes{$newtype} =~ s/`//g; 
20158:             my $position = $env{'form.addinststatus_pos'};
20159:             $position =~ s/\D+//g;
20160:             if ($position ne '') {
20161:                 $allpos[$position] = $newtype;
20162:             }
20163:         }
20164:     }
20165:     my @orderedstatus;
20166:     foreach my $type (@allpos) {
20167:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
20168:             push(@orderedstatus,$type);
20169:         }
20170:     }
20171:     foreach my $type (keys(%alltypes)) {
20172:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
20173:             delete($alltypes{$type});
20174:         }
20175:     }
20176:     $defaults_hash{'inststatus'} = {
20177:                                      inststatustypes => \%alltypes,
20178:                                      inststatusorder => \@orderedstatus,
20179:                                      inststatusguest => \@inststatusguest,
20180:                                    };
20181:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
20182:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
20183:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
20184:         }
20185:     }
20186:     if ($currorder ne join(',',@orderedstatus)) {
20187:         $changes{'inststatus'}{'inststatusorder'} = 1;
20188:     }
20189:     my $newtitles;
20190:     foreach my $item (@orderedstatus) {
20191:         $newtitles .= $alltypes{$item}.',';
20192:     }
20193:     $newtitles =~ s/,$//;
20194:     if ($currtitles ne $newtitles) {
20195:         $changes{'inststatus'}{'inststatustypes'} = 1;
20196:     }
20197:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
20198:                                              $dom);
20199:     if ($putresult eq 'ok') {
20200:         if (keys(%changes) > 0) {
20201:             $resulttext = &mt('Changes made:').'<ul>';
20202:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
20203:             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";
20204:             foreach my $item (sort(keys(%changes))) {
20205:                 if ($item eq 'inststatus') {
20206:                     if (ref($changes{'inststatus'}) eq 'HASH') {
20207:                         if (@orderedstatus) {
20208:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
20209:                             foreach my $type (@orderedstatus) { 
20210:                                 $resulttext .= $alltypes{$type}.', ';
20211:                             }
20212:                             $resulttext =~ s/, $//;
20213:                             $resulttext .= '</li>';
20214:                         } else {
20215:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
20216:                         }
20217:                     }
20218:                 } elsif ($item eq 'unamemap_rule') {
20219:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20220:                         my @rulenames;
20221:                         if (ref($unamemaprules) eq 'HASH') {
20222:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
20223:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
20224:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
20225:                                 }
20226:                             }
20227:                         }
20228:                         if (@rulenames) {
20229:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
20230:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
20231:                                            '</li>';
20232:                         } else {
20233:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
20234:                         }
20235:                     } else {
20236:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
20237:                     }
20238:                 } else {
20239:                     my $value = $env{'form.'.$item};
20240:                     if ($value eq '') {
20241:                         $value = &mt('none');
20242:                     } elsif ($item eq 'auth_def') {
20243:                         my %authnames = &authtype_names();
20244:                         my %shortauth = (
20245:                                           internal   => 'int',
20246:                                           krb4       => 'krb4',
20247:                                           krb5       => 'krb5',
20248:                                           localauth  => 'loc',
20249:                                           lti        => 'lti',
20250:                         );
20251:                         $value = $authnames{$shortauth{$value}};
20252:                     }
20253:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
20254:                     $mailmsgtext .= "$title->{$item} set to $value\n";
20255:                     if ($item eq 'portal_def') {
20256:                         if ($env{'form.'.$item} ne '') {
20257:                             foreach my $field ('email','web') {
20258:                                 $value = $env{'form.'.$item.'_'.$field};
20259:                                 if ($value) {
20260:                                     $value = &mt('Yes');
20261:                                 } else {
20262:                                     $value = &mt('No');
20263:                                 }
20264:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
20265:                             }
20266:                         }
20267:                     }
20268:                 }
20269:             }
20270:             $resulttext .= '</ul>';
20271:             $mailmsgtext .= "\n";
20272:             my $cachetime = 24*60*60;
20273:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20274:             if (ref($lastactref) eq 'HASH') {
20275:                 $lastactref->{'domdefaults'} = 1;
20276:             }
20277:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
20278:                 my $notify = 1;
20279:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
20280:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
20281:                         $notify = 0;
20282:                     }
20283:                 }
20284:                 if ($notify) {
20285:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
20286:                                                "LON-CAPA Domain Settings Change - $dom",
20287:                                                $mailmsgtext);
20288:                 }
20289:             }
20290:         } else {
20291:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
20292:         }
20293:     } else {
20294:         $resulttext = '<span class="LC_error">'.
20295:             &mt('An error occurred: [_1]',$putresult).'</span>';
20296:     }
20297:     if (@errors > 0) {
20298:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
20299:         foreach my $item (@errors) {
20300:             $resulttext .= ' "'.$title->{$item}.'",';
20301:         }
20302:         $resulttext =~ s/,$//;
20303:     }
20304:     return $resulttext;
20305: }
20306: 
20307: sub modify_scantron {
20308:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20309:     my ($resulttext,%confhash,%changes,$errors);
20310:     my $custom = 'custom.tab';
20311:     my $default = 'default.tab';
20312:     my $servadm = $r->dir_config('lonAdmEMail');
20313:     my ($configuserok,$author_ok,$switchserver) =
20314:         &config_check($dom,$confname,$servadm);
20315:     if ($env{'form.scantronformat.filename'} ne '') {
20316:         my $error;
20317:         if ($configuserok eq 'ok') {
20318:             if ($switchserver) {
20319:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
20320:             } else {
20321:                 if ($author_ok eq 'ok') {
20322:                     my $modified = [];
20323:                     my ($result,$scantronurl) =
20324:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
20325:                                                                 $confname,'scantron','','',$custom,
20326:                                                                 $modified);
20327:                     if ($result eq 'ok') {
20328:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
20329:                         $changes{'scantronformat'} = 1;
20330:                         &update_modify_urls($r,$modified);
20331:                     } else {
20332:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
20333:                     }
20334:                 } else {
20335:                     $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);
20336:                 }
20337:             }
20338:         } else {
20339:             $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);
20340:         }
20341:         if ($error) {
20342:             &Apache::lonnet::logthis($error);
20343:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
20344:         }
20345:     }
20346:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20347:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
20348:             if ($env{'form.scantronformat_del'}) {
20349:                 $confhash{'scantron'}{'scantronformat'} = '';
20350:                 $changes{'scantronformat'} = 1;
20351:             } else {
20352:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
20353:             }
20354:         }
20355:     }
20356:     my @options = ('hdr','pad','rem');
20357:     my @fields = &scantroncsv_fields();
20358:     my %titles = &scantronconfig_titles();
20359:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
20360:     my ($newdat,$currdat,%newcol,%currcol);
20361:     if (grep(/^dat$/,@formats)) {
20362:         $confhash{'scantron'}{config}{dat} = 1;
20363:         $newdat = 1;
20364:     } else {
20365:         $newdat = 0;
20366:     }
20367:     if (grep(/^csv$/,@formats)) {
20368:         my %bynum;
20369:         foreach my $field (@fields) {
20370:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
20371:                 my $posscol = $1;
20372:                 if (($posscol < 20) && (!$bynum{$posscol})) {
20373:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
20374:                     $bynum{$posscol} = $field;
20375:                     $newcol{$field} = $posscol;
20376:                 }
20377:             }
20378:         }
20379:         if (keys(%newcol)) {
20380:             foreach my $option (@options) {
20381:                 if ($env{'form.scantroncsv_'.$option}) {
20382:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
20383:                 }
20384:             }
20385:         }
20386:     }
20387:     $currdat = 1;
20388:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20389:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
20390:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
20391:                 $currdat = 0;
20392:             }
20393:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20394:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20395:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
20396:                 }
20397:             }
20398:         }
20399:     }
20400:     if ($currdat != $newdat) {
20401:         $changes{'config'} = 1;
20402:     } else {
20403:         foreach my $field (@fields) {
20404:             if ($currcol{$field} ne '') {
20405:                 if ($currcol{$field} ne $newcol{$field}) {
20406:                     $changes{'config'} = 1;
20407:                     last;
20408:                 }
20409:             } elsif ($newcol{$field} ne '') {
20410:                 $changes{'config'} = 1;
20411:                 last;
20412:             }
20413:         }
20414:     }
20415:     if (keys(%confhash) > 0) {
20416:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
20417:                                                  $dom);
20418:         if ($putresult eq 'ok') {
20419:             if (keys(%changes) > 0) {
20420:                 if (ref($confhash{'scantron'}) eq 'HASH') {
20421:                     $resulttext = &mt('Changes made:').'<ul>';
20422:                     if ($changes{'scantronformat'}) {
20423:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
20424:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
20425:                         } else {
20426:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
20427:                         }
20428:                     }
20429:                     if ($changes{'config'}) {
20430:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
20431:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
20432:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
20433:                             }
20434:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20435:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20436:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
20437:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
20438:                                         foreach my $field (@fields) {
20439:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
20440:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
20441:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
20442:                                             }
20443:                                         }
20444:                                         $resulttext .= '</ul></li>';
20445:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
20446:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
20447:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
20448:                                                 foreach my $option (@options) {
20449:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
20450:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
20451:                                                     }
20452:                                                 }
20453:                                                 $resulttext .= '</ul></li>';
20454:                                             }
20455:                                         }
20456:                                     }
20457:                                 }
20458:                             }
20459:                         } else {
20460:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
20461:                         }
20462:                     }
20463:                     $resulttext .= '</ul>';
20464:                 } else {
20465:                     $resulttext = &mt('Changes made to bubblesheet format file.');
20466:                 }
20467:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
20468:                 if (ref($lastactref) eq 'HASH') {
20469:                     $lastactref->{'domainconfig'} = 1;
20470:                 }
20471:             } else {
20472:                 $resulttext = &mt('No changes made to bubblesheet format settings');
20473:             }
20474:         } else {
20475:             $resulttext = '<span class="LC_error">'.
20476:                 &mt('An error occurred: [_1]',$putresult).'</span>';
20477:         }
20478:     } else {
20479:         $resulttext = &mt('No changes made to bubblesheet format file'); 
20480:     }
20481:     if ($errors) {
20482:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
20483:                        $errors.'</ul></p>';
20484:     }
20485:     return $resulttext;
20486: }
20487: 
20488: sub modify_coursecategories {
20489:     my ($dom,$lastactref,%domconfig) = @_;
20490:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
20491:         $cathash);
20492:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
20493:     my @catitems = ('unauth','auth');
20494:     my @cattypes = ('std','domonly','codesrch','none');
20495:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20496:         $cathash = $domconfig{'coursecategories'}{'cats'};
20497:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
20498:             $changes{'togglecats'} = 1;
20499:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
20500:         }
20501:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
20502:             $changes{'categorize'} = 1;
20503:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
20504:         }
20505:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
20506:             $changes{'togglecatscomm'} = 1;
20507:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
20508:         }
20509:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
20510:             $changes{'categorizecomm'} = 1;
20511:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
20512: 
20513:         }
20514:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
20515:             $changes{'togglecatsplace'} = 1;
20516:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
20517:         }
20518:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
20519:             $changes{'categorizeplace'} = 1;
20520:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
20521:         }
20522:         foreach my $item (@catitems) {
20523:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20524:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
20525:                     $changes{$item} = 1;
20526:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20527:                 }
20528:             }
20529:         }
20530:     } else {
20531:         $changes{'togglecats'} = 1;
20532:         $changes{'categorize'} = 1;
20533:         $changes{'togglecatscomm'} = 1;
20534:         $changes{'categorizecomm'} = 1;
20535:         $changes{'togglecatsplace'} = 1;
20536:         $changes{'categorizeplace'} = 1;
20537:         $domconfig{'coursecategories'} = {
20538:                                              togglecats => $env{'form.togglecats'},
20539:                                              categorize => $env{'form.categorize'},
20540:                                              togglecatscomm => $env{'form.togglecatscomm'},
20541:                                              categorizecomm => $env{'form.categorizecomm'},
20542:                                              togglecatsplace => $env{'form.togglecatsplace'},
20543:                                              categorizeplace => $env{'form.categorizeplace'},
20544:                                          };
20545:         foreach my $item (@catitems) {
20546:             if ($env{'form.coursecat_'.$item} ne 'std') {
20547:                 $changes{$item} = 1;
20548:             }
20549:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20550:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20551:             }
20552:         }
20553:     }
20554:     if (ref($cathash) eq 'HASH') {
20555:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
20556:             push (@deletecategory,'instcode::0');
20557:         }
20558:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
20559:             push(@deletecategory,'communities::0');
20560:         }
20561:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
20562:             push(@deletecategory,'placement::0');
20563:         }
20564:     }
20565:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
20566:     if (ref($cathash) eq 'HASH') {
20567:         if (@deletecategory > 0) {
20568:             #FIXME Need to remove category from all courses using a deleted category 
20569:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
20570:             foreach my $item (@deletecategory) {
20571:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
20572:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
20573:                     $deletions{$item} = 1;
20574:                     &recurse_cat_deletes($item,$cathash,\%deletions);
20575:                 }
20576:             }
20577:         }
20578:         foreach my $item (keys(%{$cathash})) {
20579:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20580:             if ($cathash->{$item} ne $env{'form.'.$item}) {
20581:                 $reorderings{$item} = 1;
20582:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
20583:             }
20584:             if ($env{'form.addcategory_name_'.$item} ne '') {
20585:                 my $newcat = $env{'form.addcategory_name_'.$item};
20586:                 my $newdepth = $depth+1;
20587:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20588:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
20589:                 $adds{$newitem} = 1; 
20590:             }
20591:             if ($env{'form.subcat_'.$item} ne '') {
20592:                 my $newcat = $env{'form.subcat_'.$item};
20593:                 my $newdepth = $depth+1;
20594:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20595:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
20596:                 $adds{$newitem} = 1;
20597:             }
20598:         }
20599:     }
20600:     if ($env{'form.instcode'} eq '1') {
20601:         if (ref($cathash) eq 'HASH') {
20602:             my $newitem = 'instcode::0';
20603:             if ($cathash->{$newitem} eq '') {  
20604:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20605:                 $adds{$newitem} = 1;
20606:             }
20607:         } else {
20608:             my $newitem = 'instcode::0';
20609:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20610:             $adds{$newitem} = 1;
20611:         }
20612:     }
20613:     if ($env{'form.communities'} eq '1') {
20614:         if (ref($cathash) eq 'HASH') {
20615:             my $newitem = 'communities::0';
20616:             if ($cathash->{$newitem} eq '') {
20617:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20618:                 $adds{$newitem} = 1;
20619:             }
20620:         } else {
20621:             my $newitem = 'communities::0';
20622:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20623:             $adds{$newitem} = 1;
20624:         }
20625:     }
20626:     if ($env{'form.placement'} eq '1') {
20627:         if (ref($cathash) eq 'HASH') {
20628:             my $newitem = 'placement::0';
20629:             if ($cathash->{$newitem} eq '') {
20630:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20631:                 $adds{$newitem} = 1;
20632:             }
20633:         } else {
20634:             my $newitem = 'placement::0';
20635:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20636:             $adds{$newitem} = 1;
20637:         }
20638:     }
20639:     if ($env{'form.addcategory_name'} ne '') {
20640:         if (($env{'form.addcategory_name'} ne 'instcode') &&
20641:             ($env{'form.addcategory_name'} ne 'communities') &&
20642:             ($env{'form.addcategory_name'} ne 'placement')) {
20643:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
20644:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
20645:             $adds{$newitem} = 1;
20646:         }
20647:     }
20648:     my $putresult;
20649:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20650:         if (keys(%deletions) > 0) {
20651:             foreach my $key (keys(%deletions)) {
20652:                 if ($predelallitems{$key} ne '') {
20653:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
20654:                 }
20655:             }
20656:         }
20657:         my (@chkcats,@chktrails,%chkallitems);
20658:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
20659:         if (ref($chkcats[0]) eq 'ARRAY') {
20660:             my $depth = 0;
20661:             my $chg = 0;
20662:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
20663:                 my $name = $chkcats[0][$i];
20664:                 my $item;
20665:                 if ($name eq '') {
20666:                     $chg ++;
20667:                 } else {
20668:                     $item = &escape($name).'::0';
20669:                     if ($chg) {
20670:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
20671:                     }
20672:                     $depth ++; 
20673:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
20674:                     $depth --;
20675:                 }
20676:             }
20677:         }
20678:     }
20679:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20680:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
20681:         if ($putresult eq 'ok') {
20682:             my %title = (
20683:                          togglecats     => 'Show/Hide a course in catalog',
20684:                          categorize     => 'Assign a category to a course',
20685:                          togglecatscomm => 'Show/Hide a community in catalog',
20686:                          categorizecomm => 'Assign a category to a community',
20687:                         );
20688:             my %level = (
20689:                          dom  => 'set in Domain ("Modify Course/Community")',
20690:                          crs  => 'set in Course ("Course Configuration")',
20691:                          comm => 'set in Community ("Community Configuration")',
20692:                          none     => 'No catalog',
20693:                          std      => 'Standard catalog',
20694:                          domonly  => 'Domain-only catalog',
20695:                          codesrch => 'Code search form',
20696:                         );
20697:             $resulttext = &mt('Changes made:').'<ul>';
20698:             if ($changes{'togglecats'}) {
20699:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
20700:             }
20701:             if ($changes{'categorize'}) {
20702:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
20703:             }
20704:             if ($changes{'togglecatscomm'}) {
20705:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
20706:             }
20707:             if ($changes{'categorizecomm'}) {
20708:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
20709:             }
20710:             if ($changes{'unauth'}) {
20711:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
20712:             }
20713:             if ($changes{'auth'}) {
20714:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
20715:             }
20716:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20717:                 my $cathash;
20718:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20719:                     $cathash = $domconfig{'coursecategories'}{'cats'};
20720:                 } else {
20721:                     $cathash = {};
20722:                 } 
20723:                 my (@cats,@trails,%allitems);
20724:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
20725:                 if (keys(%deletions) > 0) {
20726:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
20727:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
20728:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
20729:                     }
20730:                     $resulttext .= '</ul></li>';
20731:                 }
20732:                 if (keys(%reorderings) > 0) {
20733:                     my %sort_by_trail;
20734:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
20735:                     foreach my $key (keys(%reorderings)) {
20736:                         if ($allitems{$key} ne '') {
20737:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20738:                         }
20739:                     }
20740:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20741:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20742:                     }
20743:                     $resulttext .= '</ul></li>';
20744:                 }
20745:                 if (keys(%adds) > 0) {
20746:                     my %sort_by_trail;
20747:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
20748:                     foreach my $key (keys(%adds)) {
20749:                         if ($allitems{$key} ne '') {
20750:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20751:                         }
20752:                     }
20753:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20754:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20755:                     }
20756:                     $resulttext .= '</ul></li>';
20757:                 }
20758:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20759:                 if (ref($lastactref) eq 'HASH') {
20760:                     $lastactref->{'cats'} = 1;
20761:                 }
20762:             }
20763:             $resulttext .= '</ul>';
20764:             if ($changes{'unauth'} || $changes{'auth'}) {
20765:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20766:                 if ($changes{'auth'}) {
20767:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20768:                 }
20769:                 if ($changes{'unauth'}) {
20770:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20771:                 }
20772:                 my $cachetime = 24*60*60;
20773:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20774:                 if (ref($lastactref) eq 'HASH') {
20775:                     $lastactref->{'domdefaults'} = 1;
20776:                 }
20777:             }
20778:         } else {
20779:             $resulttext = '<span class="LC_error">'.
20780:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20781:         }
20782:     } else {
20783:         $resulttext = &mt('No changes made to course and community categories');
20784:     }
20785:     return $resulttext;
20786: }
20787: 
20788: sub modify_serverstatuses {
20789:     my ($dom,%domconfig) = @_;
20790:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20791:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20792:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20793:     }
20794:     my @pages = &serverstatus_pages();
20795:     foreach my $type (@pages) {
20796:         $newserverstatus{$type}{'namedusers'} = '';
20797:         $newserverstatus{$type}{'machines'} = '';
20798:         if (defined($env{'form.'.$type.'_namedusers'})) {
20799:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20800:             my @okusers;
20801:             foreach my $user (@users) {
20802:                 my ($uname,$udom) = split(/:/,$user);
20803:                 if (($udom =~ /^$match_domain$/) &&   
20804:                     (&Apache::lonnet::domain($udom)) &&
20805:                     ($uname =~ /^$match_username$/)) {
20806:                     if (!grep(/^\Q$user\E/,@okusers)) {
20807:                         push(@okusers,$user);
20808:                     }
20809:                 }
20810:             }
20811:             if (@okusers > 0) {
20812:                  @okusers = sort(@okusers);
20813:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20814:             }
20815:         }
20816:         if (defined($env{'form.'.$type.'_machines'})) {
20817:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20818:             my @okmachines;
20819:             foreach my $ip (@machines) {
20820:                 my @parts = split(/\./,$ip);
20821:                 next if (@parts < 4);
20822:                 my $badip = 0;
20823:                 for (my $i=0; $i<4; $i++) {
20824:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20825:                         $badip = 1;
20826:                         last;
20827:                     }
20828:                 }
20829:                 if (!$badip) {
20830:                     push(@okmachines,$ip);     
20831:                 }
20832:             }
20833:             @okmachines = sort(@okmachines);
20834:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20835:         }
20836:     }
20837:     my %serverstatushash =  (
20838:                                 serverstatuses => \%newserverstatus,
20839:                             );
20840:     foreach my $type (@pages) {
20841:         foreach my $setting ('namedusers','machines') {
20842:             my (@current,@new);
20843:             if (ref($currserverstatus{$type}) eq 'HASH') {
20844:                 if ($currserverstatus{$type}{$setting} ne '') { 
20845:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20846:                 }
20847:             }
20848:             if ($newserverstatus{$type}{$setting} ne '') {
20849:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20850:             }
20851:             if (@current > 0) {
20852:                 if (@new > 0) {
20853:                     foreach my $item (@current) {
20854:                         if (!grep(/^\Q$item\E$/,@new)) {
20855:                             $changes{$type}{$setting} = 1;
20856:                             last;
20857:                         }
20858:                     }
20859:                     foreach my $item (@new) {
20860:                         if (!grep(/^\Q$item\E$/,@current)) {
20861:                             $changes{$type}{$setting} = 1;
20862:                             last;
20863:                         }
20864:                     }
20865:                 } else {
20866:                     $changes{$type}{$setting} = 1;
20867:                 }
20868:             } elsif (@new > 0) {
20869:                 $changes{$type}{$setting} = 1;
20870:             }
20871:         }
20872:     }
20873:     if (keys(%changes) > 0) {
20874:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20875:         my $putresult = &Apache::lonnet::put_dom('configuration',
20876:                                                  \%serverstatushash,$dom);
20877:         if ($putresult eq 'ok') {
20878:             $resulttext .= &mt('Changes made:').'<ul>';
20879:             foreach my $type (@pages) {
20880:                 if (ref($changes{$type}) eq 'HASH') {
20881:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20882:                     if ($changes{$type}{'namedusers'}) {
20883:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20884:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20885:                         } else {
20886:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20887:                         }
20888:                     }
20889:                     if ($changes{$type}{'machines'}) {
20890:                         if ($newserverstatus{$type}{'machines'} eq '') {
20891:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20892:                         } else {
20893:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20894:                         }
20895: 
20896:                     }
20897:                     $resulttext .= '</ul></li>';
20898:                 }
20899:             }
20900:             $resulttext .= '</ul>';
20901:         } else {
20902:             $resulttext = '<span class="LC_error">'.
20903:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20904: 
20905:         }
20906:     } else {
20907:         $resulttext = &mt('No changes made to access to server status pages');
20908:     }
20909:     return $resulttext;
20910: }
20911: 
20912: sub modify_helpsettings {
20913:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20914:     my ($resulttext,$errors,%changes,%helphash);
20915:     my %defaultchecked = ('submitbugs' => 'on');
20916:     my @offon = ('off','on');
20917:     my @toggles = ('submitbugs');
20918:     my %current = ('submitbugs' => '',
20919:                    'adhoc'      => {},
20920:                   );
20921:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20922:         %current = %{$domconfig{'helpsettings'}};
20923:     }
20924:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20925:     foreach my $item (@toggles) {
20926:         if ($defaultchecked{$item} eq 'on') { 
20927:             if ($current{$item} eq '') {
20928:                 if ($env{'form.'.$item} eq '0') {
20929:                     $changes{$item} = 1;
20930:                 }
20931:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20932:                 $changes{$item} = 1;
20933:             }
20934:         } elsif ($defaultchecked{$item} eq 'off') {
20935:             if ($current{$item} eq '') {
20936:                 if ($env{'form.'.$item} eq '1') {
20937:                     $changes{$item} = 1;
20938:                 }
20939:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20940:                 $changes{$item} = 1;
20941:             }
20942:         }
20943:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20944:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20945:         }
20946:     }
20947:     my $maxnum = $env{'form.helproles_maxnum'};
20948:     my $confname = $dom.'-domainconfig';
20949:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20950:     my (@allpos,%newsettings,%changedprivs,$newrole);
20951:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20952:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20953:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20954:     my %lt = &Apache::lonlocal::texthash(
20955:                     s      => 'system',
20956:                     d      => 'domain',
20957:                     order  => 'Display order',
20958:                     access => 'Role usage',
20959:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20960:                     dh     => 'All with domain helpdesk role',
20961:                     da     => 'All with domain helpdesk assistant role',
20962:                     none   => 'None',
20963:                     status => 'Determined based on institutional status',
20964:                     inc    => 'Include all, but exclude specific personnel',
20965:                     exc    => 'Exclude all, but include specific personnel',
20966:     );
20967:     for (my $num=0; $num<=$maxnum; $num++) {
20968:         my ($prefix,$identifier,$rolename,%curr);
20969:         if ($num == $maxnum) {
20970:             next unless ($env{'form.newcusthelp'} == $maxnum);
20971:             $identifier = 'custhelp'.$num;
20972:             $prefix = 'helproles_'.$num;
20973:             $rolename = $env{'form.custhelpname'.$num};
20974:             $rolename=~s/[^A-Za-z0-9]//gs;
20975:             next if ($rolename eq '');
20976:             next if (exists($existing{'rolesdef_'.$rolename}));
20977:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20978:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20979:                                                      $newprivs{'c'},$confname,$dom);
20980:             if ($result ne 'ok') {
20981:                 $errors .= '<li><span class="LC_error">'.
20982:                            &mt('An error occurred storing the new custom role: [_1]',
20983:                            $result).'</span></li>';
20984:                 next;
20985:             } else {
20986:                 $changedprivs{$rolename} = \%newprivs;
20987:                 $newrole = $rolename;
20988:             }
20989:         } else {
20990:             $prefix = 'helproles_'.$num;
20991:             $rolename = $env{'form.'.$prefix};
20992:             next if ($rolename eq '');
20993:             next unless (exists($existing{'rolesdef_'.$rolename}));
20994:             $identifier = 'custhelp'.$num;
20995:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20996:             my %currprivs;
20997:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20998:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20999:             foreach my $level ('c','d','s') {
21000:                 if ($newprivs{$level} ne $currprivs{$level}) {
21001:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
21002:                                                              $newprivs{'c'},$confname,$dom);
21003:                     if ($result ne 'ok') {
21004:                         $errors .= '<li><span class="LC_error">'.
21005:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
21006:                                        $rolename,$result).'</span></li>';
21007:                     } else {
21008:                         $changedprivs{$rolename} = \%newprivs;
21009:                     }
21010:                     last;
21011:                 }
21012:             }
21013:             if (ref($current{'adhoc'}) eq 'HASH') {
21014:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21015:                     %curr = %{$current{'adhoc'}{$rolename}};
21016:                 }
21017:             }
21018:         }
21019:         my $newpos = $env{'form.'.$prefix.'_pos'};
21020:         $newpos =~ s/\D+//g;
21021:         $allpos[$newpos] = $rolename;
21022:         my $newdesc = $env{'form.'.$prefix.'_desc'};
21023:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
21024:         if ($curr{'desc'}) {
21025:             if ($curr{'desc'} ne $newdesc) {
21026:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
21027:                 $newsettings{$rolename}{'desc'} = $newdesc;
21028:             }
21029:         } elsif ($newdesc ne '') {
21030:             $changes{'customrole'}{$rolename}{'desc'} = 1;
21031:             $newsettings{$rolename}{'desc'} = $newdesc;
21032:         }
21033:         my $access = $env{'form.'.$prefix.'_access'};
21034:         if (grep(/^\Q$access\E$/,@accesstypes)) {
21035:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
21036:             if ($access eq 'status') {
21037:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
21038:                 if (scalar(@statuses) == 0) {
21039:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
21040:                 } else {
21041:                     my (@shownstatus,$numtypes);
21042:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21043:                     if (ref($types) eq 'ARRAY') {
21044:                         $numtypes = scalar(@{$types});
21045:                         foreach my $type (sort(@statuses)) {
21046:                             if ($type eq 'default') {
21047:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21048:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
21049:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21050:                                 push(@shownstatus,$usertypes->{$type});
21051:                             }
21052:                         }
21053:                     }
21054:                     if (grep(/^default$/,@statuses)) {
21055:                         push(@shownstatus,$othertitle);
21056:                     }
21057:                     if (scalar(@shownstatus) == 1+$numtypes) {
21058:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
21059:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
21060:                     } else {
21061:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
21062:                         if (ref($curr{'status'}) eq 'ARRAY') {
21063:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21064:                             if (@diffs) {
21065:                                 $changes{'customrole'}{$rolename}{$access} = 1;
21066:                             }
21067:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21068:                             $changes{'customrole'}{$rolename}{$access} = 1;
21069:                         }
21070:                     }
21071:                 }
21072:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
21073:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
21074:                 my @newspecstaff;
21075:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21076:                 foreach my $person (sort(@personnel)) {
21077:                     if ($domhelpdesk{$person}) {
21078:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
21079:                     }
21080:                 }
21081:                 if (ref($curr{$access}) eq 'ARRAY') {
21082:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21083:                     if (@diffs) {
21084:                         $changes{'customrole'}{$rolename}{$access} = 1;
21085:                     }
21086:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21087:                     $changes{'customrole'}{$rolename}{$access} = 1;
21088:                 }
21089:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21090:                     my ($uname,$udom) = split(/:/,$person);
21091:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
21092:                 }
21093:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
21094:             }
21095:         } else {
21096:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
21097:         }
21098:         unless ($curr{'access'} eq $access) {
21099:             $changes{'customrole'}{$rolename}{'access'} = 1;
21100:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
21101:         }
21102:     }
21103:     if (@allpos > 0) {
21104:         my $idx = 0;
21105:         foreach my $rolename (@allpos) {
21106:             if ($rolename ne '') {
21107:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
21108:                 if (ref($current{'adhoc'}) eq 'HASH') {
21109:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21110:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
21111:                             $changes{'customrole'}{$rolename}{'order'} = 1;
21112:                             $newsettings{$rolename}{'order'} = $idx+1;
21113:                         }
21114:                     }
21115:                 }
21116:                 $idx ++;
21117:             }
21118:         }
21119:     }
21120:     my $putresult;
21121:     if (keys(%changes) > 0) {
21122:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
21123:         if ($putresult eq 'ok') {
21124:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
21125:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
21126:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
21127:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
21128:                 }
21129:             }
21130:             my $cachetime = 24*60*60;
21131:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21132:             if (ref($lastactref) eq 'HASH') {
21133:                 $lastactref->{'domdefaults'} = 1;
21134:             }
21135:         } else {
21136:             $errors .= '<li><span class="LC_error">'.
21137:                        &mt('An error occurred storing the settings: [_1]',
21138:                            $putresult).'</span></li>';
21139:         }
21140:     }
21141:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
21142:         $resulttext = &mt('Changes made:').'<ul>';
21143:         my (%shownprivs,@levelorder);
21144:         @levelorder = ('c','d','s');
21145:         if ((keys(%changes)) && ($putresult eq 'ok')) {
21146:             foreach my $item (sort(keys(%changes))) {
21147:                 if ($item eq 'submitbugs') {
21148:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
21149:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
21150:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
21151:                 } elsif ($item eq 'customrole') {
21152:                     if (ref($changes{'customrole'}) eq 'HASH') {
21153:                         my @keyorder = ('order','desc','access','status','exc','inc');
21154:                         my %keytext = &Apache::lonlocal::texthash(
21155:                                                                    order  => 'Order',
21156:                                                                    desc   => 'Role description',
21157:                                                                    access => 'Role usage',
21158:                                                                    status => 'Allowed institutional types',
21159:                                                                    exc    => 'Allowed personnel',
21160:                                                                    inc    => 'Disallowed personnel',
21161:                         );
21162:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
21163:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
21164:                                 if ($role eq $newrole) {
21165:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
21166:                                                               $role).'<ul>';
21167:                                 } else {
21168:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21169:                                                               $role).'<ul>';
21170:                                 }
21171:                                 foreach my $key (@keyorder) {
21172:                                     if ($changes{'customrole'}{$role}{$key}) {
21173:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
21174:                                                                   $keytext{$key},$newsettings{$role}{$key}).
21175:                                                        '</li>';
21176:                                     }
21177:                                 }
21178:                                 if (ref($changedprivs{$role}) eq 'HASH') {
21179:                                     $shownprivs{$role} = 1;
21180:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
21181:                                     foreach my $level (@levelorder) {
21182:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21183:                                             next if ($item eq '');
21184:                                             my ($priv) = split(/\&/,$item,2);
21185:                                             if (&Apache::lonnet::plaintext($priv)) {
21186:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21187:                                                 unless ($level eq 'c') {
21188:                                                     $resulttext .= ' ('.$lt{$level}.')';
21189:                                                 }
21190:                                                 $resulttext .= '</li>';
21191:                                             }
21192:                                         }
21193:                                     }
21194:                                     $resulttext .= '</ul>';
21195:                                 }
21196:                                 $resulttext .= '</ul></li>';
21197:                             }
21198:                         }
21199:                     }
21200:                 }
21201:             }
21202:         }
21203:         if (keys(%changedprivs)) {
21204:             foreach my $role (sort(keys(%changedprivs))) {
21205:                 unless ($shownprivs{$role}) {
21206:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21207:                                               $role).'<ul>'.
21208:                                    '<li>'.&mt('Privileges set to :').'<ul>';
21209:                     foreach my $level (@levelorder) {
21210:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21211:                             next if ($item eq '');
21212:                             my ($priv) = split(/\&/,$item,2);
21213:                             if (&Apache::lonnet::plaintext($priv)) {
21214:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21215:                                 unless ($level eq 'c') {
21216:                                     $resulttext .= ' ('.$lt{$level}.')';
21217:                                 }
21218:                                 $resulttext .= '</li>';
21219:                             }
21220:                         }
21221:                     }
21222:                     $resulttext .= '</ul></li></ul></li>';
21223:                 }
21224:             }
21225:         }
21226:         $resulttext .= '</ul>';
21227:     } else {
21228:         $resulttext = &mt('No changes made to help settings');
21229:     }
21230:     if ($errors) {
21231:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
21232:                                     $errors.'</ul>';
21233:     }
21234:     return $resulttext;
21235: }
21236: 
21237: sub modify_coursedefaults {
21238:     my ($dom,$lastactref,%domconfig) = @_;
21239:     my ($resulttext,$errors,%changes,%defaultshash);
21240:     my %defaultchecked = (
21241:                            'canuse_pdfforms' => 'off',
21242:                            'uselcmath'       => 'on',
21243:                            'usejsme'         => 'on',
21244:                            'inline_chem'     => 'on',
21245:                            'ltiauth'         => 'off',
21246:                          );
21247:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
21248:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
21249:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
21250:                    'coursequota_official','coursequota_unofficial','coursequota_community',
21251:                    'coursequota_textbook','coursequota_placement','mysqltables_official',
21252:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook',
21253:                    'mysqltables_placement');
21254:     my @types = ('official','unofficial','community','textbook','placement');
21255:     my %staticdefaults = (
21256:                            anonsurvey_threshold => 10,
21257:                            uploadquota          => 500,
21258:                            coursequota          => 20,
21259:                            postsubmit           => 60,
21260:                            mysqltables          => 172800,
21261:                            domexttool           => 1,
21262:                            crsauthor            => 1,
21263:                            crseditors           => ['edit','xml'],
21264:                          );
21265:     my %texoptions = (
21266:                         MathJax  => 'MathJax',
21267:                         mimetex  => &mt('Convert to Images'),
21268:                         tth      => &mt('TeX to HTML'),
21269:                      );
21270: 
21271:     my @editors = ('edit','xml','daxe');
21272:     my %editornames = &crseditor_titles();
21273: 
21274:     $defaultshash{'coursedefaults'} = {};
21275: 
21276:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
21277:         if ($domconfig{'coursedefaults'} eq '') {
21278:             $domconfig{'coursedefaults'} = {};
21279:         }
21280:     }
21281: 
21282:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
21283:         foreach my $item (@toggles) {
21284:             if ($defaultchecked{$item} eq 'on') {
21285:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21286:                     ($env{'form.'.$item} eq '0')) {
21287:                     $changes{$item} = 1;
21288:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21289:                     $changes{$item} = 1;
21290:                 }
21291:             } elsif ($defaultchecked{$item} eq 'off') {
21292:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21293:                     ($env{'form.'.$item} eq '1')) {
21294:                     $changes{$item} = 1;
21295:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21296:                     $changes{$item} = 1;
21297:                 }
21298:             }
21299:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
21300:         }
21301:         foreach my $item (@numbers) {
21302:             my ($currdef,$newdef);
21303:             $newdef = $env{'form.'.$item};
21304:             if ($item eq 'anonsurvey_threshold') {
21305:                 $currdef = $domconfig{'coursedefaults'}{$item};
21306:                 $newdef =~ s/\D//g;
21307:                 if ($newdef eq '' || $newdef < 1) {
21308:                     $newdef = 1;
21309:                 }
21310:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
21311:             } else {
21312:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
21313:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
21314:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
21315:                 }
21316:                 $newdef =~ s/[^\w.\-]//g;
21317:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
21318:             }
21319:             if ($currdef ne $newdef) {
21320:                 if ($item eq 'anonsurvey_threshold') {
21321:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
21322:                         $changes{$item} = 1;
21323:                     }
21324:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
21325:                     my $setting = $1;
21326:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
21327:                         $changes{$setting} = 1;
21328:                     }
21329:                 }
21330:             }
21331:         }
21332:         my $texengine;
21333:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
21334:             $texengine = $env{'form.texengine'};
21335:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
21336:             if ($currdef eq '') {
21337:                 unless ($texengine eq $Apache::lonnet::deftex) {
21338:                     $changes{'texengine'} = 1;
21339:                 }
21340:             } elsif ($currdef ne $texengine) {
21341:                 $changes{'texengine'} = 1;
21342:             }
21343:         }
21344:         if ($texengine ne '') {
21345:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
21346:         }
21347:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
21348:         my @currclonecode;
21349:         if (ref($currclone) eq 'HASH') {
21350:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
21351:                 @currclonecode = @{$currclone->{'instcode'}};
21352:             }
21353:         }
21354:         my $newclone;
21355:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
21356:             $newclone = $env{'form.canclone'};
21357:         }
21358:         if ($newclone eq 'instcode') {
21359:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
21360:             my (%codedefaults,@code_order,@clonecode);
21361:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
21362:                                                     \@code_order);
21363:             foreach my $item (@code_order) {
21364:                 if (grep(/^\Q$item\E$/,@newcodes)) {
21365:                     push(@clonecode,$item);
21366:                 }
21367:             }
21368:             if (@clonecode) {
21369:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
21370:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
21371:                 if (@diffs) {
21372:                     $changes{'canclone'} = 1;
21373:                 }
21374:             } else {
21375:                 $newclone eq '';
21376:             }
21377:         } elsif ($newclone ne '') {
21378:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
21379:         }
21380:         if ($newclone ne $currclone) {
21381:             $changes{'canclone'} = 1;
21382:         }
21383:         my %credits;
21384:         foreach my $type (@types) {
21385:             unless ($type eq 'community') {
21386:                 $credits{$type} = $env{'form.'.$type.'_credits'};
21387:                 $credits{$type} =~ s/[^\d.]+//g;
21388:             }
21389:         }
21390:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
21391:             ($env{'form.coursecredits'} eq '1')) {
21392:             $changes{'coursecredits'} = 1;
21393:             foreach my $type (keys(%credits)) {
21394:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21395:             }
21396:         } else {
21397:             if ($env{'form.coursecredits'} eq '1') {
21398:                 foreach my $type (@types) {
21399:                     unless ($type eq 'community') {
21400:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
21401:                             $changes{'coursecredits'} = 1;
21402:                         }
21403:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21404:                     }
21405:                 }
21406:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21407:                 foreach my $type (@types) {
21408:                     unless ($type eq 'community') {
21409:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
21410:                             $changes{'coursecredits'} = 1;
21411:                             last;
21412:                         }
21413:                     }
21414:                 }
21415:             }
21416:         }
21417:         if ($env{'form.postsubmit'} eq '1') {
21418:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
21419:             my %currtimeout;
21420:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21421:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
21422:                     $changes{'postsubmit'} = 1;
21423:                 }
21424:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21425:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
21426:                 }
21427:             } else {
21428:                 $changes{'postsubmit'} = 1;
21429:             }
21430:             foreach my $type (@types) {
21431:                 my $timeout = $env{'form.'.$type.'_timeout'};
21432:                 $timeout =~ s/\D//g;
21433:                 if ($timeout == $staticdefaults{'postsubmit'}) {
21434:                     $timeout = '';
21435:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
21436:                     $timeout = '0';
21437:                 }
21438:                 unless ($timeout eq '') {
21439:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
21440:                 }
21441:                 if (exists($currtimeout{$type})) {
21442:                     if ($timeout ne $currtimeout{$type}) {
21443:                         $changes{'postsubmit'} = 1;
21444:                     }
21445:                 } elsif ($timeout ne '') {
21446:                     $changes{'postsubmit'} = 1;
21447:                 }
21448:             }
21449:         } else {
21450:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
21451:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21452:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
21453:                     $changes{'postsubmit'} = 1;
21454:                 }
21455:             } else {
21456:                 $changes{'postsubmit'} = 1;
21457:             }
21458:         }
21459:         my (%newdomexttool,%newexttool,%newcrsauthor,%olddomexttool,%oldexttool,%oldcrsauthor,
21460:             %posscrseditors);
21461:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
21462:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
21463:         map { $newcrsauthor{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crsauthor');
21464:         map { $posscrseditors{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crseditors');
21465:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21466:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
21467:         } else {
21468:            foreach my $type (@types) {
21469:                if ($staticdefaults{'domexttool'}) {
21470:                    $olddomexttool{$type} = 1;
21471:                } else {
21472:                    $olddomexttool{$type} = 0;
21473:                }
21474:             }
21475:         }
21476:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
21477:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
21478:         } else {
21479:             foreach my $type (@types) {
21480:                if ($staticdefaults{'exttool'}) {
21481:                    $oldexttool{$type} = 1;
21482:                } else {
21483:                    $oldexttool{$type} = 0;
21484:                }
21485:             }
21486:         }
21487:         if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21488:             %oldcrsauthor = %{$domconfig{'coursedefaults'}{'crsauthor'}};
21489:         } else {
21490:             foreach my $type (@types) {
21491:                if ($staticdefaults{'crsauthor'}) {
21492:                    $oldcrsauthor{$type} = 1;
21493:                } else {
21494:                    $oldcrsauthor{$type} = 0;
21495:                }
21496:             }
21497:         }
21498:         my @newcrseditors = ();
21499:         foreach my $editor (@editors) {
21500:             if ($posscrseditors{$editor}) {
21501:                 push(@newcrseditors,$editor);
21502:             }
21503:         }
21504:         if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21505:             my @diffs =
21506:                 &Apache::loncommon::compare_arrays($domconfig{'coursedefaults'}{'crseditors'},
21507:                                                    \@newcrseditors);
21508:             if (@diffs) {
21509:                 $changes{'crseditors'} = 1; 
21510:             }
21511:         } else {
21512:             my @diffs =
21513:                 &Apache::loncommon::compare_arrays($staticdefaults{'crseditors'},
21514:                                                    \@newcrseditors);
21515:             unless (@diffs == 0) {
21516:                 $changes{'crseditors'} = 1;
21517:             }       
21518:         }
21519:         foreach my $type (@types) {
21520:             unless ($newdomexttool{$type}) {
21521:                 $newdomexttool{$type} = 0;
21522:             }
21523:             unless ($newexttool{$type}) {
21524:                 $newexttool{$type} = 0;
21525:             }
21526:             unless ($newcrsauthor{$type}) {
21527:                 $newcrsauthor{$type} = 0;
21528:             }
21529:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
21530:                 $changes{'domexttool'} = 1;
21531:             }
21532:             if ($newexttool{$type} != $oldexttool{$type}) {
21533:                 $changes{'exttool'} = 1;
21534:             }
21535:             if ($newcrsauthor{$type} != $oldcrsauthor{$type}) {
21536:                 $changes{'crsauthor'} = 1;
21537:             }
21538:         }
21539:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
21540:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
21541:         $defaultshash{'coursedefaults'}{'crsauthor'} = \%newcrsauthor;
21542:         $defaultshash{'coursedefaults'}{'crseditors'} = \@newcrseditors;
21543:     }
21544:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21545:                                              $dom);
21546:     if ($putresult eq 'ok') {
21547:         if (keys(%changes) > 0) {
21548:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21549:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
21550:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
21551:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
21552:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
21553:                 ($changes{'exttool'}) || ($changes{'coursequota'}) || ($changes{'crsauthor'})) {
21554:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
21555:                                   'ltiauth') {
21556:                     if ($changes{$item}) {
21557:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
21558:                     }
21559:                 }
21560:                 if ($changes{'coursecredits'}) {
21561:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21562:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
21563:                             $domdefaults{$type.'credits'} =
21564:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
21565:                         }
21566:                     }
21567:                 }
21568:                 if ($changes{'postsubmit'}) {
21569:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21570:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
21571:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21572:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
21573:                                 $domdefaults{$type.'postsubtimeout'} =
21574:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21575:                             }
21576:                         }
21577:                     }
21578:                 }
21579:                 if ($changes{'uploadquota'}) {
21580:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21581:                         foreach my $type (@types) {
21582:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
21583:                         }
21584:                     }
21585:                 }
21586:                 if ($changes{'coursequota'}) {
21587:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21588:                         foreach my $type (@types) {
21589:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
21590:                         }
21591:                     }
21592:                 }
21593:                 if ($changes{'canclone'}) {
21594:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21595:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21596:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
21597:                             if (@clonecodes) {
21598:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
21599:                             }
21600:                         }
21601:                     } else {
21602:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
21603:                     }
21604:                 }
21605:                 if ($changes{'domexttool'}) {
21606:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21607:                         foreach my $type (@types) {
21608:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
21609:                         }
21610:                     }
21611:                 }
21612:                 if ($changes{'exttool'}) {
21613:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21614:                         foreach my $type (@types) {
21615:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
21616:                         }
21617:                     }
21618:                 }
21619:                 if ($changes{'crsauthor'}) {
21620:                     if (ref($defaultshash{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21621:                         foreach my $type (@types) {
21622:                             $domdefaults{$type.'crsauthor'}=$defaultshash{'coursedefaults'}{'crsauthor'}{$type};
21623:                         }
21624:                     }
21625:                 }
21626:                 if ($changes{'crseditors'}) {
21627:                     if (ref($defaultshash{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21628:                         $domdefaults{'crseditors'}=join(',',@{$defaultshash{'coursedefaults'}{'crseditors'}});
21629:                     }
21630:                 }
21631:                 my $cachetime = 24*60*60;
21632:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21633:                 if (ref($lastactref) eq 'HASH') {
21634:                     $lastactref->{'domdefaults'} = 1;
21635:                 }
21636:             }
21637:             $resulttext = &mt('Changes made:').'<ul>';
21638:             foreach my $item (sort(keys(%changes))) {
21639:                 if ($item eq 'canuse_pdfforms') {
21640:                     if ($env{'form.'.$item} eq '1') {
21641:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
21642:                     } else {
21643:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
21644:                     }
21645:                 } elsif ($item eq 'uselcmath') {
21646:                     if ($env{'form.'.$item} eq '1') {
21647:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
21648:                     } else {
21649:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
21650:                     }
21651:                 } elsif ($item eq 'usejsme') {
21652:                     if ($env{'form.'.$item} eq '1') {
21653:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
21654:                     } else {
21655:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
21656:                     }
21657:                 } elsif ($item eq 'inline_chem') {
21658:                     if ($env{'form.'.$item} eq '1') {
21659:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
21660:                     } else {
21661:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
21662:                     }
21663:                 } elsif ($item eq 'texengine') {
21664:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
21665:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
21666:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
21667:                     }
21668:                 } elsif ($item eq 'anonsurvey_threshold') {
21669:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
21670:                 } elsif ($item eq 'uploadquota') {
21671:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21672:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
21673:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
21674:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
21675:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
21676:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
21677:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
21678:                                        '</ul>'.
21679:                                        '</li>';
21680:                     } else {
21681:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
21682:                     }
21683:                 } elsif ($item eq 'coursequota') {
21684:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21685:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
21686:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
21687:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
21688:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
21689:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'placement'}.'</b>').'</li>'.
21690:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
21691:                                        '</ul>'.
21692:                                        '</li>';
21693:                     } else {
21694:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
21695:                     }
21696:                 } elsif ($item eq 'mysqltables') {
21697:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
21698:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
21699:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
21700:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
21701:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
21702:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
21703:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
21704:                                        '</ul>'.
21705:                                        '</li>';
21706:                     } else {
21707:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
21708:                     }
21709:                 } elsif ($item eq 'postsubmit') {
21710:                     if ($domdefaults{'postsubmit'} eq 'off') {
21711:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
21712:                     } else {
21713:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
21714:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21715:                             $resulttext .= &mt('durations:').'<ul>';
21716:                             foreach my $type (@types) {
21717:                                 $resulttext .= '<li>';
21718:                                 my $timeout;
21719:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21720:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21721:                                 }
21722:                                 my $display;
21723:                                 if ($timeout eq '0') {
21724:                                     $display = &mt('unlimited');
21725:                                 } elsif ($timeout eq '') {
21726:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
21727:                                 } else {
21728:                                     $display = &mt('[quant,_1,second]',$timeout);
21729:                                 }
21730:                                 if ($type eq 'community') {
21731:                                     $resulttext .= &mt('Communities');
21732:                                 } elsif ($type eq 'official') {
21733:                                     $resulttext .= &mt('Official courses');
21734:                                 } elsif ($type eq 'unofficial') {
21735:                                     $resulttext .= &mt('Unofficial courses');
21736:                                 } elsif ($type eq 'textbook') {
21737:                                     $resulttext .= &mt('Textbook courses');
21738:                                 } elsif ($type eq 'placement') {
21739:                                     $resulttext .= &mt('Placement tests');
21740:                                 }
21741:                                 $resulttext .= ' -- '.$display.'</li>';
21742:                             }
21743:                             $resulttext .= '</ul>';
21744:                         }
21745:                         $resulttext .= '</li>';
21746:                     }
21747:                 } elsif ($item eq 'coursecredits') {
21748:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21749:                         if (($domdefaults{'officialcredits'} eq '') &&
21750:                             ($domdefaults{'unofficialcredits'} eq '') &&
21751:                             ($domdefaults{'textbookcredits'} eq '')) {
21752:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21753:                         } else {
21754:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
21755:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
21756:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
21757:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
21758:                                            '</ul>'.
21759:                                            '</li>';
21760:                         }
21761:                     } else {
21762:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21763:                     }
21764:                 } elsif ($item eq 'canclone') {
21765:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21766:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21767:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
21768:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
21769:                         }
21770:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
21771:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
21772:                     } else {
21773:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
21774:                     }
21775:                 } elsif ($item eq 'ltiauth') {
21776:                     if ($env{'form.'.$item} eq '1') {
21777:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
21778:                     } else {
21779:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
21780:                     }
21781:                 } elsif (($item eq 'domexttool') || ($item eq 'exttool') || ($item eq 'crsauthor')) {
21782:                     my @noyes = (&mt('no'),&mt('yes'));
21783:                     my %status = (
21784:                                    domexttool => {
21785:                                                   ishash => &mt('External Tools defined in the domain may be used as follows:'), 
21786:                                                   default => &mt('External Tools defined in the domain may be used in all course types, by default'),
21787:                                                  },
21788:                                    exttool => {
21789:                                                   ishash => &mt('External Tools can be defined and configured in course containers as follows:'),
21790:                                                   default => &mt('External Tools can not be defined in any course types, by default'),
21791:                                               },
21792:                                    crsauthor => {
21793:                                                   ishash => &mt('Standard Problems can be created within course containers as follows:'),
21794:                                                   default => &mt('Standard Problems can be created within any course type, by default'),
21795:                                                 },
21796:                                  );
21797:                                    
21798:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'HASH') {
21799:                         $resulttext .= '<li>'.$status{$item}{'ishash'}.'<ul>'.
21800:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'official'}].'</b>').'</li>'.
21801:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'unofficial'}].'</b>').'</li>'.
21802:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'textbook'}].'</b>').'</li>'.
21803:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'placement'}].'</b>').'</li>'.
21804:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'community'}].'</b>').'</li>'.
21805:                                        '</ul>'.
21806:                                        '</li>';
21807:                     } else {
21808:                         $resulttext .= '<li>'.$status{$item}{'default'}.'</li>';
21809:                     }
21810:                 } elsif ($item eq 'crseditors') {
21811:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'ARRAY') {
21812:                         my $shown;
21813:                         if (@{$defaultshash{'coursedefaults'}{$item}}) {
21814:                             $shown = join(', ', map { $editornames{$_} } @{$defaultshash{'coursedefaults'}{$item}});
21815:                         } else {
21816:                             $shown = &mt('None');
21817:                         }
21818:                         $resulttext .= '<li>'.&mt('Available editors for course/community resources: [_1]',$shown).'</li>';
21819:                     }
21820:                 }
21821:             }
21822:             $resulttext .= '</ul>';
21823:         } else {
21824:             $resulttext = &mt('No changes made to course defaults');
21825:         }
21826:     } else {
21827:         $resulttext = '<span class="LC_error">'.
21828:             &mt('An error occurred: [_1]',$putresult).'</span>';
21829:     }
21830:     return $resulttext;
21831: }
21832: 
21833: sub modify_selfenrollment {
21834:     my ($dom,$lastactref,%domconfig) = @_;
21835:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
21836:     my @types = ('official','unofficial','community','textbook','placement');
21837:     my %titles = &tool_titles();
21838:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
21839:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
21840:     $ordered{'default'} = ['types','registered','approval','limit'];
21841: 
21842:     my (%roles,%shown,%toplevel);
21843:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
21844: 
21845:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
21846:         if ($domconfig{'selfenrollment'} eq '') {
21847:             $domconfig{'selfenrollment'} = {};
21848:         }
21849:     }
21850:     %toplevel = (
21851:                   admin      => 'Configuration Rights',
21852:                   default    => 'Default settings',
21853:                   validation => 'Validation of self-enrollment requests',
21854:                 );
21855:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21856: 
21857:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21858:         foreach my $item (@{$ordered{'admin'}}) {
21859:             foreach my $type (@types) {
21860:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21861:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21862:                 } else {
21863:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21864:                 }
21865:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21866:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21867:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21868:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21869:                             push(@{$changes{'admin'}{$type}},$item);
21870:                         }
21871:                     } else {
21872:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21873:                             push(@{$changes{'admin'}{$type}},$item);
21874:                         }
21875:                     }
21876:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21877:                     push(@{$changes{'admin'}{$type}},$item);
21878:                 }
21879:             }
21880:         }
21881:     }
21882: 
21883:     foreach my $item (@{$ordered{'default'}}) {
21884:         foreach my $type (@types) {
21885:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21886:             if ($item eq 'types') {
21887:                 unless (($value eq 'all') || ($value eq 'dom')) {
21888:                     $value = '';
21889:                 }
21890:             } elsif ($item eq 'registered') {
21891:                 unless ($value eq '1') {
21892:                     $value = 0;
21893:                 }
21894:             } elsif ($item eq 'approval') {
21895:                 unless ($value =~ /^[012]$/) {
21896:                     $value = 0;
21897:                 }
21898:             } else {
21899:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21900:                     $value = 'none';
21901:                 }
21902:             }
21903:             $selfenrollhash{'default'}{$type}{$item} = $value;
21904:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21905:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21906:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21907:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21908:                          push(@{$changes{'default'}{$type}},$item);
21909:                     }
21910:                 } else {
21911:                     push(@{$changes{'default'}{$type}},$item);
21912:                 }
21913:             } else {
21914:                 push(@{$changes{'default'}{$type}},$item);
21915:             }
21916:             if ($item eq 'limit') {
21917:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21918:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21919:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21920:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21921:                     }
21922:                 } else {
21923:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21924:                 }
21925:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21926:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21927:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21928:                          push(@{$changes{'default'}{$type}},'cap');
21929:                     }
21930:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21931:                     push(@{$changes{'default'}{$type}},'cap');
21932:                 }
21933:             }
21934:         }
21935:     }
21936: 
21937:     foreach my $item (@{$itemsref}) {
21938:         if ($item eq 'fields') {
21939:             my @changed;
21940:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21941:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21942:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21943:             }
21944:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21945:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21946:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21947:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21948:                 } else {
21949:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21950:                 }
21951:             } else {
21952:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21953:             }
21954:             if (@changed) {
21955:                 if ($selfenrollhash{'validation'}{$item}) { 
21956:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21957:                 } else {
21958:                     $changes{'validation'}{$item} = &mt('None');
21959:                 }
21960:             }
21961:         } else {
21962:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
21963:             if ($item eq 'markup') {
21964:                if ($env{'form.selfenroll_validation_'.$item}) {
21965:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
21966:                }
21967:             }
21968:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21969:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
21970:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
21971:                 }
21972:             }
21973:         }
21974:     }
21975: 
21976:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
21977:                                              $dom);
21978:     if ($putresult eq 'ok') {
21979:         if (keys(%changes) > 0) {
21980:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21981:             $resulttext = &mt('Changes made:').'<ul>';
21982:             foreach my $key ('admin','default','validation') {
21983:                 if (ref($changes{$key}) eq 'HASH') {
21984:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
21985:                     if ($key eq 'validation') {
21986:                         foreach my $item (@{$itemsref}) {
21987:                             if (exists($changes{$key}{$item})) {
21988:                                 if ($item eq 'markup') {
21989:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21990:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
21991:                                 } else {  
21992:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21993:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
21994:                                 }
21995:                             }
21996:                         }
21997:                     } else {
21998:                         foreach my $type (@types) {
21999:                             if ($type eq 'community') {
22000:                                 $roles{'1'} = &mt('Community personnel');
22001:                             } else {
22002:                                 $roles{'1'} = &mt('Course personnel');
22003:                             }
22004:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
22005:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
22006:                                     if ($key eq 'admin') {
22007:                                         my @mgrdc = ();
22008:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22009:                                             foreach my $item (@{$ordered{'admin'}}) {
22010:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
22011:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
22012:                                                         push(@mgrdc,$item);
22013:                                                     }
22014:                                                 }
22015:                                             }
22016:                                             if (@mgrdc) {
22017:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
22018:                                             } else {
22019:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
22020:                                             }
22021:                                         }
22022:                                     } else {
22023:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22024:                                             foreach my $item (@{$ordered{$key}}) {
22025:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22026:                                                     $domdefaults{$type.'selfenroll'.$item} =
22027:                                                         $selfenrollhash{$key}{$type}{$item};
22028:                                                 }
22029:                                             }
22030:                                         }
22031:                                     }
22032:                                 }
22033:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
22034:                                 foreach my $item (@{$ordered{$key}}) {
22035:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22036:                                         $resulttext .= '<li>';
22037:                                         if ($key eq 'admin') {
22038:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
22039:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
22040:                                         } else {
22041:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
22042:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
22043:                                         }
22044:                                         $resulttext .= '</li>';
22045:                                     }
22046:                                 }
22047:                                 $resulttext .= '</ul></li>';
22048:                             }
22049:                         }
22050:                         $resulttext .= '</ul></li>'; 
22051:                     }
22052:                 }
22053:             }
22054:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
22055:                 my $cachetime = 24*60*60;
22056:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22057:                 if (ref($lastactref) eq 'HASH') {
22058:                     $lastactref->{'domdefaults'} = 1;
22059:                 }
22060:             }
22061:             $resulttext .= '</ul>';
22062:         } else {
22063:             $resulttext = &mt('No changes made to self-enrollment settings');
22064:         }
22065:     } else {
22066:         $resulttext = '<span class="LC_error">'.
22067:             &mt('An error occurred: [_1]',$putresult).'</span>';
22068:     }
22069:     return $resulttext;
22070: }
22071: 
22072: sub modify_wafproxy {
22073:     my ($dom,$action,$lastactref,%domconfig) = @_;
22074:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22075:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
22076:         %wafproxy,%changes,%expirecache,%expiresaml);
22077:     foreach my $server (sort(keys(%servers))) {
22078:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22079:         if ($serverhome eq $server) {
22080:             my $serverdom = &Apache::lonnet::host_domain($server);
22081:             if ($serverdom eq $dom) {
22082:                 $canset{$server} = 1;
22083:             }
22084:         }
22085:     }
22086:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
22087:         %{$values{$dom}} = ();
22088:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
22089:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
22090:         }
22091:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
22092:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
22093:         }
22094:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22095:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
22096:         }
22097:     }
22098:     my $output;
22099:     if (keys(%canset)) {
22100:         %{$wafproxy{'alias'}} = ();
22101:         %{$wafproxy{'saml'}} = ();
22102:         foreach my $key (sort(keys(%canset))) {
22103:             if ($env{'form.wafproxy_'.$dom}) {
22104:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
22105:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
22106:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
22107:                     $changes{'alias'} = 1;
22108:                 }
22109:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
22110:                     $wafproxy{'saml'}{$key} = 1;
22111:                 }
22112:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
22113:                     $changes{'saml'} = 1;
22114:                 }
22115:             } else {
22116:                 $wafproxy{'alias'}{$key} = '';
22117:                 $wafproxy{'saml'}{$key} = '';
22118:                 if ($curralias{$key}) {
22119:                     $changes{'alias'} = 1;
22120:                 }
22121:                 if ($currsaml{$key}) {
22122:                     $changes{'saml'} = 1;
22123:                 }
22124:             }
22125:             if ($wafproxy{'alias'}{$key} eq '') {
22126:                 if ($curralias{$key}) {
22127:                     $expirecache{$key} = 1;
22128:                 }
22129:                 delete($wafproxy{'alias'}{$key});
22130:             }
22131:             if ($wafproxy{'saml'}{$key} eq '') {
22132:                 if ($currsaml{$key}) {
22133:                     $expiresaml{$key} = 1;
22134:                 }
22135:                 delete($wafproxy{'saml'}{$key});
22136:             }
22137:         }
22138:         unless (keys(%{$wafproxy{'alias'}})) {
22139:             delete($wafproxy{'alias'});
22140:         }
22141:         unless (keys(%{$wafproxy{'saml'}})) {
22142:             delete($wafproxy{'saml'});
22143:         }
22144:         # Localization for values in %warn occurs in &mt() calls separately.
22145:         my %warn = (
22146:                      trusted => 'trusted IP range(s)',
22147:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
22148:                      vpnext => 'IP range(s) for backend WAF connections',
22149:                    );
22150:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22151:             my $possible = $env{'form.wafproxy_'.$item};
22152:             $possible =~ s/^\s+|\s+$//g;
22153:             if ($possible ne '') {
22154:                 if ($item eq 'remoteip') {
22155:                     if ($possible =~ /^[mhn]$/) {
22156:                         $wafproxy{$item} = $possible;
22157:                     }
22158:                 } elsif ($item eq 'ipheader') {
22159:                     if ($wafproxy{'remoteip'} eq 'h') {
22160:                         $wafproxy{$item} = $possible;
22161:                     }
22162:                 } elsif ($item eq 'sslopt') {
22163:                     if ($possible =~ /^0|1$/) {
22164:                         $wafproxy{$item} = $possible;
22165:                     }
22166:                 } else {
22167:                     my (@ok,$count);
22168:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
22169:                         unless ($env{'form.wafproxy_vpnaccess'}) {
22170:                             $possible = '';
22171:                         }
22172:                     } elsif ($item eq 'trusted') {
22173:                         unless ($wafproxy{'remoteip'} eq 'h') {
22174:                             $possible = '';
22175:                         }
22176:                     }
22177:                     unless ($possible eq '') {
22178:                         $possible =~ s/[\r\n]+/\s/g;
22179:                         $possible =~ s/\s*-\s*/-/g;
22180:                         $possible =~ s/\s+/,/g;
22181:                         $possible =~ s/,+/,/g;
22182:                     }
22183:                     $count = 0;
22184:                     if ($possible ne '') {
22185:                         foreach my $poss (split(/\,/,$possible)) {
22186:                             $count ++;
22187:                             $poss = &validate_ip_pattern($poss);
22188:                             if ($poss ne '') {
22189:                                 push(@ok,$poss);
22190:                             }
22191:                         }
22192:                         my $diff = $count - scalar(@ok);
22193:                         if ($diff) {
22194:                             push(@warnings,'<li>'.
22195:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
22196:                                      $diff,$warn{$item}).
22197:                                  '</li>');
22198:                         }
22199:                         if (@ok) {
22200:                             my @cidr_list;
22201:                             foreach my $item (@ok) {
22202:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
22203:                             }
22204:                             $wafproxy{$item} = join(',',@cidr_list);
22205:                         }
22206:                     }
22207:                 }
22208:                 if ($wafproxy{$item} ne $currvalue{$item}) {
22209:                     $changes{$item} = 1;
22210:                 }
22211:             } elsif ($currvalue{$item}) {
22212:                 $changes{$item} = 1;
22213:             }
22214:         }
22215:     } else {
22216:         if (keys(%curralias)) {
22217:             $changes{'alias'} = 1;
22218:         }
22219:         if (keys(%currsaml)) {
22220:             $changes{'saml'} = 1;
22221:         }
22222:         if (keys(%currvalue)) {
22223:             foreach my $key (keys(%currvalue)) {
22224:                 $changes{$key} = 1;
22225:             }
22226:         }
22227:     }
22228:     if (keys(%changes)) {
22229:         my %defaultshash = (
22230:                               wafproxy => \%wafproxy,
22231:                            );
22232:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22233:                                                  $dom);
22234:         if ($putresult eq 'ok') {
22235:             my $cachetime = 24*60*60;
22236:             my (%domdefaults,$updatedomdefs);
22237:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
22238:                 if ($changes{$item}) {
22239:                     unless ($updatedomdefs) {
22240:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
22241:                         $updatedomdefs = 1;
22242:                     }
22243:                     if ($wafproxy{$item}) {
22244:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
22245:                     } elsif (exists($domdefaults{'waf_'.$item})) {
22246:                         delete($domdefaults{'waf_'.$item});
22247:                     }
22248:                 }
22249:             }
22250:             if ($updatedomdefs) {
22251:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22252:                 if (ref($lastactref) eq 'HASH') {
22253:                     $lastactref->{'domdefaults'} = 1;
22254:                 }
22255:             }
22256:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
22257:                 my %updates = %expirecache;
22258:                 foreach my $key (keys(%expirecache)) {
22259:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
22260:                 }
22261:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
22262:                     my $cachetime = 24*60*60;
22263:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
22264:                         $updates{$key} = 1;
22265:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
22266:                                                       $cachetime);
22267:                     }
22268:                 }
22269:                 if (ref($lastactref) eq 'HASH') {
22270:                     $lastactref->{'proxyalias'} = \%updates;
22271:                 }
22272:             }
22273:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
22274:                 my %samlupdates = %expiresaml;
22275:                 foreach my $key (keys(%expiresaml)) {
22276:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
22277:                 }
22278:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
22279:                     my $cachetime = 24*60*60;
22280:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
22281:                         $samlupdates{$key} = 1;
22282:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
22283:                                                       $cachetime);
22284:                     }
22285:                 }
22286:                 if (ref($lastactref) eq 'HASH') {
22287:                     $lastactref->{'proxysaml'} = \%samlupdates;
22288:                 }
22289:             }
22290:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
22291:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22292:                 if ($changes{$item}) {
22293:                     if ($item eq 'alias') {
22294:                         my $numaliased = 0;
22295:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
22296:                             my $shown;
22297:                             if (keys(%{$wafproxy{'alias'}})) {
22298:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
22299:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
22300:                                                          &Apache::lonnet::hostname($server),
22301:                                                          $wafproxy{'alias'}{$server}).'</li>';
22302:                                     $numaliased ++;
22303:                                 }
22304:                                 if ($numaliased) {
22305:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
22306:                                                           '<ul>'.$shown.'</ul>').'</li>';
22307:                                 }
22308:                             }
22309:                         }
22310:                         unless ($numaliased) {
22311:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
22312:                         }
22313:                     } elsif ($item eq 'saml') {
22314:                         my $shown;
22315:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
22316:                             if (keys(%{$wafproxy{'saml'}})) {
22317:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
22318:                             }
22319:                         }
22320:                         if ($shown) {
22321:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
22322:                                                   $shown).'</li>';
22323:                         } else {
22324:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
22325:                         }
22326:                     } else {
22327:                         if ($item eq 'remoteip') {
22328:                             my %ip_methods = &remoteip_methods();
22329:                             if ($wafproxy{$item} =~ /^[mh]$/) {
22330:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
22331:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
22332:                             } else {
22333:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
22334:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
22335:                                                '</li>';
22336:                                 } else {
22337:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
22338:                                 }
22339:                             }
22340:                         } elsif ($item eq 'ipheader') {
22341:                             if ($wafproxy{$item}) {
22342:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
22343:                                                       $wafproxy{$item}).'</li>';
22344:                             } else {
22345:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
22346:                             }
22347:                         } elsif ($item eq 'trusted') {
22348:                             if ($wafproxy{$item}) {
22349:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
22350:                                                       $wafproxy{$item}).'</li>';
22351:                             } else {
22352:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
22353:                             }
22354:                         } elsif ($item eq 'vpnint') {
22355:                             if ($wafproxy{$item}) {
22356:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
22357:                                                        $wafproxy{$item}).'</li>';
22358:                             } else {
22359:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
22360:                             }
22361:                         } elsif ($item eq 'vpnext') {
22362:                             if ($wafproxy{$item}) {
22363:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
22364:                                                        $wafproxy{$item}).'</li>';
22365:                             } else {
22366:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
22367:                             }
22368:                         } elsif ($item eq 'sslopt') {
22369:                             if ($wafproxy{$item}) {
22370:                                 $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>';
22371:                             } else {
22372:                                 $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>';
22373:                             }
22374:                         }
22375:                     }
22376:                 }
22377:             }
22378:             $output .= '</ul>';
22379:         } else {
22380:             $output = '<span class="LC_error">'.
22381:                       &mt('An error occurred: [_1]',$putresult).'</span>';
22382:         }
22383:     } elsif (keys(%canset)) {
22384:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
22385:     }
22386:     if (@warnings) {
22387:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
22388:                        join("\n",@warnings).'</ul>';
22389:     }
22390:     return $output;
22391: }
22392: 
22393: sub validate_ip_pattern {
22394:     my ($pattern) = @_;
22395:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
22396:         my ($start,$end) = ($1,$2);
22397:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
22398:             if (($start !~ m{/}) && ($end !~ m{/})) {
22399:                 return $start.'-'.$end;
22400:             }
22401:         }
22402:     } elsif ($pattern ne '') {
22403:         $pattern = &Net::CIDR::cidrvalidate($pattern);
22404:         if ($pattern ne '') {
22405:             return $pattern;
22406:         }
22407:     }
22408:     return;
22409: }
22410: 
22411: sub modify_usersessions {
22412:     my ($dom,$lastactref,%domconfig) = @_;
22413:     my @hostingtypes = ('version','excludedomain','includedomain');
22414:     my @offloadtypes = ('primary','default');
22415:     my %types = (
22416:                   remote => \@hostingtypes,
22417:                   hosted => \@hostingtypes,
22418:                   spares => \@offloadtypes,
22419:                 );
22420:     my @prefixes = ('remote','hosted','spares');
22421:     my @lcversions = &Apache::lonnet::all_loncaparevs();
22422:     my (%by_ip,%by_location,@intdoms,@instdoms);
22423:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22424:     my @locations = sort(keys(%by_location));
22425:     my (%defaultshash,%changes);
22426:     foreach my $prefix (@prefixes) {
22427:         $defaultshash{'usersessions'}{$prefix} = {};
22428:     }
22429:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22430:     my $resulttext;
22431:     my %iphost = &Apache::lonnet::get_iphost();
22432:     foreach my $prefix (@prefixes) {
22433:         next if ($prefix eq 'spares');
22434:         foreach my $type (@{$types{$prefix}}) {
22435:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22436:             if ($type eq 'version') {
22437:                 my $value = $env{'form.'.$prefix.'_'.$type};
22438:                 my $okvalue;
22439:                 if ($value ne '') {
22440:                     if (grep(/^\Q$value\E$/,@lcversions)) {
22441:                         $okvalue = $value;
22442:                     }
22443:                 }
22444:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22445:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22446:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
22447:                             if ($inuse == 0) {
22448:                                 $changes{$prefix}{$type} = 1;
22449:                             } else {
22450:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
22451:                                     $changes{$prefix}{$type} = 1;
22452:                                 }
22453:                                 if ($okvalue ne '') {
22454:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22455:                                 } 
22456:                             }
22457:                         } else {
22458:                             if (($inuse == 1) && ($okvalue ne '')) {
22459:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22460:                                 $changes{$prefix}{$type} = 1;
22461:                             }
22462:                         }
22463:                     } else {
22464:                         if (($inuse == 1) && ($okvalue ne '')) {
22465:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22466:                             $changes{$prefix}{$type} = 1;
22467:                         }
22468:                     }
22469:                 } else {
22470:                     if (($inuse == 1) && ($okvalue ne '')) {
22471:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22472:                         $changes{$prefix}{$type} = 1;
22473:                     }
22474:                 }
22475:             } else {
22476:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22477:                 my @okvals;
22478:                 foreach my $val (@vals) {
22479:                     if ($val =~ /:/) {
22480:                         my @items = split(/:/,$val);
22481:                         foreach my $item (@items) {
22482:                             if (ref($by_location{$item}) eq 'ARRAY') {
22483:                                 push(@okvals,$item);
22484:                             }
22485:                         }
22486:                     } else {
22487:                         if (ref($by_location{$val}) eq 'ARRAY') {
22488:                             push(@okvals,$val);
22489:                         }
22490:                     }
22491:                 }
22492:                 @okvals = sort(@okvals);
22493:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22494:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22495:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22496:                             if ($inuse == 0) {
22497:                                 $changes{$prefix}{$type} = 1; 
22498:                             } else {
22499:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22500:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
22501:                                 if (@changed > 0) {
22502:                                     $changes{$prefix}{$type} = 1;
22503:                                 }
22504:                             }
22505:                         } else {
22506:                             if ($inuse == 1) {
22507:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22508:                                 $changes{$prefix}{$type} = 1;
22509:                             }
22510:                         } 
22511:                     } else {
22512:                         if ($inuse == 1) {
22513:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22514:                             $changes{$prefix}{$type} = 1;
22515:                         }
22516:                     }
22517:                 } else {
22518:                     if ($inuse == 1) {
22519:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22520:                         $changes{$prefix}{$type} = 1;
22521:                     }
22522:                 }
22523:             }
22524:         }
22525:     }
22526: 
22527:     my @alldoms = &Apache::lonnet::all_domains();
22528:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22529:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
22530:     my $savespares;
22531: 
22532:     foreach my $lonhost (sort(keys(%servers))) {
22533:         my $serverhomeID =
22534:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
22535:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
22536:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
22537:         my %spareschg;
22538:         foreach my $type (@{$types{'spares'}}) {
22539:             my @okspares;
22540:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
22541:             foreach my $server (@checked) {
22542:                 if (&Apache::lonnet::hostname($server) ne '') {
22543:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
22544:                         unless (grep(/^\Q$server\E$/,@okspares)) {
22545:                             push(@okspares,$server);
22546:                         }
22547:                     }
22548:                 }
22549:             }
22550:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
22551:             my $newspare;
22552:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
22553:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
22554:                     $newspare = $new;
22555:                 }
22556:             }
22557:             my @spares;
22558:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
22559:                 @spares = sort(@okspares,$newspare);
22560:             } else {
22561:                 @spares = sort(@okspares);
22562:             }
22563:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
22564:             if (ref($spareid{$lonhost}) eq 'HASH') {
22565:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
22566:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
22567:                     if (@diffs > 0) {
22568:                         $spareschg{$type} = 1;
22569:                     }
22570:                 }
22571:             }
22572:         }
22573:         if (keys(%spareschg) > 0) {
22574:             $changes{'spares'}{$lonhost} = \%spareschg;
22575:         }
22576:     }
22577:     $defaultshash{'usersessions'}{'offloadnow'} = {};
22578:     $defaultshash{'usersessions'}{'offloadoth'} = {};
22579:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
22580:     my @okoffload;
22581:     if (@offloadnow) {
22582:         foreach my $server (@offloadnow) {
22583:             if (&Apache::lonnet::hostname($server) ne '') {
22584:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
22585:                     push(@okoffload,$server);
22586:                 }
22587:             }
22588:         }
22589:         if (@okoffload) {
22590:             foreach my $lonhost (@okoffload) {
22591:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
22592:             }
22593:         }
22594:     }
22595:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
22596:     my @okoffloadoth;
22597:     if (@offloadoth) {
22598:         foreach my $server (@offloadoth) {
22599:             if (&Apache::lonnet::hostname($server) ne '') {
22600:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
22601:                     push(@okoffloadoth,$server);
22602:                 }
22603:             }
22604:         }
22605:         if (@okoffloadoth) {
22606:             foreach my $lonhost (@okoffloadoth) {
22607:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
22608:             }
22609:         }
22610:     }
22611:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
22612:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
22613:             if (ref($changes{'spares'}) eq 'HASH') {
22614:                 if (keys(%{$changes{'spares'}}) > 0) {
22615:                     $savespares = 1;
22616:                 }
22617:             }
22618:         } else {
22619:             $savespares = 1;
22620:         }
22621:         foreach my $offload ('offloadnow','offloadoth') {
22622:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
22623:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
22624:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
22625:                         $changes{$offload} = 1;
22626:                         last;
22627:                     }
22628:                 }
22629:                 unless ($changes{$offload}) {
22630:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
22631:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
22632:                             $changes{$offload} = 1;
22633:                             last;
22634:                         }
22635:                     }
22636:                 }
22637:             } else {
22638:                 if (($offload eq 'offloadnow') && (@okoffload)) {
22639:                      $changes{'offloadnow'} = 1;
22640:                 }
22641:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
22642:                     $changes{'offloadoth'} = 1;
22643:                 }
22644:             }
22645:         }
22646:     } else {
22647:         if (@okoffload) {
22648:             $changes{'offloadnow'} = 1;
22649:         }
22650:         if (@okoffloadoth) {
22651:             $changes{'offloadoth'} = 1;
22652:         }
22653:     }
22654:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
22655:     if ((keys(%changes) > 0) || ($savespares)) {
22656:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22657:                                                  $dom);
22658:         if ($putresult eq 'ok') {
22659:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22660:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
22661:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
22662:                 }
22663:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
22664:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
22665:                 }
22666:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22667:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
22668:                 }
22669:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22670:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
22671:                 }
22672:             }
22673:             my $cachetime = 24*60*60;
22674:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22675:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
22676:             if (ref($lastactref) eq 'HASH') {
22677:                 $lastactref->{'domdefaults'} = 1;
22678:                 $lastactref->{'usersessions'} = 1;
22679:             }
22680:             if (keys(%changes) > 0) {
22681:                 my %lt = &usersession_titles();
22682:                 $resulttext = &mt('Changes made:').'<ul>';
22683:                 foreach my $prefix (@prefixes) {
22684:                     if (ref($changes{$prefix}) eq 'HASH') {
22685:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22686:                         if ($prefix eq 'spares') {
22687:                             if (ref($changes{$prefix}) eq 'HASH') {
22688:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
22689:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
22690:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
22691:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
22692:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22693:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
22694:                                         foreach my $type (@{$types{$prefix}}) {
22695:                                             if ($changes{$prefix}{$lonhost}{$type}) {
22696:                                                 my $offloadto = &mt('None');
22697:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
22698:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
22699:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
22700:                                                     }
22701:                                                 }
22702:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
22703:                                             }
22704:                                         }
22705:                                     }
22706:                                     $resulttext .= '</li>';
22707:                                 }
22708:                             }
22709:                         } else {
22710:                             foreach my $type (@{$types{$prefix}}) {
22711:                                 if (defined($changes{$prefix}{$type})) {
22712:                                     my ($newvalue,$notinuse);
22713:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22714:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
22715:                                             if ($type eq 'version') {
22716:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
22717:                                             } else {
22718:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22719:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
22720:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
22721:                                                     }
22722:                                                 } else {
22723:                                                     $notinuse = 1;
22724:                                                 }
22725:                                             }
22726:                                         }
22727:                                     }
22728:                                     if ($newvalue eq '') {
22729:                                         if ($type eq 'version') {
22730:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
22731:                                         } elsif ($notinuse) {
22732:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22733:                                         } else {
22734:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22735:                                         }
22736:                                     } else {
22737:                                         if ($type eq 'version') {
22738:                                             $newvalue .= ' '.&mt('(or later)');
22739:                                         }
22740:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22741:                                     }
22742:                                 }
22743:                             }
22744:                         }
22745:                         $resulttext .= '</ul>';
22746:                     }
22747:                 }
22748:                 if ($changes{'offloadnow'}) {
22749:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22750:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
22751:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
22752:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
22753:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22754:                             }
22755:                             $resulttext .= '</ul>';
22756:                         } else {
22757:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
22758:                         }
22759:                     } else {
22760:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
22761:                     }
22762:                 }
22763:                 if ($changes{'offloadoth'}) {
22764:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22765:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
22766:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
22767:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
22768:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22769:                             }
22770:                             $resulttext .= '</ul>';
22771:                         } else {
22772:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
22773:                         }
22774:                     } else {
22775:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
22776:                     }
22777:                 }
22778:                 $resulttext .= '</ul>';
22779:             } else {
22780:                 $resulttext = $nochgmsg;
22781:             }
22782:         } else {
22783:             $resulttext = '<span class="LC_error">'.
22784:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22785:         }
22786:     } else {
22787:         $resulttext = $nochgmsg;
22788:     }
22789:     return $resulttext;
22790: }
22791: 
22792: sub modify_ssl {
22793:     my ($dom,$lastactref,%domconfig) = @_;
22794:     my (%by_ip,%by_location,@intdoms,@instdoms);
22795:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22796:     my @locations = sort(keys(%by_location));
22797:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22798:     my (%defaultshash,%changes);
22799:     my $action = 'ssl';
22800:     my @prefixes = ('connto','connfrom','replication');
22801:     foreach my $prefix (@prefixes) {
22802:         $defaultshash{$action}{$prefix} = {};
22803:     }
22804:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22805:     my $resulttext;
22806:     my %iphost = &Apache::lonnet::get_iphost();
22807:     my @reptypes = ('certreq','nocertreq');
22808:     my @connecttypes = ('dom','intdom','other');
22809:     my %types = (
22810:                   connto      => \@connecttypes,
22811:                   connfrom    => \@connecttypes,
22812:                   replication => \@reptypes,
22813:                 );
22814:     foreach my $prefix (sort(keys(%types))) {
22815:         foreach my $type (@{$types{$prefix}}) {
22816:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22817:                 my $value = 'yes';
22818:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
22819:                     $value = $env{'form.'.$prefix.'_'.$type};
22820:                 }
22821:                 if (ref($domconfig{$action}) eq 'HASH') {
22822:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22823:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
22824:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
22825:                                 $changes{$prefix}{$type} = 1;
22826:                             }
22827:                             $defaultshash{$action}{$prefix}{$type} = $value;
22828:                         } else {
22829:                             $defaultshash{$action}{$prefix}{$type} = $value;
22830:                             $changes{$prefix}{$type} = 1;
22831:                         }
22832:                     } else {
22833:                         $defaultshash{$action}{$prefix}{$type} = $value;
22834:                         $changes{$prefix}{$type} = 1;
22835:                     }
22836:                 } else {
22837:                     $defaultshash{$action}{$prefix}{$type} = $value;
22838:                     $changes{$prefix}{$type} = 1;
22839:                 }
22840:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
22841:                     delete($changes{$prefix}{$type});
22842:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
22843:                     delete($changes{$prefix}{$type});
22844:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
22845:                     delete($changes{$prefix}{$type});
22846:                 }
22847:             } elsif ($prefix eq 'replication') {
22848:                 if (@locations > 0) {
22849:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22850:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22851:                     my @okvals;
22852:                     foreach my $val (@vals) {
22853:                         if ($val =~ /:/) {
22854:                             my @items = split(/:/,$val);
22855:                             foreach my $item (@items) {
22856:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22857:                                     push(@okvals,$item);
22858:                                 }
22859:                             }
22860:                         } else {
22861:                             if (ref($by_location{$val}) eq 'ARRAY') {
22862:                                 push(@okvals,$val);
22863:                             }
22864:                         }
22865:                     }
22866:                     @okvals = sort(@okvals);
22867:                     if (ref($domconfig{$action}) eq 'HASH') {
22868:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22869:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22870:                                 if ($inuse == 0) {
22871:                                     $changes{$prefix}{$type} = 1;
22872:                                 } else {
22873:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22874:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22875:                                     if (@changed > 0) {
22876:                                         $changes{$prefix}{$type} = 1;
22877:                                     }
22878:                                 }
22879:                             } else {
22880:                                 if ($inuse == 1) {
22881:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22882:                                     $changes{$prefix}{$type} = 1;
22883:                                 }
22884:                             }
22885:                         } else {
22886:                             if ($inuse == 1) {
22887:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22888:                                 $changes{$prefix}{$type} = 1;
22889:                             }
22890:                         }
22891:                     } else {
22892:                         if ($inuse == 1) {
22893:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22894:                             $changes{$prefix}{$type} = 1;
22895:                         }
22896:                     }
22897:                 }
22898:             }
22899:         }
22900:     }
22901:     if (keys(%changes)) {
22902:         foreach my $prefix (keys(%changes)) {
22903:             if (ref($changes{$prefix}) eq 'HASH') {
22904:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22905:                     delete($changes{$prefix});
22906:                 }
22907:             } else {
22908:                 delete($changes{$prefix});
22909:             }
22910:         }
22911:     }
22912:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22913:     if (keys(%changes) > 0) {
22914:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22915:                                                  $dom);
22916:         if ($putresult eq 'ok') {
22917:             if (ref($defaultshash{$action}) eq 'HASH') {
22918:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22919:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22920:                 }
22921:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22922:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22923:                 }
22924:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22925:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22926:                 }
22927:             }
22928:             my $cachetime = 24*60*60;
22929:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22930:             if (ref($lastactref) eq 'HASH') {
22931:                 $lastactref->{'domdefaults'} = 1;
22932:             }
22933:             if (keys(%changes) > 0) {
22934:                 my %titles = &ssl_titles();
22935:                 $resulttext = &mt('Changes made:').'<ul>';
22936:                 foreach my $prefix (@prefixes) {
22937:                     if (ref($changes{$prefix}) eq 'HASH') {
22938:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22939:                         foreach my $type (@{$types{$prefix}}) {
22940:                             if (defined($changes{$prefix}{$type})) {
22941:                                 my ($newvalue,$notinuse);
22942:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22943:                                     if (ref($defaultshash{$action}{$prefix})) {
22944:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22945:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22946:                                         } else {
22947:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22948:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22949:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22950:                                                 }
22951:                                             } else {
22952:                                                 $notinuse = 1;
22953:                                             }
22954:                                         }
22955:                                     }
22956:                                     if ($notinuse) {
22957:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22958:                                     } elsif ($newvalue eq '') {
22959:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
22960:                                     } else {
22961:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
22962:                                     }
22963:                                 }
22964:                             }
22965:                         }
22966:                         $resulttext .= '</ul>';
22967:                     }
22968:                 }
22969:             } else {
22970:                 $resulttext = $nochgmsg;
22971:             }
22972:         } else {
22973:             $resulttext = '<span class="LC_error">'.
22974:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22975:         }
22976:     } else {
22977:         $resulttext = $nochgmsg;
22978:     }
22979:     return $resulttext;
22980: }
22981: 
22982: sub modify_trust {
22983:     my ($dom,$lastactref,%domconfig) = @_;
22984:     my (%by_ip,%by_location,@intdoms,@instdoms);
22985:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22986:     my @locations = sort(keys(%by_location));
22987:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
22988:     my @types = ('exc','inc');
22989:     my (%defaultshash,%changes);
22990:     foreach my $prefix (@prefixes) {
22991:         $defaultshash{'trust'}{$prefix} = {};
22992:     }
22993:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22994:     my $resulttext;
22995:     foreach my $prefix (@prefixes) {
22996:         foreach my $type (@types) {
22997:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22998:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22999:             my @okvals;
23000:             foreach my $val (@vals) {
23001:                 if ($val =~ /:/) {
23002:                     my @items = split(/:/,$val);
23003:                     foreach my $item (@items) {
23004:                         if (ref($by_location{$item}) eq 'ARRAY') {
23005:                             push(@okvals,$item);
23006:                         }
23007:                     }
23008:                 } else {
23009:                     if (ref($by_location{$val}) eq 'ARRAY') {
23010:                         push(@okvals,$val);
23011:                     }
23012:                 }
23013:             }
23014:             @okvals = sort(@okvals);
23015:             if (ref($domconfig{'trust'}) eq 'HASH') {
23016:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
23017:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23018:                         if ($inuse == 0) {
23019:                             $changes{$prefix}{$type} = 1;
23020:                         } else {
23021:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23022:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
23023:                             if (@changed > 0) {
23024:                                 $changes{$prefix}{$type} = 1;
23025:                             }
23026:                         }
23027:                     } else {
23028:                         if ($inuse == 1) {
23029:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23030:                             $changes{$prefix}{$type} = 1;
23031:                         }
23032:                     }
23033:                 } else {
23034:                     if ($inuse == 1) {
23035:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23036:                         $changes{$prefix}{$type} = 1;
23037:                     }
23038:                 }
23039:             } else {
23040:                 if ($inuse == 1) {
23041:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23042:                     $changes{$prefix}{$type} = 1;
23043:                 }
23044:             }
23045:         }
23046:     }
23047:     my $nochgmsg = &mt('No changes made to trust settings.');
23048:     if (keys(%changes) > 0) {
23049:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
23050:                                                  $dom);
23051:         if ($putresult eq 'ok') {
23052:             if (ref($defaultshash{'trust'}) eq 'HASH') {
23053:                 foreach my $prefix (@prefixes) {
23054:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
23055:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
23056:                     }
23057:                 }
23058:             }
23059:             my $cachetime = 24*60*60;
23060:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
23061:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
23062:             if (ref($lastactref) eq 'HASH') {
23063:                 $lastactref->{'domdefaults'} = 1;
23064:                 $lastactref->{'trust'} = 1;
23065:             }
23066:             if (keys(%changes) > 0) {
23067:                 my %lt = &trust_titles();
23068:                 $resulttext = &mt('Changes made:').'<ul>';
23069:                 foreach my $prefix (@prefixes) {
23070:                     if (ref($changes{$prefix}) eq 'HASH') {
23071:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
23072:                         foreach my $type (@types) {
23073:                             if (defined($changes{$prefix}{$type})) {
23074:                                 my ($newvalue,$notinuse);
23075:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
23076:                                     if (ref($defaultshash{'trust'}{$prefix})) {
23077:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23078:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
23079:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
23080:                                             }
23081:                                         } else {
23082:                                             $notinuse = 1;
23083:                                         }
23084:                                     }
23085:                                 }
23086:                                 if ($notinuse) {
23087:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
23088:                                 } elsif ($newvalue eq '') {
23089:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
23090:                                 } else {
23091:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
23092:                                 }
23093:                             }
23094:                         }
23095:                         $resulttext .= '</ul>';
23096:                     }
23097:                 }
23098:                 $resulttext .= '</ul>';
23099:             } else {
23100:                 $resulttext = $nochgmsg;
23101:             }
23102:         } else {
23103:             $resulttext = '<span class="LC_error">'.
23104:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23105:         }
23106:     } else {
23107:         $resulttext = $nochgmsg;
23108:     }
23109:     return $resulttext;
23110: }
23111: 
23112: sub modify_loadbalancing {
23113:     my ($dom,%domconfig) = @_;
23114:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
23115:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
23116:     my ($othertitle,$usertypes,$types) =
23117:         &Apache::loncommon::sorted_inst_types($dom);
23118:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23119:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
23120:     my @sparestypes = ('primary','default');
23121:     my %typetitles = &sparestype_titles();
23122:     my $resulttext;
23123:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23124:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23125:         %existing = %{$domconfig{'loadbalancing'}};
23126:     }
23127:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
23128:                               \%currtargets,\%currrules,\%currcookies);
23129:     my ($saveloadbalancing,%defaultshash,%changes);
23130:     my ($alltypes,$othertypes,$titles) =
23131:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
23132:     my %ruletitles = &offloadtype_text();
23133:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
23134:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
23135:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
23136:         if ($balancer eq '') {
23137:             next;
23138:         }
23139:         if (!exists($servers{$balancer})) {
23140:             if (exists($currbalancer{$balancer})) {
23141:                 push(@{$changes{'delete'}},$balancer);
23142:             }
23143:             next;
23144:         }
23145:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
23146:             push(@{$changes{'delete'}},$balancer);
23147:             next;
23148:         }
23149:         if (!exists($currbalancer{$balancer})) {
23150:             push(@{$changes{'add'}},$balancer);
23151:         }
23152:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
23153:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
23154:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
23155:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23156:             $saveloadbalancing = 1;
23157:         }
23158:         foreach my $sparetype (@sparestypes) {
23159:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
23160:             my @offloadto;
23161:             foreach my $target (@targets) {
23162:                 if (($servers{$target}) && ($target ne $balancer)) {
23163:                     if ($sparetype eq 'default') {
23164:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
23165:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
23166:                         }
23167:                     }
23168:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
23169:                         push(@offloadto,$target);
23170:                     }
23171:                 }
23172:             }
23173:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
23174:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
23175:                     push(@offloadto,$balancer);
23176:                 }
23177:             }
23178:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
23179:         }
23180:         if ($env{'form.loadbalancing_cookie_'.$i}) {
23181:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
23182:             if (exists($currbalancer{$balancer})) {
23183:                 unless ($currcookies{$balancer}) {
23184:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
23185:                 }
23186:             }
23187:         } elsif (exists($currbalancer{$balancer})) {
23188:             if ($currcookies{$balancer}) {
23189:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
23190:             }
23191:         }
23192:         if (ref($currtargets{$balancer}) eq 'HASH') {
23193:             foreach my $sparetype (@sparestypes) {
23194:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
23195:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
23196:                     if (@targetdiffs > 0) {
23197:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23198:                     }
23199:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23200:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23201:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23202:                     }
23203:                 }
23204:             }
23205:         } else {
23206:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
23207:                 foreach my $sparetype (@sparestypes) {
23208:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23209:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23210:                             $changes{'curr'}{$balancer}{'targets'} = 1;
23211:                         }
23212:                     }
23213:                 }
23214:             }
23215:         }
23216:         my $ishomedom;
23217:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
23218:             $ishomedom = 1;
23219:         }
23220:         if (ref($alltypes) eq 'ARRAY') {
23221:             foreach my $type (@{$alltypes}) {
23222:                 my $rule;
23223:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
23224:                          (!$ishomedom)) {
23225:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
23226:                 }
23227:                 if ($rule eq 'specific') {
23228:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
23229:                     if (exists($servers{$specifiedhost})) {
23230:                         $rule = $specifiedhost;
23231:                     }
23232:                 }
23233:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
23234:                 if (ref($currrules{$balancer}) eq 'HASH') {
23235:                     if ($rule ne $currrules{$balancer}{$type}) {
23236:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23237:                     }
23238:                 } elsif ($rule ne '') {
23239:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23240:                 }
23241:             }
23242:         }
23243:     }
23244:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
23245:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
23246:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
23247:             $defaultshash{'loadbalancing'} = {};
23248:         }
23249:         my $putresult = &Apache::lonnet::put_dom('configuration',
23250:                                                  \%defaultshash,$dom);
23251:         if ($putresult eq 'ok') {
23252:             if (keys(%changes) > 0) {
23253:                 my %toupdate;
23254:                 if (ref($changes{'delete'}) eq 'ARRAY') {
23255:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
23256:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
23257:                         $toupdate{$balancer} = 1;
23258:                     }
23259:                 }
23260:                 if (ref($changes{'add'}) eq 'ARRAY') {
23261:                     foreach my $balancer (sort(@{$changes{'add'}})) {
23262:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
23263:                         $toupdate{$balancer} = 1;
23264:                     }
23265:                 }
23266:                 if (ref($changes{'curr'}) eq 'HASH') {
23267:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
23268:                         $toupdate{$balancer} = 1;
23269:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
23270:                             if ($changes{'curr'}{$balancer}{'targets'}) {
23271:                                 my %offloadstr;
23272:                                 foreach my $sparetype (@sparestypes) {
23273:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23274:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23275:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23276:                                         }
23277:                                     }
23278:                                 }
23279:                                 if (keys(%offloadstr) == 0) {
23280:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
23281:                                 } else {
23282:                                     my $showoffload;
23283:                                     foreach my $sparetype (@sparestypes) {
23284:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
23285:                                         if (defined($offloadstr{$sparetype})) {
23286:                                             $showoffload .= $offloadstr{$sparetype};
23287:                                         } else {
23288:                                             $showoffload .= &mt('None');
23289:                                         }
23290:                                         $showoffload .= ('&nbsp;'x3);
23291:                                     }
23292:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
23293:                                 }
23294:                             }
23295:                         }
23296:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
23297:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
23298:                                 foreach my $type (@{$alltypes}) {
23299:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
23300:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23301:                                         my $balancetext;
23302:                                         if ($rule eq '') {
23303:                                             $balancetext =  $ruletitles{'default'};
23304:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
23305:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23306:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23307:                                                 foreach my $sparetype (@sparestypes) {
23308:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23309:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23310:                                                     }
23311:                                                 }
23312:                                                 foreach my $item (@{$alltypes}) {
23313:                                                     next if ($item =~  /^_LC_ipchange/);
23314:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
23315:                                                     if ($hasrule eq 'homeserver') {
23316:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
23317:                                                     } else {
23318:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
23319:                                                             if ($servers{$hasrule}) {
23320:                                                                 $toupdate{$hasrule} = 1;
23321:                                                             }
23322:                                                         }
23323:                                                     }
23324:                                                 }
23325:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
23326:                                                     $balancetext =  $ruletitles{$rule};
23327:                                                 } else {
23328:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23329:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
23330:                                                     if ($receiver) {
23331:                                                         $toupdate{$receiver};
23332:                                                     }
23333:                                                 }
23334:                                             } else {
23335:                                                 $balancetext =  $ruletitles{$rule};
23336:                                             }
23337:                                         } else {
23338:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
23339:                                         }
23340:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
23341:                                     }
23342:                                 }
23343:                             }
23344:                         }
23345:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
23346:                             if ($currcookies{$balancer}) {
23347:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
23348:                                                           $balancer).'</li>';
23349:                             } else {
23350:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
23351:                                                           $balancer).'</li>';
23352:                             }
23353:                         }
23354:                     }
23355:                 }
23356:                 if (keys(%toupdate)) {
23357:                     my %thismachine;
23358:                     my $updatedhere;
23359:                     my $cachetime = 60*60*24;
23360:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23361:                     foreach my $lonhost (keys(%toupdate)) {
23362:                         if ($thismachine{$lonhost}) {
23363:                             unless ($updatedhere) {
23364:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
23365:                                                               $defaultshash{'loadbalancing'},
23366:                                                               $cachetime);
23367:                                 $updatedhere = 1;
23368:                             }
23369:                         } else {
23370:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
23371:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
23372:                         }
23373:                     }
23374:                 }
23375:                 if ($resulttext ne '') {
23376:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
23377:                 } else {
23378:                     $resulttext = $nochgmsg;
23379:                 }
23380:             } else {
23381:                 $resulttext = $nochgmsg;
23382:             }
23383:         } else {
23384:             $resulttext = '<span class="LC_error">'.
23385:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23386:         }
23387:     } else {
23388:         $resulttext = $nochgmsg;
23389:     }
23390:     return $resulttext;
23391: }
23392: 
23393: sub recurse_check {
23394:     my ($chkcats,$categories,$depth,$name) = @_;
23395:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
23396:         my $chg = 0;
23397:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
23398:             my $category = $chkcats->[$depth]{$name}[$j];
23399:             my $item;
23400:             if ($category eq '') {
23401:                 $chg ++;
23402:             } else {
23403:                 my $deeper = $depth + 1;
23404:                 $item = &escape($category).':'.&escape($name).':'.$depth;
23405:                 if ($chg) {
23406:                     $categories->{$item} -= $chg;
23407:                 }
23408:                 &recurse_check($chkcats,$categories,$deeper,$category);
23409:                 $deeper --;
23410:             }
23411:         }
23412:     }
23413:     return;
23414: }
23415: 
23416: sub recurse_cat_deletes {
23417:     my ($item,$coursecategories,$deletions) = @_;
23418:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
23419:     my $subdepth = $depth + 1;
23420:     if (ref($coursecategories) eq 'HASH') {
23421:         foreach my $subitem (keys(%{$coursecategories})) {
23422:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
23423:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
23424:                 delete($coursecategories->{$subitem});
23425:                 $deletions->{$subitem} = 1;
23426:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
23427:             }
23428:         }
23429:     }
23430:     return;
23431: }
23432: 
23433: sub active_dc_picker {
23434:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
23435:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
23436:     my @domcoord = keys(%domcoords);
23437:     if (keys(%currhash)) {
23438:         foreach my $dc (keys(%currhash)) {
23439:             unless (exists($domcoords{$dc})) {
23440:                 push(@domcoord,$dc);
23441:             }
23442:         }
23443:     }
23444:     @domcoord = sort(@domcoord);
23445:     my $numdcs = scalar(@domcoord);
23446:     my $rows = 0;
23447:     my $table;
23448:     if ($numdcs > 1) {
23449:         $table = '<table>';
23450:         for (my $i=0; $i<@domcoord; $i++) {
23451:             my $rem = $i%($numinrow);
23452:             if ($rem == 0) {
23453:                 if ($i > 0) {
23454:                     $table .= '</tr>';
23455:                 }
23456:                 $table .= '<tr>';
23457:                 $rows ++;
23458:             }
23459:             my $check = '';
23460:             if ($inputtype eq 'radio') {
23461:                 if (keys(%currhash) == 0) {
23462:                     if (!$i) {
23463:                         $check = ' checked="checked"';
23464:                     }
23465:                 } elsif (exists($currhash{$domcoord[$i]})) {
23466:                     $check = ' checked="checked"';
23467:                 }
23468:             } else {
23469:                 if (exists($currhash{$domcoord[$i]})) {
23470:                     $check = ' checked="checked"';
23471:                 }
23472:             }
23473:             if ($i == @domcoord - 1) {
23474:                 my $colsleft = $numinrow - $rem;
23475:                 if ($colsleft > 1) {
23476:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
23477:                 } else {
23478:                     $table .= '<td class="LC_left_item">';
23479:                 }
23480:             } else {
23481:                 $table .= '<td class="LC_left_item">';
23482:             }
23483:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
23484:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23485:             $table .= '<span class="LC_nobreak"><label>'.
23486:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
23487:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
23488:             if ($user ne $dcname.':'.$dcdom) {
23489:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23490:             }
23491:             $table .= '</label></span></td>';
23492:         }
23493:         $table .= '</tr></table>';
23494:     } elsif ($numdcs == 1) {
23495:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
23496:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23497:         if ($inputtype eq 'radio') {
23498:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
23499:             if ($user ne $dcname.':'.$dcdom) {
23500:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23501:             }
23502:         } else {
23503:             my $check;
23504:             if (exists($currhash{$domcoord[0]})) {
23505:                 $check = ' checked="checked"';
23506:             }
23507:             $table = '<span class="LC_nobreak"><label>'.
23508:                      '<input type="checkbox" name="'.$name.'" '.
23509:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
23510:             if ($user ne $dcname.':'.$dcdom) {
23511:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23512:             }
23513:             $table .= '</label></span>';
23514:             $rows ++;
23515:         }
23516:     }
23517:     return ($numdcs,$table,$rows);
23518: }
23519: 
23520: sub usersession_titles {
23521:     return &Apache::lonlocal::texthash(
23522:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
23523:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
23524:                spares => 'Servers offloaded to, when busy',
23525:                version => 'LON-CAPA version requirement',
23526:                excludedomain => 'Allow all, but exclude specific domains',
23527:                includedomain => 'Deny all, but include specific domains',
23528:                primary => 'Primary (checked first)',
23529:                default => 'Default',
23530:            );
23531: }
23532: 
23533: sub id_for_thisdom {
23534:     my (%servers) = @_;
23535:     my %altids;
23536:     foreach my $server (keys(%servers)) {
23537:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
23538:         if ($serverhome ne $server) {
23539:             $altids{$serverhome} = $server;
23540:         }
23541:     }
23542:     return %altids;
23543: }
23544: 
23545: sub count_servers {
23546:     my ($currbalancer,%servers) = @_;
23547:     my (@spares,$numspares);
23548:     foreach my $lonhost (sort(keys(%servers))) {
23549:         next if ($currbalancer eq $lonhost);
23550:         push(@spares,$lonhost);
23551:     }
23552:     if ($currbalancer) {
23553:         $numspares = scalar(@spares);
23554:     } else {
23555:         $numspares = scalar(@spares) - 1;
23556:     }
23557:     return ($numspares,@spares);
23558: }
23559: 
23560: sub lonbalance_targets_js {
23561:     my ($dom,$types,$servers,$settings) = @_;
23562:     my $select = &mt('Select');
23563:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
23564:     if (ref($servers) eq 'HASH') {
23565:         $alltargets = join("','",sort(keys(%{$servers})));
23566:         my @homedoms;
23567:         foreach my $server (sort(keys(%{$servers}))) {
23568:             if (&Apache::lonnet::host_domain($server) eq $dom) {
23569:                 push(@homedoms,'1');
23570:             } else {
23571:                 push(@homedoms,'0');
23572:             }
23573:         }
23574:         $allishome = join("','",@homedoms);
23575:     }
23576:     if (ref($types) eq 'ARRAY') {
23577:         if (@{$types} > 0) {
23578:             @alltypes = @{$types};
23579:         }
23580:     }
23581:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
23582:     $allinsttypes = join("','",@alltypes);
23583:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23584:     if (ref($settings) eq 'HASH') {
23585:         %existing = %{$settings};
23586:     }
23587:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
23588:                               \%currtargets,\%currrules,\%currcookies);
23589:     my $balancers = join("','",sort(keys(%currbalancer)));
23590:     return <<"END";
23591: 
23592: <script type="text/javascript">
23593: // <![CDATA[
23594: 
23595: currBalancers = new Array('$balancers');
23596: 
23597: function toggleTargets(balnum) {
23598:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23599:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
23600:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
23601:     var prevbalancer = prevhostitem.value;
23602:     var baltotal = document.getElementById('loadbalancing_total').value;
23603:     prevhostitem.value = balancer;
23604:     if (prevbalancer != '') {
23605:         var prevIdx = currBalancers.indexOf(prevbalancer);
23606:         if (prevIdx != -1) {
23607:             currBalancers.splice(prevIdx,1);
23608:         }
23609:     }
23610:     if (balancer == '') {
23611:         hideSpares(balnum);
23612:     } else {
23613:         var currIdx = currBalancers.indexOf(balancer);
23614:         if (currIdx == -1) {
23615:             currBalancers.push(balancer);
23616:         }
23617:         var homedoms = new Array('$allishome');
23618:         var ishomedom = homedoms[lonhostitem.selectedIndex];
23619:         showSpares(balancer,ishomedom,balnum);
23620:     }
23621:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
23622:     return;
23623: }
23624: 
23625: function showSpares(balancer,ishomedom,balnum) {
23626:     var alltargets = new Array('$alltargets');
23627:     var insttypes = new Array('$allinsttypes');
23628:     var offloadtypes = new Array('primary','default');
23629: 
23630:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
23631:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
23632:  
23633:     for (var i=0; i<offloadtypes.length; i++) {
23634:         var count = 0;
23635:         for (var j=0; j<alltargets.length; j++) {
23636:             if (alltargets[j] != balancer) {
23637:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
23638:                 item.value = alltargets[j];
23639:                 item.style.textAlign='left';
23640:                 item.style.textFace='normal';
23641:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
23642:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
23643:                     item.disabled = '';
23644:                 } else {
23645:                     item.disabled = 'disabled';
23646:                     item.checked = false;
23647:                 }
23648:                 count ++;
23649:             }
23650:         }
23651:     }
23652:     for (var k=0; k<insttypes.length; k++) {
23653:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
23654:             if (ishomedom == 1) {
23655:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23656:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23657:             } else {
23658:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23659:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23660:             }
23661:         } else {
23662:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23663:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23664:         }
23665:         if ((insttypes[k] != '_LC_external') && 
23666:             ((insttypes[k] != '_LC_internetdom') ||
23667:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
23668:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
23669:             item.options.length = 0;
23670:             item.options[0] = new Option("","",true,true);
23671:             var idx = 0;
23672:             for (var m=0; m<alltargets.length; m++) {
23673:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
23674:                     idx ++;
23675:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23676:                 }
23677:             }
23678:         }
23679:     }
23680:     return;
23681: }
23682: 
23683: function hideSpares(balnum) {
23684:     var alltargets = new Array('$alltargets');
23685:     var insttypes = new Array('$allinsttypes');
23686:     var offloadtypes = new Array('primary','default');
23687: 
23688:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
23689:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
23690: 
23691:     var total = alltargets.length - 1;
23692:     for (var i=0; i<offloadtypes; i++) {
23693:         for (var j=0; j<total; j++) {
23694:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
23695:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
23696:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
23697:         }
23698:     }
23699:     for (var k=0; k<insttypes.length; k++) {
23700:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23701:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23702:         if (insttypes[k] != '_LC_external') {
23703:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
23704:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
23705:         }
23706:     }
23707:     return;
23708: }
23709: 
23710: function checkOffloads(item,balnum,type) {
23711:     var alltargets = new Array('$alltargets');
23712:     var offloadtypes = new Array('primary','default');
23713:     if (item.checked) {
23714:         var total = alltargets.length - 1;
23715:         var other;
23716:         if (type == offloadtypes[0]) {
23717:             other = offloadtypes[1];
23718:         } else {
23719:             other = offloadtypes[0];
23720:         }
23721:         for (var i=0; i<total; i++) {
23722:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
23723:             if (server == item.value) {
23724:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
23725:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
23726:                 }
23727:             }
23728:         }
23729:     }
23730:     return;
23731: }
23732: 
23733: function singleServerToggle(balnum,type) {
23734:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
23735:     if (offloadtoSelIdx == 0) {
23736:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
23737:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23738: 
23739:     } else {
23740:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
23741:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23742:     }
23743:     return;
23744: }
23745: 
23746: function balanceruleChange(formname,balnum,type) {
23747:     if (type == '_LC_external') {
23748:         return;
23749:     }
23750:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
23751:     for (var i=0; i<typesRules.length; i++) {
23752:         if (formname.elements[typesRules[i]].checked) {
23753:             if (formname.elements[typesRules[i]].value != 'specific') {
23754:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
23755:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23756:             } else {
23757:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23758:             }
23759:         }
23760:     }
23761:     return;
23762: }
23763: 
23764: function balancerDeleteChange(balnum) {
23765:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23766:     var baltotal = document.getElementById('loadbalancing_total').value;
23767:     var addtarget;
23768:     var removetarget;
23769:     var action = 'delete';
23770:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
23771:         var lonhost = hostitem.value;
23772:         var currIdx = currBalancers.indexOf(lonhost);
23773:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
23774:             if (currIdx != -1) {
23775:                 currBalancers.splice(currIdx,1);
23776:             }
23777:             addtarget = lonhost;
23778:         } else {
23779:             if (currIdx == -1) {
23780:                 currBalancers.push(lonhost);
23781:             }
23782:             removetarget = lonhost;
23783:             action = 'undelete';
23784:         }
23785:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
23786:     }
23787:     return;
23788: }
23789: 
23790: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
23791:     if (baltotal > 1) {
23792:         var offloadtypes = new Array('primary','default');
23793:         var alltargets = new Array('$alltargets');
23794:         var insttypes = new Array('$allinsttypes');
23795:         for (var i=0; i<baltotal; i++) {
23796:             if (i != balnum) {
23797:                 for (var j=0; j<offloadtypes.length; j++) {
23798:                     var total = alltargets.length - 1;
23799:                     for (var k=0; k<total; k++) {
23800:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
23801:                         var server = serveritem.value;
23802:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
23803:                             if (server == addtarget) {
23804:                                 serveritem.disabled = '';
23805:                             }
23806:                         }
23807:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23808:                             if (server == removetarget) {
23809:                                 serveritem.disabled = 'disabled';
23810:                                 serveritem.checked = false;
23811:                             }
23812:                         }
23813:                     }
23814:                 }
23815:                 for (var j=0; j<insttypes.length; j++) {
23816:                     if (insttypes[j] != '_LC_external') {
23817:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
23818:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
23819:                             var currSel = singleserver.selectedIndex;
23820:                             var currVal = singleserver.options[currSel].value;
23821:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
23822:                                 var numoptions = singleserver.options.length;
23823:                                 var needsnew = 1;
23824:                                 for (var k=0; k<numoptions; k++) {
23825:                                     if (singleserver.options[k] == addtarget) {
23826:                                         needsnew = 0;
23827:                                         break;
23828:                                     }
23829:                                 }
23830:                                 if (needsnew == 1) {
23831:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
23832:                                 }
23833:                             }
23834:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23835:                                 singleserver.options.length = 0;
23836:                                 if ((currVal) && (currVal != removetarget)) {
23837:                                     singleserver.options[0] = new Option("","",false,false);
23838:                                 } else {
23839:                                     singleserver.options[0] = new Option("","",true,true);
23840:                                 }
23841:                                 var idx = 0;
23842:                                 for (var m=0; m<alltargets.length; m++) {
23843:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
23844:                                         idx ++;
23845:                                         if (currVal == alltargets[m]) {
23846:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
23847:                                         } else {
23848:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23849:                                         }
23850:                                     }
23851:                                 }
23852:                             }
23853:                         }
23854:                     }
23855:                 }
23856:             }
23857:         }
23858:     }
23859:     return;
23860: }
23861: 
23862: // ]]>
23863: </script>
23864: 
23865: END
23866: }
23867: 
23868: sub new_spares_js {
23869:     my @sparestypes = ('primary','default');
23870:     my $types = join("','",@sparestypes);
23871:     my $select = &mt('Select');
23872:     return <<"END";
23873: 
23874: <script type="text/javascript">
23875: // <![CDATA[
23876: 
23877: function updateNewSpares(formname,lonhost) {
23878:     var types = new Array('$types');
23879:     var include = new Array();
23880:     var exclude = new Array();
23881:     for (var i=0; i<types.length; i++) {
23882:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23883:         for (var j=0; j<spareboxes.length; j++) {
23884:             if (formname.elements[spareboxes[j]].checked) {
23885:                 exclude.push(formname.elements[spareboxes[j]].value);
23886:             } else {
23887:                 include.push(formname.elements[spareboxes[j]].value);
23888:             }
23889:         }
23890:     }
23891:     for (var i=0; i<types.length; i++) {
23892:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23893:         var selIdx = newSpare.selectedIndex;
23894:         var currnew = newSpare.options[selIdx].value;
23895:         var okSpares = new Array();
23896:         for (var j=0; j<newSpare.options.length; j++) {
23897:             var possible = newSpare.options[j].value;
23898:             if (possible != '') {
23899:                 if (exclude.indexOf(possible) == -1) {
23900:                     okSpares.push(possible);
23901:                 } else {
23902:                     if (currnew == possible) {
23903:                         selIdx = 0;
23904:                     }
23905:                 }
23906:             }
23907:         }
23908:         for (var k=0; k<include.length; k++) {
23909:             if (okSpares.indexOf(include[k]) == -1) {
23910:                 okSpares.push(include[k]);
23911:             }
23912:         }
23913:         okSpares.sort();
23914:         newSpare.options.length = 0;
23915:         if (selIdx == 0) {
23916:             newSpare.options[0] = new Option("$select","",true,true);
23917:         } else {
23918:             newSpare.options[0] = new Option("$select","",false,false);
23919:         }
23920:         for (var m=0; m<okSpares.length; m++) {
23921:             var idx = m+1;
23922:             var selThis = 0;
23923:             if (selIdx != 0) {
23924:                 if (okSpares[m] == currnew) {
23925:                     selThis = 1;
23926:                 }
23927:             }
23928:             if (selThis == 1) {
23929:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23930:             } else {
23931:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23932:             }
23933:         }
23934:     }
23935:     return;
23936: }
23937: 
23938: function checkNewSpares(lonhost,type) {
23939:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23940:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23941:     if (chosen != '') {
23942:         var othertype;
23943:         var othernewSpare;
23944:         if (type == 'primary') {
23945:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23946:         }
23947:         if (type == 'default') {
23948:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23949:         }
23950:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23951:             othernewSpare.selectedIndex = 0;
23952:         }
23953:     }
23954:     return;
23955: }
23956: 
23957: // ]]>
23958: </script>
23959: 
23960: END
23961: 
23962: }
23963: 
23964: sub common_domprefs_js {
23965:     return <<"END";
23966: 
23967: <script type="text/javascript">
23968: // <![CDATA[
23969: 
23970: function getIndicesByName(formname,item) {
23971:     var group = new Array();
23972:     for (var i=0;i<formname.elements.length;i++) {
23973:         if (formname.elements[i].name == item) {
23974:             group.push(formname.elements[i].id);
23975:         }
23976:     }
23977:     return group;
23978: }
23979: 
23980: // ]]>
23981: </script>
23982: 
23983: END
23984: 
23985: }
23986: 
23987: sub recaptcha_js {
23988:     my %lt = &captcha_phrases();
23989:     return <<"END";
23990: 
23991: <script type="text/javascript">
23992: // <![CDATA[
23993: 
23994: function updateCaptcha(caller,context) {
23995:     var privitem;
23996:     var pubitem;
23997:     var privtext;
23998:     var pubtext;
23999:     var versionitem;
24000:     var versiontext;
24001:     if (document.getElementById(context+'_recaptchapub')) {
24002:         pubitem = document.getElementById(context+'_recaptchapub');
24003:     } else {
24004:         return;
24005:     }
24006:     if (document.getElementById(context+'_recaptchapriv')) {
24007:         privitem = document.getElementById(context+'_recaptchapriv');
24008:     } else {
24009:         return;
24010:     }
24011:     if (document.getElementById(context+'_recaptchapubtxt')) {
24012:         pubtext = document.getElementById(context+'_recaptchapubtxt');
24013:     } else {
24014:         return;
24015:     }
24016:     if (document.getElementById(context+'_recaptchaprivtxt')) {
24017:         privtext = document.getElementById(context+'_recaptchaprivtxt');
24018:     } else {
24019:         return;
24020:     }
24021:     if (document.getElementById(context+'_recaptchaversion')) {
24022:         versionitem = document.getElementById(context+'_recaptchaversion');
24023:     } else {
24024:         return;
24025:     }
24026:     if (document.getElementById(context+'_recaptchavertxt')) {
24027:         versiontext = document.getElementById(context+'_recaptchavertxt');
24028:     } else {
24029:         return;
24030:     }
24031:     if (caller.checked) {
24032:         if (caller.value == 'recaptcha') {
24033:             pubitem.type = 'text';
24034:             privitem.type = 'text';
24035:             pubitem.size = '40';
24036:             privitem.size = '40';
24037:             pubtext.innerHTML = "$lt{'pub'}";
24038:             privtext.innerHTML = "$lt{'priv'}";
24039:             versionitem.type = 'text';
24040:             versionitem.size = '3';
24041:             versiontext.innerHTML = "$lt{'ver'}";
24042:         } else {
24043:             pubitem.type = 'hidden';
24044:             privitem.type = 'hidden';
24045:             versionitem.type = 'hidden';
24046:             pubtext.innerHTML = '';
24047:             privtext.innerHTML = '';
24048:             versiontext.innerHTML = '';
24049:         }
24050:     }
24051:     return;
24052: }
24053: 
24054: // ]]>
24055: </script>
24056: 
24057: END
24058: 
24059: }
24060: 
24061: sub toggle_display_js {
24062:     return <<"END";
24063: 
24064: <script type="text/javascript">
24065: // <![CDATA[
24066: 
24067: function toggleDisplay(domForm,caller) {
24068:     if (document.getElementById(caller)) {
24069:         var divitem = document.getElementById(caller);
24070:         var optionsElement = domForm.coursecredits;
24071:         var checkval = 1;
24072:         var dispval = 'block';
24073:         var selfcreateRegExp = /^cancreate_emailverified/;
24074:         if (caller == 'emailoptions') {
24075:             optionsElement = domForm.cancreate_email;
24076:         }
24077:         if (caller == 'studentsubmission') {
24078:             optionsElement = domForm.postsubmit;
24079:         }
24080:         if (caller == 'cloneinstcode') {
24081:             optionsElement = domForm.canclone;
24082:             checkval = 'instcode';
24083:         }
24084:         if (selfcreateRegExp.test(caller)) {
24085:             optionsElement = domForm.elements[caller];
24086:             checkval = 'other';
24087:             dispval = 'inline'
24088:         }
24089:         if (optionsElement.length) {
24090:             var currval;
24091:             for (var i=0; i<optionsElement.length; i++) {
24092:                 if (optionsElement[i].checked) {
24093:                    currval = optionsElement[i].value;
24094:                 }
24095:             }
24096:             if (currval == checkval) {
24097:                 divitem.style.display = dispval;
24098:             } else {
24099:                 divitem.style.display = 'none';
24100:             }
24101:         }
24102:     }
24103:     return;
24104: }
24105: 
24106: // ]]>
24107: </script>
24108: 
24109: END
24110: 
24111: }
24112: 
24113: sub captcha_phrases {
24114:     return &Apache::lonlocal::texthash (
24115:                  priv => 'Private key',
24116:                  pub  => 'Public key',
24117:                  original  => 'original (CAPTCHA)',
24118:                  recaptcha => 'successor (ReCAPTCHA)',
24119:                  notused   => 'unused',
24120:                  ver => 'ReCAPTCHA version (1 or 2)',
24121:     );
24122: }
24123: 
24124: sub devalidate_remote_domconfs {
24125:     my ($dom,$cachekeys) = @_;
24126:     return unless (ref($cachekeys) eq 'HASH');
24127:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
24128:     my %thismachine;
24129:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
24130:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
24131:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
24132:                       'ipaccess','trust');
24133:     my %cache_by_lonhost;
24134:     if (exists($cachekeys->{'samllanding'})) {
24135:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
24136:             my %landing = %{$cachekeys->{'samllanding'}};
24137:             my %domservers = &Apache::lonnet::get_servers($dom);
24138:             if (keys(%domservers)) {
24139:                 foreach my $server (keys(%domservers)) {
24140:                     my @cached;
24141:                     next if ($thismachine{$server});
24142:                     if ($landing{$server}) {
24143:                         push(@cached,&escape('samllanding').':'.&escape($server));
24144:                     }
24145:                     if (@cached) {
24146:                         $cache_by_lonhost{$server} = \@cached;
24147:                     }
24148:                 }
24149:             }
24150:         }
24151:     }
24152:     if (keys(%servers)) {
24153:         foreach my $server (keys(%servers)) {
24154:             next if ($thismachine{$server});
24155:             my @cached;
24156:             foreach my $name (@posscached) {
24157:                 if ($cachekeys->{$name}) {
24158:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
24159:                         if (ref($cachekeys->{$name}) eq 'HASH') {
24160:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
24161:                                 push(@cached,&escape($name).':'.&escape($key));
24162:                             }
24163:                         }
24164:                     } else {
24165:                         push(@cached,&escape($name).':'.&escape($dom));
24166:                     }
24167:                 }
24168:             }
24169:             if ((exists($cache_by_lonhost{$server})) &&
24170:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
24171:                 push(@cached,@{$cache_by_lonhost{$server}});
24172:             }
24173:             if (@cached) {
24174:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
24175:             }
24176:         }
24177:     }
24178:     return;
24179: }
24180: 
24181: 1;

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