File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.394: download - view: text, annotated - select for diffs
Sun Nov 28 19:17:59 2021 UTC (2 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6955. Domain configuration to limit selection of course role based on
  user's IP address.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.394 2021/11/28 19:17:59 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use 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: 
  181: my $registered_cleanup;
  182: my $modified_urls;
  183: 
  184: sub handler {
  185:     my $r=shift;
  186:     if ($r->header_only) {
  187:         &Apache::loncommon::content_type($r,'text/html');
  188:         $r->send_http_header;
  189:         return OK;
  190:     }
  191: 
  192:     my $context = 'domain';
  193:     my $dom = $env{'request.role.domain'};
  194:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  195:     if (&Apache::lonnet::allowed('mau',$dom)) {
  196:         &Apache::loncommon::content_type($r,'text/html');
  197:         $r->send_http_header;
  198:     } else {
  199:         $env{'user.error.msg'}=
  200:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  201:         return HTTP_NOT_ACCEPTABLE;
  202:     }
  203: 
  204:     $registered_cleanup=0;
  205:     @{$modified_urls}=();
  206: 
  207:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  209:                                             ['phase','actions']);
  210:     my $phase = 'pickactions';
  211:     if ( exists($env{'form.phase'}) ) {
  212:         $phase = $env{'form.phase'};
  213:     }
  214:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  215:     my %domconfig =
  216:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  217:                 'quotas','autoenroll','autoupdate','autocreate',
  218:                 'directorysrch','usercreation','usermodification',
  219:                 'contacts','defaults','scantron','coursecategories',
  220:                 'serverstatuses','requestcourses','helpsettings',
  221:                 'coursedefaults','usersessions','loadbalancing',
  222:                 'requestauthor','selfenrollment','inststatus',
  223:                 'ltitools','ssl','trust','lti','privacy','passwords',
  224:                 'proctoring','wafproxy','ipaccess'],$dom);
  225:     my %encconfig =
  226:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring'],$dom,undef,1);
  227:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  228:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  229:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  230:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  231:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  232:                     foreach my $item ('key','secret') {
  233:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  234:                     }
  235:                 }
  236:             }
  237:         }
  238:     }
  239:     if (ref($domconfig{'lti'}) eq 'HASH') {
  240:         if (ref($encconfig{'lti'}) eq 'HASH') {
  241:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  242:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  243:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  244:                     foreach my $item ('key','secret') {
  245:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  252:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  253:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  254:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  255:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  256:                     foreach my $item ('key','secret') {
  257:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  258:                     }
  259:                 }
  260:             }
  261:         }
  262:     }
  263:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  264:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  265:                        'contacts','privacy','usercreation','selfcreation',
  266:                        'usermodification','scantron','requestcourses','requestauthor',
  267:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  268:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  269:                        'trust','lti');
  270:     my %existing;
  271:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  272:         %existing = %{$domconfig{'loadbalancing'}};
  273:     }
  274:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  275:         push(@prefs_order,'loadbalancing');
  276:     }
  277:     my %prefs = (
  278:         'rolecolors' =>
  279:                    { text => 'Default color schemes',
  280:                      help => 'Domain_Configuration_Color_Schemes',
  281:                      header => [{col1 => 'Student Settings',
  282:                                  col2 => '',},
  283:                                 {col1 => 'Coordinator Settings',
  284:                                  col2 => '',},
  285:                                 {col1 => 'Author Settings',
  286:                                  col2 => '',},
  287:                                 {col1 => 'Administrator Settings',
  288:                                  col2 => '',}],
  289:                       print => \&print_rolecolors,
  290:                       modify => \&modify_rolecolors,
  291:                     },
  292:         'login' =>
  293:                     { text => 'Log-in page options',
  294:                       help => 'Domain_Configuration_Login_Page',
  295:                       header => [{col1 => 'Log-in Page Items',
  296:                                   col2 => '',},
  297:                                  {col1 => 'Log-in Help',
  298:                                   col2 => 'Value'},
  299:                                  {col1 => 'Custom HTML in document head',
  300:                                   col2 => 'Value'},
  301:                                  {col1 => 'SSO',
  302:                                   col2 => 'Dual login: SSO and non-SSO options'},
  303:                                 ],
  304:                       print => \&print_login,
  305:                       modify => \&modify_login,
  306:                     },
  307:         'defaults' => 
  308:                     { text => 'Default authentication/language/timezone/portal/types',
  309:                       help => 'Domain_Configuration_LangTZAuth',
  310:                       header => [{col1 => 'Setting',
  311:                                   col2 => 'Value'},
  312:                                  {col1 => 'Institutional user types',
  313:                                   col2 => 'Name displayed'}],
  314:                       print => \&print_defaults,
  315:                       modify => \&modify_defaults,
  316:                     },
  317:         'wafproxy' =>
  318:                     { text => 'Web Application Firewall/Reverse Proxy',
  319:                       help => 'Domain_Configuration_WAF_Proxy',
  320:                       header => [{col1 => 'Domain(s)',
  321:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  322:                                  },
  323:                                  {col1 => 'Domain(s)',
  324:                                   col2 => 'WAF Configuration',}],
  325:                       print => \&print_wafproxy,
  326:                       modify => \&modify_wafproxy,
  327:                     },
  328:         'passwords' =>
  329:                     { text => 'Passwords (Internal authentication)',
  330:                       help => 'Domain_Configuration_Passwords',
  331:                       header => [{col1 => 'Resetting Forgotten Password',
  332:                                   col2 => 'Settings'},
  333:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  334:                                   col2 => 'Settings'},
  335:                                  {col1 => 'Rules for LON-CAPA Passwords',
  336:                                   col2 => 'Settings'},
  337:                                  {col1 => 'Course Owner Changing Student Passwords',
  338:                                   col2 => 'Settings'}],
  339:                       print => \&print_passwords,
  340:                       modify => \&modify_passwords,
  341:                     },
  342:         'quotas' => 
  343:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  344:                       help => 'Domain_Configuration_Quotas',
  345:                       header => [{col1 => 'User affiliation',
  346:                                   col2 => 'Available tools',
  347:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  348:                       print => \&print_quotas,
  349:                       modify => \&modify_quotas,
  350:                     },
  351:         'autoenroll' =>
  352:                    { text => 'Auto-enrollment settings',
  353:                      help => 'Domain_Configuration_Auto_Enrollment',
  354:                      header => [{col1 => 'Configuration setting',
  355:                                  col2 => 'Value(s)'}],
  356:                      print => \&print_autoenroll,
  357:                      modify => \&modify_autoenroll,
  358:                    },
  359:         'autoupdate' => 
  360:                    { text => 'Auto-update settings',
  361:                      help => 'Domain_Configuration_Auto_Updates',
  362:                      header => [{col1 => 'Setting',
  363:                                  col2 => 'Value',},
  364:                                 {col1 => 'Setting',
  365:                                  col2 => 'Affiliation'},
  366:                                 {col1 => 'User population',
  367:                                  col2 => 'Updatable user data'}],
  368:                      print => \&print_autoupdate,
  369:                      modify => \&modify_autoupdate,
  370:                   },
  371:         'autocreate' => 
  372:                   { text => 'Auto-course creation settings',
  373:                      help => 'Domain_Configuration_Auto_Creation',
  374:                      header => [{col1 => 'Configuration Setting',
  375:                                  col2 => 'Value',}],
  376:                      print => \&print_autocreate,
  377:                      modify => \&modify_autocreate,
  378:                   },
  379:         'directorysrch' => 
  380:                   { text => 'Directory searches',
  381:                     help => 'Domain_Configuration_InstDirectory_Search',
  382:                     header => [{col1 => 'Institutional Directory Setting',
  383:                                 col2 => 'Value',},
  384:                                {col1 => 'LON-CAPA Directory Setting',
  385:                                 col2 => 'Value',}],
  386:                     print => \&print_directorysrch,
  387:                     modify => \&modify_directorysrch,
  388:                   },
  389:         'contacts' =>
  390:                   { text => 'E-mail addresses and helpform',
  391:                     help => 'Domain_Configuration_Contact_Info',
  392:                     header => [{col1 => 'Default e-mail addresses',
  393:                                 col2 => 'Value',},
  394:                                {col1 => 'Recipient(s) for notifications',
  395:                                 col2 => 'Value',},
  396:                                {col1 => 'Nightly status check e-mail',
  397:                                 col2 => 'Settings',},
  398:                                {col1 => 'Ask helpdesk form settings',
  399:                                 col2 => 'Value',},],
  400:                     print => \&print_contacts,
  401:                     modify => \&modify_contacts,
  402:                   },
  403:         'usercreation' => 
  404:                   { text => 'User creation',
  405:                     help => 'Domain_Configuration_User_Creation',
  406:                     header => [{col1 => 'Format rule type',
  407:                                 col2 => 'Format rules in force'},
  408:                                {col1 => 'User account creation',
  409:                                 col2 => 'Usernames which may be created',},
  410:                                {col1 => 'Context',
  411:                                 col2 => 'Assignable authentication types'}],
  412:                     print => \&print_usercreation,
  413:                     modify => \&modify_usercreation,
  414:                   },
  415:         'selfcreation' => 
  416:                   { text => 'Users self-creating accounts',
  417:                     help => 'Domain_Configuration_Self_Creation', 
  418:                     header => [{col1 => 'Self-creation with institutional username',
  419:                                 col2 => 'Enabled?'},
  420:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  421:                                 col2 => 'Information user can enter'},
  422:                                {col1 => 'Self-creation with e-mail verification',
  423:                                 col2 => 'Settings'}],
  424:                     print => \&print_selfcreation,
  425:                     modify => \&modify_selfcreation,
  426:                   },
  427:         'usermodification' =>
  428:                   { text => 'User modification',
  429:                     help => 'Domain_Configuration_User_Modification',
  430:                     header => [{col1 => 'Target user has role',
  431:                                 col2 => 'User information updatable in author context'},
  432:                                {col1 => 'Target user has role',
  433:                                 col2 => 'User information updatable in course context'}],
  434:                     print => \&print_usermodification,
  435:                     modify => \&modify_usermodification,
  436:                   },
  437:         'scantron' =>
  438:                   { text => 'Bubblesheet format',
  439:                     help => 'Domain_Configuration_Scantron_Format',
  440:                     header => [ {col1 => 'Bubblesheet format file',
  441:                                  col2 => ''},
  442:                                 {col1 => 'Bubblesheet data upload formats',
  443:                                  col2 => 'Settings'}],
  444:                     print => \&print_scantron,
  445:                     modify => \&modify_scantron,
  446:                   },
  447:         'requestcourses' => 
  448:                  {text => 'Request creation of courses',
  449:                   help => 'Domain_Configuration_Request_Courses',
  450:                   header => [{col1 => 'User affiliation',
  451:                               col2 => 'Availability/Processing of requests',},
  452:                              {col1 => 'Setting',
  453:                               col2 => 'Value'},
  454:                              {col1 => 'Available textbooks',
  455:                               col2 => ''},
  456:                              {col1 => 'Available templates',
  457:                               col2 => ''},
  458:                              {col1 => 'Validation (not official courses)',
  459:                               col2 => 'Value'},],
  460:                   print => \&print_quotas,
  461:                   modify => \&modify_quotas,
  462:                  },
  463:         'requestauthor' =>
  464:                  {text => 'Request Authoring Space',
  465:                   help => 'Domain_Configuration_Request_Author',
  466:                   header => [{col1 => 'User affiliation',
  467:                               col2 => 'Availability/Processing of requests',},
  468:                              {col1 => 'Setting',
  469:                               col2 => 'Value'}],
  470:                   print => \&print_quotas,
  471:                   modify => \&modify_quotas,
  472:                  },
  473:         'coursecategories' =>
  474:                   { text => 'Cataloging of courses/communities',
  475:                     help => 'Domain_Configuration_Cataloging_Courses',
  476:                     header => [{col1 => 'Catalog type/availability',
  477:                                 col2 => '',},
  478:                                {col1 => 'Category settings for standard catalog',
  479:                                 col2 => '',},
  480:                                {col1 => 'Categories',
  481:                                 col2 => '',
  482:                                }],
  483:                     print => \&print_coursecategories,
  484:                     modify => \&modify_coursecategories,
  485:                   },
  486:         'serverstatuses' =>
  487:                  {text   => 'Access to server status pages',
  488:                   help   => 'Domain_Configuration_Server_Status',
  489:                   header => [{col1 => 'Status Page',
  490:                               col2 => 'Other named users',
  491:                               col3 => 'Specific IPs',
  492:                             }],
  493:                   print => \&print_serverstatuses,
  494:                   modify => \&modify_serverstatuses,
  495:                  },
  496:         'helpsettings' =>
  497:                  {text   => 'Support settings',
  498:                   help   => 'Domain_Configuration_Help_Settings',
  499:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  500:                               col2 => 'Value'},
  501:                              {col1 => 'Helpdesk Roles',
  502:                               col2 => 'Settings'},],
  503:                   print  => \&print_helpsettings,
  504:                   modify => \&modify_helpsettings,
  505:                  },
  506:         'coursedefaults' => 
  507:                  {text => 'Course/Community defaults',
  508:                   help => 'Domain_Configuration_Course_Defaults',
  509:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  510:                               col2 => 'Value',},
  511:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  512:                               col2 => 'Value',},],
  513:                   print => \&print_coursedefaults,
  514:                   modify => \&modify_coursedefaults,
  515:                  },
  516:         'selfenrollment' => 
  517:                  {text   => 'Self-enrollment in Course/Community',
  518:                   help   => 'Domain_Configuration_Selfenrollment',
  519:                   header => [{col1 => 'Configuration Rights',
  520:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  521:                              {col1 => 'Defaults',
  522:                               col2 => 'Value'},
  523:                              {col1 => 'Self-enrollment validation (optional)',
  524:                               col2 => 'Value'},],
  525:                   print => \&print_selfenrollment,
  526:                   modify => \&modify_selfenrollment,
  527:                  },
  528:         'privacy' => 
  529:                  {text   => 'Availability of User Information',
  530:                   help   => 'Domain_Configuration_User_Privacy',
  531:                   header => [{col1 => 'Role assigned in different domain',
  532:                               col2 => 'Approval options'},
  533:                              {col1 => 'Role assigned in different domain to user of type',
  534:                               col2 => 'User information available in that domain'},
  535:                              {col1 => "Role assigned in user's domain",
  536:                               col2 => 'Information viewable by privileged user'},
  537:                              {col1 => "Role assigned in user's domain",
  538:                               col2 => 'Information viewable by unprivileged user'}],
  539:                   print => \&print_privacy,
  540:                   modify => \&modify_privacy,
  541:                  },
  542:         'usersessions' =>
  543:                  {text  => 'User session hosting/offloading',
  544:                   help  => 'Domain_Configuration_User_Sessions',
  545:                   header => [{col1 => 'Domain server',
  546:                               col2 => 'Servers to offload sessions to when busy'},
  547:                              {col1 => 'Hosting of users from other domains',
  548:                               col2 => 'Rules'},
  549:                              {col1 => "Hosting domain's own users elsewhere",
  550:                               col2 => 'Rules'}],
  551:                   print => \&print_usersessions,
  552:                   modify => \&modify_usersessions,
  553:                  },
  554:         'loadbalancing' =>
  555:                  {text  => 'Dedicated Load Balancer(s)',
  556:                   help  => 'Domain_Configuration_Load_Balancing',
  557:                   header => [{col1 => 'Balancers',
  558:                               col2 => 'Default destinations',
  559:                               col3 => 'User affiliation',
  560:                               col4 => 'Overrides'},
  561:                             ],
  562:                   print => \&print_loadbalancing,
  563:                   modify => \&modify_loadbalancing,
  564:                  },
  565:         'ltitools' => 
  566:                  {text => 'External Tools (LTI)',
  567:                   help => 'Domain_Configuration_LTI_Tools',
  568:                   header => [{col1 => 'Setting',
  569:                               col2 => 'Value',}],
  570:                   print => \&print_ltitools,
  571:                   modify => \&modify_ltitools,
  572:                  },
  573:         'proctoring' =>
  574:                  {text => 'Remote Proctoring Integration',
  575:                   help => 'Domain_Configuration_Proctoring',
  576:                   header => [{col1 => 'Name',
  577:                               col2 => 'Configuration'}],
  578:                   print => \&print_proctoring,
  579:                   modify => \&modify_proctoring,
  580:                  },
  581:         'ssl' =>
  582:                  {text  => 'LON-CAPA Network (SSL)',
  583:                   help  => 'Domain_Configuration_Network_SSL',
  584:                   header => [{col1 => 'Server',
  585:                               col2 => 'Certificate Status'},
  586:                              {col1 => 'Connections to other servers',
  587:                               col2 => 'Rules'},
  588:                              {col1 => 'Connections from other servers',
  589:                               col2 => 'Rules'},
  590:                              {col1 => "Replicating domain's published content",
  591:                               col2 => 'Rules'}],
  592:                   print => \&print_ssl,
  593:                   modify => \&modify_ssl,
  594:                  },
  595:         'trust' =>
  596:                  {text   => 'Trust Settings',
  597:                   help   => 'Domain_Configuration_Trust',
  598:                   header => [{col1 => "Access to this domain's content by others",
  599:                               col2 => 'Rules'},
  600:                              {col1 => "Access to other domain's content by this domain",
  601:                               col2 => 'Rules'},
  602:                              {col1 => "Enrollment in this domain's courses by others",
  603:                               col2 => 'Rules',},
  604:                              {col1 => "Co-author roles in this domain for others",
  605:                               col2 => 'Rules',},
  606:                              {col1 => "Co-author roles for this domain's users elsewhere",
  607:                               col2 => 'Rules',},
  608:                              {col1 => "Domain roles in this domain assignable to others",
  609:                               col2 => 'Rules'},
  610:                              {col1 => "Course catalog for this domain displayed elsewhere",
  611:                               col2 => 'Rules'},
  612:                              {col1 => "Requests for creation of courses in this domain by others",
  613:                               col2 => 'Rules'},
  614:                              {col1 => "Users in other domains can send messages to this domain",
  615:                               col2 => 'Rules'},],
  616:                   print => \&print_trust,
  617:                   modify => \&modify_trust,
  618:                  },
  619:           'lti' =>
  620:                  {text => 'LTI Provider',
  621:                   help => 'Domain_Configuration_LTI_Provider',
  622:                   header => [{col1 => 'Setting',
  623:                               col2 => 'Value',}],
  624:                   print => \&print_lti,
  625:                   modify => \&modify_lti,
  626:                  },
  627:            'ipaccess' =>
  628:                        {text => 'IP-based access control',
  629:                         help => 'Domain_Configuration_IP_Access',
  630:                         header => [{col1 => 'Setting',
  631:                                     col2 => 'Value'},],
  632:                         print  => \&print_ipaccess,
  633:                         modify => \&modify_ipaccess,
  634:                        },
  635:     );
  636:     if (keys(%servers) > 1) {
  637:         $prefs{'login'}  = { text   => 'Log-in page options',
  638:                              help   => 'Domain_Configuration_Login_Page',
  639:                             header => [{col1 => 'Log-in Service',
  640:                                         col2 => 'Server Setting',},
  641:                                        {col1 => 'Log-in Page Items',
  642:                                         col2 => ''},
  643:                                        {col1 => 'Log-in Help',
  644:                                         col2 => 'Value'},
  645:                                        {col1 => 'Custom HTML in document head',
  646:                                         col2 => 'Value'},
  647:                                        {col1 => 'SSO',
  648:                                         col2 => 'Dual login: SSO and non-SSO options'},
  649:                                       ],
  650:                             print => \&print_login,
  651:                             modify => \&modify_login,
  652:                            };
  653:     }
  654: 
  655:     my @roles = ('student','coordinator','author','admin');
  656:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  657:     &Apache::lonhtmlcommon::add_breadcrumb
  658:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  659:       text=>"Settings to display/modify"});
  660:     my $confname = $dom.'-domainconfig';
  661: 
  662:     if ($phase eq 'process') {
  663:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  664:                                                               \%prefs,\%domconfig,$confname,\@roles);
  665:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  666:             $r->rflush();
  667:             &devalidate_remote_domconfs($dom,$result);
  668:         }
  669:     } elsif ($phase eq 'display') {
  670:         my $js = &recaptcha_js().
  671:                  &toggle_display_js();
  672:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  673:             my ($othertitle,$usertypes,$types) =
  674:                 &Apache::loncommon::sorted_inst_types($dom);
  675:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  676:                                           $domconfig{'loadbalancing'}).
  677:                    &new_spares_js().
  678:                    &common_domprefs_js().
  679:                    &Apache::loncommon::javascript_array_indexof();
  680:         }
  681:         if (grep(/^requestcourses$/,@actions)) {
  682:             my $javascript_validations;
  683:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  684:             $js .= <<END;
  685: <script type="text/javascript">
  686: $javascript_validations
  687: </script>
  688: $coursebrowserjs
  689: END
  690:         } elsif (grep(/^ipaccess$/,@actions)) {
  691:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  692:         }
  693:         if (grep(/^selfcreation$/,@actions)) {
  694:             $js .= &selfcreate_javascript();
  695:         }
  696:         if (grep(/^contacts$/,@actions)) {
  697:             $js .= &contacts_javascript();
  698:         }
  699:         if (grep(/^scantron$/,@actions)) {
  700:             $js .= &scantron_javascript();
  701:         }
  702:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  703:     } else {
  704: # check if domconfig user exists for the domain.
  705:         my $servadm = $r->dir_config('lonAdmEMail');
  706:         my ($configuserok,$author_ok,$switchserver) =
  707:             &config_check($dom,$confname,$servadm);
  708:         unless ($configuserok eq 'ok') {
  709:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  710:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  711:                           $confname).
  712:                       '<br />'
  713:             );
  714:             if ($switchserver) {
  715:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  716:                           '<br />'.
  717:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  718:                           '<br />'.
  719:                           &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).
  720:                           '<br />'.
  721:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  722:                 );
  723:             } else {
  724:                 $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.').
  725:                           '<br />'.
  726:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  727:                 );
  728:             }
  729:             $r->print(&Apache::loncommon::end_page());
  730:             return OK;
  731:         }
  732:         if (keys(%domconfig) == 0) {
  733:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  734:             my @ids=&Apache::lonnet::current_machine_ids();
  735:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  736:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  737:                 my @loginimages = ('img','logo','domlogo','login');
  738:                 my $custom_img_count = 0;
  739:                 foreach my $img (@loginimages) {
  740:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  741:                         $custom_img_count ++;
  742:                     }
  743:                 }
  744:                 foreach my $role (@roles) {
  745:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  746:                         $custom_img_count ++;
  747:                     }
  748:                 }
  749:                 if ($custom_img_count > 0) {
  750:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  751:                     my $switch_server = &check_switchserver($dom,$confname);
  752:                     $r->print(
  753:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  754:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  755:     &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 />'.
  756:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  757:                     if ($switch_server) {
  758:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  759:                     }
  760:                     $r->print(&Apache::loncommon::end_page());
  761:                     return OK;
  762:                 }
  763:             }
  764:         }
  765:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  766:     }
  767:     return OK;
  768: }
  769: 
  770: sub process_changes {
  771:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  772:     my %domconfig;
  773:     if (ref($values) eq 'HASH') {
  774:         %domconfig = %{$values};
  775:     }
  776:     my $output;
  777:     if ($action eq 'login') {
  778:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  779:     } elsif ($action eq 'rolecolors') {
  780:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  781:                                      $lastactref,%domconfig);
  782:     } elsif ($action eq 'quotas') {
  783:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  784:     } elsif ($action eq 'autoenroll') {
  785:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  786:     } elsif ($action eq 'autoupdate') {
  787:         $output = &modify_autoupdate($dom,%domconfig);
  788:     } elsif ($action eq 'autocreate') {
  789:         $output = &modify_autocreate($dom,%domconfig);
  790:     } elsif ($action eq 'directorysrch') {
  791:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  792:     } elsif ($action eq 'usercreation') {
  793:         $output = &modify_usercreation($dom,%domconfig);
  794:     } elsif ($action eq 'selfcreation') {
  795:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  796:     } elsif ($action eq 'usermodification') {
  797:         $output = &modify_usermodification($dom,%domconfig);
  798:     } elsif ($action eq 'contacts') {
  799:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  800:     } elsif ($action eq 'defaults') {
  801:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  802:     } elsif ($action eq 'scantron') {
  803:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  804:     } elsif ($action eq 'coursecategories') {
  805:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  806:     } elsif ($action eq 'serverstatuses') {
  807:         $output = &modify_serverstatuses($dom,%domconfig);
  808:     } elsif ($action eq 'requestcourses') {
  809:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  810:     } elsif ($action eq 'requestauthor') {
  811:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  812:     } elsif ($action eq 'helpsettings') {
  813:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  814:     } elsif ($action eq 'coursedefaults') {
  815:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  816:     } elsif ($action eq 'selfenrollment') {
  817:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  818:     } elsif ($action eq 'usersessions') {
  819:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  820:     } elsif ($action eq 'loadbalancing') {
  821:         $output = &modify_loadbalancing($dom,%domconfig);
  822:     } elsif ($action eq 'ltitools') {
  823:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  824:     } elsif ($action eq 'proctoring') {
  825:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  826:     } elsif ($action eq 'ssl') {
  827:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  828:     } elsif ($action eq 'trust') {
  829:         $output = &modify_trust($dom,$lastactref,%domconfig);
  830:     } elsif ($action eq 'lti') {
  831:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  832:     } elsif ($action eq 'privacy') {
  833:         $output = &modify_privacy($dom,%domconfig);
  834:     } elsif ($action eq 'passwords') {
  835:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  836:     } elsif ($action eq 'wafproxy') {
  837:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  838:     } elsif ($action eq 'ipaccess') {
  839:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  840:     }
  841:     return $output;
  842: }
  843: 
  844: sub print_config_box {
  845:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  846:     my $rowtotal = 0;
  847:     my $output;
  848:     if ($action eq 'coursecategories') {
  849:         $output = &coursecategories_javascript($settings);
  850:     } elsif ($action eq 'defaults') {
  851:         $output = &defaults_javascript($settings); 
  852:     } elsif ($action eq 'passwords') {
  853:         $output = &passwords_javascript();
  854:     } elsif ($action eq 'helpsettings') {
  855:         my (%privs,%levelscurrent);
  856:         my %full=();
  857:         my %levels=(
  858:                      course => {},
  859:                      domain => {},
  860:                      system => {},
  861:                    );
  862:         my $context = 'domain';
  863:         my $crstype = 'Course';
  864:         my $formname = 'display';
  865:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  866:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  867:         $output =
  868:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  869:                                                       \@templateroles);
  870:     } elsif ($action eq 'ltitools') {
  871:         $output .= &ltitools_javascript($settings);
  872:     } elsif ($action eq 'lti') {
  873:         $output .= &lti_javascript($settings);
  874:     } elsif ($action eq 'proctoring') {
  875:         $output .= &proctoring_javascript($settings);
  876:     } elsif ($action eq 'wafproxy') {
  877:         $output .= &wafproxy_javascript($dom);
  878:     } elsif ($action eq 'autoupdate') {
  879:         $output .= &autoupdate_javascript();
  880:     } elsif ($action eq 'login') {
  881:         $output .= &saml_javascript();
  882:     } elsif ($action eq 'ipaccess') {
  883:         $output .= &ipaccess_javascript($settings);
  884:     }
  885:     $output .=
  886:          '<table class="LC_nested_outer">
  887:           <tr>
  888:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  889:            &mt($item->{text}).'&nbsp;'.
  890:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  891:           '</tr>';
  892:     $rowtotal ++;
  893:     my $numheaders = 1;
  894:     if (ref($item->{'header'}) eq 'ARRAY') {
  895:         $numheaders = scalar(@{$item->{'header'}});
  896:     }
  897:     if ($numheaders > 1) {
  898:         my $colspan = '';
  899:         my $rightcolspan = '';
  900:         my $leftnobr = '';
  901:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  902:             ($action eq 'directorysrch') ||
  903:             (($action eq 'login') && ($numheaders < 5))) {
  904:             $colspan = ' colspan="2"';
  905:         }
  906:         if ($action eq 'usersessions') {
  907:             $rightcolspan = ' colspan="3"'; 
  908:         }
  909:         if ($action eq 'passwords') {
  910:             $leftnobr = ' LC_nobreak';
  911:         }
  912:         $output .= '
  913:           <tr>
  914:            <td>
  915:             <table class="LC_nested">
  916:              <tr class="LC_info_row">
  917:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  918:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  919:              </tr>';
  920:         $rowtotal ++;
  921:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  922:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  923:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  924:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  925:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy')) {
  926:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  927:         } elsif ($action eq 'passwords') {
  928:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  929:         } elsif ($action eq 'coursecategories') {
  930:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  931:         } elsif ($action eq 'scantron') {
  932:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  933:         } elsif ($action eq 'login') {
  934:             if ($numheaders == 5) {
  935:                 $colspan = ' colspan="2"';
  936:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  937:             } else {
  938:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  939:             }
  940:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  941:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  942:         } elsif ($action eq 'rolecolors') {
  943:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  944:         }
  945:         $output .= '
  946:            </table>
  947:           </td>
  948:          </tr>
  949:          <tr>
  950:            <td>
  951:             <table class="LC_nested">
  952:              <tr class="LC_info_row">
  953:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  954:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  955:              </tr>';
  956:             $rowtotal ++;
  957:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  958:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  959:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  960:             ($action eq 'trust') || ($action eq 'contacts') ||
  961:             ($action eq 'privacy') || ($action eq 'passwords')) {
  962:             if ($action eq 'coursecategories') {
  963:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  964:                 $colspan = ' colspan="2"';
  965:             } elsif ($action eq 'trust') {
  966:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  967:             } elsif ($action eq 'passwords') {
  968:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  969:             } else {
  970:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  971:             }
  972:             if ($action eq 'trust') {
  973:                 $output .= '
  974:             </table>
  975:           </td>
  976:          </tr>';
  977:                 my @trusthdrs = qw(2 3 4 5 6 7);
  978:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  979:                 for (my $i=0; $i<@trusthdrs; $i++) {
  980:                     $output .= '
  981:          <tr>
  982:            <td>
  983:             <table class="LC_nested">
  984:              <tr class="LC_info_row">
  985:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  986:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  987:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  988:             </table>
  989:           </td>
  990:          </tr>';
  991:                 }
  992:                 $output .= '
  993:          <tr>
  994:            <td>
  995:             <table class="LC_nested">
  996:              <tr class="LC_info_row">
  997:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  998:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  999:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1000:             } else {
 1001:                 $output .= '
 1002:            </table>
 1003:           </td>
 1004:          </tr>
 1005:          <tr>
 1006:            <td>
 1007:             <table class="LC_nested">
 1008:              <tr class="LC_info_row">
 1009:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1010:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1011:              </tr>'."\n";
 1012:                 if ($action eq 'coursecategories') {
 1013:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1014:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
 1015:                     if ($action eq 'passwords') {
 1016:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1017:                     } else {
 1018:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1019:                     }
 1020:                     $output .= '
 1021:              </tr>
 1022:             </table>
 1023:            </td>
 1024:           </tr>
 1025:           <tr>
 1026:            <td>
 1027:             <table class="LC_nested">
 1028:              <tr class="LC_info_row">
 1029:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1030:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1031:                     if ($action eq 'passwords') {
 1032:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1033:                     } else {
 1034:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1035:                     }
 1036:                     $output .= '
 1037:             </table>
 1038:           </td>
 1039:          </tr>
 1040:          <tr>';
 1041:                 } else {
 1042:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1043:                 }
 1044:             }
 1045:             $rowtotal ++;
 1046:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1047:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1048:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
 1049:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1050:         } elsif ($action eq 'scantron') {
 1051:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1052:         } elsif ($action eq 'ssl') {
 1053:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1054:             </table>
 1055:           </td>
 1056:          </tr>
 1057:          <tr>
 1058:            <td>
 1059:             <table class="LC_nested">
 1060:              <tr class="LC_info_row">
 1061:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1062:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1063:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1064:             </table>
 1065:           </td>
 1066:          </tr>
 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'}->[3]->{'col1'}).'</td>
 1072:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1073:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1074:         } elsif ($action eq 'login') {
 1075:             if ($numheaders == 5) {
 1076:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1077:            </table>
 1078:           </td>
 1079:          </tr>
 1080:          <tr>
 1081:            <td>
 1082:             <table class="LC_nested">
 1083:              <tr class="LC_info_row">
 1084:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1085:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1086:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1087:                 $rowtotal ++;
 1088:             } else {
 1089:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 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:             if ($numheaders == 5) {
 1100:                 $output .= '
 1101:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1102:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1103:              </tr>';
 1104:             } else {
 1105:                 $output .= '
 1106:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1107:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1108:              </tr>';
 1109:             }
 1110:             $rowtotal ++;
 1111:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1112:            </table>
 1113:           </td>
 1114:          </tr>
 1115:          <tr>
 1116:            <td>
 1117:             <table class="LC_nested">
 1118:              <tr class="LC_info_row">';
 1119:             if ($numheaders == 5) {
 1120:                 $output .= '
 1121:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1122:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1123:              </tr>';
 1124:             } else {
 1125:                 $output .= '
 1126:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1127:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1128:              </tr>';
 1129:             }
 1130:             $rowtotal ++;
 1131:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1132:         } elsif ($action eq 'requestcourses') {
 1133:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1134:             $rowtotal ++;
 1135:             $output .= &print_studentcode($settings,\$rowtotal).'
 1136:            </table>
 1137:           </td>
 1138:          </tr>
 1139:          <tr>
 1140:            <td>
 1141:             <table class="LC_nested">
 1142:              <tr class="LC_info_row">
 1143:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1144:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1145:                        &textbookcourses_javascript($settings).
 1146:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1147:             </table>
 1148:            </td>
 1149:           </tr>
 1150:          <tr>
 1151:            <td>
 1152:             <table class="LC_nested">
 1153:              <tr class="LC_info_row">
 1154:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1155:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1156:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1157:             </table>
 1158:            </td>
 1159:           </tr>
 1160:           <tr>
 1161:            <td>
 1162:             <table class="LC_nested">
 1163:              <tr class="LC_info_row">
 1164:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1165:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1166:              </tr>'.
 1167:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1168:         } elsif ($action eq 'requestauthor') {
 1169:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1170:             $rowtotal ++;
 1171:         } elsif ($action eq 'rolecolors') {
 1172:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1173:            </table>
 1174:           </td>
 1175:          </tr>
 1176:          <tr>
 1177:            <td>
 1178:             <table class="LC_nested">
 1179:              <tr class="LC_info_row">
 1180:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1181:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1182:               <td class="LC_right_item" style="vertical-align: top">'.
 1183:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1184:              </tr>'.
 1185:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1186:            </table>
 1187:           </td>
 1188:          </tr>
 1189:          <tr>
 1190:            <td>
 1191:             <table class="LC_nested">
 1192:              <tr class="LC_info_row">
 1193:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1194:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1195:              </tr>'.
 1196:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1197:             $rowtotal += 2;
 1198:         }
 1199:     } else {
 1200:         $output .= '
 1201:           <tr>
 1202:            <td>
 1203:             <table class="LC_nested">
 1204:              <tr class="LC_info_row">';
 1205:         if ($action eq 'login') {
 1206:             $output .= '  
 1207:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1208:         } elsif ($action eq 'serverstatuses') {
 1209:             $output .= '
 1210:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1211:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1212: 
 1213:         } else {
 1214:             $output .= '
 1215:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1216:         }
 1217:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1218:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1219:                        &mt($item->{'header'}->[0]->{'col2'});
 1220:             if ($action eq 'serverstatuses') {
 1221:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1222:             } 
 1223:         } else {
 1224:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1225:                        &mt($item->{'header'}->[0]->{'col2'});
 1226:         }
 1227:         $output .= '</td>';
 1228:         if ($item->{'header'}->[0]->{'col3'}) {
 1229:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1230:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1231:                             &mt($item->{'header'}->[0]->{'col3'});
 1232:             } else {
 1233:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1234:                            &mt($item->{'header'}->[0]->{'col3'});
 1235:             }
 1236:             if ($action eq 'serverstatuses') {
 1237:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1238:             }
 1239:             $output .= '</td>';
 1240:         }
 1241:         if ($item->{'header'}->[0]->{'col4'}) {
 1242:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1243:                        &mt($item->{'header'}->[0]->{'col4'});
 1244:         }
 1245:         $output .= '</tr>';
 1246:         $rowtotal ++;
 1247:         if ($action eq 'quotas') {
 1248:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1249:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1250:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1251:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1252:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1253:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1254:         }
 1255:     }
 1256:     $output .= '
 1257:    </table>
 1258:   </td>
 1259:  </tr>
 1260: </table><br />';
 1261:     return ($output,$rowtotal);
 1262: }
 1263: 
 1264: sub print_login {
 1265:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1266:     my ($css_class,$datatable,$switchserver,%lt);
 1267:     my %choices = &login_choices();
 1268:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1269:         %lt = &login_file_options();
 1270:         $switchserver = &check_switchserver($dom,$confname);
 1271:     }
 1272:     if ($caller eq 'service') {
 1273:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1274:         my $choice = $choices{'disallowlogin'};
 1275:         $css_class = ' class="LC_odd_row"';
 1276:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1277:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1278:                       '<th>'.$choices{'server'}.'</th>'.
 1279:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1280:                       '<th>'.$choices{'custompath'}.'</th>'.
 1281:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1282:         my %disallowed;
 1283:         if (ref($settings) eq 'HASH') {
 1284:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1285:                %disallowed = %{$settings->{'loginvia'}};
 1286:             }
 1287:         }
 1288:         foreach my $lonhost (sort(keys(%servers))) {
 1289:             my $direct = 'selected="selected"';
 1290:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1291:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1292:                     $direct = '';
 1293:                 }
 1294:             }
 1295:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1296:                           '<td><select name="'.$lonhost.'_server">'.
 1297:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1298:                           '</option>';
 1299:             foreach my $hostid (sort(keys(%servers))) {
 1300:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1301:                 my $selected = '';
 1302:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1303:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1304:                         $selected = 'selected="selected"';
 1305:                     }
 1306:                 }
 1307:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1308:                               $servers{$hostid}.'</option>';
 1309:             }
 1310:             $datatable .= '</select></td>'.
 1311:                           '<td><select name="'.$lonhost.'_serverpath">';
 1312:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1313:                 my $pathname = $path;
 1314:                 if ($path eq 'custom') {
 1315:                     $pathname = &mt('Custom Path').' ->';
 1316:                 }
 1317:                 my $selected = '';
 1318:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1319:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1320:                         $selected = 'selected="selected"';
 1321:                     }
 1322:                 } elsif ($path eq '') {
 1323:                     $selected = 'selected="selected"';
 1324:                 }
 1325:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1326:             }
 1327:             $datatable .= '</select></td>';
 1328:             my ($custom,$exempt);
 1329:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1330:                 $custom = $disallowed{$lonhost}{'custompath'};
 1331:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1332:             }
 1333:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1334:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1335:                           '</tr>';
 1336:         }
 1337:         $datatable .= '</table></td></tr>';
 1338:         return $datatable;
 1339:     } elsif ($caller eq 'page') {
 1340:         my %defaultchecked = ( 
 1341:                                'coursecatalog' => 'on',
 1342:                                'helpdesk'      => 'on',
 1343:                                'adminmail'     => 'off',
 1344:                                'newuser'       => 'off',
 1345:                              );
 1346:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1347:         my (%checkedon,%checkedoff);
 1348:         foreach my $item (@toggles) {
 1349:             if ($defaultchecked{$item} eq 'on') { 
 1350:                 $checkedon{$item} = ' checked="checked" ';
 1351:                 $checkedoff{$item} = ' ';
 1352:             } elsif ($defaultchecked{$item} eq 'off') {
 1353:                 $checkedoff{$item} = ' checked="checked" ';
 1354:                 $checkedon{$item} = ' ';
 1355:             }
 1356:         }
 1357:         my @images = ('img','logo','domlogo','login');
 1358:         my @logintext = ('textcol','bgcol');
 1359:         my @bgs = ('pgbg','mainbg','sidebg');
 1360:         my @links = ('link','alink','vlink');
 1361:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1362:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1363:         my (%is_custom,%designs);
 1364:         my %defaults = (
 1365:                        font => $defaultdesign{'login.font'},
 1366:                        );
 1367:         foreach my $item (@images) {
 1368:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1369:             $defaults{'showlogo'}{$item} = 1;
 1370:         }
 1371:         foreach my $item (@bgs) {
 1372:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1373:         }
 1374:         foreach my $item (@logintext) {
 1375:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1376:         }
 1377:         foreach my $item (@links) {
 1378:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1379:         }
 1380:         if (ref($settings) eq 'HASH') {
 1381:             foreach my $item (@toggles) {
 1382:                 if ($settings->{$item} eq '1') {
 1383:                     $checkedon{$item} =  ' checked="checked" ';
 1384:                     $checkedoff{$item} = ' ';
 1385:                 } elsif ($settings->{$item} eq '0') {
 1386:                     $checkedoff{$item} =  ' checked="checked" ';
 1387:                     $checkedon{$item} = ' ';
 1388:                 }
 1389:             }
 1390:             foreach my $item (@images) {
 1391:                 if (defined($settings->{$item})) {
 1392:                     $designs{$item} = $settings->{$item};
 1393:                     $is_custom{$item} = 1;
 1394:                 }
 1395:                 if (defined($settings->{'showlogo'}{$item})) {
 1396:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1397:                 }
 1398:             }
 1399:             foreach my $item (@logintext) {
 1400:                 if ($settings->{$item} ne '') {
 1401:                     $designs{'logintext'}{$item} = $settings->{$item};
 1402:                     $is_custom{$item} = 1;
 1403:                 }
 1404:             }
 1405:             if ($settings->{'font'} ne '') {
 1406:                 $designs{'font'} = $settings->{'font'};
 1407:                 $is_custom{'font'} = 1;
 1408:             }
 1409:             foreach my $item (@bgs) {
 1410:                 if ($settings->{$item} ne '') {
 1411:                     $designs{'bgs'}{$item} = $settings->{$item};
 1412:                     $is_custom{$item} = 1;
 1413:                 }
 1414:             }
 1415:             foreach my $item (@links) {
 1416:                 if ($settings->{$item} ne '') {
 1417:                     $designs{'links'}{$item} = $settings->{$item};
 1418:                     $is_custom{$item} = 1;
 1419:                 }
 1420:             }
 1421:         } else {
 1422:             if ($designhash{$dom.'.login.font'} ne '') {
 1423:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1424:                 $is_custom{'font'} = 1;
 1425:             }
 1426:             foreach my $item (@images) {
 1427:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1428:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1429:                     $is_custom{$item} = 1;
 1430:                 }
 1431:             }
 1432:             foreach my $item (@bgs) {
 1433:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1434:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1435:                     $is_custom{$item} = 1;
 1436:                 }
 1437:             }
 1438:             foreach my $item (@links) {
 1439:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1440:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1441:                     $is_custom{$item} = 1;
 1442:                 }
 1443:             }
 1444:         }
 1445:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1446:                                                       logo => 'Institution Logo',
 1447:                                                       domlogo => 'Domain Logo',
 1448:                                                       login => 'Login box');
 1449:         my $itemcount = 1;
 1450:         foreach my $item (@toggles) {
 1451:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1452:             $datatable .=  
 1453:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1454:                 '</td><td>'.
 1455:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1456:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1457:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1458:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1459:                 '</tr>';
 1460:             $itemcount ++;
 1461:         }
 1462:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1463:         $datatable .= '</tr></table></td></tr>';
 1464:     } elsif ($caller eq 'help') {
 1465:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1466:         my $itemcount = 1;
 1467:         $defaulturl = '/adm/loginproblems.html';
 1468:         $defaulttype = 'default';
 1469:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1470:         my @currlangs;
 1471:         if (ref($settings) eq 'HASH') {
 1472:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1473:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1474:                     next if ($settings->{'helpurl'}{$key} eq '');
 1475:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1476:                     $type{$key} = 'custom';
 1477:                     unless ($key eq 'nolang') {
 1478:                         push(@currlangs,$key);
 1479:                     }
 1480:                 }
 1481:             } elsif ($settings->{'helpurl'} ne '') {
 1482:                 $type{'nolang'} = 'custom';
 1483:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1484:             }
 1485:         }
 1486:         foreach my $lang ('nolang',sort(@currlangs)) {
 1487:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1488:             $datatable .= '<tr'.$css_class.'>';
 1489:             if ($url{$lang} eq '') {
 1490:                 $url{$lang} = $defaulturl;
 1491:             }
 1492:             if ($type{$lang} eq '') {
 1493:                 $type{$lang} = $defaulttype;
 1494:             }
 1495:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1496:             if ($lang eq 'nolang') {
 1497:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1498:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1499:             } else {
 1500:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1501:                                   $langchoices{$lang},
 1502:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1503:             }
 1504:             $datatable .= '</span></td>'."\n".
 1505:                           '<td class="LC_left_item">';
 1506:             if ($type{$lang} eq 'custom') {
 1507:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1508:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1509:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1510:             } else {
 1511:                 $datatable .= $lt{'upl'};
 1512:             }
 1513:             $datatable .='<br />';
 1514:             if ($switchserver) {
 1515:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1516:             } else {
 1517:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1518:             }
 1519:             $datatable .= '</td></tr>';
 1520:             $itemcount ++;
 1521:         }
 1522:         my @addlangs;
 1523:         foreach my $lang (sort(keys(%langchoices))) {
 1524:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1525:             push(@addlangs,$lang);
 1526:         }
 1527:         if (@addlangs > 0) {
 1528:             my %toadd;
 1529:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1530:             $toadd{''} = &mt('Select');
 1531:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1532:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1533:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1534:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1535:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1536:             if ($switchserver) {
 1537:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1538:             } else {
 1539:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1540:             }
 1541:             $datatable .= '</td></tr>';
 1542:             $itemcount ++;
 1543:         }
 1544:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1545:     } elsif ($caller eq 'headtag') {
 1546:         my %domservers = &Apache::lonnet::get_servers($dom);
 1547:         my $choice = $choices{'headtag'};
 1548:         $css_class = ' class="LC_odd_row"';
 1549:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1550:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1551:                       '<th>'.$choices{'current'}.'</th>'.
 1552:                       '<th>'.$choices{'action'}.'</th>'.
 1553:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1554:         my (%currurls,%currexempt);
 1555:         if (ref($settings) eq 'HASH') {
 1556:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1557:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1558:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1559:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1560:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1561:                     }
 1562:                 }
 1563:             }
 1564:         }
 1565:         foreach my $lonhost (sort(keys(%domservers))) {
 1566:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1567:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1568:             if ($currurls{$lonhost}) {
 1569:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1570:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1571:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1572:                               '">'.$lt{'curr'}.'</a></td>'.
 1573:                               '<td><span class="LC_nobreak"><label>'.
 1574:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1575:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1576:             } else {
 1577:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1578:             }
 1579:             $datatable .='<br />';
 1580:             if ($switchserver) {
 1581:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1582:             } else {
 1583:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1584:             }
 1585:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1586:         }
 1587:         $datatable .= '</table></td></tr>';
 1588:     } elsif ($caller eq 'saml') {
 1589:         my %domservers = &Apache::lonnet::get_servers($dom);
 1590:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1591:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1592:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1593:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1594:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso,%styleon,%styleoff);
 1595:         foreach my $lonhost (keys(%domservers)) {
 1596:             $samlurl{$lonhost} = '/adm/sso';
 1597:             $styleon{$lonhost} = 'display:none';
 1598:             $styleoff{$lonhost} = '';
 1599:         }
 1600:         if (ref($settings->{'saml'}) eq 'HASH') {
 1601:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1602:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1603:                     $saml{$lonhost} = 1;
 1604:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1605:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1606:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1607:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1608:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1609:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1610:                     $styleon{$lonhost} = '';
 1611:                     $styleoff{$lonhost} = 'display:none';
 1612:                 } else {
 1613:                     $styleon{$lonhost} = 'display:none';
 1614:                     $styleoff{$lonhost} = '';
 1615:                 }
 1616:             }
 1617:         }
 1618:         my $itemcount = 1;
 1619:         foreach my $lonhost (sort(keys(%domservers))) {
 1620:             my $samlon = ' ';
 1621:             my $samloff = ' checked="checked" ';
 1622:             if ($saml{$lonhost}) {
 1623:                 $samlon = $samloff;
 1624:                 $samloff = ' ';
 1625:             }
 1626:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1627:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1628:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1629:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1630:                           &mt('No').'</label>'.('&nbsp;'x2).
 1631:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1632:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1633:                           &mt('Yes').'</label></span></td>'.
 1634:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1635:                           '<table><tr><th colspan="5" align="center">'.&mt('SSO').'</th><th align="center">'.
 1636:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1637:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1638:                           '<th>'.&mt('Alt Text').'</th><th>'.&mt('URL').'</th>'.
 1639:                           '<th>'.&mt('Tool Tip').'</th><th>'.&mt('Text').'</th></tr>'.
 1640:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="8" value="'.
 1641:                           $samltext{$lonhost}.'" /></td><td>';
 1642:             if ($samlimg{$lonhost}) {
 1643:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1644:                               '<span class="LC_nobreak"><label>'.
 1645:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1646:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1647:             } else {
 1648:                 $datatable .= $lt{'upl'};
 1649:             }
 1650:             $datatable .='<br />';
 1651:             if ($switchserver) {
 1652:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1653:             } else {
 1654:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1655:             }
 1656:             $datatable .= '</td>'.
 1657:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="20" '.
 1658:                           'value="'.$samlalt{$lonhost}.'" /></td>'.
 1659:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="8" '.
 1660:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1661:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="15">'.
 1662:                           $samltitle{$lonhost}.'</textarea></td>'.
 1663:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="8" '.
 1664:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1665:                           '</table></td>'.
 1666:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1667:            $itemcount ++;
 1668:         }
 1669:         $datatable .= '</table></td></tr>';
 1670:     }
 1671:     return $datatable;
 1672: }
 1673: 
 1674: sub login_choices {
 1675:     my %choices =
 1676:         &Apache::lonlocal::texthash (
 1677:             coursecatalog => 'Display Course/Community Catalog link?',
 1678:             adminmail     => "Display Administrator's E-mail Address?",
 1679:             helpdesk      => 'Display "Contact Helpdesk" link',
 1680:             disallowlogin => "Login page requests redirected",
 1681:             hostid        => "Server",
 1682:             server        => "Redirect to:",
 1683:             serverpath    => "Path",
 1684:             custompath    => "Custom", 
 1685:             exempt        => "Exempt IP(s)",
 1686:             directlogin   => "No redirect",
 1687:             newuser       => "Link to create a user account",
 1688:             img           => "Header",
 1689:             logo          => "Main Logo",
 1690:             domlogo       => "Domain Logo",
 1691:             login         => "Log-in Header", 
 1692:             textcol       => "Text color",
 1693:             bgcol         => "Box color",
 1694:             bgs           => "Background colors",
 1695:             links         => "Link colors",
 1696:             font          => "Font color",
 1697:             pgbg          => "Header",
 1698:             mainbg        => "Page",
 1699:             sidebg        => "Login box",
 1700:             link          => "Link",
 1701:             alink         => "Active link",
 1702:             vlink         => "Visited link",
 1703:             headtag       => "Custom markup",
 1704:             action        => "Action",
 1705:             current       => "Current",
 1706:             samllanding   => "Dual login?",
 1707:             samloptions   => "Options",
 1708:         );
 1709:     return %choices;
 1710: }
 1711: 
 1712: sub login_file_options {
 1713:       return &Apache::lonlocal::texthash(
 1714:                                            del     => 'Delete?',
 1715:                                            rep     => 'Replace:',
 1716:                                            upl     => 'Upload:',
 1717:                                            curr    => 'View contents',
 1718:                                            default => 'Default',
 1719:                                            custom  => 'Custom',
 1720:                                            none    => 'None',
 1721:       );
 1722: }
 1723: 
 1724: sub print_ipaccess {
 1725:     my ($dom,$settings,$rowtotal) = @_;
 1726:     my $css_class;
 1727:     my $itemcount = 0;
 1728:     my $datatable;
 1729:     my %ordered;
 1730:     if (ref($settings) eq 'HASH') {
 1731:         foreach my $item (keys(%{$settings})) {
 1732:             if (ref($settings->{$item}) eq 'HASH') {
 1733:                 my $num = $settings->{$item}{'order'};
 1734:                 if ($num eq '') {
 1735:                     $num = scalar(keys(%{$settings}));
 1736:                 }
 1737:                 $ordered{$num} = $item;
 1738:             }
 1739:         }
 1740:     }
 1741:     my $maxnum = scalar(keys(%ordered));
 1742:     if (keys(%ordered)) {
 1743:         my @items = sort { $a <=> $b } keys(%ordered);
 1744:         for (my $i=0; $i<@items; $i++) {
 1745:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1746:             my $item = $ordered{$items[$i]};
 1747:             my ($name,$ipranges,%commblocks,%courses);
 1748:             if (ref($settings->{$item}) eq 'HASH') {
 1749:                 $name = $settings->{$item}->{'name'};
 1750:                 $ipranges = $settings->{$item}->{'ip'};
 1751:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1752:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1753:                 }
 1754:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1755:                     %courses = %{$settings->{$item}->{'courses'}};
 1756:                 }
 1757:             }
 1758:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1759:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1760:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1761:             for (my $k=0; $k<=$maxnum; $k++) {
 1762:                 my $vpos = $k+1;
 1763:                 my $selstr;
 1764:                 if ($k == $i) {
 1765:                     $selstr = ' selected="selected" ';
 1766:                 }
 1767:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1768:             }
 1769:             $datatable .= '</select>'.('&nbsp;'x2).
 1770:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1771:                 &mt('Delete?').'</label></span></td>'.
 1772:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1773:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1774:                 '</td></tr>';
 1775:             $itemcount ++;
 1776:         }
 1777:     }
 1778:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1779:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1780:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1781:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1782:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1783:     for (my $k=0; $k<$maxnum+1; $k++) {
 1784:         my $vpos = $k+1;
 1785:         my $selstr;
 1786:         if ($k == $maxnum) {
 1787:             $selstr = ' selected="selected" ';
 1788:         }
 1789:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1790:     }
 1791:     $datatable .= '</select>&nbsp;'."\n".
 1792:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1793:                   '<td colspan="2">'.
 1794:                   &ipaccess_options('add',$itemcount,$dom).
 1795:                   '</td>'."\n".
 1796:                   '</tr>'."\n";
 1797:     $$rowtotal ++;
 1798:     return $datatable;
 1799: }
 1800: 
 1801: sub ipaccess_options {
 1802:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1803:     my (%currblocks,%currcourses,$output);
 1804:     if (ref($blocksref) eq 'HASH') {
 1805:         %currblocks = %{$blocksref};
 1806:     }
 1807:     if (ref($coursesref) eq 'HASH') {
 1808:         %currcourses = %{$coursesref};
 1809:     }
 1810:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1811:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1812:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1813:               '</span></fieldset>'.
 1814:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1815:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1816:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1817:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1818:               $ipranges.'</textarea></fieldset>'.
 1819:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1820:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1821:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1822:               '<table>';
 1823:     foreach my $cid (sort(keys(%currcourses))) {
 1824:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1825:         $output .= '<tr><td><span class="LC_nobreak">'.
 1826:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1827:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1828:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1829:     }
 1830:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1831:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1832:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1833:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1834:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1835:                '</span></td></tr></table>'."\n".
 1836:                '</fieldset>';
 1837:     return $output;
 1838: }
 1839: 
 1840: sub blocker_checkboxes {
 1841:     my ($num,$blocks) = @_;
 1842:     my ($typeorder,$types) = &commblocktype_text();
 1843:     my $numinrow = 6;
 1844:     my $output = '<table>';
 1845:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1846:         my $block = $typeorder->[$i];
 1847:         my $blockstatus;
 1848:         if (ref($blocks) eq 'HASH') {
 1849:             if ($blocks->{$block} eq 'on') {
 1850:                 $blockstatus = 'checked="checked"';
 1851:             }
 1852:         }
 1853:         my $rem = $i%($numinrow);
 1854:         if ($rem == 0) {
 1855:             if ($i > 0) {
 1856:                 $output .= '</tr>';
 1857:             }
 1858:             $output .= '<tr>';
 1859:         }
 1860:         if ($i == scalar(@{$typeorder})-1) {
 1861:             my $colsleft = $numinrow-$rem;
 1862:             if ($colsleft > 1) {
 1863:                 $output .= '<td colspan="'.$colsleft.'">';
 1864:             } else {
 1865:                 $output .= '<td>';
 1866:             }
 1867:         } else {
 1868:             $output .= '<td>';
 1869:         }
 1870:         my $item = 'ipaccess_block_'.$num;
 1871:         if ($blockstatus) {
 1872:             $blockstatus = ' '.$blockstatus;
 1873:         }
 1874:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1875:                    '<input type="checkbox" name="'.$item.'"'.
 1876:                    $blockstatus.' value="'.$block.'"'.' />'.
 1877:                    $types->{$block}.'</label></span>'."\n".
 1878:                    '<br /></td>';
 1879:     }
 1880:     $output .= '</tr></table>';
 1881:     return $output;
 1882: }
 1883: 
 1884: sub commblocktype_text {
 1885:     my %types = &Apache::lonlocal::texthash(
 1886:         'com' => 'Messaging',
 1887:         'chat' => 'Chat Room',
 1888:         'boards' => 'Discussion',
 1889:         'port' => 'Portfolio',
 1890:         'groups' => 'Groups',
 1891:         'blogs' => 'Blogs',
 1892:         'about' => 'User Information',
 1893:         'printout' => 'Printouts',
 1894:         'passwd' => 'Change Password',
 1895:         'grades' => 'Gradebook',
 1896:     );
 1897:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','printout','grades','passwd'];
 1898:     return ($typeorder,\%types);
 1899: }
 1900: 
 1901: sub print_rolecolors {
 1902:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1903:     my %choices = &color_font_choices();
 1904:     my @bgs = ('pgbg','tabbg','sidebg');
 1905:     my @links = ('link','alink','vlink');
 1906:     my @images = ('img');
 1907:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1908:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1909:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1910:     my (%is_custom,%designs);
 1911:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1912:     if (ref($settings) eq 'HASH') {
 1913:         if (ref($settings->{$role}) eq 'HASH') {
 1914:             if ($settings->{$role}->{'img'} ne '') {
 1915:                 $designs{'img'} = $settings->{$role}->{'img'};
 1916:                 $is_custom{'img'} = 1;
 1917:             }
 1918:             if ($settings->{$role}->{'font'} ne '') {
 1919:                 $designs{'font'} = $settings->{$role}->{'font'};
 1920:                 $is_custom{'font'} = 1;
 1921:             }
 1922:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1923:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1924:                 $is_custom{'fontmenu'} = 1;
 1925:             }
 1926:             foreach my $item (@bgs) {
 1927:                 if ($settings->{$role}->{$item} ne '') {
 1928:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1929:                     $is_custom{$item} = 1;
 1930:                 }
 1931:             }
 1932:             foreach my $item (@links) {
 1933:                 if ($settings->{$role}->{$item} ne '') {
 1934:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1935:                     $is_custom{$item} = 1;
 1936:                 }
 1937:             }
 1938:         }
 1939:     } else {
 1940:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1941:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1942:             $is_custom{'img'} = 1;
 1943:         }
 1944:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1945:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1946:             $is_custom{'fontmenu'} = 1; 
 1947:         }
 1948:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1949:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1950:             $is_custom{'font'} = 1;
 1951:         }
 1952:         foreach my $item (@bgs) {
 1953:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1954:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1955:                 $is_custom{$item} = 1;
 1956:             
 1957:             }
 1958:         }
 1959:         foreach my $item (@links) {
 1960:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1961:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1962:                 $is_custom{$item} = 1;
 1963:             }
 1964:         }
 1965:     }
 1966:     my $itemcount = 1;
 1967:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1968:     $datatable .= '</tr></table></td></tr>';
 1969:     return $datatable;
 1970: }
 1971: 
 1972: sub role_defaults {
 1973:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1974:     my %defaults;
 1975:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1976:         return %defaults;
 1977:     }
 1978:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1979:     if ($role eq 'login') {
 1980:         %defaults = (
 1981:                        font => $defaultdesign{$role.'.font'},
 1982:                     );
 1983:         if (ref($logintext) eq 'ARRAY') {
 1984:             foreach my $item (@{$logintext}) {
 1985:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1986:             }
 1987:         }
 1988:         foreach my $item (@{$images}) {
 1989:             $defaults{'showlogo'}{$item} = 1;
 1990:         }
 1991:     } else {
 1992:         %defaults = (
 1993:                        img => $defaultdesign{$role.'.img'},
 1994:                        font => $defaultdesign{$role.'.font'},
 1995:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1996:                     );
 1997:     }
 1998:     foreach my $item (@{$bgs}) {
 1999:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2000:     }
 2001:     foreach my $item (@{$links}) {
 2002:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2003:     }
 2004:     foreach my $item (@{$images}) {
 2005:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2006:     }
 2007:     return %defaults;
 2008: }
 2009: 
 2010: sub display_color_options {
 2011:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2012:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2013:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2014:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2015:     my $datatable = '<tr'.$css_class.'>'.
 2016:         '<td>'.$choices->{'font'}.'</td>';
 2017:     if (!$is_custom->{'font'}) {
 2018:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2019:     } else {
 2020:         $datatable .= '<td>&nbsp;</td>';
 2021:     }
 2022:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2023: 
 2024:     $datatable .= '<td><span class="LC_nobreak">'.
 2025:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2026:                   ' value="'.$current_color.'" />&nbsp;'.
 2027:                   '&nbsp;</span></td></tr>';
 2028:     unless ($role eq 'login') { 
 2029:         $datatable .= '<tr'.$css_class.'>'.
 2030:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2031:         if (!$is_custom->{'fontmenu'}) {
 2032:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2033:         } else {
 2034:             $datatable .= '<td>&nbsp;</td>';
 2035:         }
 2036: 	$current_color = $designs->{'fontmenu'} ?
 2037: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2038:         $datatable .= '<td><span class="LC_nobreak">'.
 2039:                       '<input class="colorchooser" type="text" size="10" name="'
 2040: 		      .$role.'_fontmenu"'.
 2041:                       ' value="'.$current_color.'" />&nbsp;'.
 2042:                       '&nbsp;</span></td></tr>';
 2043:     }
 2044:     my $switchserver = &check_switchserver($dom,$confname);
 2045:     foreach my $img (@{$images}) {
 2046: 	$itemcount ++;
 2047:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2048:         $datatable .= '<tr'.$css_class.'>'.
 2049:                       '<td>'.$choices->{$img};
 2050:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 2051:         if ($role eq 'login') {
 2052:             if ($img eq 'login') {
 2053:                 $login_hdr_pick =
 2054:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2055:                 $logincolors =
 2056:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2057:                                        $designs,$defaults);
 2058:             } elsif ($img ne 'domlogo') {
 2059:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 2060:             }
 2061:         }
 2062:         $datatable .= '</td>';
 2063:         if ($designs->{$img} ne '') {
 2064:             $imgfile = $designs->{$img};
 2065: 	    $img_import = ($imgfile =~ m{^/adm/});
 2066:         } else {
 2067:             $imgfile = $defaults->{$img};
 2068:         }
 2069:         if ($imgfile) {
 2070:             my ($showfile,$fullsize);
 2071:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2072:                 my $urldir = $1;
 2073:                 my $filename = $2;
 2074:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2075:                 if (@info) {
 2076:                     my $thumbfile = 'tn-'.$filename;
 2077:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2078:                     if (@thumb) {
 2079:                         $showfile = $urldir.'/'.$thumbfile;
 2080:                     } else {
 2081:                         $showfile = $imgfile;
 2082:                     }
 2083:                 } else {
 2084:                     $showfile = '';
 2085:                 }
 2086:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2087:                 $showfile = $imgfile;
 2088:                 my $imgdir = $1;
 2089:                 my $filename = $2;
 2090:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2091:                     $showfile = "/$imgdir/tn-".$filename;
 2092:                 } else {
 2093:                     my $input = $londocroot.$imgfile;
 2094:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2095:                     if (!-e $output) {
 2096:                         my ($width,$height) = &thumb_dimensions();
 2097:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2098:                         if ($fullwidth ne '' && $fullheight ne '') {
 2099:                             if ($fullwidth > $width && $fullheight > $height) { 
 2100:                                 my $size = $width.'x'.$height;
 2101:                                 my @args = ('convert','-sample',$size,$input,$output);
 2102:                                 system({$args[0]} @args);
 2103:                                 $showfile = "/$imgdir/tn-".$filename;
 2104:                             }
 2105:                         }
 2106:                     }
 2107:                 }
 2108:             }
 2109:             if ($showfile) {
 2110:                 if ($showfile =~ m{^/(adm|res)/}) {
 2111:                     if ($showfile =~ m{^/res/}) {
 2112:                         my $local_showfile =
 2113:                             &Apache::lonnet::filelocation('',$showfile);
 2114:                         &Apache::lonnet::repcopy($local_showfile);
 2115:                     }
 2116:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2117:                 }
 2118:                 if ($imgfile) {
 2119:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2120:                         if ($imgfile =~ m{^/res/}) {
 2121:                             my $local_imgfile =
 2122:                                 &Apache::lonnet::filelocation('',$imgfile);
 2123:                             &Apache::lonnet::repcopy($local_imgfile);
 2124:                         }
 2125:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2126:                     } else {
 2127:                         $fullsize = $imgfile;
 2128:                     }
 2129:                 }
 2130:                 $datatable .= '<td>';
 2131:                 if ($img eq 'login') {
 2132:                     $datatable .= $login_hdr_pick;
 2133:                 } 
 2134:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2135:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2136:             } else {
 2137:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2138:                               &mt('Upload:').'<br />';
 2139:             }
 2140:         } else {
 2141:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2142:                           &mt('Upload:').'<br />';
 2143:         }
 2144:         if ($switchserver) {
 2145:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2146:         } else {
 2147:             if ($img ne 'login') { # suppress file selection for Log-in header
 2148:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2149:             }
 2150:         }
 2151:         $datatable .= '</td></tr>';
 2152:     }
 2153:     $itemcount ++;
 2154:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2155:     $datatable .= '<tr'.$css_class.'>'.
 2156:                   '<td>'.$choices->{'bgs'}.'</td>';
 2157:     my $bgs_def;
 2158:     foreach my $item (@{$bgs}) {
 2159:         if (!$is_custom->{$item}) {
 2160:             $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>';
 2161:         }
 2162:     }
 2163:     if ($bgs_def) {
 2164:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2165:     } else {
 2166:         $datatable .= '<td>&nbsp;</td>';
 2167:     }
 2168:     $datatable .= '<td class="LC_right_item">'.
 2169:                   '<table border="0"><tr>';
 2170: 
 2171:     foreach my $item (@{$bgs}) {
 2172:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2173: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2174:         if ($designs->{'bgs'}{$item}) {
 2175:             $datatable .= '&nbsp;';
 2176:         }
 2177:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2178:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2179:     }
 2180:     $datatable .= '</tr></table></td></tr>';
 2181:     $itemcount ++;
 2182:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2183:     $datatable .= '<tr'.$css_class.'>'.
 2184:                   '<td>'.$choices->{'links'}.'</td>';
 2185:     my $links_def;
 2186:     foreach my $item (@{$links}) {
 2187:         if (!$is_custom->{$item}) {
 2188:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2189:         }
 2190:     }
 2191:     if ($links_def) {
 2192:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2193:     } else {
 2194:         $datatable .= '<td>&nbsp;</td>';
 2195:     }
 2196:     $datatable .= '<td class="LC_right_item">'.
 2197:                   '<table border="0"><tr>';
 2198:     foreach my $item (@{$links}) {
 2199: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2200:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2201:         if ($designs->{'links'}{$item}) {
 2202:             $datatable.='&nbsp;';
 2203:         }
 2204:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2205:                       '" /></td>';
 2206:     }
 2207:     $$rowtotal += $itemcount;
 2208:     return $datatable;
 2209: }
 2210: 
 2211: sub logo_display_options {
 2212:     my ($img,$defaults,$designs) = @_;
 2213:     my $checkedon;
 2214:     if (ref($defaults) eq 'HASH') {
 2215:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2216:             if ($defaults->{'showlogo'}{$img}) {
 2217:                 $checkedon = 'checked="checked" ';     
 2218:             }
 2219:         } 
 2220:     }
 2221:     if (ref($designs) eq 'HASH') {
 2222:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2223:             if (defined($designs->{'showlogo'}{$img})) {
 2224:                 if ($designs->{'showlogo'}{$img} == 0) {
 2225:                     $checkedon = '';
 2226:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2227:                     $checkedon = 'checked="checked" ';
 2228:                 }
 2229:             }
 2230:         }
 2231:     }
 2232:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2233:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2234:            &mt('show').'</label>'."\n";
 2235: }
 2236: 
 2237: sub login_header_options  {
 2238:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2239:     my $output = '';
 2240:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2241:         $output .= &mt('Text default(s):').'<br />';
 2242:         if (!$is_custom->{'textcol'}) {
 2243:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2244:                        '&nbsp;&nbsp;&nbsp;';
 2245:         }
 2246:         if (!$is_custom->{'bgcol'}) {
 2247:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2248:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2249:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2250:         }
 2251:         $output .= '<br />';
 2252:     }
 2253:     $output .='<br />';
 2254:     return $output;
 2255: }
 2256: 
 2257: sub login_text_colors {
 2258:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2259:     my $color_menu = '<table border="0"><tr>';
 2260:     foreach my $item (@{$logintext}) {
 2261:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2262:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2263:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2264:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2265:     }
 2266:     $color_menu .= '</tr></table><br />';
 2267:     return $color_menu;
 2268: }
 2269: 
 2270: sub image_changes {
 2271:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2272:     my $output;
 2273:     if ($img eq 'login') {
 2274:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2275:     } elsif (!$is_custom) {
 2276:         if ($img ne 'domlogo') {
 2277:             $output = &mt('Default image:').'<br />';
 2278:         } else {
 2279:             $output = &mt('Default in use:').'<br />';
 2280:         }
 2281:     }
 2282:     if ($img ne 'login') {
 2283:         if ($img_import) {
 2284:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2285:         }
 2286:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2287:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2288:         if ($is_custom) {
 2289:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2290:                        '<input type="checkbox" name="'.
 2291:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2292:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2293:         } else {
 2294:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2295:         }
 2296:     }
 2297:     return $output;
 2298: }
 2299: 
 2300: sub print_quotas {
 2301:     my ($dom,$settings,$rowtotal,$action) = @_;
 2302:     my $context;
 2303:     if ($action eq 'quotas') {
 2304:         $context = 'tools';
 2305:     } else {
 2306:         $context = $action;
 2307:     }
 2308:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2309:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2310:     my $typecount = 0;
 2311:     my ($css_class,%titles);
 2312:     if ($context eq 'requestcourses') {
 2313:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2314:         @options =('norequest','approval','validate','autolimit');
 2315:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2316:         %titles = &courserequest_titles();
 2317:     } elsif ($context eq 'requestauthor') {
 2318:         @usertools = ('author');
 2319:         @options = ('norequest','approval','automatic');
 2320:         %titles = &authorrequest_titles();
 2321:     } else {
 2322:         @usertools = ('aboutme','blog','webdav','portfolio');
 2323:         %titles = &tool_titles();
 2324:     }
 2325:     if (ref($types) eq 'ARRAY') {
 2326:         foreach my $type (@{$types}) {
 2327:             my ($currdefquota,$currauthorquota);
 2328:             unless (($context eq 'requestcourses') ||
 2329:                     ($context eq 'requestauthor')) {
 2330:                 if (ref($settings) eq 'HASH') {
 2331:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2332:                         $currdefquota = $settings->{defaultquota}->{$type};
 2333:                     } else {
 2334:                         $currdefquota = $settings->{$type};
 2335:                     }
 2336:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2337:                         $currauthorquota = $settings->{authorquota}->{$type};
 2338:                     }
 2339:                 }
 2340:             }
 2341:             if (defined($usertypes->{$type})) {
 2342:                 $typecount ++;
 2343:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2344:                 $datatable .= '<tr'.$css_class.'>'.
 2345:                               '<td>'.$usertypes->{$type}.'</td>'.
 2346:                               '<td class="LC_left_item">';
 2347:                 if ($context eq 'requestcourses') {
 2348:                     $datatable .= '<table><tr>';
 2349:                 }
 2350:                 my %cell;  
 2351:                 foreach my $item (@usertools) {
 2352:                     if ($context eq 'requestcourses') {
 2353:                         my ($curroption,$currlimit);
 2354:                         if (ref($settings) eq 'HASH') {
 2355:                             if (ref($settings->{$item}) eq 'HASH') {
 2356:                                 $curroption = $settings->{$item}->{$type};
 2357:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2358:                                     $currlimit = $1; 
 2359:                                 }
 2360:                             }
 2361:                         }
 2362:                         if (!$curroption) {
 2363:                             $curroption = 'norequest';
 2364:                         }
 2365:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2366:                         foreach my $option (@options) {
 2367:                             my $val = $option;
 2368:                             if ($option eq 'norequest') {
 2369:                                 $val = 0;  
 2370:                             }
 2371:                             if ($option eq 'validate') {
 2372:                                 my $canvalidate = 0;
 2373:                                 if (ref($validations{$item}) eq 'HASH') { 
 2374:                                     if ($validations{$item}{$type}) {
 2375:                                         $canvalidate = 1;
 2376:                                     }
 2377:                                 }
 2378:                                 next if (!$canvalidate);
 2379:                             }
 2380:                             my $checked = '';
 2381:                             if ($option eq $curroption) {
 2382:                                 $checked = ' checked="checked"';
 2383:                             } elsif ($option eq 'autolimit') {
 2384:                                 if ($curroption =~ /^autolimit/) {
 2385:                                     $checked = ' checked="checked"';
 2386:                                 }                       
 2387:                             } 
 2388:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2389:                                   '<input type="radio" name="crsreq_'.$item.
 2390:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2391:                                   $titles{$option}.'</label>';
 2392:                             if ($option eq 'autolimit') {
 2393:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2394:                                                 $item.'_limit_'.$type.'" size="1" '.
 2395:                                                 'value="'.$currlimit.'" />';
 2396:                             }
 2397:                             $cell{$item} .= '</span> ';
 2398:                             if ($option eq 'autolimit') {
 2399:                                 $cell{$item} .= $titles{'unlimited'};
 2400:                             }
 2401:                         }
 2402:                     } elsif ($context eq 'requestauthor') {
 2403:                         my $curroption;
 2404:                         if (ref($settings) eq 'HASH') {
 2405:                             $curroption = $settings->{$type};
 2406:                         }
 2407:                         if (!$curroption) {
 2408:                             $curroption = 'norequest';
 2409:                         }
 2410:                         foreach my $option (@options) {
 2411:                             my $val = $option;
 2412:                             if ($option eq 'norequest') {
 2413:                                 $val = 0;
 2414:                             }
 2415:                             my $checked = '';
 2416:                             if ($option eq $curroption) {
 2417:                                 $checked = ' checked="checked"';
 2418:                             }
 2419:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2420:                                   '<input type="radio" name="authorreq_'.$type.
 2421:                                   '" value="'.$val.'"'.$checked.' />'.
 2422:                                   $titles{$option}.'</label></span>&nbsp; ';
 2423:                         }
 2424:                     } else {
 2425:                         my $checked = 'checked="checked" ';
 2426:                         if (ref($settings) eq 'HASH') {
 2427:                             if (ref($settings->{$item}) eq 'HASH') {
 2428:                                 if ($settings->{$item}->{$type} == 0) {
 2429:                                     $checked = '';
 2430:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2431:                                     $checked =  'checked="checked" ';
 2432:                                 }
 2433:                             }
 2434:                         }
 2435:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2436:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2437:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2438:                                       '</label></span>&nbsp; ';
 2439:                     }
 2440:                 }
 2441:                 if ($context eq 'requestcourses') {
 2442:                     $datatable .= '</tr><tr>';
 2443:                     foreach my $item (@usertools) {
 2444:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2445:                     }
 2446:                     $datatable .= '</tr></table>';
 2447:                 }
 2448:                 $datatable .= '</td>';
 2449:                 unless (($context eq 'requestcourses') ||
 2450:                         ($context eq 'requestauthor')) {
 2451:                     $datatable .= 
 2452:                               '<td class="LC_right_item">'.
 2453:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2454:                               '<input type="text" name="quota_'.$type.
 2455:                               '" value="'.$currdefquota.
 2456:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2457:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2458:                               '<input type="text" name="authorquota_'.$type.
 2459:                               '" value="'.$currauthorquota.
 2460:                               '" size="5" /></span></td>';
 2461:                 }
 2462:                 $datatable .= '</tr>';
 2463:             }
 2464:         }
 2465:     }
 2466:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2467:         $defaultquota = '20';
 2468:         $authorquota = '500';
 2469:         if (ref($settings) eq 'HASH') {
 2470:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2471:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2472:             } elsif (defined($settings->{'default'})) {
 2473:                 $defaultquota = $settings->{'default'};
 2474:             }
 2475:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2476:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2477:             }
 2478:         }
 2479:     }
 2480:     $typecount ++;
 2481:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2482:     $datatable .= '<tr'.$css_class.'>'.
 2483:                   '<td>'.$othertitle.'</td>'.
 2484:                   '<td class="LC_left_item">';
 2485:     if ($context eq 'requestcourses') {
 2486:         $datatable .= '<table><tr>';
 2487:     }
 2488:     my %defcell;
 2489:     foreach my $item (@usertools) {
 2490:         if ($context eq 'requestcourses') {
 2491:             my ($curroption,$currlimit);
 2492:             if (ref($settings) eq 'HASH') {
 2493:                 if (ref($settings->{$item}) eq 'HASH') {
 2494:                     $curroption = $settings->{$item}->{'default'};
 2495:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2496:                         $currlimit = $1;
 2497:                     }
 2498:                 }
 2499:             }
 2500:             if (!$curroption) {
 2501:                 $curroption = 'norequest';
 2502:             }
 2503:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2504:             foreach my $option (@options) {
 2505:                 my $val = $option;
 2506:                 if ($option eq 'norequest') {
 2507:                     $val = 0;
 2508:                 }
 2509:                 if ($option eq 'validate') {
 2510:                     my $canvalidate = 0;
 2511:                     if (ref($validations{$item}) eq 'HASH') {
 2512:                         if ($validations{$item}{'default'}) {
 2513:                             $canvalidate = 1;
 2514:                         }
 2515:                     }
 2516:                     next if (!$canvalidate);
 2517:                 }
 2518:                 my $checked = '';
 2519:                 if ($option eq $curroption) {
 2520:                     $checked = ' checked="checked"';
 2521:                 } elsif ($option eq 'autolimit') {
 2522:                     if ($curroption =~ /^autolimit/) {
 2523:                         $checked = ' checked="checked"';
 2524:                     }
 2525:                 }
 2526:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2527:                                   '<input type="radio" name="crsreq_'.$item.
 2528:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2529:                                   $titles{$option}.'</label>';
 2530:                 if ($option eq 'autolimit') {
 2531:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2532:                                        $item.'_limit_default" size="1" '.
 2533:                                        'value="'.$currlimit.'" />';
 2534:                 }
 2535:                 $defcell{$item} .= '</span> ';
 2536:                 if ($option eq 'autolimit') {
 2537:                     $defcell{$item} .= $titles{'unlimited'};
 2538:                 }
 2539:             }
 2540:         } elsif ($context eq 'requestauthor') {
 2541:             my $curroption;
 2542:             if (ref($settings) eq 'HASH') {
 2543:                 $curroption = $settings->{'default'};
 2544:             }
 2545:             if (!$curroption) {
 2546:                 $curroption = 'norequest';
 2547:             }
 2548:             foreach my $option (@options) {
 2549:                 my $val = $option;
 2550:                 if ($option eq 'norequest') {
 2551:                     $val = 0;
 2552:                 }
 2553:                 my $checked = '';
 2554:                 if ($option eq $curroption) {
 2555:                     $checked = ' checked="checked"';
 2556:                 }
 2557:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2558:                               '<input type="radio" name="authorreq_default"'.
 2559:                               ' value="'.$val.'"'.$checked.' />'.
 2560:                               $titles{$option}.'</label></span>&nbsp; ';
 2561:             }
 2562:         } else {
 2563:             my $checked = 'checked="checked" ';
 2564:             if (ref($settings) eq 'HASH') {
 2565:                 if (ref($settings->{$item}) eq 'HASH') {
 2566:                     if ($settings->{$item}->{'default'} == 0) {
 2567:                         $checked = '';
 2568:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2569:                         $checked = 'checked="checked" ';
 2570:                     }
 2571:                 }
 2572:             }
 2573:             $datatable .= '<span class="LC_nobreak"><label>'.
 2574:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2575:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2576:                           '</label></span>&nbsp; ';
 2577:         }
 2578:     }
 2579:     if ($context eq 'requestcourses') {
 2580:         $datatable .= '</tr><tr>';
 2581:         foreach my $item (@usertools) {
 2582:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2583:         }
 2584:         $datatable .= '</tr></table>';
 2585:     }
 2586:     $datatable .= '</td>';
 2587:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2588:         $datatable .= '<td class="LC_right_item">'.
 2589:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2590:                       '<input type="text" name="defaultquota" value="'.
 2591:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2592:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2593:                       '<input type="text" name="authorquota" value="'.
 2594:                       $authorquota.'" size="5" /></span></td>';
 2595:     }
 2596:     $datatable .= '</tr>';
 2597:     $typecount ++;
 2598:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2599:     $datatable .= '<tr'.$css_class.'>'.
 2600:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2601:     if ($context eq 'requestcourses') {
 2602:         $datatable .= &mt('(overrides affiliation, if set)').
 2603:                       '</td>'.
 2604:                       '<td class="LC_left_item">'.
 2605:                       '<table><tr>';
 2606:     } else {
 2607:         $datatable .= &mt('(overrides affiliation, if checked)').
 2608:                       '</td>'.
 2609:                       '<td class="LC_left_item" colspan="2">'.
 2610:                       '<br />';
 2611:     }
 2612:     my %advcell;
 2613:     foreach my $item (@usertools) {
 2614:         if ($context eq 'requestcourses') {
 2615:             my ($curroption,$currlimit);
 2616:             if (ref($settings) eq 'HASH') {
 2617:                 if (ref($settings->{$item}) eq 'HASH') {
 2618:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2619:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2620:                         $currlimit = $1;
 2621:                     }
 2622:                 }
 2623:             }
 2624:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2625:             my $checked = '';
 2626:             if ($curroption eq '') {
 2627:                 $checked = ' checked="checked"';
 2628:             }
 2629:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2630:                                '<input type="radio" name="crsreq_'.$item.
 2631:                                '__LC_adv" value=""'.$checked.' />'.
 2632:                                &mt('No override set').'</label></span>&nbsp; ';
 2633:             foreach my $option (@options) {
 2634:                 my $val = $option;
 2635:                 if ($option eq 'norequest') {
 2636:                     $val = 0;
 2637:                 }
 2638:                 if ($option eq 'validate') {
 2639:                     my $canvalidate = 0;
 2640:                     if (ref($validations{$item}) eq 'HASH') {
 2641:                         if ($validations{$item}{'_LC_adv'}) {
 2642:                             $canvalidate = 1;
 2643:                         }
 2644:                     }
 2645:                     next if (!$canvalidate);
 2646:                 }
 2647:                 my $checked = '';
 2648:                 if ($val eq $curroption) {
 2649:                     $checked = ' checked="checked"';
 2650:                 } elsif ($option eq 'autolimit') {
 2651:                     if ($curroption =~ /^autolimit/) {
 2652:                         $checked = ' checked="checked"';
 2653:                     }
 2654:                 }
 2655:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2656:                                   '<input type="radio" name="crsreq_'.$item.
 2657:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2658:                                   $titles{$option}.'</label>';
 2659:                 if ($option eq 'autolimit') {
 2660:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2661:                                        $item.'_limit__LC_adv" size="1" '.
 2662:                                        'value="'.$currlimit.'" />';
 2663:                 }
 2664:                 $advcell{$item} .= '</span> ';
 2665:                 if ($option eq 'autolimit') {
 2666:                     $advcell{$item} .= $titles{'unlimited'};
 2667:                 }
 2668:             }
 2669:         } elsif ($context eq 'requestauthor') {
 2670:             my $curroption;
 2671:             if (ref($settings) eq 'HASH') {
 2672:                 $curroption = $settings->{'_LC_adv'};
 2673:             }
 2674:             my $checked = '';
 2675:             if ($curroption eq '') {
 2676:                 $checked = ' checked="checked"';
 2677:             }
 2678:             $datatable .= '<span class="LC_nobreak"><label>'.
 2679:                           '<input type="radio" name="authorreq__LC_adv"'.
 2680:                           ' value=""'.$checked.' />'.
 2681:                           &mt('No override set').'</label></span>&nbsp; ';
 2682:             foreach my $option (@options) {
 2683:                 my $val = $option;
 2684:                 if ($option eq 'norequest') {
 2685:                     $val = 0;
 2686:                 }
 2687:                 my $checked = '';
 2688:                 if ($val eq $curroption) {
 2689:                     $checked = ' checked="checked"';
 2690:                 }
 2691:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2692:                               '<input type="radio" name="authorreq__LC_adv"'.
 2693:                               ' value="'.$val.'"'.$checked.' />'.
 2694:                               $titles{$option}.'</label></span>&nbsp; ';
 2695:             }
 2696:         } else {
 2697:             my $checked = 'checked="checked" ';
 2698:             if (ref($settings) eq 'HASH') {
 2699:                 if (ref($settings->{$item}) eq 'HASH') {
 2700:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2701:                         $checked = '';
 2702:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2703:                         $checked = 'checked="checked" ';
 2704:                     }
 2705:                 }
 2706:             }
 2707:             $datatable .= '<span class="LC_nobreak"><label>'.
 2708:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2709:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2710:                           '</label></span>&nbsp; ';
 2711:         }
 2712:     }
 2713:     if ($context eq 'requestcourses') {
 2714:         $datatable .= '</tr><tr>';
 2715:         foreach my $item (@usertools) {
 2716:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2717:         }
 2718:         $datatable .= '</tr></table>';
 2719:     }
 2720:     $datatable .= '</td></tr>';
 2721:     $$rowtotal += $typecount;
 2722:     return $datatable;
 2723: }
 2724: 
 2725: sub print_requestmail {
 2726:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2727:     my ($now,$datatable,%currapp);
 2728:     $now = time;
 2729:     if (ref($settings) eq 'HASH') {
 2730:         if (ref($settings->{'notify'}) eq 'HASH') {
 2731:             if ($settings->{'notify'}{'approval'} ne '') {
 2732:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2733:             }
 2734:         }
 2735:     }
 2736:     my $numinrow = 2;
 2737:     my $css_class;
 2738:     if ($$rowtotal%2) {
 2739:         $css_class = 'LC_odd_row';
 2740:     }
 2741:     if ($customcss) {
 2742:         $css_class .= " $customcss";
 2743:     }
 2744:     $css_class =~ s/^\s+//;
 2745:     if ($css_class) {
 2746:         $css_class = ' class="'.$css_class.'"';
 2747:     }
 2748:     if ($rowstyle) {
 2749:         $css_class .= ' style="'.$rowstyle.'"';
 2750:     }
 2751:     my $text;
 2752:     if ($action eq 'requestcourses') {
 2753:         $text = &mt('Receive notification of course requests requiring approval');
 2754:     } elsif ($action eq 'requestauthor') {
 2755:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2756:     } else {
 2757:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2758:     }
 2759:     $datatable = '<tr'.$css_class.'>'.
 2760:                  ' <td>'.$text.'</td>'.
 2761:                  ' <td class="LC_left_item">';
 2762:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2763:                                                  $action.'notifyapproval',%currapp);
 2764:     if ($numdc > 0) {
 2765:         $datatable .= $table;
 2766:     } else {
 2767:         $datatable .= &mt('There are no active Domain Coordinators');
 2768:     }
 2769:     $datatable .='</td></tr>';
 2770:     return $datatable;
 2771: }
 2772: 
 2773: sub print_studentcode {
 2774:     my ($settings,$rowtotal) = @_;
 2775:     my $rownum = 0; 
 2776:     my ($output,%current);
 2777:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2778:     if (ref($settings) eq 'HASH') {
 2779:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2780:             foreach my $type (@crstypes) {
 2781:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2782:             }
 2783:         }
 2784:     }
 2785:     $output .= '<tr>'.
 2786:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2787:                '<td class="LC_left_item">';
 2788:     foreach my $type (@crstypes) {
 2789:         my $check = ' ';
 2790:         if ($current{$type}) {
 2791:             $check = ' checked="checked" ';
 2792:         }
 2793:         $output .= '<span class="LC_nobreak"><label>'.
 2794:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2795:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2796:     }
 2797:     $output .= '</td></tr>';
 2798:     $$rowtotal ++;
 2799:     return $output;
 2800: }
 2801: 
 2802: sub print_textbookcourses {
 2803:     my ($dom,$type,$settings,$rowtotal) = @_;
 2804:     my $rownum = 0;
 2805:     my $css_class;
 2806:     my $itemcount = 1;
 2807:     my $maxnum = 0;
 2808:     my $bookshash;
 2809:     if (ref($settings) eq 'HASH') {
 2810:         $bookshash = $settings->{$type};
 2811:     }
 2812:     my %ordered;
 2813:     if (ref($bookshash) eq 'HASH') {
 2814:         foreach my $item (keys(%{$bookshash})) {
 2815:             if (ref($bookshash->{$item}) eq 'HASH') {
 2816:                 my $num = $bookshash->{$item}{'order'};
 2817:                 $ordered{$num} = $item;
 2818:             }
 2819:         }
 2820:     }
 2821:     my $confname = $dom.'-domainconfig';
 2822:     my $switchserver = &check_switchserver($dom,$confname);
 2823:     my $maxnum = scalar(keys(%ordered));
 2824:     my $datatable;
 2825:     if (keys(%ordered)) {
 2826:         my @items = sort { $a <=> $b } keys(%ordered);
 2827:         for (my $i=0; $i<@items; $i++) {
 2828:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2829:             my $key = $ordered{$items[$i]};
 2830:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2831:             my $coursetitle = $coursehash{'description'};
 2832:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2833:             if (ref($bookshash->{$key}) eq 'HASH') {
 2834:                 $subject = $bookshash->{$key}->{'subject'};
 2835:                 $title = $bookshash->{$key}->{'title'};
 2836:                 if ($type eq 'textbooks') {
 2837:                     $publisher = $bookshash->{$key}->{'publisher'};
 2838:                     $author = $bookshash->{$key}->{'author'};
 2839:                     $image = $bookshash->{$key}->{'image'};
 2840:                     if ($image ne '') {
 2841:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2842:                         my $imagethumb = "$path/tn-".$imagefile;
 2843:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2844:                     }
 2845:                 }
 2846:             }
 2847:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2848:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2849:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2850:             for (my $k=0; $k<=$maxnum; $k++) {
 2851:                 my $vpos = $k+1;
 2852:                 my $selstr;
 2853:                 if ($k == $i) {
 2854:                     $selstr = ' selected="selected" ';
 2855:                 }
 2856:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2857:             }
 2858:             $datatable .= '</select>'.('&nbsp;'x2).
 2859:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2860:                 &mt('Delete?').'</label></span></td>'.
 2861:                 '<td colspan="2">'.
 2862:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2863:                 ('&nbsp;'x2).
 2864:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2865:             if ($type eq 'textbooks') {
 2866:                 $datatable .= ('&nbsp;'x2).
 2867:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2868:                               ('&nbsp;'x2).
 2869:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2870:                               ('&nbsp;'x2).
 2871:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2872:                 if ($image) {
 2873:                     $datatable .= $imgsrc.
 2874:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2875:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2876:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2877:                 }
 2878:                 if ($switchserver) {
 2879:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2880:                 } else {
 2881:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2882:                 }
 2883:             }
 2884:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2885:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2886:                           $coursetitle.'</span></td></tr>'."\n";
 2887:             $itemcount ++;
 2888:         }
 2889:     }
 2890:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2891:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2892:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2893:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2894:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2895:     for (my $k=0; $k<$maxnum+1; $k++) {
 2896:         my $vpos = $k+1;
 2897:         my $selstr;
 2898:         if ($k == $maxnum) {
 2899:             $selstr = ' selected="selected" ';
 2900:         }
 2901:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2902:     }
 2903:     $datatable .= '</select>&nbsp;'."\n".
 2904:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2905:                   '<td colspan="2">'.
 2906:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2907:                   ('&nbsp;'x2).
 2908:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2909:                   ('&nbsp;'x2);
 2910:     if ($type eq 'textbooks') {
 2911:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2912:                       ('&nbsp;'x2).
 2913:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2914:                       ('&nbsp;'x2).
 2915:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2916:         if ($switchserver) {
 2917:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2918:         } else {
 2919:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2920:         }
 2921:         $datatable .= '</span>'."\n";
 2922:     }
 2923:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2924:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2925:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2926:                   &Apache::loncommon::selectcourse_link
 2927:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2928:                   '</span></td>'."\n".
 2929:                   '</tr>'."\n";
 2930:     $itemcount ++;
 2931:     return $datatable;
 2932: }
 2933: 
 2934: sub textbookcourses_javascript {
 2935:     my ($settings) = @_;
 2936:     return unless(ref($settings) eq 'HASH');
 2937:     my (%ordered,%total,%jstext);
 2938:     foreach my $type ('textbooks','templates') {
 2939:         $total{$type} = 0;
 2940:         if (ref($settings->{$type}) eq 'HASH') {
 2941:             foreach my $item (keys(%{$settings->{$type}})) {
 2942:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2943:                     my $num = $settings->{$type}->{$item}{'order'};
 2944:                     $ordered{$type}{$num} = $item;
 2945:                 }
 2946:             }
 2947:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2948:         }
 2949:         my @jsarray = ();
 2950:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2951:             push(@jsarray,$ordered{$type}{$item});
 2952:         }
 2953:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2954:     }
 2955:     return <<"ENDSCRIPT";
 2956: <script type="text/javascript">
 2957: // <![CDATA[
 2958: function reorderBooks(form,item,caller) {
 2959:     var changedVal;
 2960: $jstext{'textbooks'};
 2961: $jstext{'templates'};
 2962:     var newpos;
 2963:     var maxh;
 2964:     if (caller == 'textbooks') {  
 2965:         newpos = 'textbooks_addbook_pos';
 2966:         maxh = 1 + $total{'textbooks'};
 2967:     } else {
 2968:         newpos = 'templates_addbook_pos';
 2969:         maxh = 1 + $total{'templates'};
 2970:     }
 2971:     var current = new Array;
 2972:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2973:     if (item == newpos) {
 2974:         changedVal = newitemVal;
 2975:     } else {
 2976:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2977:         current[newitemVal] = newpos;
 2978:     }
 2979:     if (caller == 'textbooks') {
 2980:         for (var i=0; i<textbooks.length; i++) {
 2981:             var elementName = 'textbooks_'+textbooks[i];
 2982:             if (elementName != item) {
 2983:                 if (form.elements[elementName]) {
 2984:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2985:                     current[currVal] = elementName;
 2986:                 }
 2987:             }
 2988:         }
 2989:     }
 2990:     if (caller == 'templates') {
 2991:         for (var i=0; i<templates.length; i++) {
 2992:             var elementName = 'templates_'+templates[i];
 2993:             if (elementName != item) {
 2994:                 if (form.elements[elementName]) {
 2995:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2996:                     current[currVal] = elementName;
 2997:                 }
 2998:             }
 2999:         }
 3000:     }
 3001:     var oldVal;
 3002:     for (var j=0; j<maxh; j++) {
 3003:         if (current[j] == undefined) {
 3004:             oldVal = j;
 3005:         }
 3006:     }
 3007:     if (oldVal < changedVal) {
 3008:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3009:            var elementName = current[k];
 3010:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3011:         }
 3012:     } else {
 3013:         for (var k=changedVal; k<oldVal; k++) {
 3014:             var elementName = current[k];
 3015:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3016:         }
 3017:     }
 3018:     return;
 3019: }
 3020: 
 3021: // ]]>
 3022: </script>
 3023: 
 3024: ENDSCRIPT
 3025: }
 3026: 
 3027: sub ltitools_javascript {
 3028:     my ($settings) = @_;
 3029:     my $togglejs = &ltitools_toggle_js();
 3030:     unless (ref($settings) eq 'HASH') {
 3031:         return $togglejs;
 3032:     }
 3033:     my (%ordered,$total,%jstext);
 3034:     $total = 0;
 3035:     foreach my $item (keys(%{$settings})) {
 3036:         if (ref($settings->{$item}) eq 'HASH') {
 3037:             my $num = $settings->{$item}{'order'};
 3038:             $ordered{$num} = $item;
 3039:         }
 3040:     }
 3041:     $total = scalar(keys(%{$settings}));
 3042:     my @jsarray = ();
 3043:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3044:         push(@jsarray,$ordered{$item});
 3045:     }
 3046:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3047:     return <<"ENDSCRIPT";
 3048: <script type="text/javascript">
 3049: // <![CDATA[
 3050: function reorderLTITools(form,item) {
 3051:     var changedVal;
 3052: $jstext
 3053:     var newpos = 'ltitools_add_pos';
 3054:     var maxh = 1 + $total;
 3055:     var current = new Array;
 3056:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3057:     if (item == newpos) {
 3058:         changedVal = newitemVal;
 3059:     } else {
 3060:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3061:         current[newitemVal] = newpos;
 3062:     }
 3063:     for (var i=0; i<ltitools.length; i++) {
 3064:         var elementName = 'ltitools_'+ltitools[i];
 3065:         if (elementName != item) {
 3066:             if (form.elements[elementName]) {
 3067:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3068:                 current[currVal] = elementName;
 3069:             }
 3070:         }
 3071:     }
 3072:     var oldVal;
 3073:     for (var j=0; j<maxh; j++) {
 3074:         if (current[j] == undefined) {
 3075:             oldVal = j;
 3076:         }
 3077:     }
 3078:     if (oldVal < changedVal) {
 3079:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3080:            var elementName = current[k];
 3081:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3082:         }
 3083:     } else {
 3084:         for (var k=changedVal; k<oldVal; k++) {
 3085:             var elementName = current[k];
 3086:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3087:         }
 3088:     }
 3089:     return;
 3090: }
 3091: 
 3092: // ]]>
 3093: </script>
 3094: 
 3095: $togglejs
 3096: 
 3097: ENDSCRIPT
 3098: }
 3099: 
 3100: sub ltitools_toggle_js {
 3101:     return <<"ENDSCRIPT";
 3102: <script type="text/javascript">
 3103: // <![CDATA[
 3104: 
 3105: function toggleLTITools(form,setting,item) {
 3106:     var radioname = '';
 3107:     var divid = '';
 3108:     if ((setting == 'passback') || (setting == 'roster')) {
 3109:         radioname = 'ltitools_'+setting+'_'+item;
 3110:         divid = 'ltitools_'+setting+'time_'+item;
 3111:         var num = form.elements[radioname].length;
 3112:         if (num) {
 3113:             var setvis = '';
 3114:             for (var i=0; i<num; i++) {
 3115:                 if (form.elements[radioname][i].checked) {
 3116:                     if (form.elements[radioname][i].value == '1') {
 3117:                         if (document.getElementById(divid)) {
 3118:                             document.getElementById(divid).style.display = 'inline-block';
 3119:                         }
 3120:                         setvis = 1;
 3121:                     }
 3122:                     break;
 3123:                 }
 3124:             }
 3125:         }
 3126:         if (!setvis) {
 3127:             if (document.getElementById(divid)) {
 3128:                 document.getElementById(divid).style.display = 'none';
 3129:             }
 3130:         }
 3131:     }
 3132:     if (setting == 'user') {
 3133:         divid = 'ltitools_'+setting+'_div_'+item;
 3134:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3135:         if (document.getElementById(divid)) {
 3136:             if (document.getElementById(checkid)) {
 3137:                 if (document.getElementById(checkid).checked) {
 3138:                     document.getElementById(divid).style.display = 'inline-block';
 3139:                 } else {
 3140:                     document.getElementById(divid).style.display = 'none';
 3141:                 }
 3142:             }
 3143:         }
 3144:     }
 3145:     return;
 3146: }
 3147: // ]]>
 3148: </script>
 3149: 
 3150: ENDSCRIPT
 3151: }
 3152: 
 3153: sub wafproxy_javascript {
 3154:     my ($dom) = @_;
 3155:     return <<"ENDSCRIPT";
 3156: <script type="text/javascript">
 3157: // <![CDATA[
 3158: function updateWAF() {
 3159:     if (document.getElementById('wafproxy_remoteip')) {
 3160:         var wafremote = 0;
 3161:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3162:             wafremote = 1;
 3163:         }
 3164:         var fields = new Array('header','trust');
 3165:         for (var i=0; i<fields.length; i++) {
 3166:             if (document.getElementById('wafproxy_'+fields[i])) {
 3167:                 if (wafremote == 1) {
 3168:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3169:                 }
 3170:                 else {
 3171:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3172:                 }
 3173:             }
 3174:         }
 3175:         if (document.getElementById('wafproxyranges_$dom')) {
 3176:             if (wafremote == 1) {
 3177:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3178:             } else {
 3179:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3180:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3181:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3182:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3183:                         }
 3184:                     }
 3185:                 }
 3186:             }
 3187:         }
 3188:     }
 3189:     return;
 3190: }
 3191: 
 3192: function checkWAF() {
 3193:     if (document.getElementById('wafproxy_remoteip')) {
 3194:         var wafvpn = 0;
 3195:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3196:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3197:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3198:                     wafvpn = 1;
 3199:                 }
 3200:                 break;
 3201:             }
 3202:         }
 3203:         var vpn = new Array('vpnint','vpnext');
 3204:         for (var i=0; i<vpn.length; i++) {
 3205:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3206:                 if (wafvpn == 1) {
 3207:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3208:                 }
 3209:                 else {
 3210:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3211:                 }
 3212:             }
 3213:         }
 3214:         if (document.getElementById('wafproxyranges_$dom')) {
 3215:             if (wafvpn == 1) {
 3216:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3217:             }
 3218:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3219:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3220:             }
 3221:         }
 3222:     }
 3223:     return;
 3224: }
 3225: 
 3226: function toggleWAF() {
 3227:     if (document.getElementById('wafproxy_table')) {
 3228:         var wafproxy = 0;
 3229:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3230:              if (document.display.wafproxy_${dom}[i].checked) {
 3231:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3232:                      wafproxy = 1;
 3233:                      break;
 3234:                 }
 3235:             }
 3236:         }
 3237:         if (wafproxy == 1) {
 3238:             document.getElementById('wafproxy_table').style.display='inline';
 3239:         }
 3240:         else {
 3241:            document.getElementById('wafproxy_table').style.display='none';
 3242:         }
 3243:         if (document.getElementById('wafproxyrow_${dom}')) {
 3244:             if (wafproxy == 1) {
 3245:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3246:             }
 3247:             else {
 3248:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3249:             }
 3250:         }
 3251:         if (document.getElementById('nowafproxyrow_$dom')) {
 3252:             if (wafproxy == 1) {
 3253:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3254:             }
 3255:             else {
 3256:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3257:             }
 3258:         }
 3259:     }
 3260:     return;
 3261: }
 3262: // ]]>
 3263: </script>
 3264: 
 3265: ENDSCRIPT
 3266: }
 3267: 
 3268: sub proctoring_javascript {
 3269:     my ($settings) = @_;
 3270:     my (%ordered,$total,%jstext);
 3271:     $total = 0;
 3272:     if (ref($settings) eq 'HASH') {
 3273:         foreach my $item (keys(%{$settings})) {
 3274:             if (ref($settings->{$item}) eq 'HASH') {
 3275:                 my $num = $settings->{$item}{'order'};
 3276:                 $ordered{$num} = $item;
 3277:             }
 3278:         }
 3279:         $total = scalar(keys(%{$settings}));
 3280:     } else {
 3281:         %ordered = (
 3282:                        0 => 'proctorio',
 3283:                        1 => 'examity',
 3284:                    );
 3285:         $total = 2; 
 3286:     }
 3287:     my @jsarray = ();
 3288:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3289:         push(@jsarray,$ordered{$item});
 3290:     }
 3291:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3292:     return <<"ENDSCRIPT";
 3293: <script type="text/javascript">
 3294: // <![CDATA[
 3295: function reorderProctoring(form,item) {
 3296:     var changedVal;
 3297: $jstext
 3298:     var maxh = $total;
 3299:     var current = new Array;
 3300:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3301:     for (var i=0; i<proctors.length; i++) {
 3302:         var elementName = 'proctoring_pos_'+proctors[i];
 3303:         if (elementName != item) {
 3304:             if (form.elements[elementName]) {
 3305:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3306:                 current[currVal] = elementName;
 3307:             }
 3308:         }
 3309:     }
 3310:     var oldVal;
 3311:     for (var j=0; j<maxh; j++) {
 3312:         if (current[j] == undefined) {
 3313:             oldVal = j;
 3314:         }
 3315:     }
 3316:     if (oldVal < changedVal) {
 3317:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3318:            var elementName = current[k];
 3319:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3320:         }
 3321:     } else {
 3322:         for (var k=changedVal; k<oldVal; k++) {
 3323:             var elementName = current[k];
 3324:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3325:         }
 3326:     }
 3327:     return;
 3328: }
 3329: 
 3330: function toggleProctoring(form,item) {
 3331:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3332:     if (fieldsets.length) {
 3333:         var radioname = 'proctoring_available_'+item;
 3334:         var num = form.elements[radioname].length;
 3335:         if (num) {
 3336:             var setvis = '';
 3337:             for (var i=0; i<num; i++) {
 3338:                 if (form.elements[radioname][i].checked) {
 3339:                     if (form.elements[radioname][i].value == '1') {
 3340:                        setvis = 1;
 3341:                        break;
 3342:                     }
 3343:                 }
 3344:             }
 3345:             for (var j=0; j<fieldsets.length; j++) {
 3346:                 if (setvis) {
 3347:                     fieldsets[j].style.display = 'block';
 3348:                 } else {
 3349:                     fieldsets[j].style.display = 'none';
 3350:                 }
 3351:             }
 3352:         }
 3353:     }
 3354:     return;
 3355: }
 3356: 
 3357: // ]]>
 3358: </script>
 3359: 
 3360: ENDSCRIPT
 3361: }
 3362: 
 3363: 
 3364: sub lti_javascript {
 3365:     my ($settings) = @_;
 3366:     my $togglejs = &lti_toggle_js();
 3367:     unless (ref($settings) eq 'HASH') {
 3368:         return $togglejs;
 3369:     }
 3370:     my (%ordered,$total,%jstext);
 3371:     $total = scalar(keys(%{$settings}));
 3372:     foreach my $item (keys(%{$settings})) {
 3373:         if (ref($settings->{$item}) eq 'HASH') {
 3374:             my $num = $settings->{$item}{'order'};
 3375:             if ($num eq '') {
 3376:                 $num = $total - 1;
 3377:             }
 3378:             $ordered{$num} = $item;
 3379:         }
 3380:     }
 3381:     my @jsarray = ();
 3382:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3383:         push(@jsarray,$ordered{$item});
 3384:     }
 3385:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3386:     return <<"ENDSCRIPT";
 3387: <script type="text/javascript">
 3388: // <![CDATA[
 3389: function reorderLTI(form,item) {
 3390:     var changedVal;
 3391: $jstext
 3392:     var newpos = 'lti_pos_add';
 3393:     var maxh = 1 + $total;
 3394:     var current = new Array;
 3395:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3396:     if (item == newpos) {
 3397:         changedVal = newitemVal;
 3398:     } else {
 3399:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3400:         current[newitemVal] = newpos;
 3401:     }
 3402:     for (var i=0; i<lti.length; i++) {
 3403:         var elementName = 'lti_pos_'+lti[i];
 3404:         if (elementName != item) {
 3405:             if (form.elements[elementName]) {
 3406:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3407:                 current[currVal] = elementName;
 3408:             }
 3409:         }
 3410:     }
 3411:     var oldVal;
 3412:     for (var j=0; j<maxh; j++) {
 3413:         if (current[j] == undefined) {
 3414:             oldVal = j;
 3415:         }
 3416:     }
 3417:     if (oldVal < changedVal) {
 3418:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3419:            var elementName = current[k];
 3420:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3421:         }
 3422:     } else {
 3423:         for (var k=changedVal; k<oldVal; k++) {
 3424:             var elementName = current[k];
 3425:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3426:         }
 3427:     }
 3428:     return;
 3429: }
 3430: // ]]>
 3431: </script>
 3432: 
 3433: $togglejs
 3434: 
 3435: ENDSCRIPT
 3436: }
 3437: 
 3438: sub lti_toggle_js {
 3439:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3440:                             localauth => 'Local auth argument',
 3441:                             krb       => 'Kerberos domain',
 3442:                          );
 3443:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3444:     &js_escape(\$crsincalert);
 3445:     return <<"ENDSCRIPT";
 3446: <script type="text/javascript">
 3447: // <![CDATA[
 3448: 
 3449: function toggleLTI(form,setting,item) {
 3450:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3451:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3452:         var setvis = '';
 3453:         var radioname = 'lti_requser_'+item;
 3454:         var num = form.elements[radioname].length;
 3455:         if (num) {
 3456:             for (var i=0; i<num; i++) {
 3457:                 if (form.elements[radioname][i].checked) {
 3458:                     if (form.elements[radioname][i].value == '1') {
 3459:                         setvis = 1;
 3460:                         break;
 3461:                     }
 3462:                 }
 3463:             }
 3464:         }
 3465:         if (usrfieldsets.length) {
 3466:             for (var j=0; j<usrfieldsets.length; j++) {
 3467:                 if (setvis) {
 3468:                     usrfieldsets[j].style.display = 'block';
 3469:                 } else {
 3470:                     usrfieldsets[j].style.display = 'none';
 3471:                 }
 3472:             }
 3473:         }
 3474:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3475:         if (crsfieldsets.length) {
 3476:             radioname = 'lti_crsinc_'+item;
 3477:             var num = form.elements[radioname].length;
 3478:             if (num) {
 3479:                 var crsvis = '';
 3480:                 for (var i=0; i<num; i++) {
 3481:                     if (form.elements[radioname][i].checked) {
 3482:                         if (form.elements[radioname][i].value == '1') {
 3483:                             if (setvis == '') {
 3484:                                 if (setting == 'crsinc'){
 3485:                                     alert("$crsincalert");
 3486:                                     form.elements[radioname][0].checked = true;
 3487:                                 }
 3488:                             } else {
 3489:                                 crsvis = 1;
 3490:                             }
 3491:                             break;
 3492:                         }
 3493:                     }
 3494:                 }
 3495:                 setvis = crsvis;
 3496:             }
 3497:             for (var j=0; j<crsfieldsets.length; j++) {
 3498:                 if (setvis) {
 3499:                     crsfieldsets[j].style.display = 'block';
 3500:                 } else {
 3501:                     crsfieldsets[j].style.display = 'none';
 3502:                 }
 3503:             }
 3504:         }
 3505:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3506:         var radioname = '';
 3507:         var divid = '';
 3508:         if (setting == 'user') {
 3509:             radioname = 'lti_mapuser_'+item;
 3510:             divid = 'lti_userfield_'+item;
 3511:         } else if (setting == 'crs') {
 3512:             radioname = 'lti_mapcrs_'+item;
 3513:             divid = 'lti_crsfield_'+item;
 3514:         } else if (setting == 'callback') {
 3515:             radioname = 'lti_callback_'+item;
 3516:             divid = 'lti_callbackfield_'+item;
 3517:         } else {
 3518:             radioname = 'lti_passback_'+item;
 3519:             divid =  'lti_passback_'+item;
 3520:         }
 3521:         var num = form.elements[radioname].length;
 3522:         if (num) {
 3523:             var setvis = '';
 3524:             for (var i=0; i<num; i++) {
 3525:                if (form.elements[radioname][i].checked) {
 3526:                    if ((setting == 'passback') || (setting == 'callback')) {
 3527:                        if (form.elements[radioname][i].value == '1') {
 3528:                            if (document.getElementById(divid)) {
 3529:                                document.getElementById(divid).style.display = 'inline-block';
 3530:                            }
 3531:                            setvis = 1;
 3532:                            break;
 3533:                        }
 3534:                    } else {
 3535:                        if (form.elements[radioname][i].value == 'other') {
 3536:                            if (document.getElementById(divid)) {
 3537:                                document.getElementById(divid).style.display = 'inline-block';
 3538:                            }
 3539:                            setvis = 1;
 3540:                            break;
 3541:                        }
 3542:                    }
 3543:                } 
 3544:             }
 3545:             if (!setvis) {
 3546:                 if (document.getElementById(divid)) {
 3547:                     document.getElementById(divid).style.display = 'none';
 3548:                 }
 3549:             }
 3550:         }
 3551:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3552:         var numsec = form.elements['lti_crssec_'+item].length;
 3553:         if (numsec) {
 3554:             var setvis = '';
 3555:             for (var i=0; i<numsec; i++) {
 3556:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3557:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3558:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3559:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3560:                             setvis = 1;
 3561:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3562:                             if (numsrcsec) {
 3563:                                 var setsrcvis = '';
 3564:                                 for (var j=0; j<numsrcsec; j++) {
 3565:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3566:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3567:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3568:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3569:                                                 setsrcvis = 1;
 3570:                                             }
 3571:                                         }
 3572:                                     }
 3573:                                 }
 3574:                                 if (!setsrcvis) {
 3575:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3576:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3577:                                     }
 3578:                                 }
 3579:                             }
 3580:                         }
 3581:                     }
 3582:                 }
 3583:             }
 3584:             if (!setvis) {
 3585:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3586:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3587:                 }
 3588:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3589:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3590:                 }
 3591:             }
 3592:         }
 3593:     } else if (setting == 'lcauth') {
 3594:         var numauth = form.elements['lti_lcauth_'+item].length;
 3595:         if (numauth) {
 3596:             for (var i=0; i<numauth; i++) {
 3597:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3598:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3599:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3600:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3601:                         } else {
 3602:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3603:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3604:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3605:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3606:                                 } else {
 3607:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3608:                                 }
 3609:                             }
 3610:                         }
 3611:                     }
 3612:                 }
 3613:             }
 3614:         }
 3615:     } else if (setting == 'lcmenu') {
 3616:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3617:         var divid = 'lti_menufield_'+item;
 3618:         var setvis = '';
 3619:         for (var i=0; i<menus.length; i++) {
 3620:             var radioname = menus[i];  
 3621:             var num = form.elements[radioname].length;
 3622:             if (num) {
 3623:                 for (var j=0; j<num; j++) {
 3624:                     if (form.elements[radioname][j].checked) {
 3625:                         if (form.elements[radioname][j].value == '1') {
 3626:                             if (document.getElementById(divid)) {
 3627:                                 document.getElementById(divid).style.display = 'inline-block';
 3628:                             }
 3629:                             setvis = 1;
 3630:                             break;
 3631:                         }
 3632:                     }
 3633:                 }
 3634:             }
 3635:             if (setvis == 1) {
 3636:                 break;
 3637:             }
 3638:         }
 3639:         if (!setvis) {
 3640:             if (document.getElementById(divid)) {
 3641:                 document.getElementById(divid).style.display = 'none';
 3642:             }
 3643:         }
 3644:     }
 3645:     return;
 3646: }
 3647: // ]]>
 3648: </script>
 3649: 
 3650: ENDSCRIPT
 3651: }
 3652: 
 3653: sub autoupdate_javascript {
 3654:     return <<"ENDSCRIPT";
 3655: <script type="text/javascript">
 3656: // <![CDATA[
 3657: function toggleLastActiveDays(form) {
 3658:     var radioname = 'lastactive';
 3659:     var divid = 'lastactive_div';
 3660:     var num = form.elements[radioname].length;
 3661:     if (num) {
 3662:         var setvis = '';
 3663:         for (var i=0; i<num; i++) {
 3664:             if (form.elements[radioname][i].checked) {
 3665:                 if (form.elements[radioname][i].value == '1') {
 3666:                     if (document.getElementById(divid)) {
 3667:                         document.getElementById(divid).style.display = 'inline-block';
 3668:                     }
 3669:                     setvis = 1;
 3670:                 }
 3671:                 break;
 3672:             }
 3673:         }
 3674:         if (!setvis) {
 3675:             if (document.getElementById(divid)) {
 3676:                 document.getElementById(divid).style.display = 'none';
 3677:             }
 3678:         }
 3679:     }
 3680:     return;
 3681: }
 3682: // ]]>
 3683: </script>
 3684: 
 3685: ENDSCRIPT
 3686: }
 3687: 
 3688: sub saml_javascript {
 3689:     return <<"ENDSCRIPT";
 3690: <script type="text/javascript">
 3691: // <![CDATA[
 3692: function toggleSamlOptions(form,hostid) { 
 3693:     var radioname = 'saml_'+hostid;
 3694:     var tablecellon = 'samloptionson_'+hostid;
 3695:     var tablecelloff = 'samloptionsoff_'+hostid;
 3696:     var num = form.elements[radioname].length;
 3697:     if (num) {
 3698:         var setvis = ''; 
 3699:         for (var i=0; i<num; i++) {
 3700:             if (form.elements[radioname][i].checked) {
 3701:                 if (form.elements[radioname][i].value == '1') { 
 3702:                     if (document.getElementById(tablecellon)) {
 3703:                         document.getElementById(tablecellon).style.display='';
 3704:                     }
 3705:                     if (document.getElementById(tablecelloff)) {
 3706:                         document.getElementById(tablecelloff).style.display='none';
 3707:                     }
 3708:                     setvis = 1;
 3709:                 }
 3710:                 break;
 3711:             }
 3712:         }
 3713:         if (!setvis) {
 3714:             if (document.getElementById(tablecellon)) {
 3715:                 document.getElementById(tablecellon).style.display='none';
 3716:             }
 3717:             if (document.getElementById(tablecelloff)) {
 3718:                 document.getElementById(tablecelloff).style.display='';
 3719:             }
 3720:         }
 3721:     }
 3722:     return;
 3723: }
 3724: // ]]>
 3725: </script>
 3726: 
 3727: ENDSCRIPT
 3728: }
 3729: 
 3730: sub ipaccess_javascript {
 3731:     my ($settings) = @_;
 3732:     my (%ordered,$total,%jstext);
 3733:     $total = 0;
 3734:     if (ref($settings) eq 'HASH') {
 3735:         foreach my $item (keys(%{$settings})) {
 3736:             if (ref($settings->{$item}) eq 'HASH') {
 3737:                 my $num = $settings->{$item}{'order'};
 3738:                 $ordered{$num} = $item;
 3739:             }
 3740:         }
 3741:         $total = scalar(keys(%{$settings}));
 3742:     }
 3743:     my @jsarray = ();
 3744:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3745:         push(@jsarray,$ordered{$item});
 3746:     }
 3747:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3748:     return <<"ENDSCRIPT";
 3749: <script type="text/javascript">
 3750: // <![CDATA[
 3751: function reorderIPaccess(form,item) {
 3752:     var changedVal;
 3753: $jstext
 3754:     var newpos = 'ipaccess_pos_add';
 3755:     var maxh = 1 + $total;
 3756:     var current = new Array;
 3757:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3758:     if (item == newpos) {
 3759:         changedVal = newitemVal;
 3760:     } else {
 3761:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3762:         current[newitemVal] = newpos;
 3763:     }
 3764:     for (var i=0; i<ipaccess.length; i++) {
 3765:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3766:         if (elementName != item) {
 3767:             if (form.elements[elementName]) {
 3768:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3769:                 current[currVal] = elementName;
 3770:             }
 3771:         }
 3772:     }
 3773:     var oldVal;
 3774:     for (var j=0; j<maxh; j++) {
 3775:         if (current[j] == undefined) {
 3776:             oldVal = j;
 3777:         }
 3778:     }
 3779:     if (oldVal < changedVal) {
 3780:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3781:            var elementName = current[k];
 3782:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3783:         }
 3784:     } else {
 3785:         for (var k=changedVal; k<oldVal; k++) {
 3786:             var elementName = current[k];
 3787:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3788:         }
 3789:     }
 3790:     return;
 3791: }
 3792: // ]]>
 3793: </script>
 3794: 
 3795: ENDSCRIPT
 3796: }
 3797: 
 3798: sub print_autoenroll {
 3799:     my ($dom,$settings,$rowtotal) = @_;
 3800:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3801:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3802:     if (ref($settings) eq 'HASH') {
 3803:         if (exists($settings->{'run'})) {
 3804:             if ($settings->{'run'} eq '0') {
 3805:                 $runoff = ' checked="checked" ';
 3806:                 $runon = ' ';
 3807:             } else {
 3808:                 $runon = ' checked="checked" ';
 3809:                 $runoff = ' ';
 3810:             }
 3811:         } else {
 3812:             if ($autorun) {
 3813:                 $runon = ' checked="checked" ';
 3814:                 $runoff = ' ';
 3815:             } else {
 3816:                 $runoff = ' checked="checked" ';
 3817:                 $runon = ' ';
 3818:             }
 3819:         }
 3820:         if (exists($settings->{'co-owners'})) {
 3821:             if ($settings->{'co-owners'} eq '0') {
 3822:                 $coownersoff = ' checked="checked" ';
 3823:                 $coownerson = ' ';
 3824:             } else {
 3825:                 $coownerson = ' checked="checked" ';
 3826:                 $coownersoff = ' ';
 3827:             }
 3828:         } else {
 3829:             $coownersoff = ' checked="checked" ';
 3830:             $coownerson = ' ';
 3831:         }
 3832:         if (exists($settings->{'sender_domain'})) {
 3833:             $defdom = $settings->{'sender_domain'};
 3834:         }
 3835:         if (exists($settings->{'autofailsafe'})) {
 3836:             $failsafe = $settings->{'autofailsafe'};
 3837:         }
 3838:     } else {
 3839:         if ($autorun) {
 3840:             $runon = ' checked="checked" ';
 3841:             $runoff = ' ';
 3842:         } else {
 3843:             $runoff = ' checked="checked" ';
 3844:             $runon = ' ';
 3845:         }
 3846:     }
 3847:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3848:     my $notif_sender;
 3849:     if (ref($settings) eq 'HASH') {
 3850:         $notif_sender = $settings->{'sender_uname'};
 3851:     }
 3852:     my $datatable='<tr class="LC_odd_row">'.
 3853:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3854:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3855:                   '<input type="radio" name="autoenroll_run"'.
 3856:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3857:                   '<label><input type="radio" name="autoenroll_run"'.
 3858:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3859:                   '</tr><tr>'.
 3860:                   '<td>'.&mt('Notification messages - sender').
 3861:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3862:                   &mt('username').':&nbsp;'.
 3863:                   '<input type="text" name="sender_uname" value="'.
 3864:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3865:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3866:                   '<tr class="LC_odd_row">'.
 3867:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3868:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3869:                   '<input type="radio" name="autoassign_coowners"'.
 3870:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3871:                   '<label><input type="radio" name="autoassign_coowners"'.
 3872:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3873:                   '</tr><tr>'.
 3874:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3875:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3876:                   '<input type="text" name="autoenroll_failsafe"'.
 3877:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3878:     $$rowtotal += 4;
 3879:     return $datatable;
 3880: }
 3881: 
 3882: sub print_autoupdate {
 3883:     my ($position,$dom,$settings,$rowtotal) = @_;
 3884:     my ($enable,$datatable);
 3885:     if ($position eq 'top') {
 3886:         my %choices = &Apache::lonlocal::texthash (
 3887:                           run        => 'Auto-update active?',
 3888:                           classlists => 'Update information in classlists?',
 3889:                           unexpired  => 'Skip updates for users without active or future roles?',
 3890:                           lastactive => 'Skip updates for inactive users?',
 3891:         );
 3892:         my $itemcount = 0;
 3893:         my $updateon = ' ';
 3894:         my $updateoff = ' checked="checked" ';
 3895:         if (ref($settings) eq 'HASH') {
 3896:             if ($settings->{'run'} eq '1') {
 3897:                 $updateon = $updateoff;
 3898:                 $updateoff = ' ';
 3899:             }
 3900:         }
 3901:         $enable = '<tr class="LC_odd_row">'.
 3902:                   '<td>'.$choices{'run'}.'</td>'.
 3903:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3904:                   '<input type="radio" name="autoupdate_run"'.
 3905:                   $updateoff.' value="0" />'.&mt('No').'</label>&nbsp;'.
 3906:                   '<label><input type="radio" name="autoupdate_run"'.
 3907:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3908:                   '</tr>';
 3909:         my @toggles = ('classlists','unexpired');
 3910:         my %defaultchecked = ('classlists' => 'off',
 3911:                               'unexpired'  => 'off'
 3912:                               );
 3913:         $$rowtotal ++;
 3914:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3915:                                                      \%choices,$itemcount,'','','left','no');
 3916:         $datatable = $enable.$datatable;
 3917:         $$rowtotal += $itemcount;
 3918:         my $lastactiveon = ' ';
 3919:         my $lastactiveoff = ' checked="checked" ';
 3920:         my $lastactivestyle = 'none';
 3921:         my $lastactivedays;
 3922:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3923:         if (ref($settings) eq 'HASH') {
 3924:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3925:                 $lastactiveon = $lastactiveoff;
 3926:                 $lastactiveoff = ' ';
 3927:                 $lastactivestyle = 'inline-block';
 3928:                 $lastactivedays = $settings->{'lastactive'};
 3929:             }
 3930:         }
 3931:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3932:         $datatable .= '<tr'.$css_class.'>'.
 3933:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3934:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3935:                       '<input type="radio" name="lastactive"'.
 3936:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3937:                       '&nbsp;<label>'.
 3938:                       '<input type="radio" name="lastactive"'.
 3939:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3940:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3941:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3942:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3943:                           $lastactivedays.'" />').
 3944:                       '</span></td>'.
 3945:                       '</tr>';
 3946:         $$rowtotal ++;
 3947:     } elsif ($position eq 'middle') {
 3948:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3949:         my $numinrow = 3;
 3950:         my $locknamesettings;
 3951:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3952:                                      $dom,$numinrow,$othertitle,
 3953:                                     'lockablenames',$rowtotal);
 3954:         $$rowtotal ++;
 3955:     } else {
 3956:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3957:         my @fields = ('lastname','firstname','middlename','generation',
 3958:                       'permanentemail','id');
 3959:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3960:         my $numrows = 0;
 3961:         if (ref($types) eq 'ARRAY') {
 3962:             if (@{$types} > 0) {
 3963:                 $datatable = 
 3964:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3965:                                          \@fields,$types,\$numrows);
 3966:                     $$rowtotal += @{$types}; 
 3967:             }
 3968:         }
 3969:         $datatable .= 
 3970:             &usertype_update_row($settings,{'default' => $othertitle},
 3971:                                  \%fieldtitles,\@fields,['default'],
 3972:                                  \$numrows);
 3973:         $$rowtotal ++;     
 3974:     }
 3975:     return $datatable;
 3976: }
 3977: 
 3978: sub print_autocreate {
 3979:     my ($dom,$settings,$rowtotal) = @_;
 3980:     my (%createon,%createoff,%currhash);
 3981:     my @types = ('xml','req');
 3982:     if (ref($settings) eq 'HASH') {
 3983:         foreach my $item (@types) {
 3984:             $createoff{$item} = ' checked="checked" ';
 3985:             $createon{$item} = ' ';
 3986:             if (exists($settings->{$item})) {
 3987:                 if ($settings->{$item}) {
 3988:                     $createon{$item} = ' checked="checked" ';
 3989:                     $createoff{$item} = ' ';
 3990:                 }
 3991:             }
 3992:         }
 3993:         if ($settings->{'xmldc'} ne '') {
 3994:             $currhash{$settings->{'xmldc'}} = 1;
 3995:         }
 3996:     } else {
 3997:         foreach my $item (@types) {
 3998:             $createoff{$item} = ' checked="checked" ';
 3999:             $createon{$item} = ' ';
 4000:         }
 4001:     }
 4002:     $$rowtotal += 2;
 4003:     my $numinrow = 2;
 4004:     my $datatable='<tr class="LC_odd_row">'.
 4005:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4006:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4007:                   '<input type="radio" name="autocreate_xml"'.
 4008:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4009:                   '<label><input type="radio" name="autocreate_xml"'.
 4010:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4011:                   '</td></tr><tr>'.
 4012:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4013:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4014:                   '<input type="radio" name="autocreate_req"'.
 4015:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4016:                   '<label><input type="radio" name="autocreate_req"'.
 4017:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4018:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4019:                                                    'autocreate_xmldc',%currhash);
 4020:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4021:     if ($numdc > 1) {
 4022:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4023:                       '</td><td class="LC_left_item">';
 4024:     } else {
 4025:         $datatable .= &mt('Course creation processed as:').
 4026:                       '</td><td class="LC_right_item">';
 4027:     }
 4028:     $datatable .= $dctable.'</td></tr>';
 4029:     $$rowtotal += $rows;
 4030:     return $datatable;
 4031: }
 4032: 
 4033: sub print_directorysrch {
 4034:     my ($position,$dom,$settings,$rowtotal) = @_;
 4035:     my $datatable;
 4036:     if ($position eq 'top') {
 4037:         my $instsrchon = ' ';
 4038:         my $instsrchoff = ' checked="checked" ';
 4039:         my ($exacton,$containson,$beginson);
 4040:         my $instlocalon = ' ';
 4041:         my $instlocaloff = ' checked="checked" ';
 4042:         if (ref($settings) eq 'HASH') {
 4043:             if ($settings->{'available'} eq '1') {
 4044:                 $instsrchon = $instsrchoff;
 4045:                 $instsrchoff = ' ';
 4046:             }
 4047:             if ($settings->{'localonly'} eq '1') {
 4048:                 $instlocalon = $instlocaloff;
 4049:                 $instlocaloff = ' ';
 4050:             }
 4051:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4052:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4053:                     if ($type eq 'exact') {
 4054:                         $exacton = ' checked="checked" ';
 4055:                     } elsif ($type eq 'contains') {
 4056:                         $containson = ' checked="checked" ';
 4057:                     } elsif ($type eq 'begins') {
 4058:                         $beginson = ' checked="checked" ';
 4059:                     }
 4060:                 }
 4061:             } else {
 4062:                 if ($settings->{'searchtypes'} eq 'exact') {
 4063:                     $exacton = ' checked="checked" ';
 4064:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4065:                     $containson = ' checked="checked" ';
 4066:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4067:                     $exacton = ' checked="checked" ';
 4068:                     $containson = ' checked="checked" ';
 4069:                 }
 4070:             }
 4071:         }
 4072:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4073:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4074: 
 4075:         my $numinrow = 4;
 4076:         my $cansrchrow = 0;
 4077:         $datatable='<tr class="LC_odd_row">'.
 4078:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4079:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4080:                    '<input type="radio" name="dirsrch_available"'.
 4081:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4082:                    '<label><input type="radio" name="dirsrch_available"'.
 4083:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4084:                    '</tr><tr>'.
 4085:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4086:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4087:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4088:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4089:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4090:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4091:                    '</tr>';
 4092:         $$rowtotal += 2;
 4093:         if (ref($usertypes) eq 'HASH') {
 4094:             if (keys(%{$usertypes}) > 0) {
 4095:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4096:                                              $numinrow,$othertitle,'cansearch',
 4097:                                              $rowtotal);
 4098:                 $cansrchrow = 1;
 4099:             }
 4100:         }
 4101:         if ($cansrchrow) {
 4102:             $$rowtotal ++;
 4103:             $datatable .= '<tr>';
 4104:         } else {
 4105:             $datatable .= '<tr class="LC_odd_row">';
 4106:         }
 4107:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4108:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4109:         foreach my $title (@{$titleorder}) {
 4110:             if (defined($searchtitles->{$title})) {
 4111:                 my $check = ' ';
 4112:                 if (ref($settings) eq 'HASH') {
 4113:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4114:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4115:                             $check = ' checked="checked" ';
 4116:                         }
 4117:                     }
 4118:                 }
 4119:                 $datatable .= '<td class="LC_left_item">'.
 4120:                               '<span class="LC_nobreak"><label>'.
 4121:                               '<input type="checkbox" name="searchby" '.
 4122:                               'value="'.$title.'"'.$check.'/>'.
 4123:                               $searchtitles->{$title}.'</label></span></td>';
 4124:             }
 4125:         }
 4126:         $datatable .= '</tr></table></td></tr>';
 4127:         $$rowtotal ++;
 4128:         if ($cansrchrow) {
 4129:             $datatable .= '<tr class="LC_odd_row">';
 4130:         } else {
 4131:             $datatable .= '<tr>';
 4132:         }
 4133:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4134:                       '<td class="LC_left_item" colspan="2">'.
 4135:                       '<span class="LC_nobreak"><label>'.
 4136:                       '<input type="checkbox" name="searchtypes" '.
 4137:                       $exacton.' value="exact" />'.&mt('Exact match').
 4138:                       '</label>&nbsp;'.
 4139:                       '<label><input type="checkbox" name="searchtypes" '.
 4140:                       $beginson.' value="begins" />'.&mt('Begins with').
 4141:                       '</label>&nbsp;'.
 4142:                       '<label><input type="checkbox" name="searchtypes" '.
 4143:                       $containson.' value="contains" />'.&mt('Contains').
 4144:                       '</label></span></td></tr>';
 4145:         $$rowtotal ++;
 4146:     } else {
 4147:         my $domsrchon = ' checked="checked" ';
 4148:         my $domsrchoff = ' ';
 4149:         my $domlocalon = ' ';
 4150:         my $domlocaloff = ' checked="checked" ';
 4151:         if (ref($settings) eq 'HASH') {
 4152:             if ($settings->{'lclocalonly'} eq '1') {
 4153:                 $domlocalon = $domlocaloff;
 4154:                 $domlocaloff = ' ';
 4155:             }
 4156:             if ($settings->{'lcavailable'} eq '0') {
 4157:                 $domsrchoff = $domsrchon;
 4158:                 $domsrchon = ' ';
 4159:             }
 4160:         }
 4161:         $datatable='<tr class="LC_odd_row">'.
 4162:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4163:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4164:                       '<input type="radio" name="dirsrch_domavailable"'.
 4165:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4166:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4167:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4168:                       '</tr><tr>'.
 4169:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4170:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4171:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4172:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4173:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4174:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4175:                       '</tr>';
 4176:         $$rowtotal += 2;
 4177:     }
 4178:     return $datatable;
 4179: }
 4180: 
 4181: sub print_contacts {
 4182:     my ($position,$dom,$settings,$rowtotal) = @_;
 4183:     my $datatable;
 4184:     my @contacts = ('adminemail','supportemail');
 4185:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4186:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4187:     if ($position eq 'top') {
 4188:         if (ref($settings) eq 'HASH') {
 4189:             foreach my $item (@contacts) {
 4190:                 if (exists($settings->{$item})) {
 4191:                     $to{$item} = $settings->{$item};
 4192:                 }
 4193:             }
 4194:         }
 4195:     } elsif ($position eq 'middle') {
 4196:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4197:                      'updatesmail','idconflictsmail','hostipmail');
 4198:         foreach my $type (@mailings) {
 4199:             $otheremails{$type} = '';
 4200:         }
 4201:     } elsif ($position eq 'lower') {
 4202:         if (ref($settings) eq 'HASH') {
 4203:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4204:                 %lonstatus = %{$settings->{'lonstatus'}};
 4205:             }
 4206:         }
 4207:     } else {
 4208:         @mailings = ('helpdeskmail','otherdomsmail');
 4209:         foreach my $type (@mailings) {
 4210:             $otheremails{$type} = '';
 4211:         }
 4212:         $bccemails{'helpdeskmail'} = '';
 4213:         $bccemails{'otherdomsmail'} = '';
 4214:         $includestr{'helpdeskmail'} = '';
 4215:         $includestr{'otherdomsmail'} = '';
 4216:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4217:     }
 4218:     if (ref($settings) eq 'HASH') {
 4219:         unless (($position eq 'top') || ($position eq 'lower')) {
 4220:             foreach my $type (@mailings) {
 4221:                 if (exists($settings->{$type})) {
 4222:                     if (ref($settings->{$type}) eq 'HASH') {
 4223:                         foreach my $item (@contacts) {
 4224:                             if ($settings->{$type}{$item}) {
 4225:                                 $checked{$type}{$item} = ' checked="checked" ';
 4226:                             }
 4227:                         }
 4228:                         $otheremails{$type} = $settings->{$type}{'others'};
 4229:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4230:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4231:                             if ($settings->{$type}{'include'} ne '') {
 4232:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4233:                                 $includestr{$type} = &unescape($includestr{$type});
 4234:                             }
 4235:                         }
 4236:                     }
 4237:                 } elsif ($type eq 'lonstatusmail') {
 4238:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4239:                 }
 4240:             }
 4241:         }
 4242:         if ($position eq 'bottom') {
 4243:             foreach my $type (@mailings) {
 4244:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4245:                 if ($settings->{$type}{'include'} ne '') {
 4246:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4247:                     $includestr{$type} = &unescape($includestr{$type});
 4248:                 }
 4249:             }
 4250:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4251:                 if (ref($fields) eq 'ARRAY') {
 4252:                     foreach my $field (@{$fields}) {
 4253:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4254:                     }
 4255:                 }
 4256:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4257:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4258:                 } else {
 4259:                     $maxsize = '1.0';
 4260:                 }
 4261:             } else {
 4262:                 if (ref($fields) eq 'ARRAY') {
 4263:                     foreach my $field (@{$fields}) {
 4264:                         $currfield{$field} = 'yes';
 4265:                     }
 4266:                 }
 4267:                 $maxsize = '1.0';
 4268:             }
 4269:         }
 4270:     } else {
 4271:         if ($position eq 'top') {
 4272:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4273:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4274:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4275:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4276:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4277:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4278:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4279:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4280:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4281:         } elsif ($position eq 'bottom') {
 4282:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4283:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4284:             if (ref($fields) eq 'ARRAY') {
 4285:                 foreach my $field (@{$fields}) {
 4286:                     $currfield{$field} = 'yes';
 4287:                 }
 4288:             }
 4289:             $maxsize = '1.0';
 4290:         }
 4291:     }
 4292:     my ($titles,$short_titles) = &contact_titles();
 4293:     my $rownum = 0;
 4294:     my $css_class;
 4295:     if ($position eq 'top') {
 4296:         foreach my $item (@contacts) {
 4297:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4298:             $datatable .= '<tr'.$css_class.'>'. 
 4299:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4300:                           '</span></td><td class="LC_right_item">'.
 4301:                           '<input type="text" name="'.$item.'" value="'.
 4302:                           $to{$item}.'" /></td></tr>';
 4303:             $rownum ++;
 4304:         }
 4305:     } elsif ($position eq 'bottom') {
 4306:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4307:         $datatable .= '<tr'.$css_class.'>'.
 4308:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4309:                       &mt('(e-mail, subject, and description always shown)').
 4310:                       '</td><td class="LC_left_item">';
 4311:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4312:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4313:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4314:             foreach my $field (@{$fields}) {
 4315:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4316:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4317:                     $datatable .= ' '.&mt('(logged-in users)');
 4318:                 }
 4319:                 $datatable .='</td><td>';
 4320:                 my $clickaction;
 4321:                 if ($field eq 'screenshot') {
 4322:                     $clickaction = ' onclick="screenshotSize(this);"';
 4323:                 }
 4324:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4325:                     foreach my $option (@{$possoptions->{$field}}) {
 4326:                         my $checked;
 4327:                         if ($currfield{$field} eq $option) {
 4328:                             $checked = ' checked="checked"';
 4329:                         }
 4330:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4331:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4332:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4333:                                       '</label></span>'.('&nbsp;'x2);
 4334:                     }
 4335:                 }
 4336:                 if ($field eq 'screenshot') {
 4337:                     my $display;
 4338:                     if ($currfield{$field} eq 'no') {
 4339:                         $display = ' style="display:none"';
 4340:                     }
 4341:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4342:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4343:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4344:                 }
 4345:                 $datatable .= '</td></tr>';
 4346:             }
 4347:             $datatable .= '</table>';
 4348:         }
 4349:         $datatable .= '</td></tr>'."\n";
 4350:         $rownum ++;
 4351:     }
 4352:     unless (($position eq 'top') || ($position eq 'lower')) {
 4353:         foreach my $type (@mailings) {
 4354:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4355:             $datatable .= '<tr'.$css_class.'>'.
 4356:                           '<td><span class="LC_nobreak">'.
 4357:                           $titles->{$type}.': </span></td>'.
 4358:                           '<td class="LC_left_item">';
 4359:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4360:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4361:             }
 4362:             $datatable .= '<span class="LC_nobreak">';
 4363:             foreach my $item (@contacts) {
 4364:                 $datatable .= '<label>'.
 4365:                               '<input type="checkbox" name="'.$type.'"'.
 4366:                               $checked{$type}{$item}.
 4367:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4368:                               '</label>&nbsp;';
 4369:             }
 4370:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4371:                           '<input type="text" name="'.$type.'_others" '.
 4372:                           'value="'.$otheremails{$type}.'"  />';
 4373:             my %locchecked;
 4374:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4375:                 foreach my $loc ('s','b') {
 4376:                     if ($includeloc{$type} eq $loc) {
 4377:                         $locchecked{$loc} = ' checked="checked"';
 4378:                         last;
 4379:                     }
 4380:                 }
 4381:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4382:                               '<input type="text" name="'.$type.'_bcc" '.
 4383:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4384:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4385:                               &mt('Text automatically added to e-mail:').' '.
 4386:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4387:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4388:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4389:                               ('&nbsp;'x2).
 4390:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4391:                               '</span></fieldset>';
 4392:             }
 4393:             $datatable .= '</td></tr>'."\n";
 4394:             $rownum ++;
 4395:         }
 4396:     }
 4397:     if ($position eq 'middle') {
 4398:         my %choices;
 4399:         my $corelink = &core_link_msu();
 4400:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4401:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4402:                                         $corelink);
 4403:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4404:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4405:         my %defaultchecked = ('reporterrors'  => 'on',
 4406:                               'reportupdates' => 'on',
 4407:                               'reportstatus'  => 'on');
 4408:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4409:                                                    \%choices,$rownum);
 4410:         $datatable .= $reports;
 4411:     } elsif ($position eq 'lower') {
 4412:         my (%current,%excluded,%weights);
 4413:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4414:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4415:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4416:         } else {
 4417:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4418:         }
 4419:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4420:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4421:         } else {
 4422:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4423:         }
 4424:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4425:             foreach my $type ('E','W','N','U') {
 4426:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4427:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4428:                 } else {
 4429:                     $weights{$type} = $defaults->{$type};
 4430:                 }
 4431:             }
 4432:         } else {
 4433:             foreach my $type ('E','W','N','U') {
 4434:                 $weights{$type} = $defaults->{$type};
 4435:             }
 4436:         }
 4437:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4438:             if (@{$lonstatus{'excluded'}} > 0) {
 4439:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4440:             }
 4441:         }
 4442:         foreach my $item ('errorthreshold','errorsysmail') { 
 4443:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4444:             $datatable .= '<tr'.$css_class.'>'.
 4445:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4446:                           $titles->{$item}.
 4447:                           '</span></td><td class="LC_left_item">'.
 4448:                           '<input type="text" name="'.$item.'" value="'.
 4449:                           $current{$item}.'" size="5" /></td></tr>';
 4450:             $rownum ++;
 4451:         }
 4452:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4453:         $datatable .= '<tr'.$css_class.'>'.
 4454:                       '<td class="LC_left_item">'.
 4455:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4456:                       '</span></td><td class="LC_left_item"><table><tr>';
 4457:         foreach my $type ('E','W','N','U') {
 4458:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4459:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4460:                           $weights{$type}.'" size="5" /></td>';
 4461:         }
 4462:         $datatable .= '</tr></table></tr>';
 4463:         $rownum ++;
 4464:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4465:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4466:                       $titles->{'errorexcluded'}.'</td>'.
 4467:                       '<td class="LC_left_item"><table>';
 4468:         my $numinrow = 4;
 4469:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4470:         for (my $i=0; $i<@ids; $i++) {
 4471:             my $rem = $i%($numinrow);
 4472:             if ($rem == 0) {
 4473:                 if ($i > 0) {
 4474:                     $datatable .= '</tr>';
 4475:                 }
 4476:                 $datatable .= '<tr>';
 4477:             }
 4478:             my $check;
 4479:             if ($excluded{$ids[$i]}) {
 4480:                 $check = ' checked="checked" ';
 4481:             }
 4482:             $datatable .= '<td class="LC_left_item">'.
 4483:                           '<span class="LC_nobreak"><label>'.
 4484:                           '<input type="checkbox" name="errorexcluded" '.
 4485:                           'value="'.$ids[$i].'"'.$check.' />'.
 4486:                           $ids[$i].'</label></span></td>';
 4487:         }
 4488:         my $colsleft = $numinrow - @ids%($numinrow);
 4489:         if ($colsleft > 1 ) {
 4490:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4491:                           '&nbsp;</td>';
 4492:         } elsif ($colsleft == 1) {
 4493:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4494:         }
 4495:         $datatable .= '</tr></table></td></tr>';
 4496:         $rownum ++;
 4497:     } elsif ($position eq 'bottom') {
 4498:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4499:         my (@posstypes,%usertypeshash);
 4500:         if (ref($types) eq 'ARRAY') {
 4501:             @posstypes = @{$types};
 4502:         }
 4503:         if (@posstypes) {
 4504:             if (ref($usertypes) eq 'HASH') {
 4505:                 %usertypeshash = %{$usertypes};
 4506:             }
 4507:             my @overridden;
 4508:             my $numinrow = 4;
 4509:             if (ref($settings) eq 'HASH') {
 4510:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4511:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4512:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4513:                             push(@overridden,$key);
 4514:                             foreach my $item (@contacts) {
 4515:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4516:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4517:                                 }
 4518:                             }
 4519:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4520:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4521:                             $includeloc{'override_'.$key} = '';
 4522:                             $includestr{'override_'.$key} = '';
 4523:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4524:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 4525:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4526:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4527:                             }
 4528:                         }
 4529:                     }
 4530:                 }
 4531:             }
 4532:             my $customclass = 'LC_helpdesk_override';
 4533:             my $optionsprefix = 'LC_options_helpdesk_';
 4534: 
 4535:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4536:  
 4537:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4538:                                          $numinrow,$othertitle,'overrides',
 4539:                                          \$rownum,$onclicktypes,$customclass);
 4540:             $rownum ++;
 4541:             $usertypeshash{'default'} = $othertitle;
 4542:             foreach my $status (@posstypes) {
 4543:                 my $css_class;
 4544:                 if ($rownum%2) {
 4545:                     $css_class = 'LC_odd_row ';
 4546:                 }
 4547:                 $css_class .= $customclass;
 4548:                 my $rowid = $optionsprefix.$status;
 4549:                 my $hidden = 1;
 4550:                 my $currstyle = 'display:none';
 4551:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4552:                     $currstyle = 'display:table-row';
 4553:                     $hidden = 0;
 4554:                 }
 4555:                 my $key = 'override_'.$status;
 4556:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4557:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4558:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4559:                                                   \@contacts,$short_titles);
 4560:                 unless ($hidden) {
 4561:                     $rownum ++;
 4562:                 }
 4563:             }
 4564:         }
 4565:     }
 4566:     $$rowtotal += $rownum;
 4567:     return $datatable;
 4568: }
 4569: 
 4570: sub core_link_msu {
 4571:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4572:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4573: }
 4574: 
 4575: sub overridden_helpdesk {
 4576:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4577:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4578:     my $class = 'LC_left_item';
 4579:     if ($css_class) {
 4580:         $css_class = ' class="'.$css_class.'"';
 4581:     }
 4582:     if ($rowid) {
 4583:         $rowid = ' id="'.$rowid.'"';
 4584:     }
 4585:     if ($rowstyle) {
 4586:         $rowstyle = ' style="'.$rowstyle.'"';
 4587:     }
 4588:     my ($output,$description);
 4589:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4590:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4591:               "<td>$description</td>\n".
 4592:               '<td class="'.$class.'" colspan="2">'.
 4593:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4594:               '<span class="LC_nobreak">';
 4595:     if (ref($contacts) eq 'ARRAY') {
 4596:         foreach my $item (@{$contacts}) {
 4597:             my $check;
 4598:             if (ref($checked) eq 'HASH') {
 4599:                $check = $checked->{$item};
 4600:             }
 4601:             my $title;
 4602:             if (ref($short_titles) eq 'HASH') {
 4603:                 $title = $short_titles->{$item}; 
 4604:             }
 4605:             $output .= '<label>'.
 4606:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4607:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4608:         }
 4609:     }
 4610:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4611:                '<input type="text" name="override_'.$type.'_others" '.
 4612:                'value="'.$otheremails.'"  />';
 4613:     my %locchecked;
 4614:     foreach my $loc ('s','b') {
 4615:         if ($includeloc eq $loc) {
 4616:             $locchecked{$loc} = ' checked="checked"';
 4617:             last;
 4618:         }
 4619:     }
 4620:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4621:                '<input type="text" name="override_'.$type.'_bcc" '.
 4622:                'value="'.$bccemails.'"  /></fieldset>'.
 4623:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4624:                &mt('Text automatically added to e-mail:').' '.
 4625:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4626:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4627:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4628:                ('&nbsp;'x2).
 4629:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4630:                '</span></fieldset>'.
 4631:                '</td></tr>'."\n";
 4632:     return $output;
 4633: }
 4634: 
 4635: sub contacts_javascript {
 4636:     return <<"ENDSCRIPT";
 4637: 
 4638: <script type="text/javascript">
 4639: // <![CDATA[
 4640: 
 4641: function screenshotSize(field) {
 4642:     if (document.getElementById('help_screenshotsize')) {
 4643:         if (field.value == 'no') {
 4644:             document.getElementById('help_screenshotsize').style.display="none";
 4645:         } else {
 4646:             document.getElementById('help_screenshotsize').style.display="";
 4647:         }
 4648:     }
 4649:     return;
 4650: }
 4651: 
 4652: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4653:     if (form.elements[checkbox].length != undefined) {
 4654:         var count = 0;
 4655:         if (docount) {
 4656:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4657:                 if (form.elements[checkbox][i].checked) {
 4658:                     count ++;
 4659:                 }
 4660:             }
 4661:         }
 4662:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4663:             var type = form.elements[checkbox][i].value;
 4664:             if (document.getElementById(prefix+type)) {
 4665:                 if (form.elements[checkbox][i].checked) {
 4666:                     document.getElementById(prefix+type).style.display = 'table-row';
 4667:                     if (count % 2 == 1) {
 4668:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4669:                     } else {
 4670:                         document.getElementById(prefix+type).className = target;
 4671:                     }
 4672:                     count ++;
 4673:                 } else {
 4674:                     document.getElementById(prefix+type).style.display = 'none';
 4675:                 }
 4676:             }
 4677:         }
 4678:     }
 4679:     return;
 4680: }
 4681: 
 4682: 
 4683: // ]]>
 4684: </script>
 4685: 
 4686: ENDSCRIPT
 4687: }
 4688: 
 4689: sub print_helpsettings {
 4690:     my ($position,$dom,$settings,$rowtotal) = @_;
 4691:     my $confname = $dom.'-domainconfig';
 4692:     my $formname = 'display';
 4693:     my ($datatable,$itemcount);
 4694:     if ($position eq 'top') {
 4695:         $itemcount = 1;
 4696:         my (%choices,%defaultchecked,@toggles);
 4697:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4698:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4699:                                      &mt('LON-CAPA bug tracker'),600,500));
 4700:         %defaultchecked = ('submitbugs' => 'on');
 4701:         @toggles = ('submitbugs');
 4702:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4703:                                                      \%choices,$itemcount);
 4704:         $$rowtotal ++;
 4705:     } else {
 4706:         my $css_class;
 4707:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4708:         my (%customroles,%ordered,%current);
 4709:         if (ref($settings) eq 'HASH') {
 4710:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4711:                 %current = %{$settings->{'adhoc'}};
 4712:             }
 4713:         }
 4714:         my $count = 0;
 4715:         foreach my $key (sort(keys(%existing))) {
 4716:             if ($key=~/^rolesdef\_(\w+)$/) {
 4717:                 my $rolename = $1;
 4718:                 my (%privs,$order);
 4719:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4720:                 $customroles{$rolename} = \%privs;
 4721:                 if (ref($current{$rolename}) eq 'HASH') {
 4722:                     $order = $current{$rolename}{'order'};
 4723:                 }
 4724:                 if ($order eq '') {
 4725:                     $order = $count;
 4726:                 }
 4727:                 $ordered{$order} = $rolename;
 4728:                 $count++;
 4729:             }
 4730:         }
 4731:         my $maxnum = scalar(keys(%ordered));
 4732:         my @roles_by_num = ();
 4733:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4734:             push(@roles_by_num,$item);
 4735:         }
 4736:         my $context = 'domprefs';
 4737:         my $crstype = 'Course';
 4738:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4739:         my @accesstypes = ('all','dh','da','none');
 4740:         my ($numstatustypes,@jsarray);
 4741:         if (ref($types) eq 'ARRAY') {
 4742:             if (@{$types} > 0) {
 4743:                 $numstatustypes = scalar(@{$types});
 4744:                 push(@accesstypes,'status');
 4745:                 @jsarray = ('bystatus');
 4746:             }
 4747:         }
 4748:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4749:         if (keys(%domhelpdesk)) {
 4750:             push(@accesstypes,('inc','exc'));
 4751:             push(@jsarray,('notinc','notexc'));
 4752:         }
 4753:         my $hiddenstr = join("','",@jsarray);
 4754:         my $context = 'domprefs';
 4755:         my $crstype = 'Course';
 4756:         my $prefix = 'helproles_';
 4757:         my $add_class = 'LC_hidden';
 4758:         foreach my $num (@roles_by_num) {
 4759:             my $role = $ordered{$num};
 4760:             my ($desc,$access,@statuses);
 4761:             if (ref($current{$role}) eq 'HASH') {
 4762:                 $desc = $current{$role}{'desc'};
 4763:                 $access = $current{$role}{'access'};
 4764:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4765:                     @statuses = @{$current{$role}{'insttypes'}};
 4766:                 }
 4767:             }
 4768:             if ($desc eq '') {
 4769:                 $desc = $role;
 4770:             }
 4771:             my $identifier = 'custhelp'.$num;
 4772:             my %full=();
 4773:             my %levels= (
 4774:                          course => {},
 4775:                          domain => {},
 4776:                          system => {},
 4777:                         );
 4778:             my %levelscurrent=(
 4779:                                course => {},
 4780:                                domain => {},
 4781:                                system => {},
 4782:                               );
 4783:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4784:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4785:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4786:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4787:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4788:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4789:             for (my $k=0; $k<=$maxnum; $k++) {
 4790:                 my $vpos = $k+1;
 4791:                 my $selstr;
 4792:                 if ($k == $num) {
 4793:                     $selstr = ' selected="selected" ';
 4794:                 }
 4795:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4796:             }
 4797:             $datatable .= '</select>'.('&nbsp;'x2).
 4798:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4799:                           '</td>'.
 4800:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4801:                           &mt('Name shown to users:').
 4802:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4803:                           '</fieldset>'.
 4804:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4805:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4806:                           '<fieldset>'.
 4807:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4808:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4809:                                                                    \%levelscurrent,$identifier,
 4810:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4811:                           '</fieldset></td>';
 4812:             $itemcount ++;
 4813:         }
 4814:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4815:         my $newcust = 'custhelp'.$count;
 4816:         my (%privs,%levelscurrent);
 4817:         my %full=();
 4818:         my %levels= (
 4819:                      course => {},
 4820:                      domain => {},
 4821:                      system => {},
 4822:                     );
 4823:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4824:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4825:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4826:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4827:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4828:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4829:         for (my $k=0; $k<$maxnum+1; $k++) {
 4830:             my $vpos = $k+1;
 4831:             my $selstr;
 4832:             if ($k == $maxnum) {
 4833:                 $selstr = ' selected="selected" ';
 4834:             }
 4835:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4836:         }
 4837:         $datatable .= '</select>&nbsp;'."\n".
 4838:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4839:                       '</label></span></td>'.
 4840:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4841:                       '<span class="LC_nobreak">'.
 4842:                       &mt('Internal name:').
 4843:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4844:                       '</span>'.('&nbsp;'x4).
 4845:                       '<span class="LC_nobreak">'.
 4846:                       &mt('Name shown to users:').
 4847:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4848:                       '</span></fieldset>'.
 4849:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4850:                                              $usertypes,$types,\%domhelpdesk).
 4851:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4852:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4853:                                                                 \@templateroles,$newcust).
 4854:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4855:                                                                \%levelscurrent,$newcust).
 4856:                       '</fieldset>'.
 4857:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4858:                       '</td></tr>';
 4859:         $count ++;
 4860:         $$rowtotal += $count;
 4861:     }
 4862:     return $datatable;
 4863: }
 4864: 
 4865: sub adhocbutton {
 4866:     my ($prefix,$num,$field,$visibility) = @_;
 4867:     my %lt = &Apache::lonlocal::texthash(
 4868:                                           show => 'Show details',
 4869:                                           hide => 'Hide details',
 4870:                                         );
 4871:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4872:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4873:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4874:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4875: }
 4876: 
 4877: sub helpsettings_javascript {
 4878:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4879:     return unless(ref($roles_by_num) eq 'ARRAY');
 4880:     my %html_js_lt = &Apache::lonlocal::texthash(
 4881:                                           show => 'Show details',
 4882:                                           hide => 'Hide details',
 4883:                                         );
 4884:     &html_escape(\%html_js_lt);
 4885:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4886:     return <<"ENDSCRIPT";
 4887: <script type="text/javascript">
 4888: // <![CDATA[
 4889: 
 4890: function reorderHelpRoles(form,item) {
 4891:     var changedVal;
 4892: $jstext
 4893:     var newpos = 'helproles_${total}_pos';
 4894:     var maxh = 1 + $total;
 4895:     var current = new Array();
 4896:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4897:     if (item == newpos) {
 4898:         changedVal = newitemVal;
 4899:     } else {
 4900:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4901:         current[newitemVal] = newpos;
 4902:     }
 4903:     for (var i=0; i<helproles.length; i++) {
 4904:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4905:         if (elementName != item) {
 4906:             if (form.elements[elementName]) {
 4907:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4908:                 current[currVal] = elementName;
 4909:             }
 4910:         }
 4911:     }
 4912:     var oldVal;
 4913:     for (var j=0; j<maxh; j++) {
 4914:         if (current[j] == undefined) {
 4915:             oldVal = j;
 4916:         }
 4917:     }
 4918:     if (oldVal < changedVal) {
 4919:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4920:            var elementName = current[k];
 4921:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4922:         }
 4923:     } else {
 4924:         for (var k=changedVal; k<oldVal; k++) {
 4925:             var elementName = current[k];
 4926:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4927:         }
 4928:     }
 4929:     return;
 4930: }
 4931: 
 4932: function helpdeskAccess(num) {
 4933:     var curraccess = null;
 4934:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4935:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4936:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4937:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4938:             }
 4939:         }
 4940:     }
 4941:     var shown = Array();
 4942:     var hidden = Array();
 4943:     if (curraccess == 'none') {
 4944:         hidden = Array('$hiddenstr');
 4945:     } else {
 4946:         if (curraccess == 'status') {
 4947:             shown = Array('bystatus');
 4948:             hidden = Array('notinc','notexc');
 4949:         } else {
 4950:             if (curraccess == 'exc') {
 4951:                 shown = Array('notexc');
 4952:                 hidden = Array('notinc','bystatus');
 4953:             }
 4954:             if (curraccess == 'inc') {
 4955:                 shown = Array('notinc');
 4956:                 hidden = Array('notexc','bystatus');
 4957:             }
 4958:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4959:                 hidden = Array('notinc','notexc','bystatus');
 4960:             }
 4961:         }
 4962:     }
 4963:     if (hidden.length > 0) {
 4964:         for (var i=0; i<hidden.length; i++) {
 4965:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4966:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4967:             }
 4968:         }
 4969:     }
 4970:     if (shown.length > 0) {
 4971:         for (var i=0; i<shown.length; i++) {
 4972:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4973:                 if (shown[i] == 'privs') {
 4974:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4975:                 } else {
 4976:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4977:                 }
 4978:             }
 4979:         }
 4980:     }
 4981:     return;
 4982: }
 4983: 
 4984: function toggleHelpdeskItem(num,field) {
 4985:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4986:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4987:             document.getElementById('helproles_'+num+'_'+field).className =
 4988:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4989:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4990:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4991:             }
 4992:         } else {
 4993:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4994:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4995:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4996:             }
 4997:         }
 4998:     }
 4999:     return;
 5000: }
 5001: 
 5002: // ]]>
 5003: </script>
 5004: 
 5005: ENDSCRIPT
 5006: }
 5007: 
 5008: sub helpdeskroles_access {
 5009:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5010:         $usertypes,$types,$domhelpdesk) = @_;
 5011:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5012:     my %lt = &Apache::lonlocal::texthash(
 5013:                     'rou'    => 'Role usage',
 5014:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5015:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5016:                     'dh'     => 'All with domain helpdesk role',
 5017:                     'da'     => 'All with domain helpdesk assistant role',
 5018:                     'none'   => 'None',
 5019:                     'status' => 'Determined based on institutional status',
 5020:                     'inc'    => 'Include all, but exclude specific personnel',
 5021:                     'exc'    => 'Exclude all, but include specific personnel',
 5022:                   );
 5023:     my %usecheck = (
 5024:                      all => ' checked="checked"',
 5025:                    );
 5026:     my %displaydiv = (
 5027:                       status => 'none',
 5028:                       inc    => 'none',
 5029:                       exc    => 'none',
 5030:                       priv   => 'block',
 5031:                      );
 5032:     my $output;
 5033:     if (ref($current) eq 'HASH') {
 5034:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5035:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5036:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5037:                 delete($usecheck{'all'});
 5038:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5039:                     my $access = $1;
 5040:                     $displaydiv{$access} = 'inline';
 5041:                 } elsif ($current->{access} eq 'none') {
 5042:                     $displaydiv{'priv'} = 'none';
 5043:                 }
 5044:             }
 5045:         }
 5046:     }
 5047:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5048:               '<p>'.$lt{'whi'}.'</p>';
 5049:     foreach my $access (@{$accesstypes}) {
 5050:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5051:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5052:                    $lt{$access}.'</label>';
 5053:         if ($access eq 'status') {
 5054:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5055:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5056:                                                                  $othertitle,$usertypes,$types).
 5057:                        '</div>';
 5058:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5059:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5060:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5061:                        '</div>';
 5062:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5063:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5064:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5065:                        '</div>';
 5066:         }
 5067:         $output .= '</p>';
 5068:     }
 5069:     $output .= '</fieldset>';
 5070:     return $output;
 5071: }
 5072: 
 5073: sub radiobutton_prefs {
 5074:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5075:         $additional,$align,$firstval) = @_;
 5076:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5077:                    (ref($choices) eq 'HASH'));
 5078: 
 5079:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5080: 
 5081:     foreach my $item (@{$toggles}) {
 5082:         if ($defaultchecked->{$item} eq 'on') {
 5083:             $checkedon{$item} = ' checked="checked" ';
 5084:             $checkedoff{$item} = ' ';
 5085:         } elsif ($defaultchecked->{$item} eq 'off') {
 5086:             $checkedoff{$item} = ' checked="checked" ';
 5087:             $checkedon{$item} = ' ';
 5088:         }
 5089:     }
 5090:     if (ref($settings) eq 'HASH') {
 5091:         foreach my $item (@{$toggles}) {
 5092:             if ($settings->{$item} eq '1') {
 5093:                 $checkedon{$item} =  ' checked="checked" ';
 5094:                 $checkedoff{$item} = ' ';
 5095:             } elsif ($settings->{$item} eq '0') {
 5096:                 $checkedoff{$item} =  ' checked="checked" ';
 5097:                 $checkedon{$item} = ' ';
 5098:             }
 5099:         }
 5100:     }
 5101:     if ($onclick) {
 5102:         $onclick = ' onclick="'.$onclick.'"';
 5103:     }
 5104:     foreach my $item (@{$toggles}) {
 5105:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5106:         $datatable .=
 5107:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5108:             '<span class="LC_nobreak">'.$choices->{$item}.
 5109:             '</span></td>';
 5110:         if ($align eq 'left') {
 5111:             $datatable .= '<td class="LC_left_item">';
 5112:         } else {
 5113:             $datatable .= '<td class="LC_right_item">';
 5114:         }
 5115:         $datatable .= '<span class="LC_nobreak">';
 5116:         if ($firstval eq 'no') {
 5117:             $datatable .=
 5118:                 '<label><input type="radio" name="'.
 5119:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5120:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5121:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5122:         } else {
 5123:             $datatable .=
 5124:                 '<label><input type="radio" name="'.
 5125:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5126:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5127:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5128:         }
 5129:         $datatable .= '</span>'.$additional.'</td></tr>';
 5130:         $itemcount ++;
 5131:     }
 5132:     return ($datatable,$itemcount);
 5133: }
 5134: 
 5135: sub print_ltitools {
 5136:     my ($dom,$settings,$rowtotal) = @_;
 5137:     my $rownum = 0;
 5138:     my $css_class;
 5139:     my $itemcount = 1;
 5140:     my $maxnum = 0;
 5141:     my %ordered;
 5142:     if (ref($settings) eq 'HASH') {
 5143:         foreach my $item (keys(%{$settings})) {
 5144:             if (ref($settings->{$item}) eq 'HASH') {
 5145:                 my $num = $settings->{$item}{'order'};
 5146:                 $ordered{$num} = $item;
 5147:             }
 5148:         }
 5149:     }
 5150:     my $confname = $dom.'-domainconfig';
 5151:     my $switchserver = &check_switchserver($dom,$confname);
 5152:     my $maxnum = scalar(keys(%ordered));
 5153:     my $datatable;
 5154:     my %lt = &ltitools_names();
 5155:     my @courseroles = ('cc','in','ta','ep','st');
 5156:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 5157:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 5158:     if (keys(%ordered)) {
 5159:         my @items = sort { $a <=> $b } keys(%ordered);
 5160:         for (my $i=0; $i<@items; $i++) {
 5161:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5162:             my $item = $ordered{$items[$i]};
 5163:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 5164:             if (ref($settings->{$item}) eq 'HASH') {
 5165:                 $title = $settings->{$item}->{'title'};
 5166:                 $url = $settings->{$item}->{'url'};
 5167:                 $key = $settings->{$item}->{'key'};
 5168:                 $secret = $settings->{$item}->{'secret'};
 5169:                 $lifetime = $settings->{$item}->{'lifetime'};
 5170:                 my $image = $settings->{$item}->{'image'};
 5171:                 if ($image ne '') {
 5172:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 5173:                 }
 5174:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 5175:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 5176:                 } else {
 5177:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 5178:                 }
 5179:             }
 5180:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 5181:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5182:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 5183:             for (my $k=0; $k<=$maxnum; $k++) {
 5184:                 my $vpos = $k+1;
 5185:                 my $selstr;
 5186:                 if ($k == $i) {
 5187:                     $selstr = ' selected="selected" ';
 5188:                 }
 5189:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5190:             }
 5191:             $datatable .= '</select>'.('&nbsp;'x2).
 5192:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 5193:                 &mt('Delete?').'</label></span></td>'.
 5194:                 '<td colspan="2">'.
 5195:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5196:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 5197:                 ('&nbsp;'x2).
 5198:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 5199:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5200:                 ('&nbsp;'x2).
 5201:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 5202:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 5203:                 ('&nbsp;'x2).
 5204:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 5205:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 5206:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 5207:                 '<br /><br />'.
 5208:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 5209:                 ' value="'.$url.'" /></span>'.
 5210:                 ('&nbsp;'x2).
 5211:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 5212:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 5213:                 ('&nbsp;'x2).
 5214:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 5215:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 5216:                 ('&nbsp;'x2).
 5217:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5218:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 5219:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_secret_'.$i.'.type='."'text'".' } else { this.form.ltitools_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 5220:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 5221:                 '</fieldset>'.
 5222:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5223:                 '<span class="LC_nobreak">'.&mt('Display target:');
 5224:             my %currdisp;
 5225:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 5226:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 5227:                     $currdisp{'window'} = ' checked="checked"';
 5228:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 5229:                     $currdisp{'tab'} = ' checked="checked"';
 5230:                 } else {
 5231:                     $currdisp{'iframe'} = ' checked="checked"';
 5232:                 }
 5233:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 5234:                     $currdisp{'width'} = $1;
 5235:                 }
 5236:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 5237:                      $currdisp{'height'} = $1;
 5238:                 }
 5239:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 5240:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 5241:             } else {
 5242:                 $currdisp{'iframe'} = ' checked="checked"';
 5243:             }
 5244:             foreach my $disp ('iframe','tab','window') {
 5245:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 5246:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 5247:             }
 5248:             $datatable .= ('&nbsp;'x4);
 5249:             foreach my $dimen ('width','height') {
 5250:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5251:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 5252:                               ('&nbsp;'x2);
 5253:             }
 5254:             $datatable .= '</span><br />'.
 5255:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5256:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 5257:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5258:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 5259:                           '</textarea></div><div style=""></div><br />';
 5260:             my %units = (
 5261:                           'passback' => 'days',
 5262:                           'roster'   => 'seconds',
 5263:                         );
 5264:             foreach my $extra ('passback','roster') {
 5265:                 my $validsty = 'none';
 5266:                 my $currvalid;
 5267:                 my $checkedon = '';
 5268:                 my $checkedoff = ' checked="checked"';
 5269:                 if ($settings->{$item}->{$extra}) {
 5270:                     $checkedon = $checkedoff;
 5271:                     $checkedoff = '';
 5272:                     $validsty = 'inline-block';
 5273:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 5274:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 5275:                     }
 5276:                 }
 5277:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 5278:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5279:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 5280:                               &mt('No').'</label>'.('&nbsp;'x2).
 5281:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 5282:                               &mt('Yes').'</label></span></div>'.
 5283:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 5284:                               '<span class="LC_nobreak">'.
 5285:                               &mt("at least [_1] $units{$extra} after launch",
 5286:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 5287:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5288:             }
 5289:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5290:             if ($imgsrc) {
 5291:                 $datatable .= $imgsrc.
 5292:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 5293:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 5294:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 5295:             } else {
 5296:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5297:             }
 5298:             if ($switchserver) {
 5299:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5300:             } else {
 5301:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 5302:             }
 5303:             $datatable .= '</span></fieldset>';
 5304:             my (%checkedfields,%rolemaps,$userincdom);
 5305:             if (ref($settings->{$item}) eq 'HASH') {
 5306:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 5307:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 5308:                 }
 5309:                 $userincdom = $settings->{$item}->{'incdom'};
 5310:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 5311:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 5312:                     $checkedfields{'roles'} = 1;
 5313:                 }
 5314:             }
 5315:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5316:                           '<span class="LC_nobreak">';
 5317:             my $userfieldstyle = 'display:none;';
 5318:             my $seluserdom = '';
 5319:             my $unseluserdom = ' selected="selected"';
 5320:             foreach my $field (@fields) {
 5321:                 my ($checked,$onclick,$id,$spacer);
 5322:                 if ($checkedfields{$field}) {
 5323:                     $checked = ' checked="checked"';
 5324:                 }
 5325:                 if ($field eq 'user') {
 5326:                     $id = ' id="ltitools_user_field_'.$i.'"';
 5327:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 5328:                     if ($checked) {
 5329:                         $userfieldstyle = 'display:inline-block';
 5330:                         if ($userincdom) {
 5331:                             $seluserdom = $unseluserdom;
 5332:                             $unseluserdom = '';
 5333:                         }
 5334:                     }
 5335:                 } else {
 5336:                     $spacer = ('&nbsp;' x2);
 5337:                 }
 5338:                 $datatable .= '<label>'.
 5339:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 5340:                               $lt{$field}.'</label>'.$spacer;
 5341:             }
 5342:             $datatable .= '</span>';
 5343:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 5344:                           '<span class="LC_nobreak"> : '.
 5345:                           '<select name="ltitools_userincdom_'.$i.'">'.
 5346:                           '<option value="">'.&mt('Select').'</option>'.
 5347:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 5348:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 5349:                           '</select></span></div>';
 5350:             $datatable .= '</fieldset>'.
 5351:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5352:             foreach my $role (@courseroles) {
 5353:                 my ($selected,$selectnone);
 5354:                 if (!$rolemaps{$role}) {
 5355:                     $selectnone = ' selected="selected"';
 5356:                 }
 5357:                 $datatable .= '<td style="text-align: center">'. 
 5358:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5359:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 5360:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5361:                 foreach my $ltirole (@ltiroles) {
 5362:                     unless ($selectnone) {
 5363:                         if ($rolemaps{$role} eq $ltirole) {
 5364:                             $selected = ' selected="selected"';
 5365:                         } else {
 5366:                             $selected = '';
 5367:                         }
 5368:                     }
 5369:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5370:                 }
 5371:                 $datatable .= '</select></td>';
 5372:             }
 5373:             $datatable .= '</tr></table></fieldset>';
 5374:             my %courseconfig;
 5375:             if (ref($settings->{$item}) eq 'HASH') {
 5376:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 5377:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 5378:                 }
 5379:             }
 5380:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5381:             foreach my $item ('label','title','target','linktext','explanation','append') {
 5382:                 my $checked;
 5383:                 if ($courseconfig{$item}) {
 5384:                     $checked = ' checked="checked"';
 5385:                 }
 5386:                 $datatable .= '<label>'.
 5387:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 5388:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 5389:             }
 5390:             $datatable .= '</span></fieldset>'.
 5391:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5392:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 5393:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 5394:                 my %custom = %{$settings->{$item}->{'custom'}};
 5395:                 if (keys(%custom) > 0) {
 5396:                     foreach my $key (sort(keys(%custom))) {
 5397:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 5398:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 5399:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5400:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 5401:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 5402:                     }
 5403:                 }
 5404:             }
 5405:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 5406:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 5407:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 5408:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 5409:             $datatable .= '</table></fieldset></td></tr>'."\n";
 5410:             $itemcount ++;
 5411:         }
 5412:     }
 5413:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5414:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 5415:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5416:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 5417:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 5418:     for (my $k=0; $k<$maxnum+1; $k++) {
 5419:         my $vpos = $k+1;
 5420:         my $selstr;
 5421:         if ($k == $maxnum) {
 5422:             $selstr = ' selected="selected" ';
 5423:         }
 5424:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5425:     }
 5426:     $datatable .= '</select>&nbsp;'."\n".
 5427:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5428:                   '<td colspan="2">'.
 5429:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5430:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 5431:                   ('&nbsp;'x2).
 5432:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 5433:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5434:                   ('&nbsp;'x2).
 5435:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 5436:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 5437:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 5438:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5439:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5440:                   '<br />'.
 5441:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 5442:                   ('&nbsp;'x2).
 5443:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 5444:                   ('&nbsp;'x2).
 5445:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 5446:                   ('&nbsp;'x2).
 5447:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 5448:                   '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_add_secret.type='."'text'".' } else { this.form.ltitools_add_secret.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n".
 5449:                   '</fieldset>'.
 5450:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5451:                   '<span class="LC_nobreak">'.&mt('Display target:');
 5452:     my %defaultdisp;
 5453:     $defaultdisp{'iframe'} = ' checked="checked"';
 5454:     foreach my $disp ('iframe','tab','window') {
 5455:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 5456:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 5457:     }
 5458:     $datatable .= ('&nbsp;'x4);
 5459:     foreach my $dimen ('width','height') {
 5460:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5461:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 5462:                       ('&nbsp;'x2);
 5463:     }
 5464:     $datatable .= '</span><br />'.
 5465:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5466:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 5467:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5468:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 5469:                   '</div><div style=""></div><br />';
 5470:     my %units = (
 5471:                   'passback' => 'days',
 5472:                   'roster'   => 'seconds',
 5473:                 );
 5474:     my %defaulttimes = (
 5475:                      'passback' => '7',
 5476:                      'roster'   => '300',
 5477:                    );
 5478:     foreach my $extra ('passback','roster') {
 5479:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 5480:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5481:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 5482:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 5483:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 5484:                       &mt('Yes').'</label></span></div>'.
 5485:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 5486:                       '<span class="LC_nobreak">'.
 5487:                       &mt("at least [_1] $units{$extra} after launch",
 5488:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 5489:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5490:     }
 5491:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 5492:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5493:     if ($switchserver) {
 5494:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5495:     } else {
 5496:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 5497:     }
 5498:     $datatable .= '</span></fieldset>'.
 5499:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5500:                   '<span class="LC_nobreak">';
 5501:     foreach my $field (@fields) {
 5502:         my ($id,$onclick,$spacer);
 5503:         if ($field eq 'user') {
 5504:             $id = ' id="ltitools_user_field_add"';
 5505:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 5506:         } else {
 5507:             $spacer = ('&nbsp;' x2);
 5508:         }
 5509:         $datatable .= '<label>'.
 5510:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 5511:                       $lt{$field}.'</label>'.$spacer;
 5512:     }
 5513:     $datatable .= '</span>'.
 5514:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 5515:                   '<span class="LC_nobreak"> : '.
 5516:                   '<select name="ltitools_userincdom_add">'.
 5517:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 5518:                   '<option value="0">'.&mt('username').'</option>'.
 5519:                   '<option value="1">'.&mt('username:domain').'</option>'.
 5520:                   '</select></span></div></fieldset>';
 5521:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5522:     foreach my $role (@courseroles) {
 5523:         my ($checked,$checkednone);
 5524:         $datatable .= '<td style="text-align: center">'.
 5525:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5526:                       '<select name="ltitools_add_roles_'.$role.'">'.
 5527:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 5528:         foreach my $ltirole (@ltiroles) {
 5529:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 5530:         }
 5531:         $datatable .= '</select></td>';
 5532:     }
 5533:     $datatable .= '</tr></table></fieldset>'.
 5534:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5535:     foreach my $item ('label','title','target','linktext','explanation','append') {
 5536:         $datatable .= '<label>'.
 5537:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 5538:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 5539:     }
 5540:     $datatable .= '</span></fieldset>'.
 5541:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5542:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5543:                   '<tr><td><span class="LC_nobreak">'.
 5544:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 5545:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 5546:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 5547:                   '</table></fieldset>'."\n".
 5548:                   '</td>'."\n".
 5549:                   '</tr>'."\n";
 5550:     $itemcount ++;
 5551:     return $datatable;
 5552: }
 5553: 
 5554: sub ltitools_names {
 5555:     my %lt = &Apache::lonlocal::texthash(
 5556:                                           'title'          => 'Title',
 5557:                                           'version'        => 'Version',
 5558:                                           'msgtype'        => 'Message Type',
 5559:                                           'sigmethod'      => 'Signature Method',
 5560:                                           'url'            => 'URL',
 5561:                                           'key'            => 'Key',
 5562:                                           'lifetime'       => 'Nonce lifetime (s)',
 5563:                                           'secret'         => 'Secret',
 5564:                                           'icon'           => 'Icon',   
 5565:                                           'user'           => 'User',
 5566:                                           'fullname'       => 'Full Name',
 5567:                                           'firstname'      => 'First Name',
 5568:                                           'lastname'       => 'Last Name',
 5569:                                           'email'          => 'E-mail',
 5570:                                           'roles'          => 'Role',
 5571:                                           'window'         => 'Window',
 5572:                                           'tab'            => 'Tab',
 5573:                                           'iframe'         => 'iFrame',
 5574:                                           'height'         => 'Height',
 5575:                                           'width'          => 'Width',
 5576:                                           'linktext'       => 'Default Link Text',
 5577:                                           'explanation'    => 'Default Explanation',
 5578:                                           'passback'       => 'Tool can return grades:',
 5579:                                           'roster'         => 'Tool can retrieve roster:',
 5580:                                           'crstarget'      => 'Display target',
 5581:                                           'crslabel'       => 'Course label',
 5582:                                           'crstitle'       => 'Course title', 
 5583:                                           'crslinktext'    => 'Link Text',
 5584:                                           'crsexplanation' => 'Explanation',
 5585:                                           'crsappend'      => 'Provider URL',
 5586:                                         );
 5587:     return %lt;
 5588: }
 5589: 
 5590: sub print_proctoring {
 5591:     my ($dom,$settings,$rowtotal) = @_;
 5592:     my $itemcount = 1;
 5593:     my (%ordered,%providernames,%current,%currentdef);
 5594:     my $confname = $dom.'-domainconfig';
 5595:     my $switchserver = &check_switchserver($dom,$confname);
 5596:     if (ref($settings) eq 'HASH') {
 5597:         foreach my $item (keys(%{$settings})) {
 5598:             if (ref($settings->{$item}) eq 'HASH') {
 5599:                 my $num = $settings->{$item}{'order'};
 5600:                 $ordered{$num} = $item;
 5601:             }
 5602:         }
 5603:     } else {
 5604:         %ordered = (
 5605:                      1 => 'proctorio',
 5606:                      2 => 'examity',
 5607:                    );
 5608:     }
 5609:     %providernames = &proctoring_providernames();
 5610:     my $maxnum = scalar(keys(%ordered));
 5611:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5612:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5613:     if (ref($requref) eq 'HASH') {
 5614:         %requserfields = %{$requref};
 5615:     }
 5616:     if (ref($opturef) eq 'HASH') {
 5617:         %optuserfields = %{$opturef};
 5618:     }
 5619:     if (ref($defref) eq 'HASH') {
 5620:         %defaults = %{$defref};
 5621:     }
 5622:     if (ref($extref) eq 'HASH') {
 5623:         %extended = %{$extref};
 5624:     }
 5625:     if (ref($crsref) eq 'HASH') {
 5626:         %crsconf = %{$crsref};
 5627:     }
 5628:     if (ref($rolesref) eq 'ARRAY') {
 5629:         @courseroles = @{$rolesref};
 5630:     }
 5631:     if (ref($ltiref) eq 'ARRAY') {
 5632:         @ltiroles = @{$ltiref};
 5633:     }
 5634:     my $datatable;
 5635:     my $css_class;
 5636:     if (keys(%ordered)) {
 5637:         my @items = sort { $a <=> $b } keys(%ordered);
 5638:         for (my $i=0; $i<@items; $i++) {
 5639:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5640:             my $provider = $ordered{$items[$i]};
 5641:             my $optionsty = 'none';
 5642:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5643:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5644:             if (ref($settings) eq 'HASH') {
 5645:                 if (ref($settings->{$provider}) eq 'HASH') {
 5646:                     %current = %{$settings->{$provider}};
 5647:                     if ($current{'available'}) {
 5648:                         $optionsty = 'block';
 5649:                         $available = 1;
 5650:                     }
 5651:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5652:                         $lifetime = $current{'lifetime'};
 5653:                     }
 5654:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5655:                         $version = $current{'version'};
 5656:                     }
 5657:                     if ($current{'image'} ne '') {
 5658:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5659:                     }
 5660:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5661:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5662:                     }
 5663:                     $userincdom = $current{'incdom'};
 5664:                     if (ref($current{'roles'}) eq 'HASH') {
 5665:                         %rolemaps = %{$current{'roles'}};
 5666:                         $checkedfields{'roles'} = 1;
 5667:                     }
 5668:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5669:                         foreach my $val (@{$current{'defaults'}}) {
 5670:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5671:                                 $inuse{$val} = 1;
 5672:                             } else {
 5673:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5674:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5675:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5676:                                             $inuse{$poss} = $val;
 5677:                                             last;
 5678:                                         }
 5679:                                     }
 5680:                                 }
 5681:                             }
 5682:                         }
 5683:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5684:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5685:                             my $currval = $current{'defaults'}{$key}; 
 5686:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5687:                                 $inuse{$key} = 1;
 5688:                             } else {
 5689:                                 my $match;
 5690:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5691:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5692:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5693:                                             $inuse{$poss} = $key;
 5694:                                             last;
 5695:                                         }
 5696:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5697:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5698:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5699:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5700:                                                     $currentdef{$inner} = $currval;
 5701:                                                     $match = 1;
 5702:                                                     last;
 5703:                                                 }
 5704:                                             } elsif ($inner eq $key) {
 5705:                                                 $currentdef{$key} = $currval;
 5706:                                                 $match = 1;
 5707:                                                 last;
 5708:                                             }
 5709:                                         }
 5710:                                     }
 5711:                                     last if ($match);
 5712:                                 }
 5713:                             }
 5714:                         }
 5715:                     }
 5716:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5717:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5718:                     }
 5719:                 }
 5720:             }
 5721:             my %lt = &proctoring_titles($provider);
 5722:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5723:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5724:             my %checkedavailable = (
 5725:                                    yes => '',
 5726:                                    no  => ' checked="checked"',
 5727:                                  );
 5728:             if ($available) {
 5729:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5730:                 $checkedavailable{'no'} = '';
 5731:             }
 5732:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5733:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5734:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5735:             for (my $k=0; $k<$maxnum; $k++) {
 5736:                 my $vpos = $k+1;
 5737:                 my $selstr;
 5738:                 if ($k == $i) {
 5739:                     $selstr = ' selected="selected" ';
 5740:                 }
 5741:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5742:             }
 5743:             if ($version eq '') {
 5744:                 if ($provider eq 'proctorio') {
 5745:                     $version = '1.0';
 5746:                 } elsif ($provider eq 'examity') {
 5747:                     $version = '1.1';
 5748:                 }
 5749:             }
 5750:             if ($lifetime eq '') {
 5751:                 $lifetime = '300';
 5752:             }
 5753:             $datatable .=
 5754:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5755:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5756:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5757:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5758:                 '</td>'.
 5759:                 '<td colspan="2">'.
 5760:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5761:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5762:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5763:                 ('&nbsp;'x2).
 5764:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5765:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5766:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5767:                 ('&nbsp;'x2).
 5768:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5769:                 '<br />'.
 5770:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5771:                 '<br />'.
 5772:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5773:                 ('&nbsp;'x2).
 5774:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5775:                 '<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";
 5776:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5777:             if ($imgsrc) {
 5778:                 $datatable .= $imgsrc.
 5779:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5780:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5781:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5782:             }
 5783:             $datatable .= '&nbsp;';
 5784:             if ($switchserver) {
 5785:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5786:             } else {
 5787:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5788:             }
 5789:             unless ($imgsrc) {
 5790:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5791:             }
 5792:             $datatable .= '</fieldset>'."\n";
 5793:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5794:                 if (@{$requserfields{$provider}} > 0) {
 5795:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5796:                     foreach my $field (@{$requserfields{$provider}}) {
 5797:                         $datatable .= '<span class="LC_nobreak">'.
 5798:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5799:                                       $lt{$field}.'</label>';
 5800:                         if ($field eq 'user') {
 5801:                             my $seluserdom = '';
 5802:                             my $unseluserdom = ' selected="selected"';
 5803:                             if ($userincdom) {
 5804:                                 $seluserdom = $unseluserdom;
 5805:                                 $unseluserdom = '';
 5806:                             }
 5807:                             $datatable .= ':&nbsp;'.
 5808:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5809:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5810:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5811:                                 '</select>&nbsp;';
 5812:                         } else {
 5813:                             $datatable .= '&nbsp;';
 5814:                             if ($field eq 'roles') {
 5815:                                 $showroles = 1; 
 5816:                             } 
 5817:                         }
 5818:                         $datatable .= '</span> ';
 5819:                     }
 5820:                 }
 5821:                 $datatable .= '</fieldset>'."\n";
 5822:             }
 5823:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5824:                 if (@{$optuserfields{$provider}} > 0) {
 5825:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5826:                     foreach my $field (@{$optuserfields{$provider}}) {
 5827:                         my $checked;
 5828:                         if ($checkedfields{$field}) {
 5829:                             $checked = ' checked="checked"';
 5830:                         }
 5831:                         $datatable .= '<span class="LC_nobreak">'.
 5832:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5833:                     }
 5834:                     $datatable .= '</fieldset>'."\n";
 5835:                 }
 5836:             }
 5837:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5838:                 if (@{$defaults{$provider}}) {
 5839:                     my (%options,@selectboxes);
 5840:                     if (ref($extended{$provider}) eq 'HASH') {
 5841:                         %options = %{$extended{$provider}};
 5842:                     }
 5843:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5844:                     my ($rem,$numinrow,$dropdowns);
 5845:                     if ($provider eq 'proctorio') { 
 5846:                         $datatable .= '<table>';
 5847:                         $numinrow = 4;
 5848:                     }
 5849:                     my $i = 0;
 5850:                     foreach my $field (@{$defaults{$provider}}) {
 5851:                         my $checked;
 5852:                         if ($inuse{$field}) {
 5853:                             $checked = ' checked="checked"';
 5854:                         }
 5855:                         if ($provider eq 'examity') {
 5856:                             if ($field eq 'display') {
 5857:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5858:                                 foreach my $option ('iframe','tab','window') {
 5859:                                     my $checkdisp;
 5860:                                     if ($currentdef{'target'} eq $option) {
 5861:                                         $checkdisp = ' checked="checked"';
 5862:                                     }
 5863:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5864:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5865:                                 }
 5866:                                 $datatable .= ('&nbsp;'x4);
 5867:                                 foreach my $dimen ('width','height') {
 5868:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5869:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5870:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5871:                                                   ('&nbsp;'x2);
 5872:                                 }
 5873:                                 $datatable .= '</span><br />'.
 5874:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5875:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5876:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5877:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5878:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5879:                                               $currentdef{'explanation'}.
 5880:                                               '</textarea></div><div style=""></div><br />';
 5881:                             }
 5882:                         } else {
 5883:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5884:                                 my ($output,$selnone);
 5885:                                 unless ($checked) {
 5886:                                     $selnone = ' selected="selected"';
 5887:                                 }
 5888:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5889:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5890:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5891:                                 foreach my $option (@{$options{$field}}) {
 5892:                                     my $sel; 
 5893:                                     if ($inuse{$field} eq $option) {
 5894:                                         $sel = ' selected="selected"';
 5895:                                     }
 5896:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5897:                                 }
 5898:                                 $output .= '</select></span>';
 5899:                                 push(@selectboxes,$output);
 5900:                             } else {
 5901:                                 $rem = $i%($numinrow);
 5902:                                 if ($rem == 0) {
 5903:                                     if ($i > 0) {
 5904:                                         $datatable .= '</tr>';
 5905:                                     }
 5906:                                     $datatable .= '<tr>';
 5907:                                 }
 5908:                                 $datatable .= '<td class="LC_left_item">'.
 5909:                                               '<span class="LC_nobreak">'.
 5910:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5911:                                               $fieldtitles{$field}.'</label></span></td>';
 5912:                                 $i++;
 5913:                             }
 5914:                         }
 5915:                     }
 5916:                     if ($provider eq 'proctorio') {
 5917:                         if ($numinrow) {
 5918:                             $rem = $i%$numinrow;
 5919:                         }
 5920:                         my $colsleft = $numinrow - $rem;
 5921:                         if ($colsleft > 1) {
 5922:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5923:                         } else {
 5924:                             $datatable .= '<td class="LC_left_item">';
 5925:                         }
 5926:                         $datatable .= '&nbsp;'.
 5927:                                       '</td></tr></table>';
 5928:                         if (@selectboxes) {
 5929:                             $datatable .= '<hr /><table>';
 5930:                             $numinrow = 2;
 5931:                             for (my $i=0; $i<@selectboxes; $i++) {
 5932:                                 $rem = $i%($numinrow);
 5933:                                 if ($rem == 0) {
 5934:                                     if ($i > 0) {
 5935:                                         $datatable .= '</tr>';
 5936:                                     }
 5937:                                     $datatable .= '<tr>';
 5938:                                 }
 5939:                                 $datatable .= '<td class="LC_left_item">'.
 5940:                                               $selectboxes[$i].'</td>';
 5941:                             }
 5942:                             if ($numinrow) {
 5943:                                 $rem = $i%$numinrow;
 5944:                             }
 5945:                             $colsleft = $numinrow - $rem;
 5946:                             if ($colsleft > 1) {
 5947:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5948:                             } else {
 5949:                                 $datatable .= '<td class="LC_left_item">';
 5950:                             }
 5951:                             $datatable .= '&nbsp;'.
 5952:                                           '</td></tr></table>';
 5953:                         }
 5954:                     }
 5955:                     $datatable .= '</fieldset>';
 5956:                 }
 5957:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5958:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5959:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5960:                     my ($rem,$numinrow);
 5961:                     if ($provider eq 'proctorio') {
 5962:                         $datatable .= '<table>';
 5963:                         $numinrow = 4;
 5964:                     }
 5965:                     my $i = 0;
 5966:                     foreach my $item (@{$crsconf{$provider}}) {
 5967:                         my $name;
 5968:                         if ($provider eq 'examity') {
 5969:                             $name = $lt{'crs'.$item};
 5970:                         } elsif ($provider eq 'proctorio') {
 5971:                             $name = $fieldtitles{$item};
 5972:                             $rem = $i%($numinrow);
 5973:                             if ($rem == 0) {
 5974:                                 if ($i > 0) {
 5975:                                     $datatable .= '</tr>';
 5976:                                 }
 5977:                                 $datatable .= '<tr>';
 5978:                             }
 5979:                             $datatable .= '<td class="LC_left_item>';
 5980:                         }
 5981:                         my $checked;
 5982:                         if ($crsconfig{$item}) {
 5983:                             $checked = ' checked="checked"';
 5984:                         }
 5985:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5986:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5987:                                       $name.'</label></span>';
 5988:                         if ($provider eq 'examity') {
 5989:                             $datatable .= '&nbsp; ';
 5990:                         }
 5991:                         $datatable .= "\n";
 5992:                         $i++;
 5993:                     }
 5994:                     if ($provider eq 'proctorio') {
 5995:                         if ($numinrow) {
 5996:                             $rem = $i%$numinrow;
 5997:                         }
 5998:                         my $colsleft = $numinrow - $rem;
 5999:                         if ($colsleft > 1) {
 6000:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 6001:                         } else {
 6002:                             $datatable .= '<td class="LC_left_item">';
 6003:                         }
 6004:                         $datatable .= '&nbsp;'.
 6005:                                       '</td></tr></table>';
 6006:                     }
 6007:                     $datatable .= '</fieldset>';
 6008:                 }
 6009:                 if ($showroles) {
 6010:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6011:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 6012:                     foreach my $role (@courseroles) {
 6013:                         my ($selected,$selectnone);
 6014:                         if (!$rolemaps{$role}) {
 6015:                             $selectnone = ' selected="selected"';
 6016:                         }
 6017:                         $datatable .= '<td style="text-align: center">'.
 6018:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 6019:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 6020:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6021:                         foreach my $ltirole (@ltiroles) {
 6022:                             unless ($selectnone) {
 6023:                                 if ($rolemaps{$role} eq $ltirole) {
 6024:                                     $selected = ' selected="selected"';
 6025:                                 } else {
 6026:                                     $selected = '';
 6027:                                 }
 6028:                             }
 6029:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 6030:                         }
 6031:                         $datatable .= '</select></td>';
 6032:                     }
 6033:                     $datatable .= '</tr></table></fieldset>'.
 6034:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6035:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 6036:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 6037:                                   '<tr><td></td><td>lms</td>'.
 6038:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 6039:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 6040:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 6041:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 6042:                         my %custom = %{$settings->{$provider}->{'custom'}};
 6043:                         if (keys(%custom) > 0) {
 6044:                             foreach my $key (sort(keys(%custom))) {
 6045:                                 next if ($key eq 'lms');
 6046:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 6047:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 6048:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6049:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6050:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6051:                             }
 6052:                         }
 6053:                     }
 6054:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6055:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6056:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6057:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6058:                                   '</table></fieldset></td></tr>'."\n";
 6059:                 }
 6060:                 $datatable .= '</td></tr>';
 6061:             }
 6062:             $itemcount ++;
 6063:         }
 6064:     }
 6065:     return $datatable;
 6066: }
 6067: 
 6068: sub proctoring_data {
 6069:     my $requserfields = {
 6070:                       proctorio => ['user'],
 6071:                       examity   => ['roles','user'],
 6072:                      };
 6073:     my $optuserfields = {
 6074:                         proctorio => ['fullname'],
 6075:                         examity   => ['fullname','firstname','lastname','email'],
 6076:                      };
 6077:     my $defaults = {
 6078:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6079:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6080:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6081:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6082:                                 'reentry','calculator','whiteboard'],
 6083:                   examity   => ['display'],
 6084:                 };
 6085:     my $extended = { 
 6086:                   proctorio => {
 6087:                                  verifyid => ['verifyidauto','verifyidlive'],
 6088:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6089:                                  tabslinks => ['notabs','linksonly'],
 6090:                                  reentry => ['noreentry','agentreentry'],
 6091:                                  calculator => ['calculatorbasic','calculatorsci'],
 6092:                                },
 6093:                   examity => {
 6094:                                display => {
 6095:                                            target      => ['iframe','tab','window'],
 6096:                                            width       => '',
 6097:                                            height      => '',
 6098:                                            linktext    => '',
 6099:                                            explanation => '',
 6100:                                           },
 6101:                              },
 6102:                 };
 6103:     my $crsconf = {
 6104:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6105:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6106:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6107:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6108:                                'reentry','calculator','whiteboard'],
 6109:                  examity => ['label','title','target','linktext','explanation','append'],
 6110:                };
 6111:     my $courseroles = ['cc','in','ta','ep','st'];
 6112:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6113:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6114: }
 6115: 
 6116: sub proctoring_titles {
 6117:     my ($item) = @_;
 6118:     my (%common_lt,%custom_lt);
 6119:     %common_lt = &Apache::lonlocal::texthash (
 6120:                      'avai'      => 'Available?',
 6121:                      'base'      => 'Basic Settings',
 6122:                      'requ'      => 'User data required to be sent on launch',
 6123:                      'optu'      => 'User data optionally sent on launch',
 6124:                      'udsl'      => 'User data sent on launch',
 6125:                      'defa'      => 'Defaults for items configurable in course',
 6126:                      'sigmethod' => 'Signature Method',
 6127:                      'key'       => 'Key',
 6128:                      'lifetime'  => 'Nonce lifetime (s)',
 6129:                      'secret'    => 'Secret',
 6130:                      'icon'      => 'Icon',
 6131:                      'fullname'  => 'Full Name',
 6132:                      'visible'   => 'Visible input',
 6133:                      'username'  => 'username',
 6134:                      'user'      => 'User',
 6135:                  );
 6136:     if ($item eq 'proctorio') {
 6137:         %custom_lt = &Apache::lonlocal::texthash (
 6138:                          'version'        => 'OAuth version',
 6139:                          'url'            => 'API URL',
 6140:                          'uname:dom'      => 'username-domain',
 6141:         );
 6142:     } elsif ($item eq 'examity') {
 6143:         %custom_lt = &Apache::lonlocal::texthash (
 6144:                          'version'        => 'LTI Version',
 6145:                          'url'            => 'URL',
 6146:                          'uname:dom'      => 'username:domain',
 6147:                          'msgtype'        => 'Message Type',
 6148:                          'firstname'      => 'First Name',
 6149:                          'lastname'       => 'Last Name',
 6150:                          'email'          => 'E-mail',
 6151:                          'roles'          => 'Role',
 6152:                          'crstarget'      => 'Display target',
 6153:                          'crslabel'       => 'Course label',
 6154:                          'crstitle'       => 'Course title', 
 6155:                          'crslinktext'    => 'Link Text',
 6156:                          'crsexplanation' => 'Explanation',
 6157:                          'crsappend'      => 'Provider URL',
 6158:         );
 6159:     }
 6160:     my %lt = (%common_lt,%custom_lt);
 6161:     return %lt;
 6162: }
 6163: 
 6164: sub proctoring_fieldtitles {
 6165:     my ($item) = @_;
 6166:     if ($item eq 'proctorio') {
 6167:         return &Apache::lonlocal::texthash (
 6168:                   'recordvideo' => 'Record video',
 6169:                   'recordaudio' => 'Record audio',
 6170:                   'recordscreen' => 'Record screen',
 6171:                   'recordwebtraffic' => 'Record web traffic',
 6172:                   'recordroomstart' => 'Record room scan',
 6173:                   'verifyvideo' => 'Verify webcam',
 6174:                   'verifyaudio' => 'Verify microphone',
 6175:                   'verifydesktop' => 'Verify desktop recording',
 6176:                   'verifyid' => 'Photo ID verification',
 6177:                   'verifysignature' => 'Require signature',
 6178:                   'fullscreen' => 'Fullscreen',
 6179:                   'clipboard' => 'Disable copy/paste',
 6180:                   'tabslinks' => 'New tabs/windows',
 6181:                   'closetabs' => 'Close other tabs',
 6182:                   'onescreen' => 'Limit to single screen',
 6183:                   'print' => 'Disable Printing',
 6184:                   'downloads' => 'Disable Downloads',
 6185:                   'cache' => 'Empty cache after exam',
 6186:                   'rightclick' => 'Disable right click',
 6187:                   'reentry' => 'Re-entry to exam',
 6188:                   'calculator' => 'Onscreen calculator',
 6189:                   'whiteboard' => 'Onscreen whiteboard',
 6190:                   'verifyidauto' => 'Automated verification',
 6191:                   'verifyidlive' => 'Live agent verification',
 6192:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6193:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6194:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6195:                   'notabs' => 'Disaallowed',
 6196:                   'linksonly' => 'Allowed from links in exam',
 6197:                   'noreentry' => 'Disallowed',
 6198:                   'agentreentry' => 'Agent required for re-entry',
 6199:                   'calculatorbasic' => 'Basic',
 6200:                   'calculatorsci' => 'Scientific',
 6201:         );
 6202:     } elsif ($item eq 'examity') {
 6203:         return &Apache::lonlocal::texthash (
 6204:                 'target'         => 'Display target',   
 6205:                 'window'         => 'Window',
 6206:                 'tab'            => 'Tab',
 6207:                 'iframe'         => 'iFrame',
 6208:                 'height'         => 'Height (pixels)',
 6209:                 'width'          => 'Width (pixels)',
 6210:                 'linktext'       => 'Default Link Text',
 6211:                 'explanation'    => 'Default Explanation',
 6212:                 'append'         => 'Provider URL',
 6213:         );
 6214:     }
 6215: }
 6216: 
 6217: sub proctoring_providernames {
 6218:     return (
 6219:              proctorio => 'Proctorio',
 6220:              examity   => 'Examity',
 6221:            );
 6222: }
 6223: 
 6224: sub print_lti {
 6225:     my ($dom,$settings,$rowtotal) = @_;
 6226:     my $itemcount = 1;
 6227:     my $maxnum = 0;
 6228:     my $css_class;
 6229:     my %ordered;
 6230:     if (ref($settings) eq 'HASH') {
 6231:         foreach my $item (keys(%{$settings})) {
 6232:             if (ref($settings->{$item}) eq 'HASH') {
 6233:                 my $num = $settings->{$item}{'order'};
 6234:                 if ($num eq '') {
 6235:                     $num = scalar(keys(%{$settings}));
 6236:                 }
 6237:                 $ordered{$num} = $item;
 6238:             }
 6239:         }
 6240:     }
 6241:     my $maxnum = scalar(keys(%ordered));
 6242:     my $datatable;
 6243:     my %lt = &lti_names();
 6244:     if (keys(%ordered)) {
 6245:         my @items = sort { $a <=> $b } keys(%ordered);
 6246:         for (my $i=0; $i<@items; $i++) {
 6247:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6248:             my $item = $ordered{$items[$i]};
 6249:             my ($key,$secret,$lifetime,$consumer,$requser,$crsinc,$current);
 6250:             if (ref($settings->{$item}) eq 'HASH') {
 6251:                 $key = $settings->{$item}->{'key'};
 6252:                 $secret = $settings->{$item}->{'secret'};
 6253:                 $lifetime = $settings->{$item}->{'lifetime'};
 6254:                 $consumer = $settings->{$item}->{'consumer'};
 6255:                 $requser = $settings->{$item}->{'requser'};
 6256:                 $crsinc = $settings->{$item}->{'crsinc'};
 6257:                 $current = $settings->{$item};
 6258:             }
 6259:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6260:             my %checkedrequser = (
 6261:                                    yes => ' checked="checked"',
 6262:                                    no  => '',
 6263:                                  );
 6264:             if (!$requser) {
 6265:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 6266:                 $checkedrequser{'yes'} = '';
 6267:             }
 6268:             my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6269:             my %checkedcrsinc = (
 6270:                                       yes => ' checked="checked"',
 6271:                                       no  => '',
 6272:                                    );
 6273:             if (!$crsinc) {
 6274:                 $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6275:                 $checkedcrsinc{'yes'} = '';
 6276:             }
 6277:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6278:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6279:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6280:             for (my $k=0; $k<=$maxnum; $k++) {
 6281:                 my $vpos = $k+1;
 6282:                 my $selstr;
 6283:                 if ($k == $i) {
 6284:                     $selstr = ' selected="selected" ';
 6285:                 }
 6286:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6287:             }
 6288:             $datatable .= '</select>'.('&nbsp;'x2).
 6289:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6290:                 &mt('Delete?').'</label></span></td>'.
 6291:                 '<td colspan="2">'.
 6292:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6293:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 6294:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6295:                 ('&nbsp;'x2).
 6296:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6297:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6298:                 ('&nbsp;'x2).
 6299:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6300:                 'value="'.$lifetime.'" size="3" /></span>'.
 6301:                 ('&nbsp;'x2).
 6302:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6303:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6304:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6305:                 '<br /><br />'.
 6306:                 '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6307:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6308:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6309:                 ('&nbsp;'x4).
 6310:                 '<span class="LC_nobreak">'.$lt{'key'}.
 6311:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 6312:                 ('&nbsp;'x2).
 6313:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6314:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 6315:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 6316:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6317:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6318:             $itemcount ++;
 6319:         }
 6320:     }
 6321:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6322:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6323:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6324:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6325:                   '<select name="lti_pos_add"'.$chgstr.'>';
 6326:     for (my $k=0; $k<$maxnum+1; $k++) {
 6327:         my $vpos = $k+1;
 6328:         my $selstr;
 6329:         if ($k == $maxnum) {
 6330:             $selstr = ' selected="selected" ';
 6331:         }
 6332:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6333:     }
 6334:     $datatable .= '</select>&nbsp;'."\n".
 6335:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6336:                   '<td colspan="2">'.
 6337:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6338:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 6339:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6340:                   ('&nbsp;'x2).
 6341:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6342:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6343:                   ('&nbsp;'x2).
 6344:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 6345:                   ('&nbsp;'x2).
 6346:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6347:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6348:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6349:                   '<br /><br />'.
 6350:                   '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6351:                   '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6352:                   '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6353:                   ('&nbsp;'x4).
 6354:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 6355:                   ('&nbsp;'x2).
 6356:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 6357:                   '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.lti_secret_add.type='."'text'".' } else { this.form.lti_secret_add.type='."'password'".' }" />'.&mt('Visible input').'</label></span> '."\n".
 6358:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6359:                   '</td>'."\n".
 6360:                   '</tr>'."\n";
 6361:     $$rowtotal ++;
 6362:     return $datatable;;
 6363: }
 6364: 
 6365: sub lti_names {
 6366:     my %lt = &Apache::lonlocal::texthash(
 6367:                                           'version'   => 'LTI Version',
 6368:                                           'url'       => 'URL',
 6369:                                           'key'       => 'Key',
 6370:                                           'lifetime'  => 'Nonce lifetime (s)',
 6371:                                           'consumer'  => 'Consumer',
 6372:                                           'secret'    => 'Secret',
 6373:                                           'requser'   => "User's identity sent",
 6374:                                           'crsinc'    => "Course's identity sent",
 6375:                                           'email'     => 'Email address',
 6376:                                           'sourcedid' => 'User ID',
 6377:                                           'other'     => 'Other',
 6378:                                           'passback'  => 'Can return grades to Consumer:',
 6379:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6380:                                           'topmenu'   => 'Display LON-CAPA page header',
 6381:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6382:                                         );
 6383:     return %lt;
 6384: }
 6385: 
 6386: sub lti_options {
 6387:     my ($num,$current,$itemcount,%lt) = @_;
 6388:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6389:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6390:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6391:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6392:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6393:     $checked{'mapcrstype'} = {};
 6394:     $checked{'makeuser'} = {};
 6395:     $checked{'selfenroll'} = {};
 6396:     $checked{'crssec'} = {};
 6397:     $checked{'crssecsrc'} = {};
 6398:     $checked{'lcauth'} = {};
 6399:     $checked{'menuitem'} = {};
 6400:     if ($num eq 'add') {
 6401:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6402:     }
 6403:     my $userfieldsty = 'none';
 6404:     my $crsfieldsty = 'none';
 6405:     my $crssecfieldsty = 'none';
 6406:     my $secsrcfieldsty = 'none';
 6407:     my $callbacksty = 'none';
 6408:     my $passbacksty = 'none';
 6409:     my $optionsty = 'block';
 6410:     my $crssty = 'block';
 6411:     my $lcauthparm;
 6412:     my $lcauthparmstyle = 'display:none';
 6413:     my $lcauthparmtext;
 6414:     my $menusty;
 6415:     my $numinrow = 4;
 6416:     my %menutitles = &ltimenu_titles();
 6417: 
 6418:     if (ref($current) eq 'HASH') {
 6419:         if (!$current->{'requser'}) {
 6420:             $optionsty = 'none';
 6421:             $crssty = 'none';
 6422:         } elsif (!$current->{'crsinc'}) {
 6423:             $crssty = 'none';
 6424:         }
 6425:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6426:             $checked{'mapuser'}{'sourcedid'} = '';
 6427:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6428:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 6429:             } else {
 6430:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6431:                 $userfield = $current->{'mapuser'};
 6432:                 $userfieldsty = 'inline-block';
 6433:             }
 6434:         }
 6435:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6436:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6437:             if ($current->{'mapcrs'} eq 'context_id') {
 6438:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 6439:             } else {
 6440:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6441:                 $cidfield = $current->{'mapcrs'};
 6442:                 $crsfieldsty = 'inline-block';
 6443:             }
 6444:         }
 6445:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6446:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6447:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6448:             }
 6449:         }
 6450:         if (!$current->{'storecrs'}) {
 6451:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6452:             $checked{'storecrs'}{'Y'} = '';
 6453:         }
 6454:         if ($current->{'makecrs'}) {
 6455:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6456:         }
 6457:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6458:             foreach my $role (@{$current->{'makeuser'}}) {
 6459:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6460:             }
 6461:         }
 6462:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6463:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6464:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6465:                 $lcauthparm = $current->{'lcauthparm'};
 6466:                 $lcauthparmstyle = 'display:table-row'; 
 6467:                 if ($current->{'lcauth'} eq 'localauth') {
 6468:                     $lcauthparmtext = &mt('Local auth argument');
 6469:                 } else {
 6470:                     $lcauthparmtext = &mt('Kerberos domain');
 6471:                 }
 6472:             }
 6473:         }
 6474:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6475:             foreach my $role (@{$current->{'selfenroll'}}) {
 6476:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6477:             }
 6478:         }
 6479:         if (ref($current->{'maproles'}) eq 'HASH') {
 6480:             %rolemaps = %{$current->{'maproles'}};
 6481:         }
 6482:         if ($current->{'section'} ne '') {
 6483:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 6484:             $crssecfieldsty = 'inline-block';
 6485:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6486:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6487:             } else {
 6488:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6489:                 $crssecsrc = $current->{'section'};
 6490:                 $secsrcfieldsty = 'inline-block';
 6491:             }
 6492:         } else {
 6493:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6494:         }
 6495:         if ($current->{'callback'} ne '') {
 6496:             $callback = $current->{'callback'};
 6497:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6498:             $callbacksty = 'inline-block';
 6499:         } else {
 6500:             $checked{'callback'}{'N'} = ' checked="checked"';
 6501:         }
 6502:         if ($current->{'topmenu'}) {
 6503:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6504:         } else {
 6505:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6506:         }
 6507:         if ($current->{'inlinemenu'}) {
 6508:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6509:         } else {
 6510:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6511:         }
 6512:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6513:             $menusty = 'inline-block';
 6514:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6515:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6516:                     if (exists($menutitles{$item})) {
 6517:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6518:                     }
 6519:                 }
 6520:             }
 6521:         } else {
 6522:             $menusty = 'none';
 6523:         }
 6524:     } else {
 6525:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6526:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6527:         $checked{'callback'}{'N'} = ' checked="checked"';
 6528:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6529:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 6530:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6531:         $menusty = 'inline-block'; 
 6532:     }
 6533:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6534:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6535:                                official   => 'Official',
 6536:                                unofficial => 'Unofficial',
 6537:                                community  => 'Community',
 6538:                                textbook   => 'Textbook',
 6539:                                placement  => 'Placement Test',
 6540:                                lti        => 'LTI Provider',
 6541:     );
 6542:     my @authtypes = ('internal','krb4','krb5','localauth');
 6543:     my %shortauth = (
 6544:                      internal => 'int',
 6545:                      krb4 => 'krb4',
 6546:                      krb5 => 'krb5',
 6547:                      localauth  => 'loc'
 6548:                     );
 6549:     my %authnames = &authtype_names();
 6550:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6551:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6552:     my @courseroles = ('cc','in','ta','ep','st');
 6553:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6554:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6555:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6556:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6557:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6558:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6559:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6560:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6561:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6562:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6563:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6564:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6565:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6566:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6567:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6568:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6569:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6570:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6571:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6572:     foreach my $option ('sourcedid','email','other') {
 6573:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6574:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6575:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6576:     }
 6577:     $output .= '</span></div>'.
 6578:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6579:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6580:                'value="'.$userfield.'" /></div></fieldset>'.
 6581:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6582:     foreach my $ltirole (@ltiroles) {
 6583:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6584:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 6585:     }
 6586:     $output .= '</fieldset>'.
 6587:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6588:                '<table>'.
 6589:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6590:                '</table>'.
 6591:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6592:                '<td class="LC_left_item">';
 6593:     foreach my $auth ('lti',@authtypes) {
 6594:         my $authtext;
 6595:         if ($auth eq 'lti') {
 6596:             $authtext = &mt('None');
 6597:         } else {
 6598:             $authtext = $authnames{$shortauth{$auth}};
 6599:         }
 6600:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6601:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6602:                    $authtext.'</label></span> &nbsp;';
 6603:     }
 6604:     $output .= '</td></tr>'.
 6605:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6606:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6607:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6608:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6609:                '</table></fieldset>'.
 6610:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6611:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6612:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6613:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6614:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6615:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6616:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6617:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6618:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6619:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6620:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6621:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6622:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6623:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6624:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6625:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6626:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6627:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6628:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6629:                    ('&nbsp;'x2);
 6630:     }
 6631:     $output .= '</span></div></fieldset>'.
 6632:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6633:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6634:                &mt('Unique course identifier').':&nbsp;';
 6635:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6636:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6637:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6638:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6639:     }
 6640:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6641:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6642:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6643:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6644:     foreach my $type (@coursetypes) {
 6645:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6646:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6647:                    ('&nbsp;'x2);
 6648:     }
 6649:     $output .= '</span><br /><br />'.
 6650:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6651:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6652:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6653:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6654:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6655:                '</fieldset>'.
 6656:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6657:     foreach my $ltirole (@lticourseroles) {
 6658:         my ($selected,$selectnone);
 6659:         if ($rolemaps{$ltirole} eq '') {
 6660:             $selectnone = ' selected="selected"';
 6661:         }
 6662:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6663:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6664:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6665:         foreach my $role (@courseroles) {
 6666:             unless ($selectnone) {
 6667:                 if ($rolemaps{$ltirole} eq $role) {
 6668:                     $selected = ' selected="selected"';
 6669:                 } else {
 6670:                     $selected = '';
 6671:                 }
 6672:             }
 6673:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6674:                        &Apache::lonnet::plaintext($role,'Course').
 6675:                        '</option>';
 6676:         }
 6677:         $output .= '</select></td>';
 6678:     }
 6679:     $output .= '</tr></table></fieldset>'.
 6680:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6681:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6682:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6683:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6684:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6685:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6686:                '</fieldset>'.
 6687:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6688:     foreach my $lticrsrole (@lticourseroles) {
 6689:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6690:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6691:     }
 6692:     $output .= '</fieldset>'.
 6693:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6694:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6695:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6696:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6697:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6698:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6699:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6700:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6701:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6702:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6703:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6704:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6705:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6706:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6707:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6708:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6709:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6710:     foreach my $extra ('roster','passback') {
 6711:         my $checkedon = '';
 6712:         my $checkedoff = ' checked="checked"';
 6713:         if ($extra eq 'passback') {
 6714:             $pb1p1chk = ' checked="checked"';
 6715:             $pb1p0chk = '';
 6716:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6717:         } else {
 6718:             $onclickpb = ''; 
 6719:         }
 6720:         if (ref($current) eq 'HASH') {
 6721:             if (($current->{$extra})) {
 6722:                 $checkedon = $checkedoff;
 6723:                 $checkedoff = '';
 6724:                 if ($extra eq 'passback') {
 6725:                     $passbacksty = 'inline-block';
 6726:                 }
 6727:                 if ($current->{'passbackformat'} eq '1.0') {
 6728:                     $pb1p0chk =  ' checked="checked"';
 6729:                     $pb1p1chk = '';
 6730:                 }
 6731:             }
 6732:         }
 6733:         $output .= $lt{$extra}.'&nbsp;'.
 6734:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6735:                    &mt('No').'</label>'.('&nbsp;'x2).
 6736:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6737:                    &mt('Yes').'</label><br />';
 6738:     }
 6739:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6740:                '<span class="LC_nobreak">'.&mt('Grade format').
 6741:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6742:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6743:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6744:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6745:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6746:     $output .= '</span></div></fieldset>';
 6747: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6748: #
 6749: #    $output .= '</fieldset>'.
 6750: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6751:     return $output;
 6752: }
 6753: 
 6754: sub ltimenu_titles {
 6755:     return &Apache::lonlocal::texthash(
 6756:                                         fullname    => 'Full name',
 6757:                                         coursetitle => 'Course title',
 6758:                                         role        => 'Role',
 6759:                                         logout      => 'Logout',
 6760:                                         grades      => 'Grades',
 6761:     );
 6762: }
 6763: 
 6764: sub print_coursedefaults {
 6765:     my ($position,$dom,$settings,$rowtotal) = @_;
 6766:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6767:     my $itemcount = 1;
 6768:     my %choices =  &Apache::lonlocal::texthash (
 6769:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6770:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6771:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6772:         coursecredits        => 'Credits can be specified for courses',
 6773:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6774:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6775:         texengine            => 'Default method to display mathematics',
 6776:         postsubmit           => 'Disable submit button/keypress following student submission',
 6777:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6778:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6779:     );
 6780:     my %staticdefaults = (
 6781:                            anonsurvey_threshold => 10,
 6782:                            uploadquota          => 500,
 6783:                            postsubmit           => 60,
 6784:                            mysqltables          => 172800,
 6785:                          );
 6786:     if ($position eq 'top') {
 6787:         %defaultchecked = (
 6788:                             'canuse_pdfforms' => 'off',
 6789:                             'uselcmath'       => 'on',
 6790:                             'usejsme'         => 'on',
 6791:                             'canclone'        => 'none',
 6792:                           );
 6793:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 6794:         my $deftex = $Apache::lonnet::deftex;
 6795:         if (ref($settings) eq 'HASH') {
 6796:             if ($settings->{'texengine'}) {
 6797:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6798:                     $deftex = $settings->{'texengine'};
 6799:                 }
 6800:             }
 6801:         }
 6802:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6803:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6804:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6805:                        '</span></td><td class="LC_right_item">'.
 6806:                        '<select name="texengine">'."\n";
 6807:         my %texoptions = (
 6808:                             MathJax  => 'MathJax',
 6809:                             mimetex  => &mt('Convert to Images'),
 6810:                             tth      => &mt('TeX to HTML'),
 6811:                          );
 6812:         foreach my $renderer ('MathJax','mimetex','tth') {
 6813:             my $selected = '';
 6814:             if ($renderer eq $deftex) {
 6815:                 $selected = ' selected="selected"';
 6816:             }
 6817:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6818:         }
 6819:         $mathdisp .= '</select></td></tr>'."\n";
 6820:         $itemcount ++;
 6821:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6822:                                                      \%choices,$itemcount);
 6823:         $datatable = $mathdisp.$datatable;
 6824:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6825:         $datatable .=
 6826:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6827:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6828:             '</span></td><td class="LC_left_item">';
 6829:         my $currcanclone = 'none';
 6830:         my $onclick;
 6831:         my @cloneoptions = ('none','domain');
 6832:         my %clonetitles = &Apache::lonlocal::texthash (
 6833:                              none     => 'No additional course requesters',
 6834:                              domain   => "Any course requester in course's domain",
 6835:                              instcode => 'Course requests for official courses ...',
 6836:                           );
 6837:         my (%codedefaults,@code_order,@posscodes);
 6838:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6839:                                                     \@code_order) eq 'ok') {
 6840:             if (@code_order > 0) {
 6841:                 push(@cloneoptions,'instcode');
 6842:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6843:             }
 6844:         }
 6845:         if (ref($settings) eq 'HASH') {
 6846:             if ($settings->{'canclone'}) {
 6847:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6848:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6849:                         if (@code_order > 0) {
 6850:                             $currcanclone = 'instcode';
 6851:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6852:                         }
 6853:                     }
 6854:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6855:                     $currcanclone = $settings->{'canclone'};
 6856:                 }
 6857:             }
 6858:         }
 6859:         foreach my $option (@cloneoptions) {
 6860:             my ($checked,$additional);
 6861:             if ($currcanclone eq $option) {
 6862:                 $checked = ' checked="checked"';
 6863:             }
 6864:             if ($option eq 'instcode') {
 6865:                 if (@code_order) {
 6866:                     my $show = 'none';
 6867:                     if ($checked) {
 6868:                         $show = 'block';
 6869:                     }
 6870:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6871:                                   &mt('Institutional codes for new and cloned course have identical:').
 6872:                                   '<br />';
 6873:                     foreach my $item (@code_order) {
 6874:                         my $codechk;
 6875:                         if ($checked) {
 6876:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6877:                                 $codechk = ' checked="checked"';
 6878:                             }
 6879:                         }
 6880:                         $additional .= '<label>'.
 6881:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6882:                                        $item.'</label>';
 6883:                     }
 6884:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6885:                 }
 6886:             }
 6887:             $datatable .=
 6888:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6889:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6890:                 '</label>&nbsp;'.$additional.'</span><br />';
 6891:         }
 6892:         $datatable .= '</td>'.
 6893:                       '</tr>';
 6894:         $itemcount ++;
 6895:     } else {
 6896:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6897:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6898:         my $currusecredits = 0;
 6899:         my $postsubmitclient = 1;
 6900:         my @types = ('official','unofficial','community','textbook','placement');
 6901:         if (ref($settings) eq 'HASH') {
 6902:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6903:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6904:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6905:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6906:                 }
 6907:             }
 6908:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6909:                 foreach my $type (@types) {
 6910:                     next if ($type eq 'community');
 6911:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6912:                     if ($defcredits{$type} ne '') {
 6913:                         $currusecredits = 1;
 6914:                     }
 6915:                 }
 6916:             }
 6917:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6918:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6919:                     $postsubmitclient = 0;
 6920:                     foreach my $type (@types) {
 6921:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6922:                     }
 6923:                 } else {
 6924:                     foreach my $type (@types) {
 6925:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6926:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6927:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6928:                             } else {
 6929:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6930:                             }
 6931:                         } else {
 6932:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6933:                         }
 6934:                     }
 6935:                 }
 6936:             } else {
 6937:                 foreach my $type (@types) {
 6938:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6939:                 }
 6940:             }
 6941:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6942:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6943:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6944:                 }
 6945:             } else {
 6946:                 foreach my $type (@types) {
 6947:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6948:                 }
 6949:             }
 6950:         } else {
 6951:             foreach my $type (@types) {
 6952:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6953:             }
 6954:         }
 6955:         if (!$currdefresponder) {
 6956:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6957:         } elsif ($currdefresponder < 1) {
 6958:             $currdefresponder = 1;
 6959:         }
 6960:         foreach my $type (@types) {
 6961:             if ($curruploadquota{$type} eq '') {
 6962:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6963:             }
 6964:         }
 6965:         $datatable .=
 6966:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6967:                 $choices{'anonsurvey_threshold'}.
 6968:                 '</span></td>'.
 6969:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6970:                 '<input type="text" name="anonsurvey_threshold"'.
 6971:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6972:                 '</td></tr>'."\n";
 6973:         $itemcount ++;
 6974:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6975:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6976:                       $choices{'uploadquota'}.
 6977:                       '</span></td>'.
 6978:                       '<td style="text-align: right" class="LC_right_item">'.
 6979:                       '<table><tr>';
 6980:         foreach my $type (@types) {
 6981:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6982:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6983:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6984:         }
 6985:         $datatable .= '</tr></table></td></tr>'."\n";
 6986:         $itemcount ++;
 6987:         my $onclick = "toggleDisplay(this.form,'credits');";
 6988:         my $display = 'none';
 6989:         if ($currusecredits) {
 6990:             $display = 'block';
 6991:         }
 6992:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6993:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6994:         foreach my $type (@types) {
 6995:             next if ($type eq 'community');
 6996:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6997:                            '<input type="text" name="'.$type.'_credits"'.
 6998:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6999:         }
 7000:         $additional .= '</tr></table></div>'."\n";
 7001:         %defaultchecked = ('coursecredits' => 'off');
 7002:         @toggles = ('coursecredits');
 7003:         my $current = {
 7004:                         'coursecredits' => $currusecredits,
 7005:                       };
 7006:         (my $table,$itemcount) =
 7007:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7008:                                \%choices,$itemcount,$onclick,$additional,'left');
 7009:         $datatable .= $table;
 7010:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7011:         my $display = 'none';
 7012:         if ($postsubmitclient) {
 7013:             $display = 'block';
 7014:         }
 7015:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7016:                       &mt('Number of seconds submit is disabled').'<br />'.
 7017:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7018:                       '<table><tr>';
 7019:         foreach my $type (@types) {
 7020:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7021:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7022:                            $deftimeout{$type}.'" size="5" /></td>';
 7023:         }
 7024:         $additional .= '</tr></table></div>'."\n";
 7025:         %defaultchecked = ('postsubmit' => 'on');
 7026:         @toggles = ('postsubmit');
 7027:         $current = {
 7028:                        'postsubmit' => $postsubmitclient,
 7029:                    };
 7030:         ($table,$itemcount) =
 7031:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7032:                                \%choices,$itemcount,$onclick,$additional,'left');
 7033:         $datatable .= $table;
 7034:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7035:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7036:                       $choices{'mysqltables'}.
 7037:                       '</span></td>'.
 7038:                       '<td style="text-align: right" class="LC_right_item">'.
 7039:                       '<table><tr>';
 7040:         foreach my $type (@types) {
 7041:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7042:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7043:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7044:         }
 7045:         $datatable .= '</tr></table></td></tr>'."\n";
 7046:         $itemcount ++;
 7047: 
 7048:     }
 7049:     $$rowtotal += $itemcount;
 7050:     return $datatable;
 7051: }
 7052: 
 7053: sub print_selfenrollment {
 7054:     my ($position,$dom,$settings,$rowtotal) = @_;
 7055:     my ($css_class,$datatable);
 7056:     my $itemcount = 1;
 7057:     my @types = ('official','unofficial','community','textbook','placement');
 7058:     if (($position eq 'top') || ($position eq 'middle')) {
 7059:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7060:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7061:         my @rows;
 7062:         my $key;
 7063:         if ($position eq 'top') {
 7064:             $key = 'admin'; 
 7065:             if (ref($rowsref) eq 'ARRAY') {
 7066:                 @rows = @{$rowsref};
 7067:             }
 7068:         } elsif ($position eq 'middle') {
 7069:             $key = 'default';
 7070:             @rows = ('types','registered','approval','limit');
 7071:         }
 7072:         foreach my $row (@rows) {
 7073:             if (defined($titlesref->{$row})) {
 7074:                 $itemcount ++;
 7075:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7076:                 $datatable .= '<tr'.$css_class.'>'.
 7077:                               '<td>'.$titlesref->{$row}.'</td>'.
 7078:                               '<td class="LC_left_item">'.
 7079:                               '<table><tr>';
 7080:                 my (%current,%currentcap);
 7081:                 if (ref($settings) eq 'HASH') {
 7082:                     if (ref($settings->{$key}) eq 'HASH') {
 7083:                         foreach my $type (@types) {
 7084:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7085:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7086:                             }
 7087:                             if (($row eq 'limit') && ($key eq 'default')) {
 7088:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7089:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7090:                                 }
 7091:                             }
 7092:                         }
 7093:                     }
 7094:                 }
 7095:                 my %roles = (
 7096:                              '0' => &Apache::lonnet::plaintext('dc'),
 7097:                             ); 
 7098:             
 7099:                 foreach my $type (@types) {
 7100:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7101:                         $datatable .= '<th>'.&mt($type).'</th>';
 7102:                     }
 7103:                 }
 7104:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7105:                     $datatable .= '</tr><tr>';
 7106:                 }
 7107:                 foreach my $type (@types) {
 7108:                     if ($type eq 'community') {
 7109:                         $roles{'1'} = &mt('Community personnel');
 7110:                     } else {
 7111:                         $roles{'1'} = &mt('Course personnel');
 7112:                     }
 7113:                     $datatable .= '<td style="vertical-align: top">';
 7114:                     if ($position eq 'top') {
 7115:                         my %checked;
 7116:                         if ($current{$type} eq '0') {
 7117:                             $checked{'0'} = ' checked="checked"';
 7118:                         } else {
 7119:                             $checked{'1'} = ' checked="checked"';
 7120:                         }
 7121:                         foreach my $role ('1','0') {
 7122:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7123:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7124:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7125:                                           $roles{$role}.'</label></span> ';
 7126:                         }
 7127:                     } else {
 7128:                         if ($row eq 'types') {
 7129:                             my %checked;
 7130:                             if ($current{$type} =~ /^(all|dom)$/) {
 7131:                                 $checked{$1} = ' checked="checked"';
 7132:                             } else {
 7133:                                 $checked{''} = ' checked="checked"';
 7134:                             }
 7135:                             foreach my $val ('','dom','all') {
 7136:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7137:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7138:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7139:                             }
 7140:                         } elsif ($row eq 'registered') {
 7141:                             my %checked;
 7142:                             if ($current{$type} eq '1') {
 7143:                                 $checked{'1'} = ' checked="checked"';
 7144:                             } else {
 7145:                                 $checked{'0'} = ' checked="checked"';
 7146:                             }
 7147:                             foreach my $val ('0','1') {
 7148:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7149:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7150:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7151:                             }
 7152:                         } elsif ($row eq 'approval') {
 7153:                             my %checked;
 7154:                             if ($current{$type} =~ /^([12])$/) {
 7155:                                 $checked{$1} = ' checked="checked"';
 7156:                             } else {
 7157:                                 $checked{'0'} = ' checked="checked"';
 7158:                             }
 7159:                             for my $val (0..2) {
 7160:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7161:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7162:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7163:                             }
 7164:                         } elsif ($row eq 'limit') {
 7165:                             my %checked;
 7166:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7167:                                 $checked{$1} = ' checked="checked"';
 7168:                             } else {
 7169:                                 $checked{'none'} = ' checked="checked"';
 7170:                             }
 7171:                             my $cap;
 7172:                             if ($currentcap{$type} =~ /^\d+$/) {
 7173:                                 $cap = $currentcap{$type};
 7174:                             }
 7175:                             foreach my $val ('none','allstudents','selfenrolled') {
 7176:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7177:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7178:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7179:                             }
 7180:                             $datatable .= '<br />'.
 7181:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7182:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7183:                                           '</span>'; 
 7184:                         }
 7185:                     }
 7186:                     $datatable .= '</td>';
 7187:                 }
 7188:                 $datatable .= '</tr>';
 7189:             }
 7190:             $datatable .= '</table></td></tr>';
 7191:         }
 7192:     } elsif ($position eq 'bottom') {
 7193:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7194:     }
 7195:     $$rowtotal += $itemcount;
 7196:     return $datatable;
 7197: }
 7198: 
 7199: sub print_validation_rows {
 7200:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7201:     my ($itemsref,$namesref,$fieldsref);
 7202:     if ($caller eq 'selfenroll') { 
 7203:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7204:     } elsif ($caller eq 'requestcourses') {
 7205:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7206:     }
 7207:     my %currvalidation;
 7208:     if (ref($settings) eq 'HASH') {
 7209:         if (ref($settings->{'validation'}) eq 'HASH') {
 7210:             %currvalidation = %{$settings->{'validation'}};
 7211:         }
 7212:     }
 7213:     my $datatable;
 7214:     my $itemcount = 0;
 7215:     foreach my $item (@{$itemsref}) {
 7216:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7217:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7218:                       $namesref->{$item}.
 7219:                       '</span></td>'.
 7220:                       '<td class="LC_left_item">';
 7221:         if (($item eq 'url') || ($item eq 'button')) {
 7222:             $datatable .= '<span class="LC_nobreak">'.
 7223:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7224:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7225:         } elsif ($item eq 'fields') {
 7226:             my @currfields;
 7227:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7228:                 @currfields = @{$currvalidation{$item}};
 7229:             }
 7230:             foreach my $field (@{$fieldsref}) {
 7231:                 my $check = '';
 7232:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7233:                     $check = ' checked="checked"';
 7234:                 }
 7235:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7236:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7237:                               ' value="'.$field.'"'.$check.' />'.$field.
 7238:                               '</label></span> ';
 7239:             }
 7240:         } elsif ($item eq 'markup') {
 7241:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7242:                            $currvalidation{$item}.
 7243:                               '</textarea>';
 7244:         }
 7245:         $datatable .= '</td></tr>'."\n";
 7246:         if (ref($rowtotal)) {
 7247:             $itemcount ++;
 7248:         }
 7249:     }
 7250:     if ($caller eq 'requestcourses') {
 7251:         my %currhash;
 7252:         if (ref($settings) eq 'HASH') {
 7253:             if (ref($settings->{'validation'}) eq 'HASH') {
 7254:                 if ($settings->{'validation'}{'dc'} ne '') {
 7255:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7256:                 }
 7257:             }
 7258:         }
 7259:         my $numinrow = 2;
 7260:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7261:                                                        'validationdc',%currhash);
 7262:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7263:         $datatable .= '<tr'.$css_class.'><td>';
 7264:         if ($numdc > 1) {
 7265:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7266:         } else {
 7267:             $datatable .=  &mt('Course creation processed as: ');
 7268:         }
 7269:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7270:         $itemcount ++;
 7271:     }
 7272:     if (ref($rowtotal)) {
 7273:         $$rowtotal += $itemcount;
 7274:     }
 7275:     return $datatable;
 7276: }
 7277: 
 7278: sub print_privacy {
 7279:     my ($position,$dom,$settings,$rowtotal) = @_;
 7280:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7281:     my $itemcount = 0;
 7282:     unless ($position eq 'top') {
 7283:         @items = ('domain','author','course','community');
 7284:         %names = &Apache::lonlocal::texthash (
 7285:                      domain => 'Assigned domain role(s)',
 7286:                      author => 'Assigned co-author role(s)',
 7287:                      course => 'Assigned course role(s)',
 7288:                      community => 'Assigned community role',
 7289:                  );
 7290:         $numinrow = 4;
 7291:         ($othertitle,$usertypes,$types) =
 7292:             &Apache::loncommon::sorted_inst_types($dom);
 7293:     }
 7294:     if (($position eq 'top') || ($position eq 'middle')) {
 7295:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7296:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7297:         if ($position eq 'top') {
 7298:             my %curr;
 7299:             my @options = ('none','user','domain','auto');
 7300:             my %titles = &Apache::lonlocal::texthash (
 7301:                 none   => 'Not allowed',
 7302:                 user   => 'User authorizes',
 7303:                 domain => 'DC authorizes',
 7304:                 auto   => 'Unrestricted',
 7305:                 instdom => 'Other domain shares institution/provider',
 7306:                 extdom => 'Other domain has different institution/provider',
 7307:             );
 7308:             my %names = &Apache::lonlocal::texthash (
 7309:                 domain => 'Domain role',
 7310:                 author => 'Co-author role',
 7311:                 course => 'Course role',
 7312:                 community => 'Community role',
 7313:             );
 7314:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7315:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7316:             foreach my $domtype ('instdom','extdom') {
 7317:                 my (%checked,$skip);
 7318:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7319:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7320:                               '<td class="LC_left_item">';
 7321:                 if ($domtype eq 'instdom') {
 7322:                     unless (@instdoms > 1) {
 7323:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7324:                         $skip = 1;
 7325:                     }
 7326:                 } elsif ($domtype eq 'extdom') {
 7327:                     if (keys(%by_location) == 0) {
 7328:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7329:                         $skip = 1;
 7330:                     }
 7331:                 }
 7332:                 unless ($skip) {
 7333:                     foreach my $roletype ('domain','author','course','community') {
 7334:                         $checked{'auto'} = ' checked="checked"';
 7335:                         if (ref($settings) eq 'HASH') {
 7336:                             if (ref($settings->{approval}) eq 'HASH') {
 7337:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7338:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7339:                                         $checked{$1} = ' checked="checked"';
 7340:                                         $checked{'auto'} = '';
 7341:                                     }
 7342:                                 }
 7343:                             }
 7344:                         }
 7345:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7346:                         foreach my $option (@options) {
 7347:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7348:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7349:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7350:                                           '</label></span>&nbsp; ';
 7351:                         }
 7352:                         $datatable .= '</fieldset>';
 7353:                     }
 7354:                 }
 7355:                 $datatable .= '</td></tr>';
 7356:                 $itemcount ++;
 7357:             }
 7358:         } elsif ($position eq 'middle') {
 7359:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7360:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7361:                     foreach my $item (@{$types}) {
 7362:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7363:                                                                $numinrow,$itemcount,'','','','','',
 7364:                                                                '',$usertypes->{$item});
 7365:                         $itemcount ++;
 7366:                     }
 7367:                 }
 7368:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7369:                                                        $numinrow,$itemcount,'','','','','',
 7370:                                                        '',$othertitle);
 7371:                 $itemcount ++;
 7372:             } else {
 7373:                 my (@insttypes,%insttitles);
 7374:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7375:                     @insttypes = @{$types};
 7376:                     %insttitles = %{$usertypes};
 7377:                 }
 7378:                 foreach my $item (@insttypes,'default') {
 7379:                     my $title;
 7380:                     if ($item eq 'default') {
 7381:                         $title = $othertitle;
 7382:                     } else {
 7383:                         $title = $insttitles{$item};
 7384:                     }
 7385:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7386:                     $datatable .= '<tr'.$css_class.'>'.
 7387:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7388:                                   '<td class="LC_left_item">'.
 7389:                                   &mt('Nothing to set here, as there are no other domains').
 7390:                                   '</td></tr>';
 7391:                     $itemcount ++;
 7392:                 }
 7393:             }
 7394:         }
 7395:     } else {
 7396:         my $prefix;
 7397:         if ($position eq 'lower') {
 7398:             $prefix = 'priv';
 7399:         } else {
 7400:             $prefix = 'unpriv';
 7401:         }
 7402:         foreach my $item (@items) {
 7403:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7404:                                                    $numinrow,$itemcount,'','','','','',
 7405:                                                    '',$names{$item});
 7406:             $itemcount ++;
 7407:         }
 7408:     }
 7409:     if (ref($rowtotal)) {
 7410:         $$rowtotal += $itemcount;
 7411:     }
 7412:     return $datatable;
 7413: }
 7414: 
 7415: sub print_passwords {
 7416:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7417:     my ($datatable,$css_class);
 7418:     my $itemcount = 0;
 7419:     my %titles = &Apache::lonlocal::texthash (
 7420:         captcha        => '"Forgot Password" CAPTCHA validation',
 7421:         link           => 'Reset link expiration (hours)',
 7422:         case           => 'Case-sensitive usernames/e-mail',
 7423:         prelink        => 'Information required (form 1)',
 7424:         postlink       => 'Information required (form 2)',
 7425:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7426:         customtext     => 'Domain specific text (HTML)',
 7427:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7428:         intauth_check  => 'Check bcrypt cost if authenticated',
 7429:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7430:         permanent      => 'Permanent e-mail address',
 7431:         critical       => 'Critical notification address',
 7432:         notify         => 'Notification address',
 7433:         min            => 'Minimum password length',
 7434:         max            => 'Maximum password length',
 7435:         chars          => 'Required characters',
 7436:         expire         => 'Password expiration (days)',
 7437:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7438:     );
 7439:     if ($position eq 'top') {
 7440:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7441:         my $shownlinklife = 2;
 7442:         my $prelink = 'both';
 7443:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7444:         if (ref($settings) eq 'HASH') {
 7445:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7446:                 $shownlinklife = $settings->{resetlink};
 7447:             }
 7448:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7449:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7450:             }
 7451:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7452:                 $prelink = $settings->{resetprelink};
 7453:             }
 7454:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7455:                 %postlink = %{$settings->{resetpostlink}};
 7456:             }
 7457:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7458:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7459:             }
 7460:             if ($settings->{resetremove}) {
 7461:                 $nostdtext = 1;
 7462:             }
 7463:             if ($settings->{resetcustom}) {
 7464:                 $customurl = $settings->{resetcustom};
 7465:             }
 7466:         } else {
 7467:             if (ref($types) eq 'ARRAY') {
 7468:                 foreach my $item (@{$types}) {
 7469:                     $casesens{$item} = 1;
 7470:                     $postlink{$item} = ['username','email'];
 7471:                 }
 7472:             }
 7473:             $casesens{'default'} = 1;
 7474:             $postlink{'default'} = ['username','email'];
 7475:             $prelink = 'both';
 7476:             %emailsrc = (
 7477:                           permanent => 1,
 7478:                           critical  => 1,
 7479:                           notify    => 1,
 7480:             );
 7481:         }
 7482:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7483:         $itemcount ++;
 7484:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7485:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7486:                       '<td class="LC_left_item">'.
 7487:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7488:                       'name="passwords_link" size="3" /></td></tr>';
 7489:         $itemcount ++;
 7490:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7491:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7492:                       '<td class="LC_left_item">';
 7493:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7494:             foreach my $item (@{$types}) {
 7495:                 my $checkedcase;
 7496:                 if ($casesens{$item}) {
 7497:                     $checkedcase = ' checked="checked"';
 7498:                 }
 7499:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7500:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7501:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7502:                               '</span>&nbsp;&nbsp; ';
 7503:             }
 7504:         }
 7505:         my $checkedcase;
 7506:         if ($casesens{'default'}) {
 7507:             $checkedcase = ' checked="checked"';
 7508:         }
 7509:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7510:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7511:                       $othertitle.'</label></span></td>';
 7512:         $itemcount ++;
 7513:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7514:         my %checkedpre = (
 7515:                              both => ' checked="checked"',
 7516:                              either => '',
 7517:                          );
 7518:         if ($prelink eq 'either') {
 7519:             $checkedpre{either} = ' checked="checked"';
 7520:             $checkedpre{both} = '';
 7521:         }
 7522:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7523:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7524:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7525:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7526:                       '<span class="LC_nobreak"><label>'.
 7527:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7528:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7529:         $itemcount ++;
 7530:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7531:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7532:                       '<td class="LC_left_item">';
 7533:         my %postlinked;
 7534:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7535:             foreach my $item (@{$types}) {
 7536:                 undef(%postlinked);
 7537:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7538:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7539:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7540:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7541:                 }
 7542:                 foreach my $field ('email','username') {
 7543:                     my $checked;
 7544:                     if ($postlinked{$field}) {
 7545:                         $checked = ' checked="checked"';
 7546:                     }
 7547:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7548:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7549:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7550:                                   '<span>&nbsp;&nbsp; ';
 7551:                 }
 7552:                 $datatable .= '</fieldset>';
 7553:             }
 7554:         }
 7555:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7556:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7557:         }
 7558:         $datatable .= '<fieldset style="display: inline-block;">'.
 7559:                       '<legend>'.$othertitle.'</legend>';
 7560:         foreach my $field ('email','username') {
 7561:             my $checked;
 7562:             if ($postlinked{$field}) {
 7563:                 $checked = ' checked="checked"';
 7564:             }
 7565:             $datatable .= '<span class="LC_nobreak"><label>'.
 7566:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7567:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7568:                           '<span>&nbsp;&nbsp; ';
 7569:         }
 7570:         $datatable .= '</fieldset></td></tr>';
 7571:         $itemcount ++;
 7572:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7573:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7574:                       '<td class="LC_left_item">';
 7575:         foreach my $type ('permanent','critical','notify') {
 7576:             my $checkedemail;
 7577:             if ($emailsrc{$type}) {
 7578:                 $checkedemail = ' checked="checked"';
 7579:             }
 7580:             $datatable .= '<span class="LC_nobreak"><label>'.
 7581:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7582:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7583:                           '<span>&nbsp;&nbsp; ';
 7584:         }
 7585:         $datatable .= '</td></tr>';
 7586:         $itemcount ++;
 7587:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7588:         my $switchserver = &check_switchserver($dom,$confname);
 7589:         my ($showstd,$noshowstd);
 7590:         if ($nostdtext) {
 7591:             $noshowstd = ' checked="checked"';
 7592:         } else {
 7593:             $showstd = ' checked="checked"';
 7594:         }
 7595:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7596:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7597:                       &mt('Retain standard text:').
 7598:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7599:                       &mt('Yes').'</label>'.'&nbsp;'.
 7600:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7601:                       &mt('No').'</label></span><br />'.
 7602:                       '<span class="LC_fontsize_small">'.
 7603:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7604:                       &mt('Include custom text:');
 7605:         if ($customurl) {
 7606:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7607:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7608:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7609:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7610:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7611:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7612:         }
 7613:         if ($switchserver) {
 7614:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7615:         } else {
 7616:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7617:                          '<input type="file" name="passwords_customfile" /></span>';
 7618:         }
 7619:         $datatable .= '</td></tr>';
 7620:     } elsif ($position eq 'middle') {
 7621:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7622:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7623:         my %defaults;
 7624:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7625:             %defaults = %{$domconf{'defaults'}};
 7626:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7627:                 $defaults{'intauth_cost'} = 10;
 7628:             }
 7629:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7630:                 $defaults{'intauth_check'} = 0;
 7631:             }
 7632:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7633:                 $defaults{'intauth_switch'} = 0;
 7634:             }
 7635:         } else {
 7636:             %defaults = (
 7637:                           'intauth_cost'   => 10,
 7638:                           'intauth_check'  => 0,
 7639:                           'intauth_switch' => 0,
 7640:                         );
 7641:         }
 7642:         foreach my $item (@items) {
 7643:             if ($itemcount%2) {
 7644:                 $css_class = '';
 7645:             } else {
 7646:                 $css_class = ' class="LC_odd_row" ';
 7647:             }
 7648:             $datatable .= '<tr'.$css_class.'>'.
 7649:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7650:                           '</span></td><td class="LC_left_item" colspan="3">';
 7651:             if ($item eq 'intauth_switch') {
 7652:                 my @options = (0,1,2);
 7653:                 my %optiondesc = &Apache::lonlocal::texthash (
 7654:                                    0 => 'No',
 7655:                                    1 => 'Yes',
 7656:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7657:                                  );
 7658:                 $datatable .= '<table width="100%">';
 7659:                 foreach my $option (@options) {
 7660:                     my $checked = ' ';
 7661:                     if ($defaults{$item} eq $option) {
 7662:                         $checked = ' checked="checked"';
 7663:                     }
 7664:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7665:                                   '<label><input type="radio" name="'.$item.
 7666:                                   '" value="'.$option.'"'.$checked.' />'.
 7667:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7668:                 }
 7669:                 $datatable .= '</table>';
 7670:             } elsif ($item eq 'intauth_check') {
 7671:                 my @options = (0,1,2);
 7672:                 my %optiondesc = &Apache::lonlocal::texthash (
 7673:                                    0 => 'No',
 7674:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7675:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7676:                                  );
 7677:                 $datatable .= '<table width="100%">';
 7678:                 foreach my $option (@options) {
 7679:                     my $checked = ' ';
 7680:                     my $onclick;
 7681:                     if ($defaults{$item} eq $option) {
 7682:                         $checked = ' checked="checked"';
 7683:                     }
 7684:                     if ($option == 2) {
 7685:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7686:                     }
 7687:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7688:                                   '<label><input type="radio" name="'.$item.
 7689:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7690:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7691:                 }
 7692:                 $datatable .= '</table>';
 7693:             } else {
 7694:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7695:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7696:             }
 7697:             $datatable .= '</td></tr>';
 7698:             $itemcount ++;
 7699:         }
 7700:     } elsif ($position eq 'lower') {
 7701:         my ($min,$max,%chars,$expire,$numsaved);
 7702:         $min = $Apache::lonnet::passwdmin;
 7703:         if (ref($settings) eq 'HASH') {
 7704:             if ($settings->{min}) {
 7705:                 $min = $settings->{min};
 7706:             }
 7707:             if ($settings->{max}) {
 7708:                 $max = $settings->{max};
 7709:             }
 7710:             if (ref($settings->{chars}) eq 'ARRAY') {
 7711:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7712:             }
 7713:             if ($settings->{expire}) {
 7714:                 $expire = $settings->{expire};
 7715:             }
 7716:             if ($settings->{numsaved}) {
 7717:                 $numsaved = $settings->{numsaved};
 7718:             }
 7719:         }
 7720:         my %rulenames = &Apache::lonlocal::texthash(
 7721:                                                      uc => 'At least one upper case letter',
 7722:                                                      lc => 'At least one lower case letter',
 7723:                                                      num => 'At least one number',
 7724:                                                      spec => 'At least one non-alphanumeric',
 7725:                                                    );
 7726:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7727:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7728:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7729:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7730:                       'onblur="javascript:warnIntPass(this);" />'.
 7731:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7732:                       '</span></td></tr>';
 7733:         $itemcount ++;
 7734:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7735:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7736:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7737:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7738:                       'onblur="javascript:warnIntPass(this);" />'.
 7739:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7740:                       '</span></td></tr>';
 7741:         $itemcount ++;
 7742:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7743:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7744:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7745:                       '</span></td>';
 7746:         my $numinrow = 2;
 7747:         my @possrules = ('uc','lc','num','spec');
 7748:         $datatable .= '<td class="LC_left_item"><table>';
 7749:         for (my $i=0; $i<@possrules; $i++) {
 7750:             my ($rem,$checked);
 7751:             if ($chars{$possrules[$i]}) {
 7752:                 $checked = ' checked="checked"';
 7753:             }
 7754:             $rem = $i%($numinrow);
 7755:             if ($rem == 0) {
 7756:                 if ($i > 0) {
 7757:                     $datatable .= '</tr>';
 7758:                 }
 7759:                 $datatable .= '<tr>';
 7760:             }
 7761:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7762:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7763:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7764:         }
 7765:         my $rem = @possrules%($numinrow);
 7766:         my $colsleft = $numinrow - $rem;
 7767:         if ($colsleft > 1 ) {
 7768:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7769:                           '&nbsp;</td>';
 7770:         } elsif ($colsleft == 1) {
 7771:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7772:         }
 7773:         $datatable .='</table></td></tr>';
 7774:         $itemcount ++;
 7775:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7776:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7777:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7778:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7779:                       'onblur="javascript:warnIntPass(this);" />'.
 7780:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7781:                       '</span></td></tr>';
 7782:         $itemcount ++;
 7783:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7784:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7785:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7786:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7787:                       'onblur="javascript:warnIntPass(this);" />'. 
 7788:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7789:                       '</span></td></tr>';
 7790:     } else {
 7791:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7792:         my %ownerchg = (
 7793:                           by  => {},
 7794:                           for => {},
 7795:                        );
 7796:         my %ownertitles = &Apache::lonlocal::texthash (
 7797:                             by  => 'Course owner status(es) allowed',
 7798:                             for => 'Student status(es) allowed',
 7799:                           );
 7800:         if (ref($settings) eq 'HASH') {
 7801:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7802:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7803:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7804:                 }
 7805:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7806:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7807:                 }
 7808:             }
 7809:         }
 7810:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7811:         $datatable .= '<tr '.$css_class.'>'.
 7812:                       '<td>'.
 7813:                       &mt('Requirements').'<ul>'.
 7814:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7815:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7816:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7817:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7818:                       '</ul>'.
 7819:                       '</td>'.
 7820:                       '<td class="LC_left_item">';
 7821:         foreach my $item ('by','for') {
 7822:             $datatable .= '<fieldset style="display: inline-block;">'.
 7823:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7824:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7825:                 foreach my $type (@{$types}) {
 7826:                     my $checked;
 7827:                     if ($ownerchg{$item}{$type}) {
 7828:                         $checked = ' checked="checked"';
 7829:                     }
 7830:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7831:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7832:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7833:                                   '</span>&nbsp;&nbsp; ';
 7834:                 }
 7835:             }
 7836:             my $checked;
 7837:             if ($ownerchg{$item}{'default'}) {
 7838:                 $checked = ' checked="checked"';
 7839:             }
 7840:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7841:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7842:                           $othertitle.'</label></span></fieldset>';
 7843:         }
 7844:         $datatable .= '</td></tr>';
 7845:     }
 7846:     return $datatable;
 7847: }
 7848: 
 7849: sub print_wafproxy {
 7850:     my ($position,$dom,$settings,$rowtotal) = @_;
 7851:     my $css_class;
 7852:     my $itemcount = 0;
 7853:     my $datatable;
 7854:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7855:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7856:     my %lt = &wafproxy_titles();
 7857:     foreach my $server (sort(keys(%servers))) {
 7858:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7859:         next if ($serverhome eq '');
 7860:         my $serverdom;
 7861:         if ($serverhome ne $server) {
 7862:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7863:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7864:                 $othercontrol{$server} = $serverdom;
 7865:             }
 7866:         } else {
 7867:             $serverdom = &Apache::lonnet::host_domain($server);
 7868:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7869:             if ($serverdom ne $dom) {
 7870:                 $othercontrol{$server} = $serverdom;
 7871:             } else {
 7872:                 $setdom = 1;
 7873:                 if (ref($settings) eq 'HASH') {
 7874:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7875:                         $aliases{$dom} = $settings->{'alias'};
 7876:                         if ($aliases{$dom} ne '') {
 7877:                             $showdom = 1;
 7878:                         }
 7879:                     }
 7880:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7881:                         $saml{$dom} = $settings->{'saml'};
 7882:                     }
 7883:                 }
 7884:             }
 7885:         }
 7886:     }
 7887:     if ($setdom) {
 7888:         %{$values{$dom}} = ();
 7889:         if (ref($settings) eq 'HASH') {
 7890:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7891:                 $values{$dom}{$item} = $settings->{$item};
 7892:             }
 7893:         }
 7894:     }
 7895:     if (keys(%othercontrol)) {
 7896:         %otherdoms = reverse(%othercontrol);
 7897:         foreach my $domain (keys(%otherdoms)) {
 7898:             %{$values{$domain}} = ();
 7899:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7900:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7901:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7902:                 if (exists($config{'wafproxy'}{'saml'})) { 
 7903:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 7904:                 }
 7905:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7906:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7907:                 }
 7908:             }
 7909:         }
 7910:     }
 7911:     if ($position eq 'top') {
 7912:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7913:         my %aliasinfo;
 7914:         foreach my $server (sort(keys(%servers))) {
 7915:             $itemcount ++;
 7916:             my $dom_in_effect;
 7917:             my $aliasrows = '<tr>'.
 7918:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7919:                             &mt('Hostname').':&nbsp;'.
 7920:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 7921:             if ($othercontrol{$server}) {
 7922:                 $dom_in_effect = $othercontrol{$server};
 7923:                 my ($current,$forsaml);
 7924:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7925:                     $current = $aliases{$dom_in_effect}{$server};
 7926:                 }
 7927:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 7928:                     if ($saml{$dom_in_effect}{$server}) {
 7929:                         $forsaml = 1;
 7930:                     }
 7931:                 }
 7932:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7933:                               &mt('Alias').':&nbsp';
 7934:                 if ($current) {
 7935:                     $aliasrows .= $current;
 7936:                     if ($forsaml) {
 7937:                          $aliasrows .= '&nbsp;('.&mt('also for Shibboleth').')';
 7938:                     }
 7939:                 } else {
 7940:                     $aliasrows .= &mt('None');
 7941:                 }
 7942:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7943:                               &mt('controlled by domain: [_1]',
 7944:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7945:             } else {
 7946:                 $dom_in_effect = $dom;
 7947:                 my ($current,$samlon,$samloff);
 7948:                 $samloff = ' checked="checked"';
 7949:                 if (ref($aliases{$dom}) eq 'HASH') {
 7950:                     if ($aliases{$dom}{$server}) {
 7951:                         $current = $aliases{$dom}{$server};
 7952:                     }
 7953:                 }
 7954:                 if (ref($saml{$dom}) eq 'HASH') {
 7955:                     if ($saml{$dom}{$server}) {
 7956:                         $samlon = $samloff;
 7957:                         undef($samloff);
 7958:                     }
 7959:                 }
 7960:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7961:                               &mt('Alias').':&nbsp;'.
 7962:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7963:                               'value="'.$current.'" size="30" />'.
 7964:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 7965:                               &mt('Alias used for Shibboleth').':&nbsp;<label>'.
 7966:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 7967:                               &mt('No').'</label>&nbsp;<label>'. 
 7968:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 7969:                               &mt('Yes').'</label></span>'.
 7970:                               '</td>'; 
 7971:             }
 7972:             $aliasrows .= '</tr>';
 7973:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7974:         }
 7975:         if ($aliasinfo{$dom}) {
 7976:             my ($onclick,$wafon,$wafoff,$showtable);
 7977:             $onclick = ' onclick="javascript:toggleWAF();"';
 7978:             $wafoff = ' checked="checked"';
 7979:             $showtable = ' style="display:none";';
 7980:             if ($showdom) {
 7981:                 $wafon = $wafoff;
 7982:                 $wafoff = '';
 7983:                 $showtable = ' style="display:inline;"';
 7984:             }
 7985:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7986:             $datatable = '<tr'.$css_class.'>'.
 7987:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7988:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7989:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7990:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7991:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7992:                          &mt('No').'</label></span></td>'.
 7993:                          '<td class="LC_left_item">'.
 7994:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 7995:                          '</table></td></tr>';
 7996:             $itemcount++;
 7997:         }
 7998:         if (keys(%otherdoms)) {
 7999:             foreach my $key (sort(keys(%otherdoms))) {
 8000:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8001:                 $datatable .= '<tr'.$css_class.'>'.
 8002:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8003:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8004:                               '</table></td></tr>';
 8005:                 $itemcount++;
 8006:             }
 8007:         }
 8008:     } else {
 8009:         my %ip_methods = &remoteip_methods();
 8010:         if ($setdom) {
 8011:             $itemcount ++;
 8012:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8013:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8014:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8015:             $wafstyle = ' style="display:none;"';
 8016:             $nowafstyle = ' style="display:table-row;"';
 8017:             $currwafdisplay = ' style="display: none"';
 8018:             $wafrangestyle = ' style="display: none"';
 8019:             $curr_remotip = 'n';
 8020:             $ssltossl = ' checked="checked"';
 8021:             if ($showdom) {
 8022:                 $wafstyle = ' style="display:table-row;"';
 8023:                 $nowafstyle =  ' style="display:none;"';
 8024:                 if (keys(%{$values{$dom}})) {
 8025:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8026:                         $curr_remotip = $values{$dom}{remoteip};
 8027:                     }
 8028:                     if ($curr_remotip eq 'h') {
 8029:                         $currwafdisplay = ' style="display:table-row"';
 8030:                         $wafrangestyle = ' style="display:inline-block;"';
 8031:                     }
 8032:                     if ($values{$dom}{'sslopt'}) {
 8033:                         $alltossl = ' checked="checked"';
 8034:                         $ssltossl = '';
 8035:                     }
 8036:                 }
 8037:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8038:                     $vpndircheck = ' checked="checked"';
 8039:                     $currwafvpn = ' style="display:table-row;"';
 8040:                     $wafrangestyle = ' style="display:inline-block;"';
 8041:                 } else {
 8042:                     $vpnaliascheck = ' checked="checked"';
 8043:                     $currwafvpn = ' style="display:none;"';
 8044:                 }
 8045:             }
 8046:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8047:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8048:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8049:                           '</tr>'.
 8050:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8051:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8052:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8053:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8054:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8055:                           '<td class="LC_left_item"><table>'.
 8056:                           '<tr>'.
 8057:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8058:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8059:             foreach my $option ('m','h','n') {
 8060:                 my $sel;
 8061:                 if ($option eq $curr_remotip) {
 8062:                    $sel = ' selected="selected"';
 8063:                 }
 8064:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8065:                               $ip_methods{$option}.'</option>';
 8066:             }
 8067:             $datatable .= '</select></td></tr>'."\n".
 8068:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8069:                           $lt{'ipheader'}.':&nbsp;'.
 8070:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8071:                           'name="wafproxy_ipheader" />'.
 8072:                           '</td></tr>'."\n".
 8073:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8074:                           $lt{'trusted'}.':<br />'.
 8075:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8076:                           $values{$dom}{'trusted'}.'</textarea>'.
 8077:                           '</td></tr>'."\n".
 8078:                           '<tr><td><hr /></td></tr>'."\n".
 8079:                           '<tr>'.
 8080:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8081:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8082:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8083:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8084:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8085:             foreach my $item ('vpnint','vpnext') {
 8086:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8087:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8088:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8089:                               $values{$dom}{$item}.'</textarea>'.
 8090:                               '</td></tr>'."\n";
 8091:             }
 8092:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8093:                           '<tr>'.
 8094:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8095:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8096:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8097:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8098:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8099:                           '</table></td></tr>';
 8100:         }
 8101:         if (keys(%otherdoms)) {
 8102:             foreach my $domain (sort(keys(%otherdoms))) {
 8103:                 $itemcount ++;
 8104:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8105:                 $datatable .= '<tr'.$css_class.'>'.
 8106:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8107:                               '<td class="LC_left_item"><table>';
 8108:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8109:                     my $showval = &mt('None');
 8110:                     if ($item eq 'ssl') {
 8111:                         $showval = $lt{'ssltossl'};
 8112:                     }
 8113:                     if ($values{$domain}{$item}) {
 8114:                         $showval = $values{$domain}{$item};
 8115:                         if ($item eq 'ssl') {
 8116:                             $showval = $lt{'alltossl'};
 8117:                         } elsif ($item eq 'remoteip') {
 8118:                             $showval = $ip_methods{$values{$domain}{$item}};
 8119:                         }
 8120:                     }
 8121:                     $datatable .= '<tr>'.
 8122:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8123:                 }
 8124:                 $datatable .= '</table></td></tr>';
 8125:             }
 8126:         }
 8127:     }
 8128:     $$rowtotal += $itemcount;
 8129:     return $datatable;
 8130: }
 8131: 
 8132: sub wafproxy_titles {
 8133:     return &Apache::lonlocal::texthash(
 8134:                remoteip   => "Method for determining user's IP",
 8135:                ipheader   => 'Request header containing remote IP',
 8136:                trusted    => 'Trusted IP range(s)',
 8137:                vpnaccess  => 'Access from institutional VPN',
 8138:                vpndirect  => 'via regular hostname (no WAF)',
 8139:                vpnaliased => 'via aliased hostname (WAF)',
 8140:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8141:                vpnext     => 'IP Range(s) for backend WAF connections',
 8142:                sslopt     => 'Forwarding http/https',
 8143:                alltossl   => 'WAF forwards both http and https requests to https',
 8144:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8145:            );
 8146: }
 8147: 
 8148: sub remoteip_methods {
 8149:     return &Apache::lonlocal::texthash(
 8150:               m => 'Use Apache mod_remoteip',
 8151:               h => 'Use headers parsed by LON-CAPA',
 8152:               n => 'Not in use',
 8153:            );
 8154: }
 8155: 
 8156: sub print_usersessions {
 8157:     my ($position,$dom,$settings,$rowtotal) = @_;
 8158:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8159:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8160:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8161: 
 8162:     my @alldoms = &Apache::lonnet::all_domains();
 8163:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8164:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8165:     my %altids = &id_for_thisdom(%servers);
 8166:     if ($position eq 'top') {
 8167:         if (keys(%serverhomes) > 1) {
 8168:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8169:             my ($curroffloadnow,$curroffloadoth);
 8170:             if (ref($settings) eq 'HASH') {
 8171:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8172:                     $curroffloadnow = $settings->{'offloadnow'};
 8173:                 }
 8174:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8175:                     $curroffloadoth = $settings->{'offloadoth'};
 8176:                 }
 8177:             }
 8178:             my $other_insts = scalar(keys(%by_location));
 8179:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8180:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8181:         } else {
 8182:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8183:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8184:                           '</td></tr>';
 8185:         }
 8186:     } else {
 8187:         my %titles = &usersession_titles();
 8188:         my ($prefix,@types);
 8189:         if ($position eq 'bottom') {
 8190:             $prefix = 'remote';
 8191:             @types = ('version','excludedomain','includedomain');
 8192:         } else {
 8193:             $prefix = 'hosted';
 8194:             @types = ('excludedomain','includedomain');
 8195:         }
 8196:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8197:     }
 8198:     $$rowtotal += $itemcount;
 8199:     return $datatable;
 8200: }
 8201: 
 8202: sub rules_by_location {
 8203:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8204:     my ($datatable,$itemcount,$css_class);
 8205:     if (keys(%{$by_location}) == 0) {
 8206:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8207:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8208:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8209:                      '</td></tr>';
 8210:         $itemcount = 1;
 8211:     } else {
 8212:         $itemcount = 0;
 8213:         my $numinrow = 5;
 8214:         my (%current,%checkedon,%checkedoff);
 8215:         my @locations = sort(keys(%{$by_location}));
 8216:         foreach my $type (@{$types}) {
 8217:             $checkedon{$type} = '';
 8218:             $checkedoff{$type} = ' checked="checked"';
 8219:         }
 8220:         if (ref($settings) eq 'HASH') {
 8221:             if (ref($settings->{$prefix}) eq 'HASH') {
 8222:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8223:                     $current{$key} = $settings->{$prefix}{$key};
 8224:                     if ($key eq 'version') {
 8225:                         if ($current{$key} ne '') {
 8226:                             $checkedon{$key} = ' checked="checked"';
 8227:                             $checkedoff{$key} = '';
 8228:                         }
 8229:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8230:                         $checkedon{$key} = ' checked="checked"';
 8231:                         $checkedoff{$key} = '';
 8232:                     }
 8233:                 }
 8234:             }
 8235:         }
 8236:         foreach my $type (@{$types}) {
 8237:             next if ($type ne 'version' && !@locations);
 8238:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8239:             $datatable .= '<tr'.$css_class.'>
 8240:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8241:                            <span class="LC_nobreak">&nbsp;
 8242:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8243:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8244:             if ($type eq 'version') {
 8245:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8246:                 my $selector = '<select name="'.$prefix.'_version">';
 8247:                 foreach my $version (@lcversions) {
 8248:                     my $selected = '';
 8249:                     if ($current{'version'} eq $version) {
 8250:                         $selected = ' selected="selected"';
 8251:                     }
 8252:                     $selector .= ' <option value="'.$version.'"'.
 8253:                                  $selected.'>'.$version.'</option>';
 8254:                 }
 8255:                 $selector .= '</select> ';
 8256:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8257:             } else {
 8258:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8259:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8260:                              ' />'.('&nbsp;'x2).
 8261:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8262:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8263:                              "\n".
 8264:                              '</div><div><table>';
 8265:                 my $rem;
 8266:                 for (my $i=0; $i<@locations; $i++) {
 8267:                     my ($showloc,$value,$checkedtype);
 8268:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8269:                         my $ip = $by_location->{$locations[$i]}->[0];
 8270:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8271:                             $value = join(':',@{$by_ip->{$ip}});
 8272:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8273:                             if (ref($current{$type}) eq 'ARRAY') {
 8274:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8275:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8276:                                         $checkedtype = ' checked="checked"';
 8277:                                         last;
 8278:                                     }
 8279:                                 }
 8280:                             }
 8281:                         }
 8282:                     }
 8283:                     $rem = $i%($numinrow);
 8284:                     if ($rem == 0) {
 8285:                         if ($i > 0) {
 8286:                             $datatable .= '</tr>';
 8287:                         }
 8288:                         $datatable .= '<tr>';
 8289:                     }
 8290:                     $datatable .= '<td class="LC_left_item">'.
 8291:                                   '<span class="LC_nobreak"><label>'.
 8292:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8293:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8294:                                   '</label></span></td>';
 8295:                 }
 8296:                 $rem = @locations%($numinrow);
 8297:                 my $colsleft = $numinrow - $rem;
 8298:                 if ($colsleft > 1 ) {
 8299:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8300:                                   '&nbsp;</td>';
 8301:                 } elsif ($colsleft == 1) {
 8302:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8303:                 }
 8304:                 $datatable .= '</tr></table>';
 8305:             }
 8306:             $datatable .= '</td></tr>';
 8307:             $itemcount ++;
 8308:         }
 8309:     }
 8310:     return ($datatable,$itemcount);
 8311: }
 8312: 
 8313: sub print_ssl {
 8314:     my ($position,$dom,$settings,$rowtotal) = @_;
 8315:     my ($css_class,$datatable);
 8316:     my $itemcount = 1;
 8317:     if ($position eq 'top') {
 8318:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8319:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8320:         my $same_institution;
 8321:         if ($intdom ne '') {
 8322:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8323:             if (ref($internet_names) eq 'ARRAY') {
 8324:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8325:                     $same_institution = 1;
 8326:                 }
 8327:             }
 8328:         }
 8329:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8330:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8331:         if ($same_institution) {
 8332:             my %domservers = &Apache::lonnet::get_servers($dom);
 8333:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8334:         } else {
 8335:             $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.");
 8336:         }
 8337:         $datatable .= '</td></tr>';
 8338:         $itemcount ++;
 8339:     } else {
 8340:         my %titles = &ssl_titles();
 8341:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8342:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8343:         my @alldoms = &Apache::lonnet::all_domains();
 8344:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8345:         my @domservers = &Apache::lonnet::get_servers($dom);
 8346:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8347:         my %altids = &id_for_thisdom(%servers);
 8348:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8349:             my $legacy;
 8350:             unless (ref($settings) eq 'HASH') {
 8351:                 my $name;
 8352:                 if ($position eq 'connto') {
 8353:                     $name = 'loncAllowInsecure';
 8354:                 } else {
 8355:                     $name = 'londAllowInsecure';
 8356:                 }
 8357:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8358:                 my @ids=&Apache::lonnet::current_machine_ids();
 8359:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8360:                     my %what = (
 8361:                                    $name => 1,
 8362:                                );
 8363:                     my ($result,$returnhash) =
 8364:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8365:                     if ($result eq 'ok') {
 8366:                         if (ref($returnhash) eq 'HASH') {
 8367:                             $legacy = $returnhash->{$name};
 8368:                         }
 8369:                     }
 8370:                 } else {
 8371:                     $legacy = $Apache::lonnet::perlvar{$name};
 8372:                 }
 8373:             }
 8374:             foreach my $type ('dom','intdom','other') {
 8375:                 my %checked;
 8376:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8377:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8378:                               '<td class="LC_right_item">';
 8379:                 my $skip; 
 8380:                 if ($type eq 'dom') {
 8381:                     unless (keys(%servers) > 1) {
 8382:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8383:                         $skip = 1;
 8384:                     }
 8385:                 }
 8386:                 if ($type eq 'intdom') {
 8387:                     unless (@instdoms > 1) {
 8388:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8389:                         $skip = 1;
 8390:                     } 
 8391:                 } elsif ($type eq 'other') {
 8392:                     if (keys(%by_location) == 0) {
 8393:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8394:                         $skip = 1;
 8395:                     }
 8396:                 }
 8397:                 unless ($skip) {
 8398:                     $checked{'yes'} = ' checked="checked"'; 
 8399:                     if (ref($settings) eq 'HASH') {
 8400:                         if (ref($settings->{$position}) eq 'HASH') {
 8401:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8402:                                 $checked{$1} = $checked{'yes'};
 8403:                                 delete($checked{'yes'}); 
 8404:                             }
 8405:                         }
 8406:                     } else {
 8407:                         if ($legacy == 0) {
 8408:                             $checked{'req'} = $checked{'yes'};
 8409:                             delete($checked{'yes'});    
 8410:                         }
 8411:                     }
 8412:                     foreach my $option ('no','yes','req') {
 8413:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8414:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8415:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8416:                                       '</label></span>'.('&nbsp;'x2);
 8417:                     }
 8418:                 }
 8419:                 $datatable .= '</td></tr>';
 8420:                 $itemcount ++; 
 8421:             }
 8422:         } else {
 8423:             my $prefix = 'replication';
 8424:             my @types = ('certreq','nocertreq');
 8425:             if (keys(%by_location) == 0) {
 8426:                 $datatable .= '<tr'.$css_class.'><td>'.
 8427:                               &mt('Nothing to set here, as there are no other institutions').
 8428:                               '</td></tr>';
 8429:                 $itemcount ++;
 8430:             } else {
 8431:                 ($datatable,$itemcount) = 
 8432:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8433:             }
 8434:         }
 8435:     }
 8436:     $$rowtotal += $itemcount;
 8437:     return $datatable;
 8438: }
 8439: 
 8440: sub ssl_titles {
 8441:     return &Apache::lonlocal::texthash (
 8442:                dom           => 'LON-CAPA servers/VMs from same domain',
 8443:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8444:                other         => 'External LON-CAPA servers/VMs',
 8445:                connto        => 'Connections to other servers',
 8446:                connfrom      => 'Connections from other servers',
 8447:                replication   => 'Replicating content to other institutions',
 8448:                certreq       => 'Client certificate required, but specific domains exempt',
 8449:                nocertreq     => 'No client certificate required, except for specific domains',
 8450:                no            => 'SSL not used',
 8451:                yes           => 'SSL Optional (used if available)',
 8452:                req           => 'SSL Required',
 8453:     );
 8454: }
 8455: 
 8456: sub print_trust {
 8457:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8458:     my ($css_class,$datatable,%checked,%choices);
 8459:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8460:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8461:     my $itemcount = 1;
 8462:     my %titles = &trust_titles();
 8463:     my @types = ('exc','inc');
 8464:     if ($prefix eq 'top') {
 8465:         $prefix = 'content';
 8466:     } elsif ($prefix eq 'bottom') {
 8467:         $prefix = 'msg';
 8468:     }
 8469:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8470:     $$rowtotal += $itemcount;
 8471:     return $datatable;
 8472: }
 8473: 
 8474: sub trust_titles {
 8475:     return &Apache::lonlocal::texthash(
 8476:                content  => "Access to this domain's content by others",
 8477:                shared   => "Access to other domain's content by this domain",
 8478:                enroll   => "Enrollment in this domain's courses by others", 
 8479:                othcoau  => "Co-author roles in this domain for others",
 8480:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8481:                domroles => "Domain roles in this domain assignable to others",
 8482:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8483:                reqcrs   => "Requests for creation of courses in this domain by others",
 8484:                msg      => "Users in other domains can send messages to this domain",
 8485:                exc      => "Allow all, but exclude specific domains",
 8486:                inc      => "Deny all, but include specific domains",
 8487:            );
 8488: } 
 8489: 
 8490: sub build_location_hashes {
 8491:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8492:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8493:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8494:     my %iphost = &Apache::lonnet::get_iphost();
 8495:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8496:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8497:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8498:         foreach my $id (@{$iphost{$primary_ip}}) {
 8499:             my $intdom = &Apache::lonnet::internet_dom($id);
 8500:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8501:                 push(@{$intdoms},$intdom);
 8502:             }
 8503:         }
 8504:     }
 8505:     foreach my $ip (keys(%iphost)) {
 8506:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8507:             foreach my $id (@{$iphost{$ip}}) {
 8508:                 my $location = &Apache::lonnet::internet_dom($id);
 8509:                 if ($location) {
 8510:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8511:                         my $dom = &Apache::lonnet::host_domain($id);
 8512:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8513:                             push(@{$instdoms},$dom);
 8514:                         }
 8515:                         next;
 8516:                     }
 8517:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8518:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8519:                             push(@{$by_ip->{$ip}},$location);
 8520:                         }
 8521:                     } else {
 8522:                         $by_ip->{$ip} = [$location];
 8523:                     }
 8524:                 }
 8525:             }
 8526:         }
 8527:     }
 8528:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8529:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8530:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8531:             my $first = $by_ip->{$ip}->[0];
 8532:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8533:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8534:                     push(@{$by_location->{$first}},$ip);
 8535:                 }
 8536:             } else {
 8537:                 $by_location->{$first} = [$ip];
 8538:             }
 8539:         }
 8540:     }
 8541:     return;
 8542: }
 8543: 
 8544: sub current_offloads_to {
 8545:     my ($dom,$settings,$servers) = @_;
 8546:     my (%spareid,%otherdomconfigs);
 8547:     if (ref($servers) eq 'HASH') {
 8548:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8549:             my $gotspares;
 8550:             if (ref($settings) eq 'HASH') {
 8551:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8552:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8553:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8554:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8555:                         $gotspares = 1;
 8556:                     }
 8557:                 }
 8558:             }
 8559:             unless ($gotspares) {
 8560:                 my $gotspares;
 8561:                 my $serverhomeID =
 8562:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8563:                 my $serverhomedom =
 8564:                     &Apache::lonnet::host_domain($serverhomeID);
 8565:                 if ($serverhomedom ne $dom) {
 8566:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8567:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8568:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8569:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8570:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8571:                                 $gotspares = 1;
 8572:                             }
 8573:                         }
 8574:                     } else {
 8575:                         $otherdomconfigs{$serverhomedom} =
 8576:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8577:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8578:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8579:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8580:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8581:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8582:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8583:                                         $gotspares = 1;
 8584:                                     }
 8585:                                 }
 8586:                             }
 8587:                         }
 8588:                     }
 8589:                 }
 8590:             }
 8591:             unless ($gotspares) {
 8592:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8593:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8594:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8595:                } else {
 8596:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8597:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8598:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8599:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8600:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8601:                     } else {
 8602:                         my %what = (
 8603:                              spareid => 1,
 8604:                         );
 8605:                         my ($result,$returnhash) = 
 8606:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8607:                         if ($result eq 'ok') { 
 8608:                             if (ref($returnhash) eq 'HASH') {
 8609:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8610:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8611:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8612:                                 }
 8613:                             }
 8614:                         }
 8615:                     }
 8616:                 }
 8617:             }
 8618:         }
 8619:     }
 8620:     return %spareid;
 8621: }
 8622: 
 8623: sub spares_row {
 8624:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8625:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8626:     my $css_class;
 8627:     my $numinrow = 4;
 8628:     my $itemcount = 1;
 8629:     my $datatable;
 8630:     my %typetitles = &sparestype_titles();
 8631:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8632:         foreach my $server (sort(keys(%{$servers}))) {
 8633:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8634:             my ($othercontrol,$serverdom);
 8635:             if ($serverhome ne $server) {
 8636:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8637:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8638:             } else {
 8639:                 $serverdom = &Apache::lonnet::host_domain($server);
 8640:                 if ($serverdom ne $dom) {
 8641:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8642:                 }
 8643:             }
 8644:             next unless (ref($spareid->{$server}) eq 'HASH');
 8645:             my ($checkednow,$checkedoth);
 8646:             if (ref($curroffloadnow) eq 'HASH') {
 8647:                 if ($curroffloadnow->{$server}) {
 8648:                     $checkednow = ' checked="checked"';
 8649:                 }
 8650:             }
 8651:             if (ref($curroffloadoth) eq 'HASH') {
 8652:                 if ($curroffloadoth->{$server}) {
 8653:                     $checkedoth = ' checked="checked"';
 8654:                 }
 8655:             }
 8656:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8657:             $datatable .= '<tr'.$css_class.'>
 8658:                            <td rowspan="2">
 8659:                             <span class="LC_nobreak">'.
 8660:                           &mt('[_1] when busy, offloads to:'
 8661:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8662:                           '<span class="LC_nobreak">'."\n".
 8663:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8664:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8665:                           "\n";
 8666:             if ($other_insts) {
 8667:                 $datatable .= '<br />'.
 8668:                               '<span class="LC_nobreak">'."\n".
 8669:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8670:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8671:                           "\n";
 8672:             }
 8673:             my (%current,%canselect);
 8674:             my @choices = 
 8675:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8676:             foreach my $type ('primary','default') {
 8677:                 if (ref($spareid->{$server}) eq 'HASH') {
 8678:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8679:                         my @spares = @{$spareid->{$server}{$type}};
 8680:                         if (@spares > 0) {
 8681:                             if ($othercontrol) {
 8682:                                 $current{$type} = join(', ',@spares);
 8683:                             } else {
 8684:                                 $current{$type} .= '<table>';
 8685:                                 my $numspares = scalar(@spares);
 8686:                                 for (my $i=0;  $i<@spares; $i++) {
 8687:                                     my $rem = $i%($numinrow);
 8688:                                     if ($rem == 0) {
 8689:                                         if ($i > 0) {
 8690:                                             $current{$type} .= '</tr>';
 8691:                                         }
 8692:                                         $current{$type} .= '<tr>';
 8693:                                     }
 8694:                                     $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;'.
 8695:                                                        $spareid->{$server}{$type}[$i].
 8696:                                                        '</label></td>'."\n";
 8697:                                 }
 8698:                                 my $rem = @spares%($numinrow);
 8699:                                 my $colsleft = $numinrow - $rem;
 8700:                                 if ($colsleft > 1 ) {
 8701:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8702:                                                        '" class="LC_left_item">'.
 8703:                                                        '&nbsp;</td>';
 8704:                                 } elsif ($colsleft == 1) {
 8705:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8706:                                 }
 8707:                                 $current{$type} .= '</tr></table>';
 8708:                             }
 8709:                         }
 8710:                     }
 8711:                     if ($current{$type} eq '') {
 8712:                         $current{$type} = &mt('None specified');
 8713:                     }
 8714:                     if ($othercontrol) {
 8715:                         if ($type eq 'primary') {
 8716:                             $canselect{$type} = $othercontrol;
 8717:                         }
 8718:                     } else {
 8719:                         $canselect{$type} = 
 8720:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8721:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8722:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8723:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8724:                         if (@choices > 0) {
 8725:                             foreach my $lonhost (@choices) {
 8726:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8727:                             }
 8728:                         }
 8729:                         $canselect{$type} .= '</select>'."\n";
 8730:                     }
 8731:                 } else {
 8732:                     $current{$type} = &mt('Could not be determined');
 8733:                     if ($type eq 'primary') {
 8734:                         $canselect{$type} =  $othercontrol;
 8735:                     }
 8736:                 }
 8737:                 if ($type eq 'default') {
 8738:                     $datatable .= '<tr'.$css_class.'>';
 8739:                 }
 8740:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8741:                               '<td>'.$current{$type}.'</td>'."\n".
 8742:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8743:             }
 8744:             $itemcount ++;
 8745:         }
 8746:     }
 8747:     $$rowtotal += $itemcount;
 8748:     return $datatable;
 8749: }
 8750: 
 8751: sub possible_newspares {
 8752:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8753:     my $serverhostname = &Apache::lonnet::hostname($server);
 8754:     my %excluded;
 8755:     if ($serverhostname ne '') {
 8756:         %excluded = (
 8757:                        $serverhostname => 1,
 8758:                     );
 8759:     }
 8760:     if (ref($currspares) eq 'HASH') {
 8761:         foreach my $type (keys(%{$currspares})) {
 8762:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8763:                 if (@{$currspares->{$type}} > 0) {
 8764:                     foreach my $curr (@{$currspares->{$type}}) {
 8765:                         my $hostname = &Apache::lonnet::hostname($curr);
 8766:                         $excluded{$hostname} = 1;
 8767:                     }
 8768:                 }
 8769:             }
 8770:         }
 8771:     }
 8772:     my @choices;
 8773:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8774:         if (keys(%{$serverhomes}) > 1) {
 8775:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8776:                 unless ($excluded{$name}) {
 8777:                     if (exists($altids->{$serverhomes->{$name}})) {
 8778:                         push(@choices,$altids->{$serverhomes->{$name}});
 8779:                     } else {
 8780:                         push(@choices,$serverhomes->{$name});
 8781:                     }
 8782:                 }
 8783:             }
 8784:         }
 8785:     }
 8786:     return sort(@choices);
 8787: }
 8788: 
 8789: sub print_loadbalancing {
 8790:     my ($dom,$settings,$rowtotal) = @_;
 8791:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8792:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8793:     my $numinrow = 1;
 8794:     my $datatable;
 8795:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8796:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8797:     if (ref($settings) eq 'HASH') {
 8798:         %existing = %{$settings};
 8799:     }
 8800:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8801:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8802:                                   \%currtargets,\%currrules,\%currcookies);
 8803:     } else {
 8804:         return;
 8805:     }
 8806:     my ($othertitle,$usertypes,$types) =
 8807:         &Apache::loncommon::sorted_inst_types($dom);
 8808:     my $rownum = 8;
 8809:     if (ref($types) eq 'ARRAY') {
 8810:         $rownum += scalar(@{$types});
 8811:     }
 8812:     my @css_class = ('LC_odd_row','LC_even_row');
 8813:     my $balnum = 0;
 8814:     my $islast;
 8815:     my (@toshow,$disabledtext);
 8816:     if (keys(%currbalancer) > 0) {
 8817:         @toshow = sort(keys(%currbalancer));
 8818:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8819:             push(@toshow,'');
 8820:         }
 8821:     } else {
 8822:         @toshow = ('');
 8823:         $disabledtext = &mt('No existing load balancer');
 8824:     }
 8825:     foreach my $lonhost (@toshow) {
 8826:         if ($balnum == scalar(@toshow)-1) {
 8827:             $islast = 1;
 8828:         } else {
 8829:             $islast = 0;
 8830:         }
 8831:         my $cssidx = $balnum%2;
 8832:         my $targets_div_style = 'display: none';
 8833:         my $disabled_div_style = 'display: block';
 8834:         my $homedom_div_style = 'display: none';
 8835:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8836:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8837:                       '<p>';
 8838:         if ($lonhost eq '') {
 8839:             $datatable .= '<span class="LC_nobreak">';
 8840:             if (keys(%currbalancer) > 0) {
 8841:                 $datatable .= &mt('Add balancer:');
 8842:             } else {
 8843:                 $datatable .= &mt('Enable balancer:');
 8844:             }
 8845:             $datatable .= '&nbsp;'.
 8846:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8847:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8848:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8849:                           '<option value="" selected="selected">'.&mt('None').
 8850:                           '</option>'."\n";
 8851:             foreach my $server (sort(keys(%servers))) {
 8852:                 next if ($currbalancer{$server});
 8853:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8854:             }
 8855:             $datatable .=
 8856:                 '</select>'."\n".
 8857:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8858:         } else {
 8859:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8860:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8861:                            &mt('Stop balancing').'</label>'.
 8862:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8863:             $targets_div_style = 'display: block';
 8864:             $disabled_div_style = 'display: none';
 8865:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8866:                 $homedom_div_style = 'display: block';
 8867:             }
 8868:         }
 8869:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8870:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8871:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8872:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8873:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8874:         my @sparestypes = ('primary','default');
 8875:         my %typetitles = &sparestype_titles();
 8876:         my %hostherechecked = (
 8877:                                   no => ' checked="checked"',
 8878:                               );
 8879:         my %balcookiechecked = (
 8880:                                   no => ' checked="checked"', 
 8881:                                );
 8882:         foreach my $sparetype (@sparestypes) {
 8883:             my $targettable;
 8884:             for (my $i=0; $i<$numspares; $i++) {
 8885:                 my $checked;
 8886:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8887:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8888:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8889:                             $checked = ' checked="checked"';
 8890:                         }
 8891:                     }
 8892:                 }
 8893:                 my ($chkboxval,$disabled);
 8894:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8895:                     $chkboxval = $spares[$i];
 8896:                 }
 8897:                 if (exists($currbalancer{$spares[$i]})) {
 8898:                     $disabled = ' disabled="disabled"';
 8899:                 }
 8900:                 $targettable .=
 8901:                     '<td><span class="LC_nobreak"><label>'.
 8902:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8903:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8904:                     '</span></label></span></td>';
 8905:                 my $rem = $i%($numinrow);
 8906:                 if ($rem == 0) {
 8907:                     if (($i > 0) && ($i < $numspares-1)) {
 8908:                         $targettable .= '</tr>';
 8909:                     }
 8910:                     if ($i < $numspares-1) {
 8911:                         $targettable .= '<tr>';
 8912:                     }
 8913:                 }
 8914:             }
 8915:             if ($targettable ne '') {
 8916:                 my $rem = $numspares%($numinrow);
 8917:                 my $colsleft = $numinrow - $rem;
 8918:                 if ($colsleft > 1 ) {
 8919:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8920:                                     '&nbsp;</td>';
 8921:                 } elsif ($colsleft == 1) {
 8922:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8923:                 }
 8924:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8925:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8926:             }
 8927:             $hostherechecked{$sparetype} = '';
 8928:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8929:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8930:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8931:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8932:                         $hostherechecked{'no'} = '';
 8933:                     }
 8934:                 }
 8935:             }
 8936:         }
 8937:         if ($currcookies{$lonhost}) {
 8938:             %balcookiechecked = (
 8939:                                     yes => ' checked="checked"',
 8940:                                 );
 8941:         }
 8942:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8943:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8944:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8945:         foreach my $sparetype (@sparestypes) {
 8946:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8947:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8948:                           '</i></label><br />';
 8949:         }
 8950:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8951:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8952:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8953:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8954:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8955:                       '</div></td></tr>'.
 8956:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8957:                                            $othertitle,$usertypes,$types,\%servers,
 8958:                                            \%currbalancer,$lonhost,
 8959:                                            $targets_div_style,$homedom_div_style,
 8960:                                            $css_class[$cssidx],$balnum,$islast);
 8961:         $$rowtotal += $rownum;
 8962:         $balnum ++;
 8963:     }
 8964:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8965:     return $datatable;
 8966: }
 8967: 
 8968: sub get_loadbalancers_config {
 8969:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8970:     return unless ((ref($servers) eq 'HASH') &&
 8971:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8972:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8973:                    (ref($currcookies) eq 'HASH'));
 8974:     if (keys(%{$existing}) > 0) {
 8975:         my $oldlonhost;
 8976:         foreach my $key (sort(keys(%{$existing}))) {
 8977:             if ($key eq 'lonhost') {
 8978:                 $oldlonhost = $existing->{'lonhost'};
 8979:                 $currbalancer->{$oldlonhost} = 1;
 8980:             } elsif ($key eq 'targets') {
 8981:                 if ($oldlonhost) {
 8982:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8983:                 }
 8984:             } elsif ($key eq 'rules') {
 8985:                 if ($oldlonhost) {
 8986:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8987:                 }
 8988:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8989:                 $currbalancer->{$key} = 1;
 8990:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8991:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8992:                 if ($existing->{$key}{'cookie'}) {
 8993:                     $currcookies->{$key} = 1;
 8994:                 }
 8995:             }
 8996:         }
 8997:     } else {
 8998:         my ($balancerref,$targetsref) =
 8999:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9000:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9001:             foreach my $server (sort(keys(%{$balancerref}))) {
 9002:                 $currbalancer->{$server} = 1;
 9003:                 $currtargets->{$server} = $targetsref->{$server};
 9004:             }
 9005:         }
 9006:     }
 9007:     return;
 9008: }
 9009: 
 9010: sub loadbalancing_rules {
 9011:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9012:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9013:         $css_class,$balnum,$islast) = @_;
 9014:     my $output;
 9015:     my $num = 0;
 9016:     my ($alltypes,$othertypes,$titles) =
 9017:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9018:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9019:         foreach my $type (@{$alltypes}) {
 9020:             $num ++;
 9021:             my $current;
 9022:             if (ref($currrules) eq 'HASH') {
 9023:                 $current = $currrules->{$type};
 9024:             }
 9025:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9026:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9027:                     $current = '';
 9028:                 }
 9029:             }
 9030:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9031:                                              $servers,$currbalancer,$lonhost,$dom,
 9032:                                              $targets_div_style,$homedom_div_style,
 9033:                                              $css_class,$balnum,$num,$islast);
 9034:         }
 9035:     }
 9036:     return $output;
 9037: }
 9038: 
 9039: sub loadbalancing_titles {
 9040:     my ($dom,$intdom,$usertypes,$types) = @_;
 9041:     my %othertypes = (
 9042:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9043:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9044:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9045:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9046:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9047:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9048:                      );
 9049:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9050:     my @available;
 9051:     if (ref($types) eq 'ARRAY') {
 9052:         @available = @{$types};
 9053:     }
 9054:     unless (grep(/^default$/,@available)) {
 9055:         push(@available,'default');
 9056:     }
 9057:     unshift(@alltypes,@available);
 9058:     my %titles;
 9059:     foreach my $type (@alltypes) {
 9060:         if ($type =~ /^_LC_/) {
 9061:             $titles{$type} = $othertypes{$type};
 9062:         } elsif ($type eq 'default') {
 9063:             $titles{$type} = &mt('All users from [_1]',$dom);
 9064:             if (ref($types) eq 'ARRAY') {
 9065:                 if (@{$types} > 0) {
 9066:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9067:                 }
 9068:             }
 9069:         } elsif (ref($usertypes) eq 'HASH') {
 9070:             $titles{$type} = $usertypes->{$type};
 9071:         }
 9072:     }
 9073:     return (\@alltypes,\%othertypes,\%titles);
 9074: }
 9075: 
 9076: sub loadbalance_rule_row {
 9077:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9078:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9079:     my @rulenames;
 9080:     my %ruletitles = &offloadtype_text();
 9081:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9082:         @rulenames = ('balancer','offloadedto','specific');
 9083:     } else {
 9084:         @rulenames = ('default','homeserver');
 9085:         if ($type eq '_LC_external') {
 9086:             push(@rulenames,'externalbalancer');
 9087:         } else {
 9088:             push(@rulenames,'specific');
 9089:         }
 9090:         push(@rulenames,'none');
 9091:     }
 9092:     my $style = $targets_div_style;
 9093:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9094:         $style = $homedom_div_style;
 9095:     }
 9096:     my $space;
 9097:     if ($islast && $num == 1) {
 9098:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9099:     }
 9100:     my $output =
 9101:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9102:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9103:         '<td valaign="top">'.$space.
 9104:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9105:     for (my $i=0; $i<@rulenames; $i++) {
 9106:         my $rule = $rulenames[$i];
 9107:         my ($checked,$extra);
 9108:         if ($rulenames[$i] eq 'default') {
 9109:             $rule = '';
 9110:         }
 9111:         if ($rulenames[$i] eq 'specific') {
 9112:             if (ref($servers) eq 'HASH') {
 9113:                 my $default;
 9114:                 if (($current ne '') && (exists($servers->{$current}))) {
 9115:                     $checked = ' checked="checked"';
 9116:                 }
 9117:                 unless ($checked) {
 9118:                     $default = ' selected="selected"';
 9119:                 }
 9120:                 $extra =
 9121:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9122:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9123:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9124:                     '<option value=""'.$default.'></option>'."\n";
 9125:                 foreach my $server (sort(keys(%{$servers}))) {
 9126:                     if (ref($currbalancer) eq 'HASH') {
 9127:                         next if (exists($currbalancer->{$server}));
 9128:                     }
 9129:                     my $selected;
 9130:                     if ($server eq $current) {
 9131:                         $selected = ' selected="selected"';
 9132:                     }
 9133:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9134:                 }
 9135:                 $extra .= '</select>';
 9136:             }
 9137:         } elsif ($rule eq $current) {
 9138:             $checked = ' checked="checked"';
 9139:         }
 9140:         $output .= '<span class="LC_nobreak"><label>'.
 9141:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9142:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9143:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9144:                    ')"'.$checked.' />&nbsp;';
 9145:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9146:             $output .= $ruletitles{'particular'};
 9147:         } else {
 9148:             $output .= $ruletitles{$rulenames[$i]};
 9149:         }
 9150:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9151:     }
 9152:     $output .= '</div></td></tr>'."\n";
 9153:     return $output;
 9154: }
 9155: 
 9156: sub offloadtype_text {
 9157:     my %ruletitles = &Apache::lonlocal::texthash (
 9158:            'default'          => 'Offloads to default destinations',
 9159:            'homeserver'       => "Offloads to user's home server",
 9160:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9161:            'specific'         => 'Offloads to specific server',
 9162:            'none'             => 'No offload',
 9163:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9164:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9165:            'particular'       => 'Session hosted (after re-auth) on server:',
 9166:     );
 9167:     return %ruletitles;
 9168: }
 9169: 
 9170: sub sparestype_titles {
 9171:     my %typestitles = &Apache::lonlocal::texthash (
 9172:                           'primary' => 'primary',
 9173:                           'default' => 'default',
 9174:                       );
 9175:     return %typestitles;
 9176: }
 9177: 
 9178: sub contact_titles {
 9179:     my %titles = &Apache::lonlocal::texthash (
 9180:                    'supportemail'    => 'Support E-mail address',
 9181:                    'adminemail'      => 'Default Server Admin E-mail address',
 9182:                    'errormail'       => 'Error reports to be e-mailed to',
 9183:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9184:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9185:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9186:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9187:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9188:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9189:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9190:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9191:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9192:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9193:                    'errorweights'    => 'Weights used to compute error count',
 9194:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9195:                  );
 9196:     my %short_titles = &Apache::lonlocal::texthash (
 9197:                            adminemail   => 'Admin E-mail address',
 9198:                            supportemail => 'Support E-mail',
 9199:                        );   
 9200:     return (\%titles,\%short_titles);
 9201: }
 9202: 
 9203: sub helpform_fields {
 9204:     my %titles =  &Apache::lonlocal::texthash (
 9205:                        'username'   => 'Name',
 9206:                        'user'       => 'Username/domain',
 9207:                        'phone'      => 'Phone',
 9208:                        'cc'         => 'Cc e-mail',
 9209:                        'course'     => 'Course Details',
 9210:                        'section'    => 'Sections',
 9211:                        'screenshot' => 'File upload',
 9212:     );
 9213:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9214:     my %possoptions = (
 9215:                         username     => ['yes','no','req'],
 9216:                         phone        => ['yes','no','req'],
 9217:                         user         => ['yes','no'],
 9218:                         cc           => ['yes','no'],
 9219:                         course       => ['yes','no'],
 9220:                         section      => ['yes','no'],
 9221:                         screenshot   => ['yes','no'],
 9222:                       );
 9223:     my %fieldoptions = &Apache::lonlocal::texthash (
 9224:                          'yes'  => 'Optional',
 9225:                          'req'  => 'Required',
 9226:                          'no'   => "Not shown",
 9227:     );
 9228:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9229: }
 9230: 
 9231: sub tool_titles {
 9232:     my %titles = &Apache::lonlocal::texthash (
 9233:                      aboutme    => 'Personal web page',
 9234:                      blog       => 'Blog',
 9235:                      webdav     => 'WebDAV',
 9236:                      portfolio  => 'Portfolio',
 9237:                      official   => 'Official courses (with institutional codes)',
 9238:                      unofficial => 'Unofficial courses',
 9239:                      community  => 'Communities',
 9240:                      textbook   => 'Textbook courses',
 9241:                      placement  => 'Placement tests',
 9242:                  );
 9243:     return %titles;
 9244: }
 9245: 
 9246: sub courserequest_titles {
 9247:     my %titles = &Apache::lonlocal::texthash (
 9248:                                    official   => 'Official',
 9249:                                    unofficial => 'Unofficial',
 9250:                                    community  => 'Communities',
 9251:                                    textbook   => 'Textbook',
 9252:                                    placement  => 'Placement tests',
 9253:                                    lti        => 'LTI Provider',
 9254:                                    norequest  => 'Not allowed',
 9255:                                    approval   => 'Approval by DC',
 9256:                                    validate   => 'With validation',
 9257:                                    autolimit  => 'Numerical limit',
 9258:                                    unlimited  => '(blank for unlimited)',
 9259:                  );
 9260:     return %titles;
 9261: }
 9262: 
 9263: sub authorrequest_titles {
 9264:     my %titles = &Apache::lonlocal::texthash (
 9265:                                    norequest  => 'Not allowed',
 9266:                                    approval   => 'Approval by Dom. Coord.',
 9267:                                    automatic  => 'Automatic approval',
 9268:                  );
 9269:     return %titles;
 9270: }
 9271: 
 9272: sub courserequest_conditions {
 9273:     my %conditions = &Apache::lonlocal::texthash (
 9274:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9275:        validate   => '(Processing of request subject to institutional validation).',
 9276:                  );
 9277:     return %conditions;
 9278: }
 9279: 
 9280: 
 9281: sub print_usercreation {
 9282:     my ($position,$dom,$settings,$rowtotal) = @_;
 9283:     my $numinrow = 4;
 9284:     my $datatable;
 9285:     if ($position eq 'top') {
 9286:         $$rowtotal ++;
 9287:         my $rowcount = 0;
 9288:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9289:         if (ref($rules) eq 'HASH') {
 9290:             if (keys(%{$rules}) > 0) {
 9291:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9292:                                                 $ruleorder,$numinrow,$rowcount);
 9293:                 $$rowtotal ++;
 9294:                 $rowcount ++;
 9295:             }
 9296:         }
 9297:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9298:         if (ref($idrules) eq 'HASH') {
 9299:             if (keys(%{$idrules}) > 0) {
 9300:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9301:                                                 $idruleorder,$numinrow,$rowcount);
 9302:                 $$rowtotal ++;
 9303:                 $rowcount ++;
 9304:             }
 9305:         }
 9306:         if ($rowcount == 0) {
 9307:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9308:             $$rowtotal ++;
 9309:             $rowcount ++;
 9310:         }
 9311:     } elsif ($position eq 'middle') {
 9312:         my @creators = ('author','course','requestcrs');
 9313:         my ($rules,$ruleorder) =
 9314:             &Apache::lonnet::inst_userrules($dom,'username');
 9315:         my %lt = &usercreation_types();
 9316:         my %checked;
 9317:         if (ref($settings) eq 'HASH') {
 9318:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9319:                 foreach my $item (@creators) {
 9320:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9321:                 }
 9322:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9323:                 foreach my $item (@creators) {
 9324:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9325:                         $checked{$item} = 'none';
 9326:                     }
 9327:                 }
 9328:             }
 9329:         }
 9330:         my $rownum = 0;
 9331:         foreach my $item (@creators) {
 9332:             $rownum ++;
 9333:             if ($checked{$item} eq '') {
 9334:                 $checked{$item} = 'any';
 9335:             }
 9336:             my $css_class;
 9337:             if ($rownum%2) {
 9338:                 $css_class = '';
 9339:             } else {
 9340:                 $css_class = ' class="LC_odd_row" ';
 9341:             }
 9342:             $datatable .= '<tr'.$css_class.'>'.
 9343:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9344:                          '</span></td><td style="text-align: right">';
 9345:             my @options = ('any');
 9346:             if (ref($rules) eq 'HASH') {
 9347:                 if (keys(%{$rules}) > 0) {
 9348:                     push(@options,('official','unofficial'));
 9349:                 }
 9350:             }
 9351:             push(@options,'none');
 9352:             foreach my $option (@options) {
 9353:                 my $type = 'radio';
 9354:                 my $check = ' ';
 9355:                 if ($checked{$item} eq $option) {
 9356:                     $check = ' checked="checked" ';
 9357:                 } 
 9358:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9359:                               '<input type="'.$type.'" name="can_createuser_'.
 9360:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9361:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9362:             }
 9363:             $datatable .= '</td></tr>';
 9364:         }
 9365:     } else {
 9366:         my @contexts = ('author','course','domain');
 9367:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9368:         my %checked;
 9369:         if (ref($settings) eq 'HASH') {
 9370:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9371:                 foreach my $item (@contexts) {
 9372:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9373:                         foreach my $auth (@authtypes) {
 9374:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9375:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9376:                             }
 9377:                         }
 9378:                     }
 9379:                 }
 9380:             }
 9381:         } else {
 9382:             foreach my $item (@contexts) {
 9383:                 foreach my $auth (@authtypes) {
 9384:                     $checked{$item}{$auth} = ' checked="checked" ';
 9385:                 }
 9386:             }
 9387:         }
 9388:         my %title = &context_names();
 9389:         my %authname = &authtype_names();
 9390:         my $rownum = 0;
 9391:         my $css_class; 
 9392:         foreach my $item (@contexts) {
 9393:             if ($rownum%2) {
 9394:                 $css_class = '';
 9395:             } else {
 9396:                 $css_class = ' class="LC_odd_row" ';
 9397:             }
 9398:             $datatable .=   '<tr'.$css_class.'>'.
 9399:                             '<td>'.$title{$item}.
 9400:                             '</td><td class="LC_left_item">'.
 9401:                             '<span class="LC_nobreak">';
 9402:             foreach my $auth (@authtypes) {
 9403:                 $datatable .= '<label>'. 
 9404:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9405:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9406:                               $authname{$auth}.'</label>&nbsp;';
 9407:             }
 9408:             $datatable .= '</span></td></tr>';
 9409:             $rownum ++;
 9410:         }
 9411:         $$rowtotal += $rownum;
 9412:     }
 9413:     return $datatable;
 9414: }
 9415: 
 9416: sub print_selfcreation {
 9417:     my ($position,$dom,$settings,$rowtotal) = @_;
 9418:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9419:         $emaildomain,$datatable);
 9420:     if (ref($settings) eq 'HASH') {
 9421:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9422:             $createsettings = $settings->{'cancreate'};
 9423:             if (ref($createsettings) eq 'HASH') {
 9424:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9425:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9426:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9427:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9428:                         @selfcreate = ('email','login','sso');
 9429:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9430:                         @selfcreate = ($createsettings->{'selfcreate'});
 9431:                     }
 9432:                 }
 9433:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9434:                     $processing = $createsettings->{'selfcreateprocessing'};
 9435:                 }
 9436:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9437:                     $emailoptions = $createsettings->{'emailoptions'};
 9438:                 }
 9439:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9440:                     $emailverified = $createsettings->{'emailverified'};
 9441:                 }
 9442:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9443:                     $emaildomain = $createsettings->{'emaildomain'};
 9444:                 }
 9445:             }
 9446:         }
 9447:     }
 9448:     my %radiohash;
 9449:     my $numinrow = 4;
 9450:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9451:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9452:     if ($position eq 'top') {
 9453:         my %choices = &Apache::lonlocal::texthash (
 9454:                                                       cancreate_login      => 'Institutional Login',
 9455:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9456:                                                   );
 9457:         my @toggles = sort(keys(%choices));
 9458:         my %defaultchecked = (
 9459:                                'cancreate_login' => 'off',
 9460:                                'cancreate_sso'   => 'off',
 9461:                              );
 9462:         my ($onclick,$itemcount);
 9463:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9464:                                                      \%choices,$itemcount,$onclick);
 9465:         $$rowtotal += $itemcount;
 9466:         
 9467:         if (ref($usertypes) eq 'HASH') {
 9468:             if (keys(%{$usertypes}) > 0) {
 9469:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9470:                                              $dom,$numinrow,$othertitle,
 9471:                                              'statustocreate',$rowtotal);
 9472:                 $$rowtotal ++;
 9473:             }
 9474:         }
 9475:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9476:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9477:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9478:         my $rem;
 9479:         my $numperrow = 2;
 9480:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9481:         $datatable .= '<tr'.$css_class.'>'.
 9482:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9483:                      '<td class="LC_left_item">'."\n".
 9484:                      '<table>'."\n";
 9485:         for (my $i=0; $i<@fields; $i++) {
 9486:             $rem = $i%($numperrow);
 9487:             if ($rem == 0) {
 9488:                 if ($i > 0) {
 9489:                     $datatable .= '</tr>';
 9490:                 }
 9491:                 $datatable .= '<tr>';
 9492:             }
 9493:             my $currval;
 9494:             if (ref($createsettings) eq 'HASH') {
 9495:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9496:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9497:                 }
 9498:             }
 9499:             $datatable .= '<td class="LC_left_item">'.
 9500:                           '<span class="LC_nobreak">'.
 9501:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9502:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9503:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9504:         }
 9505:         my $colsleft = $numperrow - $rem;
 9506:         if ($colsleft > 1 ) {
 9507:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9508:                          '&nbsp;</td>';
 9509:         } elsif ($colsleft == 1) {
 9510:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9511:         }
 9512:         $datatable .= '</tr></table></td></tr>';
 9513:         $$rowtotal ++;
 9514:     } elsif ($position eq 'middle') {
 9515:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9516:         my @posstypes;
 9517:         if (ref($types) eq 'ARRAY') {
 9518:             @posstypes = @{$types};
 9519:         }
 9520:         unless (grep(/^default$/,@posstypes)) {
 9521:             push(@posstypes,'default');
 9522:         }
 9523:         my %usertypeshash;
 9524:         if (ref($usertypes) eq 'HASH') {
 9525:             %usertypeshash = %{$usertypes};
 9526:         }
 9527:         $usertypeshash{'default'} = $othertitle;
 9528:         foreach my $status (@posstypes) {
 9529:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9530:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9531:             $$rowtotal ++;
 9532:         }
 9533:     } else {
 9534:         my %choices = &Apache::lonlocal::texthash (
 9535:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9536:                                                   );
 9537:         my @toggles = sort(keys(%choices));
 9538:         my %defaultchecked = (
 9539:                                'cancreate_email' => 'off',
 9540:                              );
 9541:         my $customclass = 'LC_selfcreate_email';
 9542:         my $classprefix = 'LC_canmodify_emailusername_';
 9543:         my $optionsprefix = 'LC_options_emailusername_';
 9544:         my $display = 'none';
 9545:         my $rowstyle = 'display:none';
 9546:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9547:             $display = 'block';
 9548:             $rowstyle = 'display:table-row';
 9549:         }
 9550:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9551:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9552:                                                      \%choices,$$rowtotal,$onclick);
 9553:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9554:                                          $rowstyle);
 9555:         $$rowtotal ++;
 9556:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9557:                                       $rowstyle);
 9558:         $$rowtotal ++;
 9559:         my (@ordered,@posstypes,%usertypeshash);
 9560:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9561:         my ($emailrules,$emailruleorder) =
 9562:             &Apache::lonnet::inst_userrules($dom,'email');
 9563:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9564:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9565:         if (ref($types) eq 'ARRAY') {
 9566:             @posstypes = @{$types};
 9567:         }
 9568:         if (@posstypes) {
 9569:             unless (grep(/^default$/,@posstypes)) {
 9570:                 push(@posstypes,'default');
 9571:             }
 9572:             if (ref($usertypes) eq 'HASH') {
 9573:                 %usertypeshash = %{$usertypes};
 9574:             }
 9575:             my $currassign;
 9576:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9577:                 $currassign = {
 9578:                                   selfassign => $domdefaults{'inststatusguest'},
 9579:                               };
 9580:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9581:             } else {
 9582:                 $currassign = { selfassign => [] };
 9583:             }
 9584:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9585:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9586:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9587:                                          $numinrow,$othertitle,'selfassign',
 9588:                                          $rowtotal,$onclicktypes,$customclass,
 9589:                                          $rowstyle);
 9590:             $$rowtotal ++;
 9591:             $usertypeshash{'default'} = $othertitle;
 9592:             foreach my $status (@posstypes) {
 9593:                 my $css_class;
 9594:                 if ($$rowtotal%2) {
 9595:                     $css_class = 'LC_odd_row ';
 9596:                 }
 9597:                 $css_class .= $customclass;
 9598:                 my $rowid = $optionsprefix.$status;
 9599:                 my $hidden = 1;
 9600:                 my $currstyle = 'display:none';
 9601:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9602:                     $currstyle = $rowstyle;
 9603:                     $hidden = 0; 
 9604:                 }
 9605:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9606:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9607:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9608:                 unless ($hidden) {
 9609:                     $$rowtotal ++;
 9610:                 }
 9611:             }
 9612:         } else {
 9613:             my $css_class;
 9614:             if ($$rowtotal%2) {
 9615:                 $css_class = 'LC_odd_row ';
 9616:             }
 9617:             $css_class .= $customclass;
 9618:             $usertypeshash{'default'} = $othertitle;
 9619:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9620:                                          $emailrules,$emailruleorder,$settings,'default','',
 9621:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9622:             $$rowtotal ++;
 9623:         }
 9624:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9625:         $numinrow = 1;
 9626:         if (@posstypes) {
 9627:             foreach my $status (@posstypes) {
 9628:                 my $rowid = $classprefix.$status;
 9629:                 my $datarowstyle = 'display:none';
 9630:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 9631:                     $datarowstyle = $rowstyle; 
 9632:                 }
 9633:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9634:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9635:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9636:                 unless ($datarowstyle eq 'display:none') {
 9637:                     $$rowtotal ++;
 9638:                 }
 9639:             }
 9640:         } else {
 9641:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9642:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9643:                                                    $infotitles,'',$customclass,$rowstyle);
 9644:         }
 9645:     }
 9646:     return $datatable;
 9647: }
 9648: 
 9649: sub selfcreate_javascript {
 9650:     return <<"ENDSCRIPT";
 9651: 
 9652: <script type="text/javascript">
 9653: // <![CDATA[
 9654: 
 9655: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9656:     var x = document.getElementsByClassName(target);
 9657:     var insttypes = 0;
 9658:     var insttypeRegExp = new RegExp(prefix);
 9659:     if ((x.length != undefined) && (x.length > 0)) {
 9660:         if (form.elements[radio].length != undefined) {
 9661:             for (var i=0; i<form.elements[radio].length; i++) {
 9662:                 if (form.elements[radio][i].checked) {
 9663:                     if (form.elements[radio][i].value == 1) {
 9664:                         for (var j=0; j<x.length; j++) {
 9665:                             if (x[j].id == 'undefined') {
 9666:                                 x[j].style.display = 'table-row';
 9667:                             } else if (insttypeRegExp.test(x[j].id)) {
 9668:                                 insttypes ++;
 9669:                             } else {
 9670:                                 x[j].style.display = 'table-row';
 9671:                             }
 9672:                         }
 9673:                     } else {
 9674:                         for (var j=0; j<x.length; j++) {
 9675:                             x[j].style.display = 'none';
 9676:                         }
 9677:                     }
 9678:                     break;
 9679:                 }
 9680:             }
 9681:             if (insttypes > 0) {
 9682:                 toggleDataRow(form,checkbox,target,altprefix);
 9683:                 toggleDataRow(form,checkbox,target,prefix,1);
 9684:             }
 9685:         }
 9686:     }
 9687:     return;
 9688: }
 9689: 
 9690: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9691:     if (form.elements[checkbox].length != undefined) {
 9692:         var count = 0;
 9693:         if (docount) {
 9694:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9695:                 if (form.elements[checkbox][i].checked) {
 9696:                     count ++;
 9697:                 }
 9698:             }
 9699:         }
 9700:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9701:             var type = form.elements[checkbox][i].value;
 9702:             if (document.getElementById(prefix+type)) {
 9703:                 if (form.elements[checkbox][i].checked) {
 9704:                     document.getElementById(prefix+type).style.display = 'table-row';
 9705:                     if (count % 2 == 1) {
 9706:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9707:                     } else {
 9708:                         document.getElementById(prefix+type).className = target;
 9709:                     }
 9710:                     count ++;
 9711:                 } else {
 9712:                     document.getElementById(prefix+type).style.display = 'none';
 9713:                 }
 9714:             }
 9715:         }
 9716:     }
 9717:     return;
 9718: }
 9719: 
 9720: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9721:     var caller = radio+'_'+status;
 9722:     if (form.elements[caller].length != undefined) {
 9723:         for (var i=0; i<form.elements[caller].length; i++) {
 9724:             if (form.elements[caller][i].checked) {
 9725:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9726:                     var curr = form.elements[caller][i].value;
 9727:                     if (prefix) {
 9728:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9729:                     }
 9730:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9731:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9732:                     if (curr == 'custom') {
 9733:                         if (prefix) { 
 9734:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9735:                         }
 9736:                     } else if (curr == 'inst') {
 9737:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9738:                     } else if (curr == 'noninst') {
 9739:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9740:                     }
 9741:                     break;
 9742:                 }
 9743:             }
 9744:         }
 9745:     }
 9746: }
 9747: 
 9748: // ]]>
 9749: </script>
 9750: 
 9751: ENDSCRIPT
 9752: }
 9753: 
 9754: sub noninst_users {
 9755:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9756:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 9757:     my $class = 'LC_left_item';
 9758:     if ($css_class) {
 9759:         $css_class = ' class="'.$css_class.'"'; 
 9760:     }
 9761:     if ($rowid) {
 9762:         $rowid = ' id="'.$rowid.'"';
 9763:     }
 9764:     if ($rowstyle) {
 9765:         $rowstyle = ' style="'.$rowstyle.'"';
 9766:     }
 9767:     my ($output,$description);
 9768:     if ($type eq 'default') {
 9769:         $description = &mt('Requests for: [_1]',$typetitle);
 9770:     } else {
 9771:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9772:     }
 9773:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9774:               "<td>$description</td>\n".           
 9775:               '<td class="'.$class.'" colspan="2">'.
 9776:               '<table><tr>';
 9777:     my %headers = &Apache::lonlocal::texthash( 
 9778:               approve  => 'Processing',
 9779:               email    => 'E-mail',
 9780:               username => 'Username',
 9781:     );
 9782:     foreach my $item ('approve','email','username') {
 9783:         $output .= '<th>'.$headers{$item}.'</th>';
 9784:     }
 9785:     $output .= '</tr><tr>';
 9786:     foreach my $item ('approve','email','username') {
 9787:         $output .= '<td style="vertical-align: top">';
 9788:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9789:         if ($item eq 'approve') {
 9790:             %choices = &Apache::lonlocal::texthash (
 9791:                                                      automatic => 'Automatically approved',
 9792:                                                      approval  => 'Queued for approval',
 9793:                                                    );
 9794:             @options = ('automatic','approval');
 9795:             $hashref = $processing;
 9796:             $defoption = 'automatic';
 9797:             $name = 'cancreate_emailprocess_'.$type;
 9798:         } elsif ($item eq 'email') {
 9799:             %choices = &Apache::lonlocal::texthash (
 9800:                                                      any     => 'Any e-mail',
 9801:                                                      inst    => 'Institutional only',
 9802:                                                      noninst => 'Non-institutional only',
 9803:                                                      custom  => 'Custom restrictions',
 9804:                                                    );
 9805:             @options = ('any','inst','noninst');
 9806:             my $showcustom;
 9807:             if (ref($emailrules) eq 'HASH') {
 9808:                 if (keys(%{$emailrules}) > 0) {
 9809:                     push(@options,'custom');
 9810:                     $showcustom = 'cancreate_emailrule';
 9811:                     if (ref($settings) eq 'HASH') {
 9812:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9813:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9814:                                 if (exists($emailrules->{$rule})) {
 9815:                                     $hascustom ++;
 9816:                                 }
 9817:                             }
 9818:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9819:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9820:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9821:                                     if (exists($emailrules->{$rule})) {
 9822:                                         $hascustom ++;
 9823:                                     }
 9824:                                 }
 9825:                             }
 9826:                         }
 9827:                     }
 9828:                 }
 9829:             }
 9830:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9831:                                                      "'cancreate_emaildomain','$type'".');"';
 9832:             $hashref = $emailoptions;
 9833:             $defoption = 'any';
 9834:             $name = 'cancreate_emailoptions_'.$type;
 9835:         } elsif ($item eq 'username') {
 9836:             %choices = &Apache::lonlocal::texthash (
 9837:                                                      all    => 'Same as e-mail',
 9838:                                                      first  => 'Omit @domain',
 9839:                                                      free   => 'Free to choose',
 9840:                                                    );
 9841:             @options = ('all','first','free');
 9842:             $hashref = $emailverified;
 9843:             $defoption = 'all';
 9844:             $name = 'cancreate_usernameoptions_'.$type;
 9845:         }
 9846:         foreach my $option (@options) {
 9847:             my $checked;
 9848:             if (ref($hashref) eq 'HASH') {
 9849:                 if ($type eq '') {
 9850:                     if (!exists($hashref->{'default'})) {
 9851:                         if ($option eq $defoption) {
 9852:                             $checked = ' checked="checked"';
 9853:                         }
 9854:                     } else {
 9855:                         if ($hashref->{'default'} eq $option) {
 9856:                             $checked = ' checked="checked"';
 9857:                         }
 9858:                     }
 9859:                 } else {
 9860:                     if (!exists($hashref->{$type})) {
 9861:                         if ($option eq $defoption) {
 9862:                             $checked = ' checked="checked"';
 9863:                         }
 9864:                     } else {
 9865:                         if ($hashref->{$type} eq $option) {
 9866:                             $checked = ' checked="checked"';
 9867:                         }
 9868:                     }
 9869:                 }
 9870:             } elsif (($item eq 'email') && ($hascustom)) {
 9871:                 if ($option eq 'custom') {
 9872:                     $checked = ' checked="checked"';
 9873:                 }
 9874:             } elsif ($option eq $defoption) {
 9875:                 $checked = ' checked="checked"';
 9876:             }
 9877:             $output .= '<span class="LC_nobreak"><label>'.
 9878:                        '<input type="radio" name="'.$name.'"'.
 9879:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9880:                        $choices{$option}.'</label></span><br />';
 9881:             if ($item eq 'email') {
 9882:                 if ($option eq 'custom') {
 9883:                     my $id = 'cancreate_emailrule_'.$type;
 9884:                     my $display = 'none';
 9885:                     if ($checked) {
 9886:                         $display = 'inline';
 9887:                     }
 9888:                     my $numinrow = 2;
 9889:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9890:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9891:                                &user_formats_row('email',$settings,$emailrules,
 9892:                                                  $emailruleorder,$numinrow,'',$type);
 9893:                               '</table></fieldset>';
 9894:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9895:                     my %text = &Apache::lonlocal::texthash (
 9896:                                                              inst    => 'must end:',
 9897:                                                              noninst => 'cannot end:',
 9898:                                                            );
 9899:                     my $value;
 9900:                     if (ref($emaildomain) eq 'HASH') {
 9901:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9902:                             $value = $emaildomain->{$type}->{$option}; 
 9903:                         }
 9904:                     }
 9905:                     if ($value eq '') {
 9906:                         $value = '@'.$intdom;
 9907:                     }
 9908:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9909:                     my $display = 'none';
 9910:                     if ($checked) {
 9911:                         $display = 'inline';
 9912:                     }
 9913:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9914:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9915:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9916:                                '</div>';
 9917:                 }
 9918:             }
 9919:         }
 9920:         $output .= '</td>'."\n";
 9921:     }
 9922:     $output .= "</tr></table></td></tr>\n";
 9923:     return $output;
 9924: }
 9925: 
 9926: sub captcha_choice {
 9927:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9928:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9929:         $vertext,$currver);
 9930:     my %lt = &captcha_phrases();
 9931:     $keyentry = 'hidden';
 9932:     my $colspan=2;
 9933:     if ($context eq 'cancreate') {
 9934:         $rowname = &mt('CAPTCHA validation');
 9935:     } elsif ($context eq 'login') {
 9936:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9937:     } elsif ($context eq 'passwords') {
 9938:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9939:         $colspan=1;
 9940:     }
 9941:     if (ref($settings) eq 'HASH') {
 9942:         if ($settings->{'captcha'}) {
 9943:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9944:         } else {
 9945:             $checked{'original'} = ' checked="checked"';
 9946:         }
 9947:         if ($settings->{'captcha'} eq 'recaptcha') {
 9948:             $pubtext = $lt{'pub'};
 9949:             $privtext = $lt{'priv'};
 9950:             $keyentry = 'text';
 9951:             $vertext = $lt{'ver'};
 9952:             $currver = $settings->{'recaptchaversion'};
 9953:             if ($currver ne '2') {
 9954:                 $currver = 1;
 9955:             }
 9956:         }
 9957:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9958:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9959:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9960:         }
 9961:     } else {
 9962:         $checked{'original'} = ' checked="checked"';
 9963:     }
 9964:     my $css_class;
 9965:     if ($itemcount%2) {
 9966:         $css_class = 'LC_odd_row';
 9967:     }
 9968:     if ($customcss) {
 9969:         $css_class .= " $customcss";
 9970:     }
 9971:     $css_class =~ s/^\s+//;
 9972:     if ($css_class) {
 9973:         $css_class = ' class="'.$css_class.'"';
 9974:     }
 9975:     if ($rowstyle) {
 9976:         $css_class .= ' style="'.$rowstyle.'"';
 9977:     }
 9978:     my $output = '<tr'.$css_class.'>'.
 9979:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9980:                  '<table><tr><td>'."\n";
 9981:     foreach my $option ('original','recaptcha','notused') {
 9982:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9983:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9984:                    $lt{$option}.'</label></span>';
 9985:         unless ($option eq 'notused') {
 9986:             $output .= ('&nbsp;'x2)."\n";
 9987:         }
 9988:     }
 9989: #
 9990: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9991: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9992: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9993: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9994: #
 9995:     $output .= '</td></tr>'."\n".
 9996:                '<tr><td class="LC_zero_height">'."\n".
 9997:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9998:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9999:                $currpub.'" size="40" /></span><br />'."\n".
10000:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10001:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10002:                $currpriv.'" size="40" /></span><br />'.
10003:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10004:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10005:                $currver.'" size="3" /></span><br />'.
10006:                '</td></tr></table>'."\n".
10007:                '</td></tr>';
10008:     return $output;
10009: }
10010: 
10011: sub user_formats_row {
10012:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10013:     my $output;
10014:     my %text = (
10015:                    'username' => 'new usernames',
10016:                    'id'       => 'IDs',
10017:                );
10018:     unless ($type eq 'email') {
10019:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10020:         $output = '<tr '.$css_class.'>'.
10021:                   '<td><span class="LC_nobreak">'.
10022:                   &mt("Format rules to check for $text{$type}: ").
10023:                   '</td><td class="LC_left_item" colspan="2"><table>';
10024:     }
10025:     my $rem;
10026:     if (ref($ruleorder) eq 'ARRAY') {
10027:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10028:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10029:                 my $rem = $i%($numinrow);
10030:                 if ($rem == 0) {
10031:                     if ($i > 0) {
10032:                         $output .= '</tr>';
10033:                     }
10034:                     $output .= '<tr>';
10035:                 }
10036:                 my $check = ' ';
10037:                 if (ref($settings) eq 'HASH') {
10038:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10039:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10040:                             $check = ' checked="checked" ';
10041:                         }
10042:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10043:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10044:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10045:                                 $check = ' checked="checked" ';
10046:                             }
10047:                         }
10048:                     }
10049:                 }
10050:                 my $name = $type.'_rule';
10051:                 if ($type eq 'email') {
10052:                     $name .= '_'.$status;
10053:                 }
10054:                 $output .= '<td class="LC_left_item">'.
10055:                            '<span class="LC_nobreak"><label>'.
10056:                            '<input type="checkbox" name="'.$name.'" '.
10057:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10058:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10059:             }
10060:         }
10061:         $rem = @{$ruleorder}%($numinrow);
10062:     }
10063:     my $colsleft;
10064:     if ($rem) {
10065:         $colsleft = $numinrow - $rem;
10066:     }
10067:     if ($colsleft > 1 ) {
10068:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10069:                    '&nbsp;</td>';
10070:     } elsif ($colsleft == 1) {
10071:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10072:     }
10073:     $output .= '</tr></table>';
10074:     unless ($type eq 'email') {
10075:         $output .= '</td></tr>';
10076:     }
10077:     return $output;
10078: }
10079: 
10080: sub usercreation_types {
10081:     my %lt = &Apache::lonlocal::texthash (
10082:                     author     => 'When adding a co-author',
10083:                     course     => 'When adding a user to a course',
10084:                     requestcrs => 'When requesting a course',
10085:                     any        => 'Any',
10086:                     official   => 'Institutional only ',
10087:                     unofficial => 'Non-institutional only',
10088:                     none       => 'None',
10089:     );
10090:     return %lt;
10091: }
10092: 
10093: sub selfcreation_types {
10094:     my %lt = &Apache::lonlocal::texthash (
10095:                     selfcreate => 'User creates own account',
10096:                     any        => 'Any',
10097:                     official   => 'Institutional only ',
10098:                     unofficial => 'Non-institutional only',
10099:                     email      => 'E-mail address',
10100:                     login      => 'Institutional Login',
10101:                     sso        => 'SSO',
10102:              );
10103: }
10104: 
10105: sub authtype_names {
10106:     my %lt = &Apache::lonlocal::texthash(
10107:                       int    => 'Internal',
10108:                       krb4   => 'Kerberos 4',
10109:                       krb5   => 'Kerberos 5',
10110:                       loc    => 'Local',
10111:                       lti    => 'LTI',
10112:                   );
10113:     return %lt;
10114: }
10115: 
10116: sub context_names {
10117:     my %context_title = &Apache::lonlocal::texthash(
10118:        author => 'Creating users when an Author',
10119:        course => 'Creating users when in a course',
10120:        domain => 'Creating users when a Domain Coordinator',
10121:     );
10122:     return %context_title;
10123: }
10124: 
10125: sub print_usermodification {
10126:     my ($position,$dom,$settings,$rowtotal) = @_;
10127:     my $numinrow = 4;
10128:     my ($context,$datatable,$rowcount);
10129:     if ($position eq 'top') {
10130:         $rowcount = 0;
10131:         $context = 'author'; 
10132:         foreach my $role ('ca','aa') {
10133:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10134:                                                    $numinrow,$rowcount);
10135:             $$rowtotal ++;
10136:             $rowcount ++;
10137:         }
10138:     } elsif ($position eq 'bottom') {
10139:         $context = 'course';
10140:         $rowcount = 0;
10141:         foreach my $role ('st','ep','ta','in','cr') {
10142:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10143:                                                    $numinrow,$rowcount);
10144:             $$rowtotal ++;
10145:             $rowcount ++;
10146:         }
10147:     }
10148:     return $datatable;
10149: }
10150: 
10151: sub print_defaults {
10152:     my ($position,$dom,$settings,$rowtotal) = @_;
10153:     my $rownum = 0;
10154:     my ($datatable,$css_class,$titles);
10155:     unless ($position eq 'bottom') {
10156:         $titles = &defaults_titles($dom);
10157:     }
10158:     if ($position eq 'top') {
10159:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10160:                      'datelocale_def','portal_def');
10161:         my %defaults;
10162:         if (ref($settings) eq 'HASH') {
10163:             %defaults = %{$settings};
10164:         } else {
10165:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10166:             foreach my $item (@items) {
10167:                 $defaults{$item} = $domdefaults{$item};
10168:             }
10169:         }
10170:         foreach my $item (@items) {
10171:             if ($rownum%2) {
10172:                 $css_class = '';
10173:             } else {
10174:                 $css_class = ' class="LC_odd_row" ';
10175:             }
10176:             $datatable .= '<tr'.$css_class.'>'.
10177:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10178:                           '</span></td><td class="LC_right_item" colspan="3">';
10179:             if ($item eq 'auth_def') {
10180:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10181:                 my %shortauth = (
10182:                                  internal => 'int',
10183:                                  krb4 => 'krb4',
10184:                                  krb5 => 'krb5',
10185:                                  localauth  => 'loc',
10186:                                  lti => 'lti',
10187:                                 );
10188:                 my %authnames = &authtype_names();
10189:                 foreach my $auth (@authtypes) {
10190:                     my $checked = ' ';
10191:                     if ($defaults{$item} eq $auth) {
10192:                         $checked = ' checked="checked" ';
10193:                     }
10194:                     $datatable .= '<label><input type="radio" name="'.$item.
10195:                                   '" value="'.$auth.'"'.$checked.'/>'.
10196:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10197:                 }
10198:             } elsif ($item eq 'timezone_def') {
10199:                 my $includeempty = 1;
10200:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10201:             } elsif ($item eq 'datelocale_def') {
10202:                 my $includeempty = 1;
10203:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10204:             } elsif ($item eq 'lang_def') {
10205:                 my $includeempty = 1;
10206:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10207:             } else {
10208:                 my $size;
10209:                 if ($item eq 'portal_def') {
10210:                     $size = ' size="25"';
10211:                 }
10212:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10213:                               $defaults{$item}.'"'.$size.' />';
10214:             }
10215:             $datatable .= '</td></tr>';
10216:             $rownum ++;
10217:         }
10218:     } else {
10219:         my %defaults;
10220:         if (ref($settings) eq 'HASH') {
10221:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10222:                 my $maxnum = @{$settings->{'inststatusorder'}};
10223:                 for (my $i=0; $i<$maxnum; $i++) {
10224:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10225:                     my $item = $settings->{'inststatusorder'}->[$i];
10226:                     my $title = $settings->{'inststatustypes'}->{$item};
10227:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10228:                     $datatable .= '<tr'.$css_class.'>'.
10229:                                   '<td><span class="LC_nobreak">'.
10230:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10231:                     for (my $k=0; $k<=$maxnum; $k++) {
10232:                         my $vpos = $k+1;
10233:                         my $selstr;
10234:                         if ($k == $i) {
10235:                             $selstr = ' selected="selected" ';
10236:                         }
10237:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10238:                     }
10239:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10240:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10241:                                   &mt('delete').'</span></td>'.
10242:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10243:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10244:                                   '</span></td></tr>';
10245:                 }
10246:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10247:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10248:                 $datatable .= '<tr '.$css_class.'>'.
10249:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10250:                 for (my $k=0; $k<=$maxnum; $k++) {
10251:                     my $vpos = $k+1;
10252:                     my $selstr;
10253:                     if ($k == $maxnum) {
10254:                         $selstr = ' selected="selected" ';
10255:                     }
10256:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10257:                 }
10258:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10259:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10260:                               '&nbsp;'.&mt('(new)').
10261:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10262:                               &mt('Name displayed').':'.
10263:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10264:                               '</tr>'."\n";
10265:                 $rownum ++;
10266:             }
10267:         }
10268:     }
10269:     $$rowtotal += $rownum;
10270:     return $datatable;
10271: }
10272: 
10273: sub get_languages_hash {
10274:     my %langchoices;
10275:     foreach my $id (&Apache::loncommon::languageids()) {
10276:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10277:         if ($code ne '') {
10278:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10279:         }
10280:     }
10281:     return %langchoices;
10282: }
10283: 
10284: sub defaults_titles {
10285:     my ($dom) = @_;
10286:     my %titles = &Apache::lonlocal::texthash (
10287:                    'auth_def'      => 'Default authentication type',
10288:                    'auth_arg_def'  => 'Default authentication argument',
10289:                    'lang_def'      => 'Default language',
10290:                    'timezone_def'  => 'Default timezone',
10291:                    'datelocale_def' => 'Default locale for dates',
10292:                    'portal_def'     => 'Portal/Default URL',
10293:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10294:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10295:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10296:                  );
10297:     if ($dom) {
10298:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10299:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10300:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10301:         $protocol = 'http' if ($protocol ne 'https');
10302:         if ($uint_dom) {
10303:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10304:                                          $uint_dom);
10305:         }
10306:     }
10307:     return (\%titles);
10308: }
10309: 
10310: sub print_scantron {
10311:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10312:     if ($position eq 'top') {
10313:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10314:     } else {
10315:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10316:     }
10317: }
10318: 
10319: sub scantron_javascript {
10320:     return <<"ENDSCRIPT";
10321: 
10322: <script type="text/javascript">
10323: // <![CDATA[
10324: 
10325: function toggleScantron(form) {
10326:     var csvfieldset = new Array();
10327:     if (document.getElementById('scantroncsv_cols')) {
10328:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10329:     }
10330:     if (document.getElementById('scantroncsv_options')) {
10331:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10332:     }
10333:     if (csvfieldset.length) {
10334:         if (document.getElementById('scantronconfcsv')) {
10335:             var scantroncsv = document.getElementById('scantronconfcsv');
10336:             if (scantroncsv.checked) {
10337:                 for (var i=0; i<csvfieldset.length; i++) {
10338:                     csvfieldset[i].style.display = 'block';
10339:                 }
10340:             } else {
10341:                 for (var i=0; i<csvfieldset.length; i++) {
10342:                     csvfieldset[i].style.display = 'none';
10343:                 }
10344:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10345:                 if (csvselects.length) {
10346:                     for (var j=0; j<csvselects.length; j++) {
10347:                         csvselects[j].selectedIndex = 0;
10348:                     }
10349:                 }
10350:             }
10351:         }
10352:     }
10353:     return;
10354: }
10355: // ]]>
10356: </script>
10357: 
10358: ENDSCRIPT
10359: 
10360: }
10361: 
10362: sub print_scantronformat {
10363:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10364:     my $itemcount = 1;
10365:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10366:         %confhash);
10367:     my $switchserver = &check_switchserver($dom,$confname);
10368:     my %lt = &Apache::lonlocal::texthash (
10369:                 default => 'Default bubblesheet format file error',
10370:                 custom  => 'Custom bubblesheet format file error',
10371:              );
10372:     my %scantronfiles = (
10373:         default => 'default.tab',
10374:         custom => 'custom.tab',
10375:     );
10376:     foreach my $key (keys(%scantronfiles)) {
10377:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10378:                               .$scantronfiles{$key};
10379:     }
10380:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10381:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10382:         if (!$switchserver) {
10383:             my $servadm = $r->dir_config('lonAdmEMail');
10384:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10385:             if ($configuserok eq 'ok') {
10386:                 if ($author_ok eq 'ok') {
10387:                     my %legacyfile = (
10388:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10389:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10390:                     );
10391:                     my %md5chk;
10392:                     foreach my $type (keys(%legacyfile)) {
10393:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10394:                         chomp($md5chk{$type});
10395:                     }
10396:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10397:                         foreach my $type (keys(%legacyfile)) {
10398:                             ($scantronurls{$type},my $error) =
10399:                                 &legacy_scantronformat($r,$dom,$confname,
10400:                                                  $type,$legacyfile{$type},
10401:                                                  $scantronurls{$type},
10402:                                                  $scantronfiles{$type});
10403:                             if ($error ne '') {
10404:                                 $error{$type} = $error;
10405:                             }
10406:                         }
10407:                         if (keys(%error) == 0) {
10408:                             $is_custom = 1;
10409:                             $confhash{'scantron'}{'scantronformat'} =
10410:                                 $scantronurls{'custom'};
10411:                             my $putresult =
10412:                                 &Apache::lonnet::put_dom('configuration',
10413:                                                          \%confhash,$dom);
10414:                             if ($putresult ne 'ok') {
10415:                                 $error{'custom'} =
10416:                                     '<span class="LC_error">'.
10417:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10418:                             }
10419:                         }
10420:                     } else {
10421:                         ($scantronurls{'default'},my $error) =
10422:                             &legacy_scantronformat($r,$dom,$confname,
10423:                                           'default',$legacyfile{'default'},
10424:                                           $scantronurls{'default'},
10425:                                           $scantronfiles{'default'});
10426:                         if ($error eq '') {
10427:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10428:                             my $putresult =
10429:                                 &Apache::lonnet::put_dom('configuration',
10430:                                                          \%confhash,$dom);
10431:                             if ($putresult ne 'ok') {
10432:                                 $error{'default'} =
10433:                                     '<span class="LC_error">'.
10434:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10435:                             }
10436:                         } else {
10437:                             $error{'default'} = $error;
10438:                         }
10439:                     }
10440:                 }
10441:             }
10442:         } else {
10443:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10444:         }
10445:     }
10446:     if (ref($settings) eq 'HASH') {
10447:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10448:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10449:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10450:                 $scantronurl = '';
10451:             } else {
10452:                 $scantronurl = $settings->{'scantronformat'};
10453:             }
10454:             $is_custom = 1;
10455:         } else {
10456:             $scantronurl = $scantronurls{'default'};
10457:         }
10458:     } else {
10459:         if ($is_custom) {
10460:             $scantronurl = $scantronurls{'custom'};
10461:         } else {
10462:             $scantronurl = $scantronurls{'default'};
10463:         }
10464:     }
10465:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10466:     $datatable .= '<tr'.$css_class.'>';
10467:     if (!$is_custom) {
10468:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10469:                       '<span class="LC_nobreak">';
10470:         if ($scantronurl) {
10471:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10472:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10473:         } else {
10474:             $datatable = &mt('File unavailable for display');
10475:         }
10476:         $datatable .= '</span></td>';
10477:         if (keys(%error) == 0) { 
10478:             $datatable .= '<td style="vertical-align: bottom">';
10479:             if (!$switchserver) {
10480:                 $datatable .= &mt('Upload:').'<br />';
10481:             }
10482:         } else {
10483:             my $errorstr;
10484:             foreach my $key (sort(keys(%error))) {
10485:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10486:             }
10487:             $datatable .= '<td>'.$errorstr;
10488:         }
10489:     } else {
10490:         if (keys(%error) > 0) {
10491:             my $errorstr;
10492:             foreach my $key (sort(keys(%error))) {
10493:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10494:             } 
10495:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10496:         } elsif ($scantronurl) {
10497:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10498:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10499:             $datatable .= '<td><span class="LC_nobreak">'.
10500:                           $link.
10501:                           '<label><input type="checkbox" name="scantronformat_del"'.
10502:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10503:                           '<td><span class="LC_nobreak">&nbsp;'.
10504:                           &mt('Replace:').'</span><br />';
10505:         }
10506:     }
10507:     if (keys(%error) == 0) {
10508:         if ($switchserver) {
10509:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10510:         } else {
10511:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10512:                          '<input type="file" name="scantronformat" /></span>';
10513:         }
10514:     }
10515:     $datatable .= '</td></tr>';
10516:     $$rowtotal ++;
10517:     return $datatable;
10518: }
10519: 
10520: sub legacy_scantronformat {
10521:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10522:     my ($url,$error);
10523:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10524:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10525:         (my $result,$url) =
10526:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
10527:                          '','',$newfile);
10528:         if ($result ne 'ok') {
10529:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10530:         }
10531:     }
10532:     return ($url,$error);
10533: }
10534: 
10535: sub print_scantronconfig {
10536:     my ($dom,$settings,$rowtotal) = @_;
10537:     my $itemcount = 2;
10538:     my $is_checked = ' checked="checked"';
10539:     my %optionson = (
10540:                      hdr => ' checked="checked"',
10541:                      pad => ' checked="checked"',
10542:                      rem => ' checked="checked"',
10543:                     );
10544:     my %optionsoff = (
10545:                       hdr => '',
10546:                       pad => '',
10547:                       rem => '',
10548:                      );
10549:     my $currcsvsty = 'none';
10550:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10551:     my @fields = &scantroncsv_fields();
10552:     my %titles = &scantronconfig_titles();
10553:     if (ref($settings) eq 'HASH') {
10554:         if (ref($settings->{config}) eq 'HASH') {
10555:             if ($settings->{config}->{dat}) {
10556:                 $checked{'dat'} = $is_checked;
10557:             }
10558:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10559:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10560:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10561:                     if (keys(%csvfields) > 0) {
10562:                         $checked{'csv'} = $is_checked;
10563:                         $currcsvsty = 'block';
10564:                     }
10565:                 }
10566:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10567:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10568:                     foreach my $option (keys(%optionson)) {
10569:                         unless ($csvoptions{$option}) {
10570:                             $optionsoff{$option} = $optionson{$option};
10571:                             $optionson{$option} = '';
10572:                         }
10573:                     }
10574:                 }
10575:             }
10576:         } else {
10577:             $checked{'dat'} = $is_checked;
10578:         }
10579:     } else {
10580:         $checked{'dat'} = $is_checked;
10581:     }
10582:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10583:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10584:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10585:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10586:     foreach my $item ('dat','csv') {
10587:         my $id;
10588:         if ($item eq 'csv') {
10589:             $id = 'id="scantronconfcsv" ';
10590:         }
10591:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10592:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10593:         if ($item eq 'csv') {
10594:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10595:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10596:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10597:             foreach my $col (@fields) {
10598:                 my $selnone;
10599:                 if ($csvfields{$col} eq '') {
10600:                     $selnone = ' selected="selected"';
10601:                 }
10602:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10603:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10604:                               '<option value=""'.$selnone.'></option>';
10605:                 for (my $i=0; $i<20; $i++) {
10606:                     my $shown = $i+1;
10607:                     my $sel;
10608:                     unless ($selnone) {
10609:                         if (exists($csvfields{$col})) {
10610:                             if ($csvfields{$col} == $i) {
10611:                                 $sel = ' selected="selected"';
10612:                             }
10613:                         }
10614:                     }
10615:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10616:                 }
10617:                 $datatable .= '</select></td></tr>';
10618:            }
10619:            $datatable .= '</table></fieldset>'.
10620:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10621:                          '<legend>'.&mt('CSV Options').'</legend>';
10622:            foreach my $option ('hdr','pad','rem') {
10623:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10624:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10625:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10626:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10627:            }
10628:            $datatable .= '</fieldset>';
10629:            $itemcount ++;
10630:         }
10631:     }
10632:     $datatable .= '</td></tr>';
10633:     $$rowtotal ++;
10634:     return $datatable;
10635: }
10636: 
10637: sub scantronconfig_titles {
10638:     return &Apache::lonlocal::texthash(
10639:                                           dat => 'Standard format (.dat)',
10640:                                           csv => 'Comma separated values (.csv)',
10641:                                           hdr => 'Remove first line in file (contains column titles)',
10642:                                           pad => 'Prepend 0s to PaperID',
10643:                                           rem => 'Remove leading spaces (except Question Response columns)',
10644:                                           CODE => 'CODE',
10645:                                           ID   => 'Student ID',
10646:                                           PaperID => 'Paper ID',
10647:                                           FirstName => 'First Name',
10648:                                           LastName => 'Last Name',
10649:                                           FirstQuestion => 'First Question Response',
10650:                                           Section => 'Section',
10651:     );
10652: }
10653: 
10654: sub scantroncsv_fields {
10655:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10656: }
10657: 
10658: sub print_coursecategories {
10659:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10660:     my $datatable;
10661:     if ($position eq 'top') {
10662:         my (%checked);
10663:         my @catitems = ('unauth','auth');
10664:         my @cattypes = ('std','domonly','codesrch','none');
10665:         $checked{'unauth'} = 'std';
10666:         $checked{'auth'} = 'std';
10667:         if (ref($settings) eq 'HASH') {
10668:             foreach my $type (@cattypes) {
10669:                 if ($type eq $settings->{'unauth'}) {
10670:                     $checked{'unauth'} = $type;
10671:                 }
10672:                 if ($type eq $settings->{'auth'}) {
10673:                     $checked{'auth'} = $type;
10674:                 }
10675:             }
10676:         }
10677:         my %lt = &Apache::lonlocal::texthash (
10678:                                                unauth   => 'Catalog type for unauthenticated users',
10679:                                                auth     => 'Catalog type for authenticated users',
10680:                                                none     => 'No catalog',
10681:                                                std      => 'Standard catalog',
10682:                                                domonly  => 'Domain-only catalog',
10683:                                                codesrch => "Code search form",
10684:                                              );
10685:        my $itemcount = 0;
10686:        foreach my $item (@catitems) {
10687:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10688:            $datatable .= '<tr '.$css_class.'>'.
10689:                          '<td>'.$lt{$item}.'</td>'.
10690:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10691:            foreach my $type (@cattypes) {
10692:                my $ischecked;
10693:                if ($checked{$item} eq $type) {
10694:                    $ischecked=' checked="checked"';
10695:                }
10696:                $datatable .= '<label>'.
10697:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10698:                              ' />'.$lt{$type}.'</label>&nbsp;';
10699:            }
10700:            $datatable .= '</span></td></tr>';
10701:            $itemcount ++;
10702:         }
10703:         $$rowtotal += $itemcount;
10704:     } elsif ($position eq 'middle') {
10705:         my $toggle_cats_crs = ' ';
10706:         my $toggle_cats_dom = ' checked="checked" ';
10707:         my $can_cat_crs = ' ';
10708:         my $can_cat_dom = ' checked="checked" ';
10709:         my $toggle_catscomm_comm = ' ';
10710:         my $toggle_catscomm_dom = ' checked="checked" ';
10711:         my $can_catcomm_comm = ' ';
10712:         my $can_catcomm_dom = ' checked="checked" ';
10713:         my $toggle_catsplace_place = ' ';
10714:         my $toggle_catsplace_dom = ' checked="checked" ';
10715:         my $can_catplace_place = ' ';
10716:         my $can_catplace_dom = ' checked="checked" ';
10717: 
10718:         if (ref($settings) eq 'HASH') {
10719:             if ($settings->{'togglecats'} eq 'crs') {
10720:                 $toggle_cats_crs = $toggle_cats_dom;
10721:                 $toggle_cats_dom = ' ';
10722:             }
10723:             if ($settings->{'categorize'} eq 'crs') {
10724:                 $can_cat_crs = $can_cat_dom;
10725:                 $can_cat_dom = ' ';
10726:             }
10727:             if ($settings->{'togglecatscomm'} eq 'comm') {
10728:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10729:                 $toggle_catscomm_dom = ' ';
10730:             }
10731:             if ($settings->{'categorizecomm'} eq 'comm') {
10732:                 $can_catcomm_comm = $can_catcomm_dom;
10733:                 $can_catcomm_dom = ' ';
10734:             }
10735:             if ($settings->{'togglecatsplace'} eq 'place') {
10736:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10737:                 $toggle_catsplace_dom = ' ';
10738:             }
10739:             if ($settings->{'categorizeplace'} eq 'place') {
10740:                 $can_catplace_place = $can_catplace_dom;
10741:                 $can_catplace_dom = ' ';
10742:             }
10743:         }
10744:         my %title = &Apache::lonlocal::texthash (
10745:                      togglecats      => 'Show/Hide a course in catalog',
10746:                      togglecatscomm  => 'Show/Hide a community in catalog',
10747:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10748:                      categorize      => 'Assign a category to a course',
10749:                      categorizecomm  => 'Assign a category to a community',
10750:                      categorizeplace => 'Assign a category to a placement test',
10751:                     );
10752:         my %level = &Apache::lonlocal::texthash (
10753:                      dom   => 'Set in Domain',
10754:                      crs   => 'Set in Course',
10755:                      comm  => 'Set in Community',
10756:                      place => 'Set in Placement Test',
10757:                     );
10758:         $datatable = '<tr class="LC_odd_row">'.
10759:                   '<td>'.$title{'togglecats'}.'</td>'.
10760:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10761:                   '<input type="radio" name="togglecats"'.
10762:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10763:                   '<label><input type="radio" name="togglecats"'.
10764:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10765:                   '</tr><tr>'.
10766:                   '<td>'.$title{'categorize'}.'</td>'.
10767:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10768:                   '<label><input type="radio" name="categorize"'.
10769:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10770:                   '<label><input type="radio" name="categorize"'.
10771:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10772:                   '</tr><tr class="LC_odd_row">'.
10773:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10774:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10775:                   '<input type="radio" name="togglecatscomm"'.
10776:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10777:                   '<label><input type="radio" name="togglecatscomm"'.
10778:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10779:                   '</tr><tr>'.
10780:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10781:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10782:                   '<label><input type="radio" name="categorizecomm"'.
10783:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10784:                   '<label><input type="radio" name="categorizecomm"'.
10785:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10786:                   '</tr><tr class="LC_odd_row">'.
10787:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10788:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10789:                   '<input type="radio" name="togglecatsplace"'.
10790:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10791:                   '<label><input type="radio" name="togglecatscomm"'.
10792:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10793:                   '</tr><tr>'.
10794:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10795:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10796:                   '<label><input type="radio" name="categorizeplace"'.
10797:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10798:                   '<label><input type="radio" name="categorizeplace"'.
10799:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10800:                   '</tr>';
10801:         $$rowtotal += 6;
10802:     } else {
10803:         my $css_class;
10804:         my $itemcount = 1;
10805:         my $cathash; 
10806:         if (ref($settings) eq 'HASH') {
10807:             $cathash = $settings->{'cats'};
10808:         }
10809:         if (ref($cathash) eq 'HASH') {
10810:             my (@cats,@trails,%allitems,%idx,@jsarray);
10811:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10812:                                                    \%allitems,\%idx,\@jsarray);
10813:             my $maxdepth = scalar(@cats);
10814:             my $colattrib = '';
10815:             if ($maxdepth > 2) {
10816:                 $colattrib = ' colspan="2" ';
10817:             }
10818:             my @path;
10819:             if (@cats > 0) {
10820:                 if (ref($cats[0]) eq 'ARRAY') {
10821:                     my $numtop = @{$cats[0]};
10822:                     my $maxnum = $numtop;
10823:                     my %default_names = (
10824:                           instcode    => &mt('Official courses'),
10825:                           communities => &mt('Communities'),
10826:                           placement   => &mt('Placement Tests'),
10827:                     );
10828: 
10829:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10830:                         ($cathash->{'instcode::0'} eq '') ||
10831:                         (!grep(/^communities$/,@{$cats[0]})) || 
10832:                         ($cathash->{'communities::0'} eq '') ||
10833:                         (!grep(/^placement$/,@{$cats[0]})) ||
10834:                         ($cathash->{'placement::0'} eq '')) {
10835:                         $maxnum ++;
10836:                     }
10837:                     my $lastidx;
10838:                     for (my $i=0; $i<$numtop; $i++) {
10839:                         my $parent = $cats[0][$i];
10840:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10841:                         my $item = &escape($parent).'::0';
10842:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10843:                         $lastidx = $idx{$item};
10844:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10845:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10846:                         for (my $k=0; $k<=$maxnum; $k++) {
10847:                             my $vpos = $k+1;
10848:                             my $selstr;
10849:                             if ($k == $i) {
10850:                                 $selstr = ' selected="selected" ';
10851:                             }
10852:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10853:                         }
10854:                         $datatable .= '</select></span></td><td>';
10855:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10856:                             $datatable .=  '<span class="LC_nobreak">'
10857:                                            .$default_names{$parent}.'</span>';
10858:                             if ($parent eq 'instcode') {
10859:                                 $datatable .= '<br /><span class="LC_nobreak">('
10860:                                               .&mt('with institutional codes')
10861:                                               .')</span></td><td'.$colattrib.'>';
10862:                             } else {
10863:                                 $datatable .= '<table><tr><td>';
10864:                             }
10865:                             $datatable .= '<span class="LC_nobreak">'
10866:                                           .'<label><input type="radio" name="'
10867:                                           .$parent.'" value="1" checked="checked" />'
10868:                                           .&mt('Display').'</label>';
10869:                             if ($parent eq 'instcode') {
10870:                                 $datatable .= '&nbsp;';
10871:                             } else {
10872:                                 $datatable .= '</span></td></tr><tr><td>'
10873:                                               .'<span class="LC_nobreak">';
10874:                             }
10875:                             $datatable .= '<label><input type="radio" name="'
10876:                                           .$parent.'" value="0" />'
10877:                                           .&mt('Do not display').'</label></span>';
10878:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
10879:                                 $datatable .= '</td></tr></table>';
10880:                             }
10881:                             $datatable .= '</td>';
10882:                         } else {
10883:                             $datatable .= $parent
10884:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10885:                                           .'<input type="checkbox" name="deletecategory" '
10886:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10887:                         }
10888:                         my $depth = 1;
10889:                         push(@path,$parent);
10890:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10891:                         pop(@path);
10892:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10893:                         $itemcount ++;
10894:                     }
10895:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10896:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10897:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10898:                     for (my $k=0; $k<=$maxnum; $k++) {
10899:                         my $vpos = $k+1;
10900:                         my $selstr;
10901:                         if ($k == $numtop) {
10902:                             $selstr = ' selected="selected" ';
10903:                         }
10904:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10905:                     }
10906:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10907:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10908:                                   .'</tr>'."\n";
10909:                     $itemcount ++;
10910:                     foreach my $default ('instcode','communities','placement') {
10911:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10912:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10913:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10914:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10915:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10916:                             for (my $k=0; $k<=$maxnum; $k++) {
10917:                                 my $vpos = $k+1;
10918:                                 my $selstr;
10919:                                 if ($k == $maxnum) {
10920:                                     $selstr = ' selected="selected" ';
10921:                                 }
10922:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10923:                             }
10924:                             $datatable .= '</select></span></td>'.
10925:                                           '<td><span class="LC_nobreak">'.
10926:                                           $default_names{$default}.'</span>';
10927:                             if ($default eq 'instcode') {
10928:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10929:                                               .&mt('with institutional codes').')</span>';
10930:                             }
10931:                             $datatable .= '</td>'
10932:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10933:                                           .&mt('Display').'</label>&nbsp;'
10934:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10935:                                           .&mt('Do not display').'</label></span></td></tr>';
10936:                         }
10937:                     }
10938:                 }
10939:             } else {
10940:                 $datatable .= &initialize_categories($itemcount);
10941:             }
10942:         } else {
10943:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10944:                           .&initialize_categories($itemcount);
10945:         }
10946:         $$rowtotal += $itemcount;
10947:     }
10948:     return $datatable;
10949: }
10950: 
10951: sub print_serverstatuses {
10952:     my ($dom,$settings,$rowtotal) = @_;
10953:     my $datatable;
10954:     my @pages = &serverstatus_pages();
10955:     my (%namedaccess,%machineaccess);
10956:     foreach my $type (@pages) {
10957:         $namedaccess{$type} = '';
10958:         $machineaccess{$type}= '';
10959:     }
10960:     if (ref($settings) eq 'HASH') {
10961:         foreach my $type (@pages) {
10962:             if (exists($settings->{$type})) {
10963:                 if (ref($settings->{$type}) eq 'HASH') {
10964:                     foreach my $key (keys(%{$settings->{$type}})) {
10965:                         if ($key eq 'namedusers') {
10966:                             $namedaccess{$type} = $settings->{$type}->{$key};
10967:                         } elsif ($key eq 'machines') {
10968:                             $machineaccess{$type} = $settings->{$type}->{$key};
10969:                         }
10970:                     }
10971:                 }
10972:             }
10973:         }
10974:     }
10975:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10976:     my $rownum = 0;
10977:     my $css_class;
10978:     foreach my $type (@pages) {
10979:         $rownum ++;
10980:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10981:         $datatable .= '<tr'.$css_class.'>'.
10982:                       '<td><span class="LC_nobreak">'.
10983:                       $titles->{$type}.'</span></td>'.
10984:                       '<td class="LC_left_item">'.
10985:                       '<input type="text" name="'.$type.'_namedusers" '.
10986:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10987:                       '<td class="LC_right_item">'.
10988:                       '<span class="LC_nobreak">'.
10989:                       '<input type="text" name="'.$type.'_machines" '.
10990:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10991:                       '</span></td></tr>'."\n";
10992:     }
10993:     $$rowtotal += $rownum;
10994:     return $datatable;
10995: }
10996: 
10997: sub serverstatus_pages {
10998:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10999:             'checksums','clusterstatus','certstatus','metadata_keywords',
11000:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11001:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11002: }
11003: 
11004: sub defaults_javascript {
11005:     my ($settings) = @_;
11006:     return unless (ref($settings) eq 'HASH');
11007:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11008:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11009:         if ($maxnum eq '') {
11010:             $maxnum = 0;
11011:         }
11012:         $maxnum ++;
11013:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11014:         return <<"ENDSCRIPT";
11015: <script type="text/javascript">
11016: // <![CDATA[
11017: function reorderTypes(form,caller) {
11018:     var changedVal;
11019: $jstext 
11020:     var newpos = 'addinststatus_pos';
11021:     var current = new Array;
11022:     var maxh = $maxnum;
11023:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11024:     var oldVal;
11025:     if (caller == newpos) {
11026:         changedVal = newitemVal;
11027:     } else {
11028:         var curritem = 'inststatus_pos_'+caller;
11029:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11030:         current[newitemVal] = newpos;
11031:     }
11032:     for (var i=0; i<inststatuses.length; i++) {
11033:         if (inststatuses[i] != caller) {
11034:             var elementName = 'inststatus_pos_'+inststatuses[i];
11035:             if (form.elements[elementName]) {
11036:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11037:                 current[currVal] = elementName;
11038:             }
11039:         }
11040:     }
11041:     for (var j=0; j<maxh; j++) {
11042:         if (current[j] == undefined) {
11043:             oldVal = j;
11044:         }
11045:     }
11046:     if (oldVal < changedVal) {
11047:         for (var k=oldVal+1; k<=changedVal ; k++) {
11048:            var elementName = current[k];
11049:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11050:         }
11051:     } else {
11052:         for (var k=changedVal; k<oldVal; k++) {
11053:             var elementName = current[k];
11054:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11055:         }
11056:     }
11057:     return;
11058: }
11059: 
11060: // ]]>
11061: </script>
11062: 
11063: ENDSCRIPT
11064:     }
11065:     return;
11066: }
11067: 
11068: sub passwords_javascript {
11069:     my %intalert = &Apache::lonlocal::texthash (
11070:         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.',
11071:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11072:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11073:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11074:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11075:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11076:     );
11077:     &js_escape(\%intalert);
11078:     my $defmin = $Apache::lonnet::passwdmin;
11079:     my $intauthjs = <<"ENDSCRIPT";
11080: 
11081: function warnIntAuth(field) {
11082:     if (field.name == 'intauth_check') {
11083:         if (field.value == '2') {
11084:             alert('$intalert{authcheck}');
11085:         }
11086:     }
11087:     if (field.name == 'intauth_cost') {
11088:         field.value.replace(/\s/g,'');
11089:         if (field.value != '') {
11090:             var regexdigit=/^\\d+\$/;
11091:             if (!regexdigit.test(field.value)) {
11092:                 alert('$intalert{authcost}');
11093:             }
11094:         }
11095:     }
11096:     return;
11097: }
11098: 
11099: function warnIntPass(field) {
11100:     field.value.replace(/^\s+/,'');
11101:     field.value.replace(/\s+\$/,'');
11102:     var regexdigit=/^\\d+\$/;
11103:     if (field.name == 'passwords_min') {
11104:         if (field.value == '') {
11105:             alert('$intalert{passmin}');
11106:             field.value = '$defmin';
11107:         } else {
11108:             if (!regexdigit.test(field.value)) {
11109:                 alert('$intalert{passmin}');
11110:                 field.value = '$defmin';
11111:             }
11112:             var minval = parseInt(field.value,10);
11113:             if (minval < $defmin) {
11114:                 alert('$intalert{passmin}');
11115:                 field.value = '$defmin';
11116:             }
11117:         }
11118:     } else {
11119:         if (field.value == '0') {
11120:             field.value = '';
11121:         }
11122:         if (field.value != '') {
11123:             if (field.name == 'passwords_expire') {
11124:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11125:                 if (!regexpposnum.test(field.value)) {
11126:                     alert('$intalert{passexp}');
11127:                     field.value = '';
11128:                 } else {
11129:                     var expval = parseFloat(field.value);
11130:                     if (expval == 0) {
11131:                         alert('$intalert{passexp}');
11132:                         field.value = '';
11133:                     }
11134:                 }
11135:             } else {
11136:                 if (!regexdigit.test(field.value)) {
11137:                     if (field.name == 'passwords_max') {
11138:                         alert('$intalert{passmax}');
11139:                     } else {
11140:                         if (field.name == 'passwords_numsaved') {
11141:                             alert('$intalert{passnum}');
11142:                         }
11143:                     }
11144:                     field.value = '';
11145:                 }
11146:             }
11147:         }
11148:     }
11149:     return;
11150: }
11151: 
11152: ENDSCRIPT
11153:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11154: }
11155: 
11156: sub coursecategories_javascript {
11157:     my ($settings) = @_;
11158:     my ($output,$jstext,$cathash);
11159:     if (ref($settings) eq 'HASH') {
11160:         $cathash = $settings->{'cats'};
11161:     }
11162:     if (ref($cathash) eq 'HASH') {
11163:         my (@cats,@jsarray,%idx);
11164:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11165:         if (@jsarray > 0) {
11166:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11167:             for (my $i=0; $i<@jsarray; $i++) {
11168:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11169:                     my $catstr = join('","',@{$jsarray[$i]});
11170:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11171:                 }
11172:             }
11173:         }
11174:     } else {
11175:         $jstext  = '    var categories = Array(1);'."\n".
11176:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11177:     }
11178:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11179:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11180:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11181:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11182:     &js_escape(\$instcode_reserved);
11183:     &js_escape(\$communities_reserved);
11184:     &js_escape(\$placement_reserved);
11185:     &js_escape(\$choose_again);
11186:     $output = <<"ENDSCRIPT";
11187: <script type="text/javascript">
11188: // <![CDATA[
11189: function reorderCats(form,parent,item,idx) {
11190:     var changedVal;
11191: $jstext
11192:     var newpos = 'addcategory_pos';
11193:     if (parent == '') {
11194:         var has_instcode = 0;
11195:         var maxtop = categories[idx].length;
11196:         for (var j=0; j<maxtop; j++) {
11197:             if (categories[idx][j] == 'instcode::0') {
11198:                 has_instcode == 1;
11199:             }
11200:         }
11201:         if (has_instcode == 0) {
11202:             categories[idx][maxtop] = 'instcode_pos';
11203:         }
11204:     } else {
11205:         newpos += '_'+parent;
11206:     }
11207:     var maxh = 1 + categories[idx].length;
11208:     var current = new Array;
11209:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11210:     if (item == newpos) {
11211:         changedVal = newitemVal;
11212:     } else {
11213:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11214:         current[newitemVal] = newpos;
11215:     }
11216:     for (var i=0; i<categories[idx].length; i++) {
11217:         var elementName = categories[idx][i];
11218:         if (elementName != item) {
11219:             if (form.elements[elementName]) {
11220:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11221:                 current[currVal] = elementName;
11222:             }
11223:         }
11224:     }
11225:     var oldVal;
11226:     for (var j=0; j<maxh; j++) {
11227:         if (current[j] == undefined) {
11228:             oldVal = j;
11229:         }
11230:     }
11231:     if (oldVal < changedVal) {
11232:         for (var k=oldVal+1; k<=changedVal ; k++) {
11233:            var elementName = current[k];
11234:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11235:         }
11236:     } else {
11237:         for (var k=changedVal; k<oldVal; k++) {
11238:             var elementName = current[k];
11239:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11240:         }
11241:     }
11242:     return;
11243: }
11244: 
11245: function categoryCheck(form) {
11246:     if (form.elements['addcategory_name'].value == 'instcode') {
11247:         alert('$instcode_reserved\\n$choose_again');
11248:         return false;
11249:     }
11250:     if (form.elements['addcategory_name'].value == 'communities') {
11251:         alert('$communities_reserved\\n$choose_again');
11252:         return false;
11253:     }
11254:     if (form.elements['addcategory_name'].value == 'placement') {
11255:         alert('$placement_reserved\\n$choose_again');
11256:         return false;
11257:     }
11258:     return true;
11259: }
11260: 
11261: // ]]>
11262: </script>
11263: 
11264: ENDSCRIPT
11265:     return $output;
11266: }
11267: 
11268: sub initialize_categories {
11269:     my ($itemcount) = @_;
11270:     my ($datatable,$css_class,$chgstr);
11271:     my %default_names = &Apache::lonlocal::texthash (
11272:                       instcode    => 'Official courses (with institutional codes)',
11273:                       communities => 'Communities',
11274:                       placement   => 'Placement Tests',
11275:                         );
11276:     my %selnum = (
11277:                    instcode    => '0',
11278:                    communities => '1',
11279:                    placement   => '2',
11280:                  );
11281:     my %selected;
11282:     foreach my $default ('instcode','communities','placement') {
11283:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11284:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11285:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11286:         $selected{$selnum{$default}} = ' selected="selected"';
11287:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11288:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11289:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11290:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11291:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11292:                      .'<option value="3">4</option></select>&nbsp;'
11293:                      .$default_names{$default}
11294:                      .'</span></td><td><span class="LC_nobreak">'
11295:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11296:                      .&mt('Display').'</label>&nbsp;<label>'
11297:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11298:                  .'</label></span></td></tr>';
11299:         $itemcount ++;
11300:     }
11301:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11302:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11303:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11304:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11305:                   .'<option value="0">1</option>'
11306:                   .'<option value="1">2</option>'
11307:                   .'<option value="2">3</option>'
11308:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11309:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11310:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11311:                   .'</td></tr>';
11312:     return $datatable;
11313: }
11314: 
11315: sub build_category_rows {
11316:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11317:     my ($text,$name,$item,$chgstr);
11318:     if (ref($cats) eq 'ARRAY') {
11319:         my $maxdepth = scalar(@{$cats});
11320:         if (ref($cats->[$depth]) eq 'HASH') {
11321:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11322:                 my $numchildren = @{$cats->[$depth]{$parent}};
11323:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11324:                 $text .= '<td><table class="LC_data_table">';
11325:                 my ($idxnum,$parent_name,$parent_item);
11326:                 my $higher = $depth - 1;
11327:                 if ($higher == 0) {
11328:                     $parent_name = &escape($parent).'::'.$higher;
11329:                 } else {
11330:                     if (ref($path) eq 'ARRAY') {
11331:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11332:                     }
11333:                 }
11334:                 $parent_item = 'addcategory_pos_'.$parent_name;
11335:                 for (my $j=0; $j<=$numchildren; $j++) {
11336:                     if ($j < $numchildren) {
11337:                         $name = $cats->[$depth]{$parent}[$j];
11338:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11339:                         $idxnum = $idx->{$item};
11340:                     } else {
11341:                         $name = $parent_name;
11342:                         $item = $parent_item;
11343:                     }
11344:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11345:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11346:                     for (my $i=0; $i<=$numchildren; $i++) {
11347:                         my $vpos = $i+1;
11348:                         my $selstr;
11349:                         if ($j == $i) {
11350:                             $selstr = ' selected="selected" ';
11351:                         }
11352:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11353:                     }
11354:                     $text .= '</select>&nbsp;';
11355:                     if ($j < $numchildren) {
11356:                         my $deeper = $depth+1;
11357:                         $text .= $name.'&nbsp;'
11358:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11359:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11360:                         if(ref($path) eq 'ARRAY') {
11361:                             push(@{$path},$name);
11362:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11363:                             pop(@{$path});
11364:                         }
11365:                     } else {
11366:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11367:                         if ($j == $numchildren) {
11368:                             $text .= $name;
11369:                         } else {
11370:                             $text .= $item;
11371:                         }
11372:                         $text .= '" value="" />';
11373:                     }
11374:                     $text .= '</td></tr>';
11375:                 }
11376:                 $text .= '</table></td>';
11377:             } else {
11378:                 my $higher = $depth-1;
11379:                 if ($higher == 0) {
11380:                     $name = &escape($parent).'::'.$higher;
11381:                 } else {
11382:                     if (ref($path) eq 'ARRAY') {
11383:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11384:                     }
11385:                 }
11386:                 my $colspan;
11387:                 if ($parent ne 'instcode') {
11388:                     $colspan = $maxdepth - $depth - 1;
11389:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11390:                 }
11391:             }
11392:         }
11393:     }
11394:     return $text;
11395: }
11396: 
11397: sub modifiable_userdata_row {
11398:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11399:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11400:     my ($role,$rolename,$statustype);
11401:     $role = $item;
11402:     if ($context eq 'cancreate') {
11403:         if ($item =~ /^(emailusername)_(.+)$/) {
11404:             $role = $1;
11405:             $statustype = $2;
11406:             if (ref($usertypes) eq 'HASH') {
11407:                 if ($usertypes->{$statustype}) {
11408:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11409:                 } else {
11410:                     $rolename = &mt('Data provided by user');
11411:                 }
11412:             }
11413:         }
11414:     } elsif ($context eq 'selfcreate') {
11415:         if (ref($usertypes) eq 'HASH') {
11416:             $rolename = $usertypes->{$role};
11417:         } else {
11418:             $rolename = $role;
11419:         }
11420:     } elsif ($context eq 'lti') {
11421:         $rolename = &mt('Institutional data used (if available)');
11422:     } elsif ($context eq 'privacy') {
11423:         $rolename = $itemdesc;
11424:     } else {
11425:         if ($role eq 'cr') {
11426:             $rolename = &mt('Custom role');
11427:         } else {
11428:             $rolename = &Apache::lonnet::plaintext($role);
11429:         }
11430:     }
11431:     my (@fields,%fieldtitles);
11432:     if (ref($fieldsref) eq 'ARRAY') {
11433:         @fields = @{$fieldsref};
11434:     } else {
11435:         @fields = ('lastname','firstname','middlename','generation',
11436:                    'permanentemail','id');
11437:     }
11438:     if ((ref($titlesref) eq 'HASH')) {
11439:         %fieldtitles = %{$titlesref};
11440:     } else {
11441:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11442:     }
11443:     my $output;
11444:     my $css_class;
11445:     if ($rowcount%2) {
11446:         $css_class = 'LC_odd_row';
11447:     }
11448:     if ($customcss) {
11449:         $css_class .= " $customcss";
11450:     }
11451:     $css_class =~ s/^\s+//;
11452:     if ($css_class) {
11453:         $css_class = ' class="'.$css_class.'"';
11454:     }
11455:     if ($rowstyle) {
11456:         $css_class .= ' style="'.$rowstyle.'"';
11457:     }
11458:     if ($rowid) {
11459:         $rowid = ' id="'.$rowid.'"';
11460:     }
11461:     $output = '<tr '.$css_class.$rowid.'>'.
11462:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11463:               '<td class="LC_left_item" colspan="2"><table>';
11464:     my $rem;
11465:     my %checks;
11466:     my %current;
11467:     if (ref($settings) eq 'HASH') {
11468:         my $hashref;
11469:         if ($context eq 'lti') {
11470:             if (ref($settings) eq 'HASH') {
11471:                 $hashref = $settings->{'instdata'};
11472:             }
11473:         } elsif ($context eq 'privacy') {
11474:             my ($key,$inner) = split(/_/,$role);
11475:             if (ref($settings) eq 'HASH') {
11476:                 if (ref($settings->{$key}) eq 'HASH') {
11477:                     $hashref = $settings->{$key}->{$inner};
11478:                 }
11479:             }
11480:         } elsif (ref($settings->{$context}) eq 'HASH') {
11481:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11482:                 $hashref = $settings->{'lti_instdata'};
11483:             }
11484:             if ($role eq 'emailusername') {
11485:                 if ($statustype) {
11486:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11487:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11488:                     }
11489:                 }
11490:             }
11491:         }
11492:         if (ref($hashref) eq 'HASH') { 
11493:             foreach my $field (@fields) {
11494:                 if ($hashref->{$field}) {
11495:                     if ($role eq 'emailusername') {
11496:                         $checks{$field} = $hashref->{$field};
11497:                     } else {
11498:                         $checks{$field} = ' checked="checked" ';
11499:                     }
11500:                 }
11501:             }
11502:         }
11503:     }
11504:  
11505:     my $total = scalar(@fields);
11506:     for (my $i=0; $i<$total; $i++) {
11507:         $rem = $i%($numinrow);
11508:         if ($rem == 0) {
11509:             if ($i > 0) {
11510:                 $output .= '</tr>';
11511:             }
11512:             $output .= '<tr>';
11513:         }
11514:         my $check = ' ';
11515:         unless ($role eq 'emailusername') {
11516:             if (exists($checks{$fields[$i]})) {
11517:                 $check = $checks{$fields[$i]};
11518:             } elsif ($context eq 'privacy') {
11519:                 if ($role =~ /^priv_(domain|course)$/) {
11520:                     if (ref($settings) ne 'HASH') {
11521:                         $check = ' checked="checked" ';
11522:                     }
11523:                 } elsif ($role =~ /^priv_(author|community)$/) {
11524:                     if (ref($settings) ne 'HASH') {
11525:                         unless ($fields[$i] eq 'id') {
11526:                             $check = ' checked="checked" ';
11527:                         }
11528:                     }
11529:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11530:                     if (ref($settings) ne 'HASH') {
11531:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11532:                             $check = ' checked="checked" ';
11533:                         }
11534:                     }
11535:                 }
11536:             } elsif ($context ne 'lti') {
11537:                 if ($role eq 'st') {
11538:                     if (ref($settings) ne 'HASH') {
11539:                         $check = ' checked="checked" '; 
11540:                     }
11541:                 }
11542:             }
11543:         }
11544:         $output .= '<td class="LC_left_item">'.
11545:                    '<span class="LC_nobreak">';
11546:         my $prefix = 'canmodify';
11547:         if ($role eq 'emailusername') {
11548:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11549:                 $checks{$fields[$i]} = 'omit';
11550:             }
11551:             foreach my $option ('required','optional','omit') {
11552:                 my $checked='';
11553:                 if ($checks{$fields[$i]} eq $option) {
11554:                     $checked='checked="checked" ';
11555:                 }
11556:                 $output .= '<label>'.
11557:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11558:                            &mt($option).'</label>'.('&nbsp;' x2);
11559:             }
11560:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11561:         } else {
11562:             if ($context eq 'lti') {
11563:                 $prefix = 'lti';
11564:             } elsif ($context eq 'privacy') {
11565:                 $prefix = 'privacy';
11566:             }
11567:             $output .= '<label>'.
11568:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11569:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11570:                        '</label>';
11571:         }
11572:         $output .= '</span></td>';
11573:     }
11574:     $rem = $total%$numinrow;
11575:     my $colsleft;
11576:     if ($rem) {
11577:         $colsleft = $numinrow - $rem;
11578:     }
11579:     if ($colsleft > 1) {
11580:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11581:                    '&nbsp;</td>';
11582:     } elsif ($colsleft == 1) {
11583:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11584:     }
11585:     $output .= '</tr></table></td></tr>';
11586:     return $output;
11587: }
11588: 
11589: sub insttypes_row {
11590:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11591:         $customcss,$rowstyle) = @_;
11592:     my %lt = &Apache::lonlocal::texthash (
11593:                       cansearch => 'Users allowed to search',
11594:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11595:                       lockablenames => 'User preference to lock name',
11596:                       selfassign    => 'Self-reportable affiliations',
11597:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11598:              );
11599:     my $showdom;
11600:     if ($context eq 'cansearch') {
11601:         $showdom = ' ('.$dom.')';
11602:     }
11603:     my $class = 'LC_left_item';
11604:     if ($context eq 'statustocreate') {
11605:         $class = 'LC_right_item';
11606:     }
11607:     my $css_class;
11608:     if ($$rowtotal%2) {
11609:         $css_class = 'LC_odd_row';
11610:     }
11611:     if ($customcss) {
11612:         $css_class .= ' '.$customcss;
11613:     }
11614:     $css_class =~ s/^\s+//;
11615:     if ($css_class) {
11616:         $css_class = ' class="'.$css_class.'"';
11617:     }
11618:     if ($rowstyle) {
11619:         $css_class .= ' style="'.$rowstyle.'"';
11620:     }
11621:     if ($onclick) {
11622:         $onclick = 'onclick="'.$onclick.'" ';
11623:     }
11624:     my $output = '<tr'.$css_class.'>'.
11625:                  '<td>'.$lt{$context}.$showdom.
11626:                  '</td><td class="'.$class.'" colspan="2"><table>';
11627:     my $rem;
11628:     if (ref($types) eq 'ARRAY') {
11629:         for (my $i=0; $i<@{$types}; $i++) {
11630:             if (defined($usertypes->{$types->[$i]})) {
11631:                 my $rem = $i%($numinrow);
11632:                 if ($rem == 0) {
11633:                     if ($i > 0) {
11634:                         $output .= '</tr>';
11635:                     }
11636:                     $output .= '<tr>';
11637:                 }
11638:                 my $check = ' ';
11639:                 if (ref($settings) eq 'HASH') {
11640:                     if (ref($settings->{$context}) eq 'ARRAY') {
11641:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11642:                             $check = ' checked="checked" ';
11643:                         }
11644:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11645:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11646:                             $check = ' checked="checked" ';
11647:                         }
11648:                     } elsif ($context eq 'statustocreate') {
11649:                         $check = ' checked="checked" ';
11650:                     }
11651:                 }
11652:                 $output .= '<td class="LC_left_item">'.
11653:                            '<span class="LC_nobreak"><label>'.
11654:                            '<input type="checkbox" name="'.$context.'" '.
11655:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11656:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11657:             }
11658:         }
11659:         $rem = @{$types}%($numinrow);
11660:     }
11661:     my $colsleft = $numinrow - $rem;
11662:     if ($context eq 'overrides') {
11663:         if ($colsleft > 1) {
11664:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11665:         } else {
11666:             $output .= '<td class="LC_left_item">';
11667:         }
11668:         $output .= '&nbsp;';  
11669:     } else {
11670:         if ($rem == 0) {
11671:             $output .= '<tr>';
11672:         }
11673:         if ($colsleft > 1) {
11674:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11675:         } else {
11676:             $output .= '<td class="LC_left_item">';
11677:         }
11678:         my $defcheck = ' ';
11679:         if (ref($settings) eq 'HASH') {  
11680:             if (ref($settings->{$context}) eq 'ARRAY') {
11681:                 if (grep(/^default$/,@{$settings->{$context}})) {
11682:                     $defcheck = ' checked="checked" ';
11683:                 }
11684:             } elsif ($context eq 'statustocreate') {
11685:                 $defcheck = ' checked="checked" ';
11686:             }
11687:         }
11688:         $output .= '<span class="LC_nobreak"><label>'.
11689:                    '<input type="checkbox" name="'.$context.'" '.
11690:                    'value="default"'.$defcheck.$onclick.' />'.
11691:                    $othertitle.'</label></span>';
11692:     }
11693:     $output .= '</td></tr></table></td></tr>';
11694:     return $output;
11695: }
11696: 
11697: sub sorted_searchtitles {
11698:     my %searchtitles = &Apache::lonlocal::texthash(
11699:                          'uname' => 'username',
11700:                          'lastname' => 'last name',
11701:                          'lastfirst' => 'last name, first name',
11702:                      );
11703:     my @titleorder = ('uname','lastname','lastfirst');
11704:     return (\%searchtitles,\@titleorder);
11705: }
11706: 
11707: sub sorted_searchtypes {
11708:     my %srchtypes_desc = (
11709:                            exact    => 'is exact match',
11710:                            contains => 'contains ..',
11711:                            begins   => 'begins with ..',
11712:                          );
11713:     my @srchtypeorder = ('exact','begins','contains');
11714:     return (\%srchtypes_desc,\@srchtypeorder);
11715: }
11716: 
11717: sub usertype_update_row {
11718:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11719:     my $datatable;
11720:     my $numinrow = 4;
11721:     foreach my $type (@{$types}) {
11722:         if (defined($usertypes->{$type})) {
11723:             $$rownums ++;
11724:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11725:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11726:                           '</td><td class="LC_left_item"><table>';
11727:             for (my $i=0; $i<@{$fields}; $i++) {
11728:                 my $rem = $i%($numinrow);
11729:                 if ($rem == 0) {
11730:                     if ($i > 0) {
11731:                         $datatable .= '</tr>';
11732:                     }
11733:                     $datatable .= '<tr>';
11734:                 }
11735:                 my $check = ' ';
11736:                 if (ref($settings) eq 'HASH') {
11737:                     if (ref($settings->{'fields'}) eq 'HASH') {
11738:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11739:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11740:                                 $check = ' checked="checked" ';
11741:                             }
11742:                         }
11743:                     }
11744:                 }
11745: 
11746:                 if ($i == @{$fields}-1) {
11747:                     my $colsleft = $numinrow - $rem;
11748:                     if ($colsleft > 1) {
11749:                         $datatable .= '<td colspan="'.$colsleft.'">';
11750:                     } else {
11751:                         $datatable .= '<td>';
11752:                     }
11753:                 } else {
11754:                     $datatable .= '<td>';
11755:                 }
11756:                 $datatable .= '<span class="LC_nobreak"><label>'.
11757:                               '<input type="checkbox" name="updateable_'.$type.
11758:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11759:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11760:             }
11761:             $datatable .= '</tr></table></td></tr>';
11762:         }
11763:     }
11764:     return $datatable;
11765: }
11766: 
11767: sub modify_login {
11768:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11769:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11770:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
11771:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso);
11772:     %title = ( coursecatalog => 'Display course catalog',
11773:                adminmail => 'Display administrator E-mail address',
11774:                helpdesk  => 'Display "Contact Helpdesk" link',
11775:                newuser => 'Link for visitors to create a user account',
11776:                loginheader => 'Log-in box header',
11777:                saml => 'Dual SSO and non-SSO login');
11778:     @offon = ('off','on');
11779:     if (ref($domconfig{login}) eq 'HASH') {
11780:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11781:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11782:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11783:             }
11784:         }
11785:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
11786:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
11787:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
11788:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
11789:                     $saml{$lonhost} = 1;
11790:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
11791:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
11792:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
11793:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
11794:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
11795:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
11796:                 }
11797:             }
11798:         }
11799:     }
11800:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11801:                                            \%domconfig,\%loginhash);
11802:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11803:     foreach my $item (@toggles) {
11804:         $loginhash{login}{$item} = $env{'form.'.$item};
11805:     }
11806:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11807:     if (ref($colchanges{'login'}) eq 'HASH') {  
11808:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11809:                                          \%loginhash);
11810:     }
11811: 
11812:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11813:     my %domservers = &Apache::lonnet::get_servers($dom);
11814:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11815:     if (keys(%servers) > 1) {
11816:         foreach my $lonhost (keys(%servers)) {
11817:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
11818:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
11819:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
11820:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
11821:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
11822:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11823:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11824:                         $changes{'loginvia'}{$lonhost} = 1;
11825:                     } else {
11826:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
11827:                         $changes{'loginvia'}{$lonhost} = 1;
11828:                     }
11829:                 } else {
11830:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11831:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11832:                         $changes{'loginvia'}{$lonhost} = 1;
11833:                     }
11834:                 }
11835:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
11836:                     foreach my $item (@loginvia_attribs) {
11837:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
11838:                     }
11839:                 } else {
11840:                     foreach my $item (@loginvia_attribs) {
11841:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11842:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11843:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
11844:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11845:                                 $new = '/';
11846:                             }
11847:                         }
11848:                         if (($item eq 'custompath') && 
11849:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11850:                             $new = '';
11851:                         }
11852:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
11853:                             $changes{'loginvia'}{$lonhost} = 1;
11854:                         }
11855:                         if ($item eq 'exempt') {
11856:                             $new = &check_exempt_addresses($new);
11857:                         }
11858:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11859:                     }
11860:                 }
11861:             } else {
11862:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11863:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11864:                     $changes{'loginvia'}{$lonhost} = 1;
11865:                     foreach my $item (@loginvia_attribs) {
11866:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11867:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11868:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11869:                                 $new = '/';
11870:                             }
11871:                         }
11872:                         if (($item eq 'custompath') && 
11873:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11874:                             $new = '';
11875:                         }
11876:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11877:                     }
11878:                 }
11879:             }
11880:         }
11881:     }
11882: 
11883:     my $servadm = $r->dir_config('lonAdmEMail');
11884:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11885:     if (ref($domconfig{'login'}) eq 'HASH') {
11886:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11887:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11888:                 if ($lang eq 'nolang') {
11889:                     push(@currlangs,$lang);
11890:                 } elsif (defined($langchoices{$lang})) {
11891:                     push(@currlangs,$lang);
11892:                 } else {
11893:                     next;
11894:                 }
11895:             }
11896:         }
11897:     }
11898:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11899:     if (@currlangs > 0) {
11900:         foreach my $lang (@currlangs) {
11901:             if (grep(/^\Q$lang\E$/,@delurls)) {
11902:                 $changes{'helpurl'}{$lang} = 1;
11903:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11904:                 $changes{'helpurl'}{$lang} = 1;
11905:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11906:                 push(@newlangs,$lang);
11907:             } else {
11908:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11909:             }
11910:         }
11911:     }
11912:     unless (grep(/^nolang$/,@currlangs)) {
11913:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11914:             $changes{'helpurl'}{'nolang'} = 1;
11915:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11916:             push(@newlangs,'nolang');
11917:         }
11918:     }
11919:     if ($env{'form.loginhelpurl_add_lang'}) {
11920:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11921:             ($env{'form.loginhelpurl_add_file.filename'})) {
11922:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11923:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11924:         }
11925:     }
11926:     if ((@newlangs > 0) || ($addedfile)) {
11927:         my $error;
11928:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11929:         if ($configuserok eq 'ok') {
11930:             if ($switchserver) {
11931:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11932:             } elsif ($author_ok eq 'ok') {
11933:                 my @allnew = @newlangs;
11934:                 if ($addedfile ne '') {
11935:                     push(@allnew,$addedfile);
11936:                 }
11937:                 foreach my $lang (@allnew) {
11938:                     my $formelem = 'loginhelpurl_'.$lang;
11939:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11940:                         $formelem = 'loginhelpurl_add_file';
11941:                     }
11942:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11943:                                                                "help/$lang",'','',$newfile{$lang});
11944:                     if ($result eq 'ok') {
11945:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11946:                         $changes{'helpurl'}{$lang} = 1;
11947:                     } else {
11948:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11949:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11950:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11951:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11952:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11953:                         }
11954:                     }
11955:                 }
11956:             } else {
11957:                 $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);
11958:             }
11959:         } else {
11960:             $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);
11961:         }
11962:         if ($error) {
11963:             &Apache::lonnet::logthis($error);
11964:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11965:         }
11966:     }
11967: 
11968:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11969:     if (ref($domconfig{'login'}) eq 'HASH') {
11970:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11971:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11972:                 if ($domservers{$lonhost}) {
11973:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11974:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11975:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11976:                     }
11977:                 }
11978:             }
11979:         }
11980:     }
11981:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11982:     foreach my $lonhost (sort(keys(%domservers))) {
11983:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11984:             $changes{'headtag'}{$lonhost} = 1;
11985:         } else {
11986:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11987:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11988:             }
11989:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11990:                 push(@newhosts,$lonhost);
11991:             } elsif ($currheadtagurls{$lonhost}) {
11992:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11993:                 if ($currexempt{$lonhost}) {
11994:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11995:                         $changes{'headtag'}{$lonhost} = 1;
11996:                     }
11997:                 } elsif ($possexempt{$lonhost}) {
11998:                     $changes{'headtag'}{$lonhost} = 1;
11999:                 }
12000:                 if ($possexempt{$lonhost}) {
12001:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12002:                 }
12003:             }
12004:         }
12005:     }
12006:     if (@newhosts) {
12007:         my $error;
12008:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12009:         if ($configuserok eq 'ok') {
12010:             if ($switchserver) {
12011:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12012:             } elsif ($author_ok eq 'ok') {
12013:                 foreach my $lonhost (@newhosts) {
12014:                     my $formelem = 'loginheadtag_'.$lonhost;
12015:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
12016:                                                                           "login/headtag/$lonhost",'','',
12017:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
12018:                     if ($result eq 'ok') {
12019:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12020:                         $changes{'headtag'}{$lonhost} = 1;
12021:                         if ($possexempt{$lonhost}) {
12022:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12023:                         }
12024:                     } else {
12025:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12026:                                            $newheadtagurls{$lonhost},$result);
12027:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12028:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12029:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12030:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12031:                         }
12032:                     }
12033:                 }
12034:             } else {
12035:                 $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);
12036:             }
12037:         } else {
12038:             $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);
12039:         }
12040:         if ($error) {
12041:             &Apache::lonnet::logthis($error);
12042:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12043:         }
12044:     }
12045:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12046:     my @newsamlimgs;
12047:     foreach my $lonhost (keys(%domservers)) {
12048:         if ($env{'form.saml_'.$lonhost}) {
12049:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12050:                 push(@newsamlimgs,$lonhost);
12051:             }
12052:             foreach my $item ('text','alt','url','title','notsso') {
12053:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12054:             }
12055:             if ($saml{$lonhost}) {
12056:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12057: #FIXME Need to obsolete published image
12058:                     delete($currsaml{$lonhost}{'img'});
12059:                     $changes{'saml'}{$lonhost} = 1;
12060:                 }
12061:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12062:                     $changes{'saml'}{$lonhost} = 1;
12063:                 }
12064:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12065:                     $changes{'saml'}{$lonhost} = 1;
12066:                 }
12067:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12068:                     $changes{'saml'}{$lonhost} = 1;
12069:                 }
12070:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12071:                     $changes{'saml'}{$lonhost} = 1;
12072:                 }
12073:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12074:                     $changes{'saml'}{$lonhost} = 1;
12075:                 }
12076:             } else {
12077:                 $changes{'saml'}{$lonhost} = 1;
12078:             }
12079:             foreach my $item ('text','alt','url','title','notsso') {
12080:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12081:             }
12082:         } else {
12083:             if ($saml{$lonhost}) {  
12084:                 $changes{'saml'}{$lonhost} = 1;
12085:                 delete($currsaml{$lonhost});
12086:             }
12087:         }
12088:     }
12089:     foreach my $posshost (keys(%currsaml)) {
12090:         unless (exists($domservers{$posshost})) { 
12091:             delete($currsaml{$posshost}); 
12092:         }
12093:     }
12094:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12095:     if (@newsamlimgs) {
12096:         my $error;
12097:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12098:         if ($configuserok eq 'ok') {
12099:             if ($switchserver) {
12100:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12101:             } elsif ($author_ok eq 'ok') {
12102:                 foreach my $lonhost (@newsamlimgs) {
12103:                     my $formelem = 'saml_img_'.$lonhost;
12104:                     my ($result,$imgurl) = &publishlogo($r,'upload',$formelem,$dom,$confname,
12105:                                                         "login/saml/$lonhost",'','',
12106:                                                         $env{'form.saml_img_'.$lonhost.'.filename'});
12107:                     if ($result eq 'ok') {
12108:                         $currsaml{$lonhost}{'img'} = $imgurl;
12109:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12110:                         $changes{'saml'}{$lonhost} = 1;
12111:                     } else {
12112:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12113:                                            $lonhost,$result);
12114:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12115:                     }
12116:                 }
12117:             } else {
12118:                 $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);
12119:             }
12120:         } else {
12121:             $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);
12122:         }
12123:         if ($error) {
12124:             &Apache::lonnet::logthis($error);
12125:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12126:         }
12127:     }
12128:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12129: 
12130:     my $defaulthelpfile = '/adm/loginproblems.html';
12131:     my $defaulttext = &mt('Default in use');
12132: 
12133:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12134:                                              $dom);
12135:     if ($putresult eq 'ok') {
12136:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12137:         my %defaultchecked = (
12138:                     'coursecatalog' => 'on',
12139:                     'helpdesk'      => 'on',
12140:                     'adminmail'     => 'off',
12141:                     'newuser'       => 'off',
12142:         );
12143:         if (ref($domconfig{'login'}) eq 'HASH') {
12144:             foreach my $item (@toggles) {
12145:                 if ($defaultchecked{$item} eq 'on') { 
12146:                     if (($domconfig{'login'}{$item} eq '0') &&
12147:                         ($env{'form.'.$item} eq '1')) {
12148:                         $changes{$item} = 1;
12149:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12150:                               $domconfig{'login'}{$item} eq '1') &&
12151:                              ($env{'form.'.$item} eq '0')) {
12152:                         $changes{$item} = 1;
12153:                     }
12154:                 } elsif ($defaultchecked{$item} eq 'off') {
12155:                     if (($domconfig{'login'}{$item} eq '1') &&
12156:                         ($env{'form.'.$item} eq '0')) {
12157:                         $changes{$item} = 1;
12158:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12159:                               $domconfig{'login'}{$item} eq '0') &&
12160:                              ($env{'form.'.$item} eq '1')) {
12161:                         $changes{$item} = 1;
12162:                     }
12163:                 }
12164:             }
12165:         }
12166:         if (keys(%changes) > 0 || $colchgtext) {
12167:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12168:             if (exists($changes{'saml'})) {
12169:                 my $hostid_in_use;
12170:                 my @hosts = &Apache::lonnet::current_machine_ids();
12171:                 if (@hosts > 1) {
12172:                     foreach my $hostid (@hosts) {
12173:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12174:                             $hostid_in_use = $hostid;
12175:                             last;
12176:                         }
12177:                     }
12178:                 } else {
12179:                     $hostid_in_use = $r->dir_config('lonHostID');
12180:                 }
12181:                 if (($hostid_in_use) &&
12182:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12183:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12184:                 }
12185:                 if (ref($lastactref) eq 'HASH') {
12186:                     if (ref($changes{'saml'}) eq 'HASH') {
12187:                         my %updates;
12188:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12189:                         $lastactref->{'samllanding'} = \%updates;
12190:                     }
12191:                 }
12192:             }
12193:             if (ref($lastactref) eq 'HASH') {
12194:                 $lastactref->{'domainconfig'} = 1;
12195:             }
12196:             $resulttext = &mt('Changes made:').'<ul>';
12197:             foreach my $item (sort(keys(%changes))) {
12198:                 if ($item eq 'loginvia') {
12199:                     if (ref($changes{$item}) eq 'HASH') {
12200:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12201:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12202:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12203:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12204:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12205:                                     $protocol = 'http' if ($protocol ne 'https');
12206:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12207: 
12208:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12209:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12210:                                     } else {
12211:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12212:                                     }
12213:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12214:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12215:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12216:                                     }
12217:                                     $resulttext .= '</li>';
12218:                                 } else {
12219:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12220:                                 }
12221:                             } else {
12222:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12223:                             }
12224:                         }
12225:                         $resulttext .= '</ul></li>';
12226:                     }
12227:                 } elsif ($item eq 'helpurl') {
12228:                     if (ref($changes{$item}) eq 'HASH') {
12229:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12230:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12231:                                 my ($chg,$link);
12232:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12233:                                 if ($lang eq 'nolang') {
12234:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12235:                                 } else {
12236:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12237:                                 }
12238:                                 $resulttext .= '<li>'.$chg.'</li>';
12239:                             } else {
12240:                                 my $chg;
12241:                                 if ($lang eq 'nolang') {
12242:                                     $chg = &mt('custom log-in help file for no preferred language');
12243:                                 } else {
12244:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12245:                                 }
12246:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12247:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12248:                                                       '?inhibitmenu=yes',$chg,600,500).
12249:                                                '</li>';
12250:                             }
12251:                         }
12252:                     }
12253:                 } elsif ($item eq 'headtag') {
12254:                     if (ref($changes{$item}) eq 'HASH') {
12255:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12256:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12257:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12258:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12259:                                 $resulttext .= '<li><a href="'.
12260:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12261:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12262:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12263:                                 if ($possexempt{$lonhost}) {
12264:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12265:                                 } else {
12266:                                     $resulttext .= &mt('included for any client IP');
12267:                                 }
12268:                                 $resulttext .= '</li>';
12269:                             }
12270:                         }
12271:                     }
12272:                 } elsif ($item eq 'saml') {
12273:                     if (ref($changes{$item}) eq 'HASH') {
12274:                         my %notlt = (
12275:                                        text   => 'Text for log-in by SSO',
12276:                                        img    => 'SSO button image',
12277:                                        alt    => 'Alt text for button image',
12278:                                        url    => 'SSO URL',
12279:                                        title  => 'Tooltip for SSO link',
12280:                                        notsso => 'Text for non-SSO log-in',
12281:                                     );
12282:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12283:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12284:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12285:                                                '<ul>';
12286:                                 foreach my $key ('text','img','alt','url','title','notsso') {
12287:                                     if ($currsaml{$lonhost}{$key} eq '') {
12288:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12289:                                     } else {
12290:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12291:                                         if ($key eq 'img') {
12292:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12293:                                         }
12294:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12295:                                                                   $value).'</li>';
12296:                                     }
12297:                                 }
12298:                                 $resulttext .= '</ul></li>';
12299:                             } else {
12300:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12301:                             }
12302:                         }
12303:                     }
12304:                 } elsif ($item eq 'captcha') {
12305:                     if (ref($loginhash{'login'}) eq 'HASH') {
12306:                         my $chgtxt;
12307:                         if ($loginhash{'login'}{$item} eq 'notused') {
12308:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12309:                         } else {
12310:                             my %captchas = &captcha_phrases();
12311:                             if ($captchas{$loginhash{'login'}{$item}}) {
12312:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12313:                             } else {
12314:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12315:                             }
12316:                         }
12317:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12318:                     }
12319:                 } elsif ($item eq 'recaptchakeys') {
12320:                     if (ref($loginhash{'login'}) eq 'HASH') {
12321:                         my ($privkey,$pubkey);
12322:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12323:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12324:                             $privkey = $loginhash{'login'}{$item}{'private'};
12325:                         }
12326:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12327:                         if (!$pubkey) {
12328:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12329:                         } else {
12330:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12331:                         }
12332:                         if (!$privkey) {
12333:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12334:                         } else {
12335:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12336:                         }
12337:                         $chgtxt .= '</ul>';
12338:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12339:                     }
12340:                 } elsif ($item eq 'recaptchaversion') {
12341:                     if (ref($loginhash{'login'}) eq 'HASH') {
12342:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12343:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12344:                                            '</li>';
12345:                         }
12346:                     }
12347:                 } else {
12348:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12349:                 }
12350:             }
12351:             $resulttext .= $colchgtext.'</ul>';
12352:         } else {
12353:             $resulttext = &mt('No changes made to log-in page settings');
12354:         }
12355:     } else {
12356:         $resulttext = '<span class="LC_error">'.
12357: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12358:     }
12359:     if ($errors) {
12360:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12361:                        $errors.'</ul>';
12362:     }
12363:     return $resulttext;
12364: }
12365: 
12366: sub check_exempt_addresses {
12367:     my ($iplist) = @_;
12368:     $iplist =~ s/^\s+//;
12369:     $iplist =~ s/\s+$//;
12370:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12371:     my (@okips,$new);
12372:     foreach my $ip (@poss_ips) {
12373:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12374:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12375:                 push(@okips,$ip);
12376:             }
12377:         }
12378:     }
12379:     if (@okips > 0) {
12380:         $new = join(',',@okips);
12381:     } else {
12382:         $new = '';
12383:     }
12384:     return $new;
12385: }
12386: 
12387: sub color_font_choices {
12388:     my %choices =
12389:         &Apache::lonlocal::texthash (
12390:             img => "Header",
12391:             bgs => "Background colors",
12392:             links => "Link colors",
12393:             images => "Images",
12394:             font => "Font color",
12395:             fontmenu => "Font menu",
12396:             pgbg => "Page",
12397:             tabbg => "Header",
12398:             sidebg => "Border",
12399:             link => "Link",
12400:             alink => "Active link",
12401:             vlink => "Visited link",
12402:         );
12403:     return %choices;
12404: }
12405: 
12406: sub modify_ipaccess {
12407:     my ($dom,$lastactref,%domconfig) = @_;
12408:     my (@allpos,%changes,%confhash,$errors,$resulttext);
12409:     my (@items,%deletions,%itemids,@warnings);
12410:     my ($typeorder,$types) = &commblocktype_text();
12411:     if ($env{'form.ipaccess_add'}) {
12412:         my $name = $env{'form.ipaccess_name_add'};
12413:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
12414:         if ($newid) {
12415:             $itemids{'add'} = $newid;
12416:             push(@items,'add');
12417:             $changes{$newid} = 1;
12418:         } else {
12419:             $error = &mt('Failed to acquire unique ID for new IP access control item');
12420:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12421:         }
12422:     }
12423:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12424:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
12425:         if (@todelete) {
12426:             map { $deletions{$_} = 1; } @todelete;
12427:         }
12428:         my $maxnum = $env{'form.ipaccess_maxnum'};
12429:         for (my $i=0; $i<$maxnum; $i++) {
12430:             my $itemid = $env{'form.ipaccess_id_'.$i};
12431:             $itemid =~ s/\D+//g;
12432:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12433:                 if ($deletions{$itemid}) {
12434:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
12435:                 } else {
12436:                     push(@items,$i);
12437:                     $itemids{$i} = $itemid;
12438:                 }
12439:             }
12440:         }
12441:     }
12442:     foreach my $idx (@items) {
12443:         my $itemid = $itemids{$idx};
12444:         next unless ($itemid);
12445:         my %current;
12446:         unless ($idx eq 'add') {
12447:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12448:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
12449:             }
12450:         }
12451:         my $position = $env{'form.ipaccess_pos_'.$itemid};
12452:         $position =~ s/\D+//g;
12453:         if ($position ne '') {
12454:             $allpos[$position] = $itemid;
12455:         }
12456:         my $name = $env{'form.ipaccess_name_'.$idx};
12457:         $name =~ s/^\s+|\s+$//g;
12458:         $confhash{$itemid}{'name'} = $name;
12459:         my $possrange = $env{'form.ipaccess_range_'.$idx};
12460:         $possrange =~ s/^\s+|\s+$//g;
12461:         unless ($possrange eq '') {
12462:             $possrange =~ s/[\r\n]+/\s/g;
12463:             $possrange =~ s/\s*-\s*/-/g;
12464:             $possrange =~ s/\s+/,/g;
12465:             $possrange =~ s/,+/,/g;
12466:             if ($possrange ne '') {
12467:                 my (@ok,$count);
12468:                 $count = 0; 
12469:                 foreach my $poss (split(/\,/,$possrange)) {
12470:                     $count ++;
12471:                     $poss = &validate_ip_pattern($poss);
12472:                     if ($poss ne '') {
12473:                         push(@ok,$poss);
12474:                     }
12475:                 }
12476:                 my $diff = $count - scalar(@ok);
12477:                 if ($diff) {
12478:                     $errors .= '<li><span class="LC_error">'.
12479:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
12480:                                    $diff,$name).
12481:                                '</span></li>';
12482:                 }
12483:                 if (@ok) {
12484:                     my @cidr_list;
12485:                     foreach my $item (@ok) {
12486:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
12487:                     }
12488:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
12489:                 }
12490:             }
12491:         }
12492:         foreach my $field ('name','ip') {
12493:             unless (($idx eq 'add') || ($changes{$itemid})) {
12494:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
12495:                     $changes{$itemid} = 1;
12496:                     last;
12497:                 }
12498:             }
12499:         }
12500:         $confhash{$itemid}{'commblocks'} = {};
12501:          
12502:         my %commblocks;
12503:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);  
12504:         foreach my $type (@{$typeorder}) {
12505:             if ($commblocks{$type}) {
12506:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
12507:             }
12508:             unless (($idx eq 'add') || ($changes{$itemid})) {
12509:                 if (ref($current{'commblocks'}) eq 'HASH') {
12510:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
12511:                         $changes{$itemid} = 1;
12512:                     }
12513:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
12514:                     $changes{$itemid} = 1;
12515:                 }
12516:             }
12517:         }
12518:         $confhash{$itemid}{'courses'} = {};
12519:         my %crsdeletions;
12520:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
12521:         if (@delcrs) {
12522:             map { $crsdeletions{$_} = 1; } @delcrs;
12523:         }
12524:         if (ref($current{'courses'}) eq 'HASH') {
12525:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
12526:                 if ($crsdeletions{$cid}) {
12527:                     $changes{$itemid} = 1;
12528:                 } else {
12529:                     $confhash{$itemid}{'courses'}{$cid} = 1;
12530:                 }
12531:             }
12532:         }
12533:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
12534:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
12535:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) && 
12536:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
12537:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
12538:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
12539:                 $errors .= '<li><span class="LC_error">'.
12540:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
12541:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
12542:                            '</span></li>';
12543:             } else {
12544:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
12545:                 $changes{$itemid} = 1;
12546:             }
12547:         }
12548:     }
12549:     if (@allpos > 0) {
12550:         my $idx = 0;
12551:         foreach my $itemid (@allpos) {
12552:             if ($itemid ne '') {
12553:                 $confhash{$itemid}{'order'} = $idx;
12554:                 unless ($changes{$itemid}) {
12555:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12556:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12557:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
12558:                                 $changes{$itemid} = 1;
12559:                             }
12560:                         }
12561:                     }
12562:                 }
12563:                 $idx ++;
12564:             }
12565:         }
12566:     }
12567:     if (keys(%changes)) {
12568:         my %defaultshash = (
12569:                               ipaccess => \%confhash,
12570:                            );
12571:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12572:                                                  $dom);
12573:         if ($putresult eq 'ok') {
12574:             my $cachetime = 1800;
12575:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
12576:             if (ref($lastactref) eq 'HASH') {
12577:                 $lastactref->{'ipaccess'} = 1;
12578:             }
12579:             $resulttext = &mt('Changes made:').'<ul>';
12580:             my %bynum;
12581:             foreach my $itemid (sort(keys(%changes))) {
12582:                 if (ref($confhash{$itemid}) eq 'HASH') {
12583:                     my $position = $confhash{$itemid}{'order'};
12584:                     if ($position =~ /^\d+$/) {
12585:                         $bynum{$position} = $itemid;
12586:                     }
12587:                 }
12588:             }
12589:             if (keys(%deletions)) {
12590:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
12591:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12592:                 }
12593:             }
12594:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12595:                 my $itemid = $bynum{$pos};
12596:                 if (ref($confhash{$itemid}) eq 'HASH') {
12597:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
12598:                     my $position = $pos + 1;
12599:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12600:                     if ($confhash{$itemid}{'ip'} eq '') {
12601:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
12602:                     } else {
12603:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
12604:                     }
12605:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
12606:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
12607:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
12608:                                        '</li>';
12609:                     } else {
12610:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
12611:                     }
12612:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
12613:                         my @courses;
12614:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
12615:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); 
12616:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
12617:                         }
12618:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
12619:                                              join('</li><li>',@courses).'</li></ul>';
12620:                     } else {
12621:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
12622:                     }
12623:                 }
12624:             }
12625:         } else {
12626:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12627:         }
12628:     } else {
12629:         $resulttext = &mt('No changes made');
12630:     }
12631:     if ($errors) {
12632:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12633:                        $errors.'</ul></p>';
12634:     }
12635:     return $resulttext;
12636: }
12637: 
12638: sub get_ipaccess_id {
12639:     my ($domain,$location) = @_;
12640:     # get lock on ipaccess db
12641:     my $lockhash = {
12642:                       lock => $env{'user.name'}.
12643:                               ':'.$env{'user.domain'},
12644:                    };
12645:     my $tries = 0;
12646:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12647:     my ($id,$error);
12648: 
12649:     while (($gotlock ne 'ok') && ($tries<10)) {
12650:         $tries ++;
12651:         sleep (0.1);
12652:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12653:     }
12654:     if ($gotlock eq 'ok') {
12655:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
12656:         if ($currids{'lock'}) {
12657:             delete($currids{'lock'});
12658:             if (keys(%currids)) {
12659:                 my @curr = sort { $a <=> $b } keys(%currids);
12660:                 if ($curr[-1] =~ /^\d+$/) {
12661:                     $id = 1 + $curr[-1];
12662:                 }
12663:             } else {
12664:                 $id = 1;
12665:             }
12666:             if ($id) {
12667:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
12668:                     $error = 'nostore';
12669:                 }
12670:             } else {
12671:                 $error = 'nonumber';
12672:             }
12673:         }
12674:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
12675:     } else {
12676:         $error = 'nolock';
12677:     }
12678:     return ($id,$error);
12679: }
12680: 
12681: sub modify_rolecolors {
12682:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12683:     my ($resulttext,%rolehash);
12684:     $rolehash{'rolecolors'} = {};
12685:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12686:         if ($domconfig{'rolecolors'} eq '') {
12687:             $domconfig{'rolecolors'} = {};
12688:         }
12689:     }
12690:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12691:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12692:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12693:                                              $dom);
12694:     if ($putresult eq 'ok') {
12695:         if (keys(%changes) > 0) {
12696:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12697:             if (ref($lastactref) eq 'HASH') {
12698:                 $lastactref->{'domainconfig'} = 1;
12699:             }
12700:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12701:                                              $rolehash{'rolecolors'});
12702:         } else {
12703:             $resulttext = &mt('No changes made to default color schemes');
12704:         }
12705:     } else {
12706:         $resulttext = '<span class="LC_error">'.
12707: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12708:     }
12709:     if ($errors) {
12710:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12711:                        $errors.'</ul>';
12712:     }
12713:     return $resulttext;
12714: }
12715: 
12716: sub modify_colors {
12717:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12718:     my (%changes,%choices);
12719:     my @bgs;
12720:     my @links = ('link','alink','vlink');
12721:     my @logintext;
12722:     my @images;
12723:     my $servadm = $r->dir_config('lonAdmEMail');
12724:     my $errors;
12725:     my %defaults;
12726:     foreach my $role (@{$roles}) {
12727:         if ($role eq 'login') {
12728:             %choices = &login_choices();
12729:             @logintext = ('textcol','bgcol');
12730:         } else {
12731:             %choices = &color_font_choices();
12732:         }
12733:         if ($role eq 'login') {
12734:             @images = ('img','logo','domlogo','login');
12735:             @bgs = ('pgbg','mainbg','sidebg');
12736:         } else {
12737:             @images = ('img');
12738:             @bgs = ('pgbg','tabbg','sidebg');
12739:         }
12740:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12741:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
12742:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
12743:         }
12744:         if ($role eq 'login') {
12745:             foreach my $item (@logintext) {
12746:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12747:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12748:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12749:                 }
12750:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
12751:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12752:                 }
12753:             }
12754:         } else {
12755:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
12756:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
12757:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
12758:             }
12759:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
12760:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
12761:             }
12762:         }
12763:         foreach my $item (@bgs) {
12764:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12765:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12766:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12767:             }
12768:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
12769:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12770:             }
12771:         }
12772:         foreach my $item (@links) {
12773:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12774:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12775:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12776:             }
12777:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
12778:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12779:             }
12780:         }
12781:         my ($configuserok,$author_ok,$switchserver) = 
12782:             &config_check($dom,$confname,$servadm);
12783:         my ($width,$height) = &thumb_dimensions();
12784:         if (ref($domconfig->{$role}) ne 'HASH') {
12785:             $domconfig->{$role} = {};
12786:         }
12787:         foreach my $img (@images) {
12788:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
12789:                 if (defined($env{'form.login_showlogo_'.$img})) {
12790:                     $confhash->{$role}{'showlogo'}{$img} = 1;
12791:                 } else { 
12792:                     $confhash->{$role}{'showlogo'}{$img} = 0;
12793:                 }
12794:             } 
12795: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
12796: 		 && !defined($domconfig->{$role}{$img})
12797: 		 && !$env{'form.'.$role.'_del_'.$img}
12798: 		 && $env{'form.'.$role.'_import_'.$img}) {
12799: 		# import the old configured image from the .tab setting
12800: 		# if they haven't provided a new one 
12801: 		$domconfig->{$role}{$img} = 
12802: 		    $env{'form.'.$role.'_import_'.$img};
12803: 	    }
12804:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
12805:                 my $error;
12806:                 if ($configuserok eq 'ok') {
12807:                     if ($switchserver) {
12808:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
12809:                     } else {
12810:                         if ($author_ok eq 'ok') {
12811:                             my ($result,$logourl) = 
12812:                                 &publishlogo($r,'upload',$role.'_'.$img,
12813:                                            $dom,$confname,$img,$width,$height);
12814:                             if ($result eq 'ok') {
12815:                                 $confhash->{$role}{$img} = $logourl;
12816:                                 $changes{$role}{'images'}{$img} = 1;
12817:                             } else {
12818:                                 $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);
12819:                             }
12820:                         } else {
12821:                             $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);
12822:                         }
12823:                     }
12824:                 } else {
12825:                     $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);
12826:                 }
12827:                 if ($error) {
12828:                     &Apache::lonnet::logthis($error);
12829:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12830:                 }
12831:             } elsif ($domconfig->{$role}{$img} ne '') {
12832:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
12833:                     my $error;
12834:                     if ($configuserok eq 'ok') {
12835: # is confname an author?
12836:                         if ($switchserver eq '') {
12837:                             if ($author_ok eq 'ok') {
12838:                                 my ($result,$logourl) = 
12839:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
12840:                                             $dom,$confname,$img,$width,$height);
12841:                                 if ($result eq 'ok') {
12842:                                     $confhash->{$role}{$img} = $logourl;
12843: 				    $changes{$role}{'images'}{$img} = 1;
12844:                                 }
12845:                             }
12846:                         }
12847:                     }
12848:                 }
12849:             }
12850:         }
12851:         if (ref($domconfig) eq 'HASH') {
12852:             if (ref($domconfig->{$role}) eq 'HASH') {
12853:                 foreach my $img (@images) {
12854:                     if ($domconfig->{$role}{$img} ne '') {
12855:                         if ($env{'form.'.$role.'_del_'.$img}) {
12856:                             $confhash->{$role}{$img} = '';
12857:                             $changes{$role}{'images'}{$img} = 1;
12858:                         } else {
12859:                             if ($confhash->{$role}{$img} eq '') {
12860:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
12861:                             }
12862:                         }
12863:                     } else {
12864:                         if ($env{'form.'.$role.'_del_'.$img}) {
12865:                             $confhash->{$role}{$img} = '';
12866:                             $changes{$role}{'images'}{$img} = 1;
12867:                         } 
12868:                     }
12869:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
12870:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
12871:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
12872:                                 $domconfig->{$role}{'showlogo'}{$img}) {
12873:                                 $changes{$role}{'showlogo'}{$img} = 1; 
12874:                             }
12875:                         } else {
12876:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12877:                                 $changes{$role}{'showlogo'}{$img} = 1;
12878:                             }
12879:                         }
12880:                     }
12881:                 }
12882:                 if ($domconfig->{$role}{'font'} ne '') {
12883:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
12884:                         $changes{$role}{'font'} = 1;
12885:                     }
12886:                 } else {
12887:                     if ($confhash->{$role}{'font'}) {
12888:                         $changes{$role}{'font'} = 1;
12889:                     }
12890:                 }
12891:                 if ($role ne 'login') {
12892:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
12893:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
12894:                             $changes{$role}{'fontmenu'} = 1;
12895:                         }
12896:                     } else {
12897:                         if ($confhash->{$role}{'fontmenu'}) {
12898:                             $changes{$role}{'fontmenu'} = 1;
12899:                         }
12900:                     }
12901:                 }
12902:                 foreach my $item (@bgs) {
12903:                     if ($domconfig->{$role}{$item} ne '') {
12904:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12905:                             $changes{$role}{'bgs'}{$item} = 1;
12906:                         } 
12907:                     } else {
12908:                         if ($confhash->{$role}{$item}) {
12909:                             $changes{$role}{'bgs'}{$item} = 1;
12910:                         }
12911:                     }
12912:                 }
12913:                 foreach my $item (@links) {
12914:                     if ($domconfig->{$role}{$item} ne '') {
12915:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12916:                             $changes{$role}{'links'}{$item} = 1;
12917:                         }
12918:                     } else {
12919:                         if ($confhash->{$role}{$item}) {
12920:                             $changes{$role}{'links'}{$item} = 1;
12921:                         }
12922:                     }
12923:                 }
12924:                 foreach my $item (@logintext) {
12925:                     if ($domconfig->{$role}{$item} ne '') {
12926:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12927:                             $changes{$role}{'logintext'}{$item} = 1;
12928:                         }
12929:                     } else {
12930:                         if ($confhash->{$role}{$item}) {
12931:                             $changes{$role}{'logintext'}{$item} = 1;
12932:                         }
12933:                     }
12934:                 }
12935:             } else {
12936:                 &default_change_checker($role,\@images,\@links,\@bgs,
12937:                                         \@logintext,$confhash,\%changes); 
12938:             }
12939:         } else {
12940:             &default_change_checker($role,\@images,\@links,\@bgs,
12941:                                     \@logintext,$confhash,\%changes); 
12942:         }
12943:     }
12944:     return ($errors,%changes);
12945: }
12946: 
12947: sub config_check {
12948:     my ($dom,$confname,$servadm) = @_;
12949:     my ($configuserok,$author_ok,$switchserver,%currroles);
12950:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12951:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12952:                                                    $confname,$servadm);
12953:     if ($configuserok eq 'ok') {
12954:         $switchserver = &check_switchserver($dom,$confname);
12955:         if ($switchserver eq '') {
12956:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12957:         }
12958:     }
12959:     return ($configuserok,$author_ok,$switchserver);
12960: }
12961: 
12962: sub default_change_checker {
12963:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12964:     foreach my $item (@{$links}) {
12965:         if ($confhash->{$role}{$item}) {
12966:             $changes->{$role}{'links'}{$item} = 1;
12967:         }
12968:     }
12969:     foreach my $item (@{$bgs}) {
12970:         if ($confhash->{$role}{$item}) {
12971:             $changes->{$role}{'bgs'}{$item} = 1;
12972:         }
12973:     }
12974:     foreach my $item (@{$logintext}) {
12975:         if ($confhash->{$role}{$item}) {
12976:             $changes->{$role}{'logintext'}{$item} = 1;
12977:         }
12978:     }
12979:     foreach my $img (@{$images}) {
12980:         if ($env{'form.'.$role.'_del_'.$img}) {
12981:             $confhash->{$role}{$img} = '';
12982:             $changes->{$role}{'images'}{$img} = 1;
12983:         }
12984:         if ($role eq 'login') {
12985:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12986:                 $changes->{$role}{'showlogo'}{$img} = 1;
12987:             }
12988:         }
12989:     }
12990:     if ($confhash->{$role}{'font'}) {
12991:         $changes->{$role}{'font'} = 1;
12992:     }
12993: }
12994: 
12995: sub display_colorchgs {
12996:     my ($dom,$changes,$roles,$confhash) = @_;
12997:     my (%choices,$resulttext);
12998:     if (!grep(/^login$/,@{$roles})) {
12999:         $resulttext = &mt('Changes made:').'<br />';
13000:     }
13001:     foreach my $role (@{$roles}) {
13002:         if ($role eq 'login') {
13003:             %choices = &login_choices();
13004:         } else {
13005:             %choices = &color_font_choices();
13006:         }
13007:         if (ref($changes->{$role}) eq 'HASH') {
13008:             if ($role ne 'login') {
13009:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13010:             }
13011:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
13012:                 if ($role ne 'login') {
13013:                     $resulttext .= '<ul>';
13014:                 }
13015:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
13016:                     if ($role ne 'login') {
13017:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
13018:                     }
13019:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
13020:                         if (($role eq 'login') && ($key eq 'showlogo')) {
13021:                             if ($confhash->{$role}{$key}{$item}) {
13022:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
13023:                             } else {
13024:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
13025:                             }
13026:                         } elsif ($confhash->{$role}{$item} eq '') {
13027:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
13028:                         } else {
13029:                             my $newitem = $confhash->{$role}{$item};
13030:                             if ($key eq 'images') {
13031:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
13032:                             }
13033:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
13034:                         }
13035:                     }
13036:                     if ($role ne 'login') {
13037:                         $resulttext .= '</ul></li>';
13038:                     }
13039:                 } else {
13040:                     if ($confhash->{$role}{$key} eq '') {
13041:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
13042:                     } else {
13043:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
13044:                     }
13045:                 }
13046:                 if ($role ne 'login') {
13047:                     $resulttext .= '</ul>';
13048:                 }
13049:             }
13050:         }
13051:     }
13052:     return $resulttext;
13053: }
13054: 
13055: sub thumb_dimensions {
13056:     return ('200','50');
13057: }
13058: 
13059: sub check_dimensions {
13060:     my ($inputfile) = @_;
13061:     my ($fullwidth,$fullheight);
13062:     if ($inputfile =~ m|^[/\w.\-]+$|) {
13063:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
13064:             my $imageinfo = <PIPE>;
13065:             if (!close(PIPE)) {
13066:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
13067:             }
13068:             chomp($imageinfo);
13069:             my ($fullsize) = 
13070:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
13071:             if ($fullsize) {
13072:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
13073:             }
13074:         }
13075:     }
13076:     return ($fullwidth,$fullheight);
13077: }
13078: 
13079: sub check_configuser {
13080:     my ($uhome,$dom,$confname,$servadm) = @_;
13081:     my ($configuserok,%currroles);
13082:     if ($uhome eq 'no_host') {
13083:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
13084:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
13085:         $configuserok = 
13086:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
13087:                              $configpass,'','','','','',undef,$servadm);
13088:     } else {
13089:         $configuserok = 'ok';
13090:         %currroles = 
13091:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
13092:     }
13093:     return ($configuserok,%currroles);
13094: }
13095: 
13096: sub check_authorstatus {
13097:     my ($dom,$confname,%currroles) = @_;
13098:     my $author_ok;
13099:     if (!$currroles{':'.$dom.':au'}) {
13100:         my $start = time;
13101:         my $end = 0;
13102:         $author_ok = 
13103:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
13104:                                         'au',$end,$start,'','','domconfig');
13105:     } else {
13106:         $author_ok = 'ok';
13107:     }
13108:     return $author_ok;
13109: }
13110: 
13111: sub publishlogo {
13112:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
13113:     my ($output,$fname,$logourl,$madethumb);
13114:     if ($action eq 'upload') {
13115:         $fname=$env{'form.'.$formname.'.filename'};
13116:         chop($env{'form.'.$formname});
13117:     } else {
13118:         ($fname) = ($formname =~ /([^\/]+)$/);
13119:     }
13120:     if ($savefileas ne '') {
13121:         $fname = $savefileas;
13122:     }
13123:     $fname=&Apache::lonnet::clean_filename($fname);
13124: # See if there is anything left
13125:     unless ($fname) { return ('error: no uploaded file'); }
13126:     $fname="$subdir/$fname";
13127:     my $docroot=$r->dir_config('lonDocRoot');
13128:     my $filepath="$docroot/priv";
13129:     my $relpath = "$dom/$confname";
13130:     my ($fnamepath,$file,$fetchthumb);
13131:     $file=$fname;
13132:     if ($fname=~m|/|) {
13133:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
13134:     }
13135:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
13136:     my $count;
13137:     for ($count=5;$count<=$#parts;$count++) {
13138:         $filepath.="/$parts[$count]";
13139:         if ((-e $filepath)!=1) {
13140:             mkdir($filepath,02770);
13141:         }
13142:     }
13143:     # Check for bad extension and disallow upload
13144:     if ($file=~/\.(\w+)$/ &&
13145:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
13146:         $output = 
13147:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
13148:     } elsif ($file=~/\.(\w+)$/ &&
13149:         !defined(&Apache::loncommon::fileembstyle($1))) {
13150:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
13151:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
13152:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
13153:     } elsif (-d "$filepath/$file") {
13154:         $output = &mt('Filename is a directory name - rename the file and re-upload');
13155:     } else {
13156:         my $source = $filepath.'/'.$file;
13157:         my $logfile;
13158:         if (!open($logfile,">>",$source.'.log')) {
13159:             return (&mt('No write permission to Authoring Space'));
13160:         }
13161:         print $logfile
13162: "\n================= Publish ".localtime()." ================\n".
13163: $env{'user.name'}.':'.$env{'user.domain'}."\n";
13164: # Save the file
13165:         if (!open(FH,">",$source)) {
13166:             &Apache::lonnet::logthis('Failed to create '.$source);
13167:             return (&mt('Failed to create file'));
13168:         }
13169:         if ($action eq 'upload') {
13170:             if (!print FH ($env{'form.'.$formname})) {
13171:                 &Apache::lonnet::logthis('Failed to write to '.$source);
13172:                 return (&mt('Failed to write file'));
13173:             }
13174:         } else {
13175:             my $original = &Apache::lonnet::filelocation('',$formname);
13176:             if(!copy($original,$source)) {
13177:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
13178:                 return (&mt('Failed to write file'));
13179:             }
13180:         }
13181:         close(FH);
13182:         chmod(0660, $source); # Permissions to rw-rw---.
13183: 
13184:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
13185:         my $copyfile=$targetdir.'/'.$file;
13186: 
13187:         my @parts=split(/\//,$targetdir);
13188:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
13189:         for (my $count=5;$count<=$#parts;$count++) {
13190:             $path.="/$parts[$count]";
13191:             if (!-e $path) {
13192:                 print $logfile "\nCreating directory ".$path;
13193:                 mkdir($path,02770);
13194:             }
13195:         }
13196:         my $versionresult;
13197:         if (-e $copyfile) {
13198:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
13199:         } else {
13200:             $versionresult = 'ok';
13201:         }
13202:         if ($versionresult eq 'ok') {
13203:             if (copy($source,$copyfile)) {
13204:                 print $logfile "\nCopied original source to ".$copyfile."\n";
13205:                 $output = 'ok';
13206:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
13207:                 push(@{$modified_urls},[$copyfile,$source]);
13208:                 my $metaoutput = 
13209:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
13210:                 unless ($registered_cleanup) {
13211:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
13212:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13213:                     $registered_cleanup=1;
13214:                 }
13215:             } else {
13216:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
13217:                 $output = &mt('Failed to copy file to RES space').", $!";
13218:             }
13219:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
13220:                 my $inputfile = $filepath.'/'.$file;
13221:                 my $outfile = $filepath.'/'.'tn-'.$file;
13222:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
13223:                 if ($fullwidth ne '' && $fullheight ne '') { 
13224:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
13225:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
13226:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
13227:                         system({$args[0]} @args);
13228:                         chmod(0660, $filepath.'/tn-'.$file);
13229:                         if (-e $outfile) {
13230:                             my $copyfile=$targetdir.'/tn-'.$file;
13231:                             if (copy($outfile,$copyfile)) {
13232:                                 print $logfile "\nCopied source to ".$copyfile."\n";
13233:                                 my $thumb_metaoutput = 
13234:                                     &write_metadata($dom,$confname,$formname,
13235:                                                     $targetdir,'tn-'.$file,$logfile);
13236:                                 push(@{$modified_urls},[$copyfile,$outfile]);
13237:                                 unless ($registered_cleanup) {
13238:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
13239:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13240:                                     $registered_cleanup=1;
13241:                                 }
13242:                                 $madethumb = 1;
13243:                             } else {
13244:                                 print $logfile "\nUnable to write ".$copyfile.
13245:                                                ':'.$!."\n";
13246:                             }
13247:                         }
13248:                     }
13249:                 }
13250:             }
13251:         } else {
13252:             $output = $versionresult;
13253:         }
13254:     }
13255:     return ($output,$logourl,$madethumb);
13256: }
13257: 
13258: sub logo_versioning {
13259:     my ($targetdir,$file,$logfile) = @_;
13260:     my $target = $targetdir.'/'.$file;
13261:     my ($maxversion,$fn,$extn,$output);
13262:     $maxversion = 0;
13263:     if ($file =~ /^(.+)\.(\w+)$/) {
13264:         $fn=$1;
13265:         $extn=$2;
13266:     }
13267:     opendir(DIR,$targetdir);
13268:     while (my $filename=readdir(DIR)) {
13269:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
13270:             $maxversion=($1>$maxversion)?$1:$maxversion;
13271:         }
13272:     }
13273:     $maxversion++;
13274:     print $logfile "\nCreating old version ".$maxversion."\n";
13275:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
13276:     if (copy($target,$copyfile)) {
13277:         print $logfile "Copied old target to ".$copyfile."\n";
13278:         $copyfile=$copyfile.'.meta';
13279:         if (copy($target.'.meta',$copyfile)) {
13280:             print $logfile "Copied old target metadata to ".$copyfile."\n";
13281:             $output = 'ok';
13282:         } else {
13283:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
13284:             $output = &mt('Failed to copy old meta').", $!, ";
13285:         }
13286:     } else {
13287:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
13288:         $output = &mt('Failed to copy old target').", $!, ";
13289:     }
13290:     return $output;
13291: }
13292: 
13293: sub write_metadata {
13294:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
13295:     my (%metadatafields,%metadatakeys,$output);
13296:     $metadatafields{'title'}=$formname;
13297:     $metadatafields{'creationdate'}=time;
13298:     $metadatafields{'lastrevisiondate'}=time;
13299:     $metadatafields{'copyright'}='public';
13300:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
13301:                                          $env{'user.domain'};
13302:     $metadatafields{'authorspace'}=$confname.':'.$dom;
13303:     $metadatafields{'domain'}=$dom;
13304:     {
13305:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
13306:         my $mfh;
13307:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
13308:             foreach (sort(keys(%metadatafields))) {
13309:                 unless ($_=~/\./) {
13310:                     my $unikey=$_;
13311:                     $unikey=~/^([A-Za-z]+)/;
13312:                     my $tag=$1;
13313:                     $tag=~tr/A-Z/a-z/;
13314:                     print $mfh "\n\<$tag";
13315:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
13316:                         my $value=$metadatafields{$unikey.'.'.$_};
13317:                         $value=~s/\"/\'\'/g;
13318:                         print $mfh ' '.$_.'="'.$value.'"';
13319:                     }
13320:                     print $mfh '>'.
13321:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
13322:                             .'</'.$tag.'>';
13323:                 }
13324:             }
13325:             $output = 'ok';
13326:             print $logfile "\nWrote metadata";
13327:             close($mfh);
13328:         } else {
13329:             print $logfile "\nFailed to open metadata file";
13330:             $output = &mt('Could not write metadata');
13331:         }
13332:     }
13333:     return $output;
13334: }
13335: 
13336: sub notifysubscribed {
13337:     foreach my $targetsource (@{$modified_urls}){
13338:         next unless (ref($targetsource) eq 'ARRAY');
13339:         my ($target,$source)=@{$targetsource};
13340:         if ($source ne '') {
13341:             if (open(my $logfh,">>",$source.'.log')) {
13342:                 print $logfh "\nCleanup phase: Notifications\n";
13343:                 my @subscribed=&subscribed_hosts($target);
13344:                 foreach my $subhost (@subscribed) {
13345:                     print $logfh "\nNotifying host ".$subhost.':';
13346:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
13347:                     print $logfh $reply;
13348:                 }
13349:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
13350:                 foreach my $subhost (@subscribedmeta) {
13351:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
13352:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
13353:                                                         $subhost);
13354:                     print $logfh $reply;
13355:                 }
13356:                 print $logfh "\n============ Done ============\n";
13357:                 close($logfh);
13358:             }
13359:         }
13360:     }
13361:     return OK;
13362: }
13363: 
13364: sub subscribed_hosts {
13365:     my ($target) = @_;
13366:     my @subscribed;
13367:     if (open(my $fh,"<","$target.subscription")) {
13368:         while (my $subline=<$fh>) {
13369:             if ($subline =~ /^($match_lonid):/) {
13370:                 my $host = $1;
13371:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
13372:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
13373:                         push(@subscribed,$host);
13374:                     }
13375:                 }
13376:             }
13377:         }
13378:     }
13379:     return @subscribed;
13380: }
13381: 
13382: sub check_switchserver {
13383:     my ($dom,$confname) = @_;
13384:     my ($allowed,$switchserver);
13385:     my $home = &Apache::lonnet::homeserver($confname,$dom);
13386:     if ($home eq 'no_host') {
13387:         $home = &Apache::lonnet::domain($dom,'primary');
13388:     }
13389:     my @ids=&Apache::lonnet::current_machine_ids();
13390:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
13391:     if (!$allowed) {
13392: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
13393:     }
13394:     return $switchserver;
13395: }
13396: 
13397: sub modify_quotas {
13398:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13399:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
13400:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
13401:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
13402:         $validationfieldsref);
13403:     if ($action eq 'quotas') {
13404:         $context = 'tools'; 
13405:     } else {
13406:         $context = $action;
13407:     }
13408:     if ($context eq 'requestcourses') {
13409:         @usertools = ('official','unofficial','community','textbook','placement','lti');
13410:         @options =('norequest','approval','validate','autolimit');
13411:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
13412:         %titles = &courserequest_titles();
13413:         $toolregexp = join('|',@usertools);
13414:         %conditions = &courserequest_conditions();
13415:         $confname = $dom.'-domainconfig';
13416:         my $servadm = $r->dir_config('lonAdmEMail');
13417:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13418:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
13419:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
13420:     } elsif ($context eq 'requestauthor') {
13421:         @usertools = ('author');
13422:         %titles = &authorrequest_titles();
13423:     } else {
13424:         @usertools = ('aboutme','blog','webdav','portfolio');
13425:         %titles = &tool_titles();
13426:     }
13427:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13428:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13429:     foreach my $key (keys(%env)) {
13430:         if ($context eq 'requestcourses') {
13431:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
13432:                 my $item = $1;
13433:                 my $type = $2;
13434:                 if ($type =~ /^limit_(.+)/) {
13435:                     $limithash{$item}{$1} = $env{$key};
13436:                 } else {
13437:                     $confhash{$item}{$type} = $env{$key};
13438:                 }
13439:             }
13440:         } elsif ($context eq 'requestauthor') {
13441:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
13442:                 $confhash{$1} = $env{$key};
13443:             }
13444:         } else {
13445:             if ($key =~ /^form\.quota_(.+)$/) {
13446:                 $confhash{'defaultquota'}{$1} = $env{$key};
13447:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
13448:                 $confhash{'authorquota'}{$1} = $env{$key};
13449:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
13450:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
13451:             }
13452:         }
13453:     }
13454:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13455:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
13456:         @approvalnotify = sort(@approvalnotify);
13457:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
13458:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
13459:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
13460:         foreach my $type (@hasuniquecode) {
13461:             if (grep(/^\Q$type\E$/,@crstypes)) {
13462:                 $confhash{'uniquecode'}{$type} = 1;
13463:             }
13464:         }
13465:         my (%newbook,%allpos);
13466:         if ($context eq 'requestcourses') {
13467:             foreach my $type ('textbooks','templates') {
13468:                 @{$allpos{$type}} = (); 
13469:                 my $invalid;
13470:                 if ($type eq 'textbooks') {
13471:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
13472:                 } else {
13473:                     $invalid = &mt('Invalid LON-CAPA course for template');
13474:                 }
13475:                 if ($env{'form.'.$type.'_addbook'}) {
13476:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
13477:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
13478:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
13479:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
13480:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13481:                         } else {
13482:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
13483:                             my $position = $env{'form.'.$type.'_addbook_pos'};
13484:                             $position =~ s/\D+//g;
13485:                             if ($position ne '') {
13486:                                 $allpos{$type}[$position] = $newbook{$type};
13487:                             }
13488:                         }
13489:                     } else {
13490:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13491:                     }
13492:                 }
13493:             } 
13494:         }
13495:         if (ref($domconfig{$action}) eq 'HASH') {
13496:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
13497:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
13498:                     $changes{'notify'}{'approval'} = 1;
13499:                 }
13500:             } else {
13501:                 if ($confhash{'notify'}{'approval'}) {
13502:                     $changes{'notify'}{'approval'} = 1;
13503:                 }
13504:             }
13505:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
13506:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
13507:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
13508:                         unless ($confhash{'uniquecode'}{$crstype}) {
13509:                             $changes{'uniquecode'} = 1;
13510:                         }
13511:                     }
13512:                     unless ($changes{'uniquecode'}) {
13513:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
13514:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
13515:                                 $changes{'uniquecode'} = 1;
13516:                             }
13517:                         }
13518:                     }
13519:                } else {
13520:                    $changes{'uniquecode'} = 1;
13521:                }
13522:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
13523:                 $changes{'uniquecode'} = 1;
13524:             }
13525:             if ($context eq 'requestcourses') {
13526:                 foreach my $type ('textbooks','templates') {
13527:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
13528:                         my %deletions;
13529:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
13530:                         if (@todelete) {
13531:                             map { $deletions{$_} = 1; } @todelete;
13532:                         }
13533:                         my %imgdeletions;
13534:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
13535:                         if (@todeleteimages) {
13536:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
13537:                         }
13538:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
13539:                         for (my $i=0; $i<=$maxnum; $i++) {
13540:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
13541:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
13542:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
13543:                                 if ($deletions{$key}) {
13544:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
13545:                                         #FIXME need to obsolete item in RES space
13546:                                     }
13547:                                     next;
13548:                                 } else {
13549:                                     my $newpos = $env{'form.'.$itemid};
13550:                                     $newpos =~ s/\D+//g;
13551:                                     foreach my $item ('subject','title','publisher','author') {
13552:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13553:                                                  ($type eq 'templates'));
13554:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
13555:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
13556:                                             $changes{$type}{$key} = 1;
13557:                                         }
13558:                                     }
13559:                                     $allpos{$type}[$newpos] = $key;
13560:                                 }
13561:                                 if ($imgdeletions{$key}) {
13562:                                     $changes{$type}{$key} = 1;
13563:                                     #FIXME need to obsolete item in RES space
13564:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
13565:                                     my ($cdom,$cnum) = split(/_/,$key);
13566:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13567:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13568:                                     } else {
13569:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
13570:                                                                                       $cdom,$cnum,$type,$configuserok,
13571:                                                                                       $switchserver,$author_ok);
13572:                                         if ($imgurl) {
13573:                                             $confhash{$type}{$key}{'image'} = $imgurl;
13574:                                             $changes{$type}{$key} = 1; 
13575:                                         }
13576:                                         if ($error) {
13577:                                             &Apache::lonnet::logthis($error);
13578:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13579:                                         }
13580:                                     }
13581:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
13582:                                     $confhash{$type}{$key}{'image'} = 
13583:                                         $domconfig{$action}{$type}{$key}{'image'};
13584:                                 }
13585:                             }
13586:                         }
13587:                     }
13588:                 }
13589:             }
13590:         } else {
13591:             if ($confhash{'notify'}{'approval'}) {
13592:                 $changes{'notify'}{'approval'} = 1;
13593:             }
13594:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
13595:                 $changes{'uniquecode'} = 1;
13596:             }
13597:         }
13598:         if ($context eq 'requestcourses') {
13599:             foreach my $type ('textbooks','templates') {
13600:                 if ($newbook{$type}) {
13601:                     $changes{$type}{$newbook{$type}} = 1;
13602:                     foreach my $item ('subject','title','publisher','author') {
13603:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13604:                                  ($type eq 'template'));
13605:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
13606:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
13607:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
13608:                         }
13609:                     }
13610:                     if ($type eq 'textbooks') {
13611:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
13612:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
13613:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13614:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13615:                             } else {
13616:                                 my ($imageurl,$error) =
13617:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
13618:                                                             $configuserok,$switchserver,$author_ok);
13619:                                 if ($imageurl) {
13620:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13621:                                 }
13622:                                 if ($error) {
13623:                                     &Apache::lonnet::logthis($error);
13624:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13625:                                 }
13626:                             }
13627:                         }
13628:                     }
13629:                 }
13630:                 if (@{$allpos{$type}} > 0) {
13631:                     my $idx = 0;
13632:                     foreach my $item (@{$allpos{$type}}) {
13633:                         if ($item ne '') {
13634:                             $confhash{$type}{$item}{'order'} = $idx;
13635:                             if (ref($domconfig{$action}) eq 'HASH') {
13636:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13637:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13638:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13639:                                             $changes{$type}{$item} = 1;
13640:                                         }
13641:                                     }
13642:                                 }
13643:                             }
13644:                             $idx ++;
13645:                         }
13646:                     }
13647:                 }
13648:             }
13649:             if (ref($validationitemsref) eq 'ARRAY') {
13650:                 foreach my $item (@{$validationitemsref}) {
13651:                     if ($item eq 'fields') {
13652:                         my @changed;
13653:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13654:                         if (@{$confhash{'validation'}{$item}} > 0) {
13655:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13656:                         }
13657:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13658:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13659:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13660:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13661:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13662:                                 } else {
13663:                                     @changed = @{$confhash{'validation'}{$item}};
13664:                                 }
13665:                             } else {
13666:                                 @changed = @{$confhash{'validation'}{$item}};
13667:                             }
13668:                         } else {
13669:                             @changed = @{$confhash{'validation'}{$item}};
13670:                         }
13671:                         if (@changed) {
13672:                             if ($confhash{'validation'}{$item}) {
13673:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13674:                             } else {
13675:                                 $changes{'validation'}{$item} = &mt('None');
13676:                             }
13677:                         }
13678:                     } else {
13679:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13680:                         if ($item eq 'markup') {
13681:                             if ($env{'form.requestcourses_validation_'.$item}) {
13682:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13683:                             }
13684:                         }
13685:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13686:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13687:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13688:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13689:                                 }
13690:                             } else {
13691:                                 if ($confhash{'validation'}{$item} ne '') {
13692:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13693:                                 }
13694:                             }
13695:                         } else {
13696:                             if ($confhash{'validation'}{$item} ne '') {
13697:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13698:                             }
13699:                         }
13700:                     }
13701:                 }
13702:             }
13703:             if ($env{'form.validationdc'}) {
13704:                 my $newval = $env{'form.validationdc'};
13705:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13706:                 if (exists($domcoords{$newval})) {
13707:                     $confhash{'validation'}{'dc'} = $newval;
13708:                 }
13709:             }
13710:             if (ref($confhash{'validation'}) eq 'HASH') {
13711:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13712:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13713:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13714:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13715:                                 if ($confhash{'validation'}{'dc'} eq '') {
13716:                                     $changes{'validation'}{'dc'} = &mt('None');
13717:                                 } else {
13718:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13719:                                 }
13720:                             }
13721:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13722:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13723:                         }
13724:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13725:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13726:                     }
13727:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13728:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13729:                 }
13730:             } else {
13731:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13732:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13733:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13734:                             $changes{'validation'}{'dc'} = &mt('None');
13735:                         }
13736:                     }
13737:                 }
13738:             }
13739:         }
13740:     } else {
13741:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13742:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
13743:     }
13744:     foreach my $item (@usertools) {
13745:         foreach my $type (@{$types},'default','_LC_adv') {
13746:             my $unset; 
13747:             if ($context eq 'requestcourses') {
13748:                 $unset = '0';
13749:                 if ($type eq '_LC_adv') {
13750:                     $unset = '';
13751:                 }
13752:                 if ($confhash{$item}{$type} eq 'autolimit') {
13753:                     $confhash{$item}{$type} .= '=';
13754:                     unless ($limithash{$item}{$type} =~ /\D/) {
13755:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13756:                     }
13757:                 }
13758:             } elsif ($context eq 'requestauthor') {
13759:                 $unset = '0';
13760:                 if ($type eq '_LC_adv') {
13761:                     $unset = '';
13762:                 }
13763:             } else {
13764:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13765:                     $confhash{$item}{$type} = 1;
13766:                 } else {
13767:                     $confhash{$item}{$type} = 0;
13768:                 }
13769:             }
13770:             if (ref($domconfig{$action}) eq 'HASH') {
13771:                 if ($action eq 'requestauthor') {
13772:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13773:                         $changes{$type} = 1;
13774:                     }
13775:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13776:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13777:                         $changes{$item}{$type} = 1;
13778:                     }
13779:                 } else {
13780:                     if ($context eq 'requestcourses') {
13781:                         if ($confhash{$item}{$type} ne $unset) {
13782:                             $changes{$item}{$type} = 1;
13783:                         }
13784:                     } else {
13785:                         if (!$confhash{$item}{$type}) {
13786:                             $changes{$item}{$type} = 1;
13787:                         }
13788:                     }
13789:                 }
13790:             } else {
13791:                 if ($context eq 'requestcourses') {
13792:                     if ($confhash{$item}{$type} ne $unset) {
13793:                         $changes{$item}{$type} = 1;
13794:                     }
13795:                 } elsif ($context eq 'requestauthor') {
13796:                     if ($confhash{$type} ne $unset) {
13797:                         $changes{$type} = 1;
13798:                     }
13799:                 } else {
13800:                     if (!$confhash{$item}{$type}) {
13801:                         $changes{$item}{$type} = 1;
13802:                     }
13803:                 }
13804:             }
13805:         }
13806:     }
13807:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13808:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13809:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13810:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13811:                     if (exists($confhash{'defaultquota'}{$key})) {
13812:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13813:                             $changes{'defaultquota'}{$key} = 1;
13814:                         }
13815:                     } else {
13816:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13817:                     }
13818:                 }
13819:             } else {
13820:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13821:                     if (exists($confhash{'defaultquota'}{$key})) {
13822:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13823:                             $changes{'defaultquota'}{$key} = 1;
13824:                         }
13825:                     } else {
13826:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13827:                     }
13828:                 }
13829:             }
13830:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13831:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
13832:                     if (exists($confhash{'authorquota'}{$key})) {
13833:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
13834:                             $changes{'authorquota'}{$key} = 1;
13835:                         }
13836:                     } else {
13837:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
13838:                     }
13839:                 }
13840:             }
13841:         }
13842:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13843:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13844:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13845:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13846:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13847:                             $changes{'defaultquota'}{$key} = 1;
13848:                         }
13849:                     } else {
13850:                         if (!exists($domconfig{'quotas'}{$key})) {
13851:                             $changes{'defaultquota'}{$key} = 1;
13852:                         }
13853:                     }
13854:                 } else {
13855:                     $changes{'defaultquota'}{$key} = 1;
13856:                 }
13857:             }
13858:         }
13859:         if (ref($confhash{'authorquota'}) eq 'HASH') {
13860:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
13861:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13862:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13863:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
13864:                             $changes{'authorquota'}{$key} = 1;
13865:                         }
13866:                     } else {
13867:                         $changes{'authorquota'}{$key} = 1;
13868:                     }
13869:                 } else {
13870:                     $changes{'authorquota'}{$key} = 1;
13871:                 }
13872:             }
13873:         }
13874:     }
13875: 
13876:     if ($context eq 'requestauthor') {
13877:         $domdefaults{'requestauthor'} = \%confhash;
13878:     } else {
13879:         foreach my $key (keys(%confhash)) {
13880:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13881:                 $domdefaults{$key} = $confhash{$key};
13882:             }
13883:         }
13884:     }
13885: 
13886:     my %quotahash = (
13887:                       $action => { %confhash }
13888:                     );
13889:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13890:                                              $dom);
13891:     if ($putresult eq 'ok') {
13892:         if (keys(%changes) > 0) {
13893:             my $cachetime = 24*60*60;
13894:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13895:             if (ref($lastactref) eq 'HASH') {
13896:                 $lastactref->{'domdefaults'} = 1;
13897:             }
13898:             $resulttext = &mt('Changes made:').'<ul>';
13899:             unless (($context eq 'requestcourses') ||
13900:                     ($context eq 'requestauthor')) {
13901:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13902:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13903:                     foreach my $type (@{$types},'default') {
13904:                         if (defined($changes{'defaultquota'}{$type})) {
13905:                             my $typetitle = $usertypes->{$type};
13906:                             if ($type eq 'default') {
13907:                                 $typetitle = $othertitle;
13908:                             }
13909:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13910:                         }
13911:                     }
13912:                     $resulttext .= '</ul></li>';
13913:                 }
13914:                 if (ref($changes{'authorquota'}) eq 'HASH') {
13915:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
13916:                     foreach my $type (@{$types},'default') {
13917:                         if (defined($changes{'authorquota'}{$type})) {
13918:                             my $typetitle = $usertypes->{$type};
13919:                             if ($type eq 'default') {
13920:                                 $typetitle = $othertitle;
13921:                             }
13922:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
13923:                         }
13924:                     }
13925:                     $resulttext .= '</ul></li>';
13926:                 }
13927:             }
13928:             my %newenv;
13929:             foreach my $item (@usertools) {
13930:                 my (%haschgs,%inconf);
13931:                 if ($context eq 'requestauthor') {
13932:                     %haschgs = %changes;
13933:                     %inconf = %confhash;
13934:                 } else {
13935:                     if (ref($changes{$item}) eq 'HASH') {
13936:                         %haschgs = %{$changes{$item}};
13937:                     }
13938:                     if (ref($confhash{$item}) eq 'HASH') {
13939:                         %inconf = %{$confhash{$item}};
13940:                     }
13941:                 }
13942:                 if (keys(%haschgs) > 0) {
13943:                     my $newacc = 
13944:                         &Apache::lonnet::usertools_access($env{'user.name'},
13945:                                                           $env{'user.domain'},
13946:                                                           $item,'reload',$context);
13947:                     if (($context eq 'requestcourses') ||
13948:                         ($context eq 'requestauthor')) {
13949:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13950:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13951:                         }
13952:                     } else {
13953:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13954:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13955:                         }
13956:                     }
13957:                     unless ($context eq 'requestauthor') {
13958:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13959:                     }
13960:                     foreach my $type (@{$types},'default','_LC_adv') {
13961:                         if ($haschgs{$type}) {
13962:                             my $typetitle = $usertypes->{$type};
13963:                             if ($type eq 'default') {
13964:                                 $typetitle = $othertitle;
13965:                             } elsif ($type eq '_LC_adv') {
13966:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13967:                             }
13968:                             if ($inconf{$type}) {
13969:                                 if ($context eq 'requestcourses') {
13970:                                     my $cond;
13971:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
13972:                                         if ($1 eq '') {
13973:                                             $cond = &mt('(Automatic processing of any request).');
13974:                                         } else {
13975:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
13976:                                         }
13977:                                     } else { 
13978:                                         $cond = $conditions{$inconf{$type}};
13979:                                     }
13980:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
13981:                                 } elsif ($context eq 'requestauthor') {
13982:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
13983:                                                              $titles{$inconf{$type}},$typetitle);
13984: 
13985:                                 } else {
13986:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
13987:                                 }
13988:                             } else {
13989:                                 if ($type eq '_LC_adv') {
13990:                                     if ($inconf{$type} eq '0') {
13991:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13992:                                     } else { 
13993:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
13994:                                     }
13995:                                 } else {
13996:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13997:                                 }
13998:                             }
13999:                         }
14000:                     }
14001:                     unless ($context eq 'requestauthor') {
14002:                         $resulttext .= '</ul></li>';
14003:                     }
14004:                 }
14005:             }
14006:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14007:                 if (ref($changes{'notify'}) eq 'HASH') {
14008:                     if ($changes{'notify'}{'approval'}) {
14009:                         if (ref($confhash{'notify'}) eq 'HASH') {
14010:                             if ($confhash{'notify'}{'approval'}) {
14011:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14012:                             } else {
14013:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14014:                             }
14015:                         }
14016:                     }
14017:                 }
14018:             }
14019:             if ($action eq 'requestcourses') {
14020:                 my @offon = ('off','on');
14021:                 if ($changes{'uniquecode'}) {
14022:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14023:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14024:                         $resulttext .= '<li>'.
14025:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14026:                                        '</li>';
14027:                     } else {
14028:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14029:                                        '</li>';
14030:                     }
14031:                 }
14032:                 foreach my $type ('textbooks','templates') {
14033:                     if (ref($changes{$type}) eq 'HASH') {
14034:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14035:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14036:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14037:                             my $coursetitle = $coursehash{'description'};
14038:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14039:                             $resulttext .= '<li>';
14040:                             foreach my $item ('subject','title','publisher','author') {
14041:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14042:                                          ($type eq 'templates'));
14043:                                 my $name = $item.':';
14044:                                 $name =~ s/^(\w)/\U$1/;
14045:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14046:                             }
14047:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14048:                             if ($type eq 'textbooks') {
14049:                                 if ($confhash{$type}{$key}{'image'}) {
14050:                                     $resulttext .= ' '.&mt('Image: [_1]',
14051:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14052:                                                    ' alt="Textbook cover" />').'<br />';
14053:                                 }
14054:                             }
14055:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14056:                         }
14057:                         $resulttext .= '</ul></li>';
14058:                     }
14059:                 }
14060:                 if (ref($changes{'validation'}) eq 'HASH') {
14061:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14062:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14063:                         foreach my $item (@{$validationitemsref}) {
14064:                             if (exists($changes{'validation'}{$item})) {
14065:                                 if ($item eq 'markup') {
14066:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14067:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14068:                                 } else {
14069:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14070:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14071:                                 }
14072:                             }
14073:                         }
14074:                         if (exists($changes{'validation'}{'dc'})) {
14075:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14076:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14077:                         }
14078:                     }
14079:                 }
14080:             }
14081:             $resulttext .= '</ul>';
14082:             if (keys(%newenv)) {
14083:                 &Apache::lonnet::appenv(\%newenv);
14084:             }
14085:         } else {
14086:             if ($context eq 'requestcourses') {
14087:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14088:             } elsif ($context eq 'requestauthor') {
14089:                 $resulttext = &mt('No changes made to rights to request author space.');
14090:             } else {
14091:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14092:             }
14093:         }
14094:     } else {
14095:         $resulttext = '<span class="LC_error">'.
14096: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14097:     }
14098:     if ($errors) {
14099:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14100:                        '<ul>'.$errors.'</ul></p>';
14101:     }
14102:     return $resulttext;
14103: }
14104: 
14105: sub process_textbook_image {
14106:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14107:     my $filename = $env{'form.'.$caller.'.filename'};
14108:     my ($error,$url);
14109:     my ($width,$height) = (50,50);
14110:     if ($configuserok eq 'ok') {
14111:         if ($switchserver) {
14112:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14113:                          $switchserver);
14114:         } elsif ($author_ok eq 'ok') {
14115:             my ($result,$imageurl) =
14116:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14117:                              "$type/$cdom/$cnum/cover",$width,$height);
14118:             if ($result eq 'ok') {
14119:                 $url = $imageurl;
14120:             } else {
14121:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14122:             }
14123:         } else {
14124:             $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);
14125:         }
14126:     } else {
14127:         $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);
14128:     }
14129:     return ($url,$error);
14130: }
14131: 
14132: sub modify_ltitools {
14133:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14134:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14135:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
14136:     my $confname = $dom.'-domainconfig';
14137:     my $servadm = $r->dir_config('lonAdmEMail');
14138:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14139:     my (%posslti,%possfield);
14140:     my @courseroles = ('cc','in','ta','ep','st');
14141:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
14142:     map { $posslti{$_} = 1; } @ltiroles;
14143:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
14144:     map { $possfield{$_} = 1; } @allfields;
14145:     my %lt = &ltitools_names(); 
14146:     if ($env{'form.ltitools_add'}) {
14147:         my $title = $env{'form.ltitools_add_title'};
14148:         $title =~ s/(`)/'/g;
14149:         ($newid,my $error) = &get_ltitools_id($dom,$title);
14150:         if ($newid) {
14151:             my $position = $env{'form.ltitools_add_pos'};
14152:             $position =~ s/\D+//g;
14153:             if ($position ne '') {
14154:                 $allpos[$position] = $newid;
14155:             }
14156:             $changes{$newid} = 1;
14157:             foreach my $item ('title','url','key','secret','lifetime') {
14158:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
14159:                 if ($item eq 'lifetime') {
14160:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
14161:                 }
14162:                 if ($env{'form.ltitools_add_'.$item}) {
14163:                     if (($item eq 'key') || ($item eq 'secret')) {
14164:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
14165:                     } else {
14166:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
14167:                     }
14168:                 }
14169:             }
14170:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
14171:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
14172:             }
14173:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
14174:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
14175:             }
14176:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
14177:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
14178:             } else {
14179:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
14180:             }
14181:             foreach my $item ('width','height','linktext','explanation') {
14182:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
14183:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
14184:                 if (($item eq 'width') || ($item eq 'height')) {
14185:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
14186:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
14187:                     }
14188:                 } else {
14189:                     if ($env{'form.ltitools_add_'.$item} ne '') {
14190:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
14191:                     }
14192:                 }
14193:             }
14194:             if ($env{'form.ltitools_add_target'} eq 'window') {
14195:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
14196:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
14197:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
14198:             } else {
14199:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
14200:             }
14201:             foreach my $item ('passback','roster') {
14202:                 if ($env{'form.ltitools_'.$item.'_add'}) {
14203:                     $confhash{$newid}{$item} = 1;
14204:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
14205:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
14206:                         $lifetime =~ s/^\s+|\s+$//g;
14207:                         if ($lifetime =~ /^\d+\.?\d*$/) {
14208:                             $confhash{$newid}{$item.'valid'} = $lifetime;
14209:                         }
14210:                     }
14211:                 }
14212:             }
14213:             if ($env{'form.ltitools_add_image.filename'} ne '') {
14214:                 my ($imageurl,$error) =
14215:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
14216:                                             $configuserok,$switchserver,$author_ok);
14217:                 if ($imageurl) {
14218:                     $confhash{$newid}{'image'} = $imageurl;
14219:                 }
14220:                 if ($error) {
14221:                     &Apache::lonnet::logthis($error);
14222:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14223:                 }
14224:             }
14225:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
14226:             foreach my $field (@fields) {
14227:                 if ($possfield{$field}) {
14228:                     if ($field eq 'roles') {
14229:                         foreach my $role (@courseroles) {
14230:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
14231:                             if (($choice ne '') && ($posslti{$choice})) {
14232:                                 $confhash{$newid}{'roles'}{$role} = $choice;
14233:                                 if ($role eq 'cc') {
14234:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
14235:                                 }
14236:                             }
14237:                         }
14238:                     } else {
14239:                         $confhash{$newid}{'fields'}{$field} = 1;
14240:                     }
14241:                 }
14242:             }
14243:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
14244:                 if ($confhash{$newid}{'fields'}{'user'}) {
14245:                     if ($env{'form.ltitools_userincdom_add'}) {
14246:                         $confhash{$newid}{'incdom'} = 1;
14247:                     }
14248:                 }
14249:             }
14250:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
14251:             foreach my $item (@courseconfig) {
14252:                 $confhash{$newid}{'crsconf'}{$item} = 1;
14253:             }
14254:             if ($env{'form.ltitools_add_custom'}) {
14255:                 my $name = $env{'form.ltitools_add_custom_name'};
14256:                 my $value = $env{'form.ltitools_add_custom_value'};
14257:                 $value =~ s/(`)/'/g;
14258:                 $name =~ s/(`)/'/g;
14259:                 $confhash{$newid}{'custom'}{$name} = $value;
14260:             }
14261:         } else {
14262:             my $error = &mt('Failed to acquire unique ID for new external tool');   
14263:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14264:         }
14265:     }
14266:     if (ref($domconfig{$action}) eq 'HASH') {
14267:         my %deletions;
14268:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
14269:         if (@todelete) {
14270:             map { $deletions{$_} = 1; } @todelete;
14271:         }
14272:         my %customadds;
14273:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
14274:         if (@newcustom) {
14275:             map { $customadds{$_} = 1; } @newcustom;
14276:         } 
14277:         my %imgdeletions;
14278:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
14279:         if (@todeleteimages) {
14280:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14281:         }
14282:         my $maxnum = $env{'form.ltitools_maxnum'};
14283:         for (my $i=0; $i<=$maxnum; $i++) {
14284:             my $itemid = $env{'form.ltitools_id_'.$i};
14285:             $itemid =~ s/\D+//g;
14286:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14287:                 if ($deletions{$itemid}) {
14288:                     if ($domconfig{$action}{$itemid}{'image'}) {
14289:                         #FIXME need to obsolete item in RES space
14290:                     }
14291:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
14292:                     next;
14293:                 } else {
14294:                     my $newpos = $env{'form.ltitools_'.$itemid};
14295:                     $newpos =~ s/\D+//g;
14296:                     foreach my $item ('title','url','lifetime') {
14297:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14298:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
14299:                             $changes{$itemid} = 1;
14300:                         }
14301:                     }
14302:                     foreach my $item ('key','secret') {
14303:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14304:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
14305:                             $changes{$itemid} = 1;
14306:                         }
14307:                     }
14308:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
14309:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
14310:                     }
14311:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
14312:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
14313:                     }
14314:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
14315:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
14316:                     } else {
14317:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
14318:                     }
14319:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
14320:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
14321:                             $changes{$itemid} = 1;
14322:                         }
14323:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
14324:                         $changes{$itemid} = 1;
14325:                     }
14326:                     foreach my $size ('width','height') {
14327:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
14328:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
14329:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
14330:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
14331:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14332:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
14333:                                     $changes{$itemid} = 1;
14334:                                 }
14335:                             } else {
14336:                                 $changes{$itemid} = 1;
14337:                             }
14338:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14339:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
14340:                                 $changes{$itemid} = 1;
14341:                             }
14342:                         }
14343:                     }
14344:                     foreach my $item ('linktext','explanation') {
14345:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
14346:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
14347:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
14348:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
14349:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14350:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
14351:                                     $changes{$itemid} = 1;
14352:                                 }
14353:                             } else {
14354:                                 $changes{$itemid} = 1;
14355:                             }
14356:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14357:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
14358:                                 $changes{$itemid} = 1;
14359:                             }
14360:                         }
14361:                     }
14362:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
14363:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
14364:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
14365:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
14366:                     } else {
14367:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
14368:                     }
14369:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
14370:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
14371:                             $changes{$itemid} = 1;
14372:                         }
14373:                     } else {
14374:                         $changes{$itemid} = 1;
14375:                     }
14376:                     foreach my $extra ('passback','roster') {
14377:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
14378:                             $confhash{$itemid}{$extra} = 1;
14379:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
14380:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
14381:                                 $lifetime =~ s/^\s+|\s+$//g;
14382:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
14383:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
14384:                                 }
14385:                             }
14386:                         }
14387:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
14388:                             $changes{$itemid} = 1;
14389:                         }
14390:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
14391:                             $changes{$itemid} = 1;
14392:                         }
14393:                     }
14394:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
14395:                     foreach my $item ('label','title','target','linktext','explanation','append') {
14396:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
14397:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
14398:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
14399:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
14400:                                     $changes{$itemid} = 1;
14401:                                 }
14402:                             } else {
14403:                                 $changes{$itemid} = 1;
14404:                             }
14405:                         }
14406:                     }
14407:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
14408:                     foreach my $field (@fields) {
14409:                         if ($possfield{$field}) {
14410:                             if ($field eq 'roles') {
14411:                                 foreach my $role (@courseroles) {
14412:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
14413:                                     if (($choice ne '') && ($posslti{$choice})) {
14414:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
14415:                                         if ($role eq 'cc') {
14416:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
14417:                                         }
14418:                                     }
14419:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
14420:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
14421:                                             $changes{$itemid} = 1;
14422:                                         }
14423:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
14424:                                         $changes{$itemid} = 1;
14425:                                     }
14426:                                 }
14427:                             } else {
14428:                                 $confhash{$itemid}{'fields'}{$field} = 1;
14429:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
14430:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
14431:                                         $changes{$itemid} = 1;
14432:                                     }
14433:                                 } else {
14434:                                     $changes{$itemid} = 1;
14435:                                 }
14436:                             }
14437:                         }
14438:                     }
14439:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
14440:                         if ($confhash{$itemid}{'fields'}{'user'}) {
14441:                             if ($env{'form.ltitools_userincdom_'.$i}) {
14442:                                 $confhash{$itemid}{'incdom'} = 1;
14443:                             }
14444:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
14445:                                 $changes{$itemid} = 1;
14446:                             }
14447:                         }
14448:                     }
14449:                     $allpos[$newpos] = $itemid;
14450:                 }
14451:                 if ($imgdeletions{$itemid}) {
14452:                     $changes{$itemid} = 1;
14453:                     #FIXME need to obsolete item in RES space
14454:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
14455:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
14456:                                                                  $itemid,$configuserok,$switchserver,
14457:                                                                  $author_ok);
14458:                     if ($imgurl) {
14459:                         $confhash{$itemid}{'image'} = $imgurl;
14460:                         $changes{$itemid} = 1;
14461:                     }
14462:                     if ($error) {
14463:                         &Apache::lonnet::logthis($error);
14464:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14465:                     }
14466:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
14467:                     $confhash{$itemid}{'image'} =
14468:                        $domconfig{$action}{$itemid}{'image'};
14469:                 }
14470:                 if ($customadds{$i}) {
14471:                     my $name = $env{'form.ltitools_custom_name_'.$i};
14472:                     $name =~ s/(`)/'/g;
14473:                     $name =~ s/^\s+//;
14474:                     $name =~ s/\s+$//;
14475:                     my $value = $env{'form.ltitools_custom_value_'.$i};
14476:                     $value =~ s/(`)/'/g;
14477:                     $value =~ s/^\s+//;
14478:                     $value =~ s/\s+$//;
14479:                     if ($name ne '') {
14480:                         $confhash{$itemid}{'custom'}{$name} = $value;
14481:                         $changes{$itemid} = 1;
14482:                     }
14483:                 }
14484:                 my %customdels;
14485:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
14486:                 if (@customdeletions) {
14487:                     $changes{$itemid} = 1;
14488:                 }
14489:                 map { $customdels{$_} = 1; } @customdeletions;
14490:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
14491:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
14492:                         unless ($customdels{$key}) {
14493:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
14494:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
14495:                             }
14496:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
14497:                                 $changes{$itemid} = 1;
14498:                             }
14499:                         }
14500:                     }
14501:                 }
14502:                 unless ($changes{$itemid}) {
14503:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
14504:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
14505:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
14506:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
14507:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
14508:                                         $changes{$itemid} = 1;
14509:                                         last;
14510:                                     }
14511:                                 }
14512:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
14513:                                 $changes{$itemid} = 1;
14514:                             }
14515:                         }
14516:                         last if ($changes{$itemid});
14517:                     }
14518:                 }
14519:             }
14520:         }
14521:     }
14522:     if (@allpos > 0) {
14523:         my $idx = 0;
14524:         foreach my $itemid (@allpos) {
14525:             if ($itemid ne '') {
14526:                 $confhash{$itemid}{'order'} = $idx;
14527:                 if (ref($domconfig{$action}) eq 'HASH') {
14528:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14529:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14530:                             $changes{$itemid} = 1;
14531:                         }
14532:                     }
14533:                 }
14534:                 $idx ++;
14535:             }
14536:         }
14537:     }
14538:     my %ltitoolshash = (
14539:                           $action => { %confhash }
14540:                        );
14541:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
14542:                                              $dom);
14543:     if ($putresult eq 'ok') {
14544:         my %ltienchash = (
14545:                              $action => { %encconfig }
14546:                          );
14547:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
14548:         if (keys(%changes) > 0) {
14549:             my $cachetime = 24*60*60;
14550:             my %ltiall = %confhash;
14551:             foreach my $id (keys(%ltiall)) {
14552:                 if (ref($encconfig{$id}) eq 'HASH') {
14553:                     foreach my $item ('key','secret') {
14554:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14555:                     }
14556:                 }
14557:             }
14558:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
14559:             if (ref($lastactref) eq 'HASH') {
14560:                 $lastactref->{'ltitools'} = 1;
14561:             }
14562:             $resulttext = &mt('Changes made:').'<ul>';
14563:             my %bynum;
14564:             foreach my $itemid (sort(keys(%changes))) {
14565:                 my $position = $confhash{$itemid}{'order'};
14566:                 $bynum{$position} = $itemid;
14567:             }
14568:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14569:                 my $itemid = $bynum{$pos}; 
14570:                 if (ref($confhash{$itemid}) ne 'HASH') {
14571:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14572:                 } else {
14573:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
14574:                     if ($confhash{$itemid}{'image'}) {
14575:                         $resulttext .= '&nbsp;'.
14576:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
14577:                                        ' alt="'.&mt('Tool Provider icon').'" />';
14578:                     }
14579:                     $resulttext .= '</li><ul>';
14580:                     my $position = $pos + 1;
14581:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14582:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
14583:                         if ($confhash{$itemid}{$item} ne '') {
14584:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14585:                         }
14586:                     }
14587:                     if ($encconfig{$itemid}{'key'} ne '') {
14588:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14589:                     }
14590:                     if ($encconfig{$itemid}{'secret'} ne '') {
14591:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14592:                         my $num = length($encconfig{$itemid}{'secret'});
14593:                         $resulttext .= ('*'x$num).'</li>';
14594:                     }
14595:                     $resulttext .= '<li>'.&mt('Configurable in course:');
14596:                     my @possconfig = ('label','title','target','linktext','explanation','append');
14597:                     my $numconfig = 0; 
14598:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
14599:                         foreach my $item (@possconfig) {
14600:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
14601:                                 $numconfig ++;
14602:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
14603:                             }
14604:                         }
14605:                     }
14606:                     if (!$numconfig) {
14607:                         $resulttext .= '&nbsp;'.&mt('None');
14608:                     }
14609:                     $resulttext .= '</li>';
14610:                     foreach my $item ('passback','roster') {
14611:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
14612:                         if ($confhash{$itemid}{$item}) {
14613:                             $resulttext .= &mt('Yes');
14614:                             if ($confhash{$itemid}{$item.'valid'}) {
14615:                                 if ($item eq 'passback') {
14616:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
14617:                                                            $confhash{$itemid}{$item.'valid'});
14618:                                 } else {
14619:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
14620:                                                            $confhash{$itemid}{$item.'valid'});
14621:                                 }
14622:                             }
14623:                         } else {
14624:                             $resulttext .= &mt('No');
14625:                         }
14626:                         $resulttext .= '</li>';
14627:                     }
14628:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
14629:                         my $displaylist;
14630:                         if ($confhash{$itemid}{'display'}{'target'}) {
14631:                             $displaylist = &mt('Display target').':&nbsp;'.
14632:                                            $confhash{$itemid}{'display'}{'target'}.',';
14633:                         }
14634:                         foreach my $size ('width','height') { 
14635:                             if ($confhash{$itemid}{'display'}{$size}) {
14636:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
14637:                                                 $confhash{$itemid}{'display'}{$size}.',';
14638:                             }
14639:                         }
14640:                         if ($displaylist) {
14641:                             $displaylist =~ s/,$//;
14642:                             $resulttext .= '<li>'.$displaylist.'</li>';
14643:                         }
14644:                         foreach my $item ('linktext','explanation') {
14645:                             if ($confhash{$itemid}{'display'}{$item}) {
14646:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
14647:                             }
14648:                         }
14649:                     }
14650:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
14651:                         my $fieldlist;
14652:                         foreach my $field (@allfields) {
14653:                             if ($confhash{$itemid}{'fields'}{$field}) {
14654:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
14655:                             }
14656:                         }
14657:                         if ($fieldlist) {
14658:                             $fieldlist =~ s/,$//;
14659:                             if ($confhash{$itemid}{'fields'}{'user'}) {
14660:                                 if ($confhash{$itemid}{'incdom'}) {
14661:                                     $fieldlist .= ' ('.&mt('username:domain').')';
14662:                                 } else {
14663:                                     $fieldlist .= ' ('.&mt('username').')';
14664:                                 }
14665:                             }
14666:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
14667:                         }
14668:                     }
14669:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
14670:                         my $rolemaps;
14671:                         foreach my $role (@courseroles) {
14672:                             if ($confhash{$itemid}{'roles'}{$role}) {
14673:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14674:                                              $confhash{$itemid}{'roles'}{$role}.',';
14675:                             }
14676:                         }
14677:                         if ($rolemaps) {
14678:                             $rolemaps =~ s/,$//; 
14679:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14680:                         }
14681:                     }
14682:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
14683:                         my $customlist;
14684:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
14685:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
14686:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
14687:                             } 
14688:                         }
14689:                         if ($customlist) {
14690:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14691:                         }
14692:                     } 
14693:                     $resulttext .= '</ul></li>';
14694:                 }
14695:             }
14696:             $resulttext .= '</ul>';
14697:         } else {
14698:             $resulttext = &mt('No changes made.');
14699:         }
14700:     } else {
14701:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14702:     }
14703:     if ($errors) {
14704:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14705:                        $errors.'</ul>';
14706:     }
14707:     return $resulttext;
14708: }
14709: 
14710: sub process_ltitools_image {
14711:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
14712:     my $filename = $env{'form.'.$caller.'.filename'};
14713:     my ($error,$url);
14714:     my ($width,$height) = (21,21);
14715:     if ($configuserok eq 'ok') {
14716:         if ($switchserver) {
14717:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
14718:                          $switchserver);
14719:         } elsif ($author_ok eq 'ok') {
14720:             my ($result,$imageurl,$madethumb) =
14721:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14722:                              "ltitools/$itemid/icon",$width,$height);
14723:             if ($result eq 'ok') {
14724:                 if ($madethumb) {
14725:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14726:                     my $imagethumb = "$path/tn-".$imagefile;
14727:                     $url = $imagethumb;
14728:                 } else {
14729:                     $url = $imageurl;
14730:                 }
14731:             } else {
14732:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14733:             }
14734:         } else {
14735:             $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);
14736:         }
14737:     } else {
14738:         $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);
14739:     }
14740:     return ($url,$error);
14741: }
14742: 
14743: sub get_ltitools_id {
14744:     my ($cdom,$title) = @_;
14745:     # get lock on ltitools db
14746:     my $lockhash = {
14747:                       lock => $env{'user.name'}.
14748:                               ':'.$env{'user.domain'},
14749:                    };
14750:     my $tries = 0;
14751:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14752:     my ($id,$error);
14753:  
14754:     while (($gotlock ne 'ok') && ($tries<10)) {
14755:         $tries ++;
14756:         sleep (0.1);
14757:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14758:     }
14759:     if ($gotlock eq 'ok') {
14760:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
14761:         if ($currids{'lock'}) {
14762:             delete($currids{'lock'});
14763:             if (keys(%currids)) {
14764:                 my @curr = sort { $a <=> $b } keys(%currids);
14765:                 if ($curr[-1] =~ /^\d+$/) {
14766:                     $id = 1 + $curr[-1];
14767:                 }
14768:             } else {
14769:                 $id = 1;
14770:             }
14771:             if ($id) {
14772:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
14773:                     $error = 'nostore';
14774:                 }
14775:             } else {
14776:                 $error = 'nonumber';
14777:             }
14778:         }
14779:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
14780:     } else {
14781:         $error = 'nolock';
14782:     }
14783:     return ($id,$error);
14784: }
14785: 
14786: sub modify_proctoring {
14787:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14788:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14789:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
14790:     my $confname = $dom.'-domainconfig';
14791:     my $servadm = $r->dir_config('lonAdmEMail');
14792:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14793:     my %providernames = &proctoring_providernames();
14794:     my $maxnum = scalar(keys(%providernames));
14795: 
14796:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
14797:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
14798:     if (ref($requref) eq 'HASH') {
14799:         %requserfields = %{$requref};
14800:     }
14801:     if (ref($opturef) eq 'HASH') {
14802:         %optuserfields = %{$opturef};
14803:     }
14804:     if (ref($defref) eq 'HASH') {
14805:         %defaults = %{$defref};
14806:     }
14807:     if (ref($extref) eq 'HASH') {
14808:         %extended = %{$extref};
14809:     }
14810:     if (ref($crsref) eq 'HASH') {
14811:         %crsconf = %{$crsref};
14812:     }
14813:     if (ref($rolesref) eq 'ARRAY') {
14814:         @courseroles = @{$rolesref};
14815:     }
14816:     if (ref($ltiref) eq 'ARRAY') {
14817:         @ltiroles = @{$ltiref};
14818:     }
14819: 
14820:     if (ref($domconfig{$action}) eq 'HASH') {
14821:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
14822:         if (@todeleteimages) {
14823:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14824:         }
14825:     }
14826:     my %customadds;
14827:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
14828:     if (@newcustom) {
14829:         map { $customadds{$_} = 1; } @newcustom;
14830:     }
14831:     foreach my $provider (sort(keys(%providernames))) {
14832:         $confhash{$provider} = {};
14833:         my $pos = $env{'form.proctoring_pos_'.$provider};
14834:         $pos =~ s/\D+//g;
14835:         $allpos[$pos] = $provider;
14836:         my (%current,%currentenc);
14837:         my $showroles = 0;
14838:         if (ref($domconfig{$action}) eq 'HASH') {
14839:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14840:                 %current = %{$domconfig{$action}{$provider}};
14841:                 foreach my $item ('key','secret') { 
14842:                     $currentenc{$item} = $current{$item};
14843:                     delete($current{$item});
14844:                 }
14845:             }
14846:         }
14847:         if ($env{'form.proctoring_available_'.$provider}) {
14848:             $confhash{$provider}{'available'} = 1;
14849:             unless ($current{'available'}) {
14850:                 $changes{$provider} = 1;
14851:             }
14852:         } else {
14853:             %{$confhash{$provider}} = %current;
14854:             %{$encconfhash{$provider}} = %currentenc;
14855:             $confhash{$provider}{'available'} = 0;
14856:             if ($current{'available'}) {
14857:                 $changes{$provider} = 1;
14858:             }
14859:         }
14860:         if ($confhash{$provider}{'available'}) {
14861:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
14862:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
14863:                 if ($field eq 'lifetime') {
14864:                     if ($possval =~ /^\d+$/) {
14865:                         $confhash{$provider}{$field} = $possval;
14866:                     }
14867:                 } elsif ($field eq 'version') {
14868:                     if ($possval =~ /^\d+\.\d+$/) {
14869:                         $confhash{$provider}{$field} = $possval;
14870:                     }
14871:                 } elsif ($field eq 'sigmethod') {
14872:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
14873:                         $confhash{$provider}{$field} = $possval;
14874:                     }
14875:                 } elsif ($field eq 'url') {
14876:                     $confhash{$provider}{$field} = $possval;
14877:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
14878:                     $encconfhash{$provider}{$field} = $possval;
14879:                     unless ($currentenc{$field} eq $possval) {
14880:                         $changes{$provider} = 1;
14881:                     }
14882:                 }
14883:                 unless (($field eq 'key') || ($field eq 'secret')) {
14884:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
14885:                         $changes{$provider} = 1;
14886:                     }
14887:                 }
14888:             }
14889:             if ($imgdeletions{$provider}) {
14890:                 $changes{$provider} = 1;
14891:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
14892:                 my ($imageurl,$error) =
14893:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
14894:                                               $configuserok,$switchserver,$author_ok);
14895:                 if ($imageurl) {
14896:                     $confhash{$provider}{'image'} = $imageurl;
14897:                     $changes{$provider} = 1; 
14898:                 }
14899:                 if ($error) {
14900:                     &Apache::lonnet::logthis($error);
14901:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14902:                 }
14903:             } elsif (exists($current{'image'})) {
14904:                 $confhash{$provider}{'image'} = $current{'image'};
14905:             }
14906:             if (ref($requserfields{$provider}) eq 'ARRAY') {
14907:                 if (@{$requserfields{$provider}} > 0) {
14908:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
14909:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
14910:                             $confhash{$provider}{'incdom'} = 1;
14911:                         }
14912:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
14913:                             $changes{$provider} = 1;
14914:                         }
14915:                     }
14916:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
14917:                         $showroles = 1;
14918:                     }
14919:                 }
14920:             }
14921:             $confhash{$provider}{'fields'} = [];
14922:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
14923:                 if (@{$optuserfields{$provider}} > 0) {
14924:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
14925:                     foreach my $field (@{$optuserfields{$provider}}) {
14926:                         if (grep(/^\Q$field\E$/,@optfields)) {
14927:                             push(@{$confhash{$provider}{'fields'}},$field);
14928:                         }
14929:                     }
14930:                 }
14931:                 if (ref($current{'fields'}) eq 'ARRAY') {
14932:                     unless ($changes{$provider}) {
14933:                         my @new = sort(@{$confhash{$provider}{'fields'}});
14934:                         my @old = sort(@{$current{'fields'}}); 
14935:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14936:                         if (@diffs) {
14937:                             $changes{$provider} = 1;
14938:                         }
14939:                     }
14940:                 } elsif (@{$confhash{$provider}{'fields'}}) {
14941:                     $changes{$provider} = 1;
14942:                 }
14943:             }
14944:             if (ref($defaults{$provider}) eq 'ARRAY') {  
14945:                 if (@{$defaults{$provider}} > 0) {
14946:                     my %options;
14947:                     if (ref($extended{$provider}) eq 'HASH') {
14948:                         %options = %{$extended{$provider}};
14949:                     }
14950:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14951:                     foreach my $field (@{$defaults{$provider}}) {
14952:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14953:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14954:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14955:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14956:                             }
14957:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14958:                             foreach my $inner (keys(%{$options{$field}})) {
14959:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14960:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14961:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14962:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14963:                                     }
14964:                                 } else {
14965:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14966:                                 }
14967:                             }
14968:                         } else {
14969:                             if (grep(/^\Q$field\E$/,@checked)) {
14970:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14971:                             }
14972:                         }
14973:                     }
14974:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14975:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14976:                             unless ($changes{$provider}) {
14977:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14978:                                 my @old = sort(@{$current{'defaults'}});
14979:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14980:                                 if (@diffs) {
14981:                                     $changes{$provider} = 1; 
14982:                                 }
14983:                             }
14984:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14985:                             if (@{$current{'defaults'}}) {
14986:                                 $changes{$provider} = 1;
14987:                             }
14988:                         }
14989:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14990:                         if (ref($current{'defaults'}) eq 'HASH') {
14991:                             unless ($changes{$provider}) {
14992:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
14993:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
14994:                                         $changes{$provider} = 1;
14995:                                         last;
14996:                                     }
14997:                                 }
14998:                             }
14999:                             unless ($changes{$provider}) {
15000:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15001:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15002:                                         $changes{$provider} = 1;
15003:                                         last;
15004:                                     }
15005:                                 }
15006:                             }
15007:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15008:                             $changes{$provider} = 1;
15009:                         }
15010:                     }
15011:                 }
15012:             }
15013:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15014:                 if (@{$crsconf{$provider}} > 0) {
15015:                     $confhash{$provider}{'crsconf'} = [];
15016:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15017:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15018:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15019:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15020:                         }
15021:                     }
15022:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15023:                         unless ($changes{$provider}) {  
15024:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15025:                             my @old = sort(@{$current{'crsconf'}});
15026:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15027:                             if (@diffs) {
15028:                                 $changes{$provider} = 1;
15029:                             }
15030:                         }
15031:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15032:                         $changes{$provider} = 1;
15033:                     }
15034:                 }
15035:             }
15036:             if ($showroles) {
15037:                 $confhash{$provider}{'roles'} = {};
15038:                 foreach my $role (@courseroles) {
15039:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15040:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15041:                         $confhash{$provider}{'roles'}{$role} = $poss;
15042:                     }
15043:                 }
15044:                 unless ($changes{$provider}) {
15045:                     if (ref($current{'roles'}) eq 'HASH') {
15046:                         foreach my $role (keys(%{$current{'roles'}})) {
15047:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15048:                                 $changes{$provider} = 1;
15049:                                 last
15050:                             }
15051:                         }
15052:                         unless ($changes{$provider}) {
15053:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15054:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15055:                                     $changes{$provider} = 1;
15056:                                     last;
15057:                                 }
15058:                             }
15059:                         }
15060:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15061:                         $changes{$provider} = 1;
15062:                     }
15063:                 }
15064:             }
15065:             if (ref($current{'custom'}) eq 'HASH') {
15066:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15067:                 foreach my $key (keys(%{$current{'custom'}})) {
15068:                     if (grep(/^\Q$key\E$/,@customdels)) {
15069:                         $changes{$provider} = 1;
15070:                     } else {
15071:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15072:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15073:                             $changes{$provider} = 1;
15074:                         }
15075:                     }
15076:                 }
15077:             }
15078:             if ($customadds{$provider}) {
15079:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15080:                 $name =~ s/(`)/'/g;
15081:                 $name =~ s/^\s+//;
15082:                 $name =~ s/\s+$//;
15083:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15084:                 $value =~ s/(`)/'/g;
15085:                 $value =~ s/^\s+//;
15086:                 $value =~ s/\s+$//;
15087:                 if ($name ne '') {
15088:                     $confhash{$provider}{'custom'}{$name} = $value;
15089:                     $changes{$provider} = 1;
15090:                 }
15091:             }
15092:         }
15093:     }
15094:     if (@allpos > 0) {
15095:         my $idx = 0;
15096:         foreach my $provider (@allpos) {
15097:             if ($provider ne '') {
15098:                 $confhash{$provider}{'order'} = $idx;
15099:                 unless ($changes{$provider}) {
15100:                     if (ref($domconfig{$action}) eq 'HASH') {
15101:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15102:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15103:                                 $changes{$provider} = 1;
15104:                             }
15105:                         }
15106:                     }
15107:                 }
15108:                 $idx ++;
15109:             }
15110:         }
15111:     }
15112:     my %proc_hash = (
15113:                           $action => { %confhash }
15114:                        );
15115:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15116:                                              $dom);
15117:     if ($putresult eq 'ok') {
15118:         my %proc_enchash = (
15119:                              $action => { %encconfhash }
15120:                          );
15121:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15122:         if (keys(%changes) > 0) {
15123:             my $cachetime = 24*60*60;
15124:             my %procall = %confhash;
15125:             foreach my $provider (keys(%procall)) {
15126:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15127:                     foreach my $key ('key','secret') {
15128:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15129:                     }
15130:                 }
15131:             }
15132:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15133:             if (ref($lastactref) eq 'HASH') {
15134:                 $lastactref->{'proctoring'} = 1;
15135:             }
15136:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15137:             my %bynum;
15138:             foreach my $provider (sort(keys(%changes))) {
15139:                 my $position = $confhash{$provider}{'order'};
15140:                 $bynum{$position} = $provider;
15141:             }
15142:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15143:                 my $provider = $bynum{$pos};
15144:                 my %lt = &proctoring_titles($provider);
15145:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15146:                 if (!$confhash{$provider}{'available'}) {
15147:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15148:                 } else {
15149:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15150:                     if ($confhash{$provider}{'image'}) {
15151:                         $resulttext .= '&nbsp;'.
15152:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15153:                                        ' alt="'.&mt('Proctoring icon').'" />';
15154:                     }
15155:                     $resulttext .= '<ul>';
15156:                     my $position = $pos + 1;
15157:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15158:                     foreach my $key ('version','sigmethod','url','lifetime') {
15159:                         if ($confhash{$provider}{$key} ne '') {
15160:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15161:                         }
15162:                     }
15163:                     if ($encconfhash{$provider}{'key'} ne '') {
15164:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15165:                     }
15166:                     if ($encconfhash{$provider}{'secret'} ne '') {
15167:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15168:                         my $num = length($encconfhash{$provider}{'secret'});
15169:                         $resulttext .= ('*'x$num).'</li>';
15170:                     }
15171:                     my (@fields,$showroles);
15172:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15173:                         push(@fields,@{$requserfields{$provider}});
15174:                     }
15175:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15176:                         push(@fields,@{$confhash{$provider}{'fields'}});
15177:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15178:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15179:                     }
15180:                     if (@fields) {
15181:                         if (grep(/^roles$/,@fields)) {
15182:                             $showroles = 1;
15183:                         }
15184:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15185:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15186:                     }
15187:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15188:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15189:                             if ($confhash{$provider}{'incdom'}) {
15190:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15191:                             } else { 
15192:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15193:                             }
15194:                         }
15195:                     }
15196:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15197:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15198:                             $resulttext .= '<li>'.$lt{'defa'};
15199:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15200:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15201:                             }
15202:                             $resulttext =~ s/,$//;
15203:                             $resulttext .= '</li>';
15204:                         }
15205:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15206:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15207:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15208:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15209:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15210:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15211:                                 }
15212:                             }
15213:                             $resulttext .= '</ul></li>';
15214:                         }
15215:                     }
15216:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15217:                         if (@{$crsconf{$provider}} > 0) {
15218:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15219:                             my $numconfig = 0;
15220:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15221:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15222:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15223:                                         $numconfig ++;
15224:                                         if ($provider eq 'examity') {
15225:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15226:                                         } else {
15227:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15228:                                         }
15229:                                     }
15230:                                     $resulttext =~ s/,$//;
15231:                                 }
15232:                             }
15233:                             if (!$numconfig) {
15234:                                 $resulttext .= '&nbsp;'.&mt('None');
15235:                             }
15236:                             $resulttext .= '</li>';
15237:                         }
15238:                     }
15239:                     if ($showroles) {
15240:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15241:                             my $rolemaps;
15242:                             foreach my $role (@courseroles) {
15243:                                 if ($confhash{$provider}{'roles'}{$role}) {
15244:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15245:                                                  $confhash{$provider}{'roles'}{$role}.',';
15246:                                 }
15247:                             }
15248:                             if ($rolemaps) {
15249:                                 $rolemaps =~ s/,$//;
15250:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15251:                             }
15252:                         }
15253:                     }
15254:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15255:                         my $customlist;
15256:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15257:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15258:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15259:                             }
15260:                             $customlist =~ s/,$//;
15261:                         }
15262:                         if ($customlist) {
15263:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15264:                         }
15265:                     } 
15266:                     $resulttext .= '</ul></li>';
15267:                 }
15268:             }
15269:             $resulttext .= '</ul>';
15270:         } else {
15271:             $resulttext = &mt('No changes made.');
15272:         }
15273:     } else {
15274:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15275:     }
15276:     if ($errors) {
15277:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15278:                        $errors.'</ul>';
15279:     }
15280:     return $resulttext;
15281: }
15282: 
15283: sub process_proctoring_image {
15284:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15285:     my $filename = $env{'form.'.$caller.'.filename'};
15286:     my ($error,$url);
15287:     my ($width,$height) = (21,21);
15288:     if ($configuserok eq 'ok') {
15289:         if ($switchserver) {
15290:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15291:                          $switchserver);
15292:         } elsif ($author_ok eq 'ok') {
15293:             my ($result,$imageurl,$madethumb) =
15294:                 &publishlogo($r,'upload',$caller,$dom,$confname,
15295:                              "proctoring/$provider/icon",$width,$height);
15296:             if ($result eq 'ok') {
15297:                 if ($madethumb) {
15298:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15299:                     my $imagethumb = "$path/tn-".$imagefile;
15300:                     $url = $imagethumb;
15301:                 } else {
15302:                     $url = $imageurl;
15303:                 }
15304:             } else {
15305:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15306:             }
15307:         } else {
15308:             $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);
15309:         }
15310:     } else {
15311:         $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);
15312:     }
15313:     return ($url,$error);
15314: }
15315: 
15316: sub modify_lti {
15317:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15318:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15319:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
15320:     my (%posslti,%posslticrs,%posscrstype);
15321:     my @courseroles = ('cc','in','ta','ep','st');
15322:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15323:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15324:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15325:     my %coursetypetitles = &Apache::lonlocal::texthash (
15326:                                official   => 'Official',
15327:                                unofficial => 'Unofficial',
15328:                                community  => 'Community',
15329:                                textbook   => 'Textbook',
15330:                                placement  => 'Placement Test',
15331:                                lti        => 'LTI Provider',
15332:     );
15333:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15334:     my %lt = &lti_names();
15335:     map { $posslti{$_} = 1; } @ltiroles;
15336:     map { $posslticrs{$_} = 1; } @lticourseroles;
15337:     map { $posscrstype{$_} = 1; } @coursetypes;
15338: 
15339:     my %menutitles = &ltimenu_titles();
15340: 
15341:     my (@items,%deletions,%itemids);
15342:     if ($env{'form.lti_add'}) {
15343:         my $consumer = $env{'form.lti_consumer_add'};
15344:         $consumer =~ s/(`)/'/g;
15345:         ($newid,my $error) = &get_lti_id($dom,$consumer);
15346:         if ($newid) {
15347:             $itemids{'add'} = $newid;
15348:             push(@items,'add');
15349:             $changes{$newid} = 1;
15350:         } else {
15351:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15352:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15353:         }
15354:     }
15355:     if (ref($domconfig{$action}) eq 'HASH') {
15356:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15357:         if (@todelete) {
15358:             map { $deletions{$_} = 1; } @todelete;
15359:         }
15360:         my $maxnum = $env{'form.lti_maxnum'};
15361:         for (my $i=0; $i<$maxnum; $i++) {
15362:             my $itemid = $env{'form.lti_id_'.$i};
15363:             $itemid =~ s/\D+//g;
15364:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15365:                 if ($deletions{$itemid}) {
15366:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15367:                 } else {
15368:                     push(@items,$i);
15369:                     $itemids{$i} = $itemid;
15370:                 }
15371:             }
15372:         }
15373:     }
15374:     foreach my $idx (@items) {
15375:         my $itemid = $itemids{$idx};
15376:         next unless ($itemid);
15377:         my $position = $env{'form.lti_pos_'.$itemid};
15378:         $position =~ s/\D+//g;
15379:         if ($position ne '') {
15380:             $allpos[$position] = $itemid;
15381:         }
15382:         foreach my $item ('consumer','key','secret','lifetime','requser','crsinc') {
15383:             my $formitem = 'form.lti_'.$item.'_'.$idx;
15384:             $env{$formitem} =~ s/(`)/'/g;
15385:             if ($item eq 'lifetime') {
15386:                 $env{$formitem} =~ s/[^\d.]//g;
15387:             }
15388:             if ($env{$formitem} ne '') {
15389:                 if (($item eq 'key') || ($item eq 'secret')) {
15390:                     $encconfig{$itemid}{$item} = $env{$formitem};
15391:                 } else {
15392:                     $confhash{$itemid}{$item} = $env{$formitem};
15393:                     unless (($idx eq 'add') || ($changes{$itemid})) {
15394:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
15395:                             $changes{$itemid} = 1;
15396:                         }
15397:                     }
15398:                 }
15399:             }
15400:         }
15401:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
15402:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
15403:         }
15404:         if ($confhash{$itemid}{'requser'}) {
15405:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
15406:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
15407:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
15408:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
15409:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
15410:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
15411:                 $mapuser =~ s/(`)/'/g;
15412:                 $mapuser =~ s/^\s+|\s+$//g; 
15413:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
15414:             }
15415:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
15416:             my @makeuser;
15417:             foreach my $ltirole (sort(@possmakeuser)) {
15418:                 if ($posslti{$ltirole}) {
15419:                     push(@makeuser,$ltirole);
15420:                 }
15421:             }
15422:             $confhash{$itemid}{'makeuser'} = \@makeuser;
15423:             if (@makeuser) {
15424:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
15425:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
15426:                     $confhash{$itemid}{'lcauth'} = $lcauth;
15427:                     if ($lcauth ne 'internal') {
15428:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
15429:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
15430:                         $lcauthparm =~ s/`//g;
15431:                         if ($lcauthparm ne '') {
15432:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
15433:                         }
15434:                     }
15435:                 } else {
15436:                     $confhash{$itemid}{'lcauth'} = 'lti';
15437:                 }
15438:             }
15439:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
15440:             if (@possinstdata) {
15441:                 foreach my $field (@possinstdata) {
15442:                     if (exists($fieldtitles{$field})) {
15443:                         push(@{$confhash{$itemid}{'instdata'}});
15444:                     }
15445:                 }
15446:             }
15447:             if ($env{'form.lti_callback_'.$idx}) {
15448:                 if ($env{'form.lti_callbackparam_'.$idx}) {
15449:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
15450:                     $callback =~ s/^\s+|\s+$//g;
15451:                     $confhash{$itemid}{'callback'} = $callback;
15452:                 }
15453:             }
15454:             foreach my $field ('topmenu','inlinemenu') {
15455:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
15456:                     $confhash{$itemid}{$field} = 1;
15457:                 }
15458:             }
15459:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
15460:                 $confhash{$itemid}{lcmenu} = [];
15461:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
15462:                 foreach my $field (@possmenu) {
15463:                     if (exists($menutitles{$field})) {
15464:                         if ($field eq 'grades') {
15465:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
15466:                         }
15467:                         push(@{$confhash{$itemid}{lcmenu}},$field);
15468:                     }
15469:                 }
15470:             }
15471:             if ($confhash{$itemid}{'crsinc'}) {
15472:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
15473:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
15474:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
15475:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
15476:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
15477:                     $mapcrs =~ s/(`)/'/g;
15478:                     $mapcrs =~ s/^\s+|\s+$//g;
15479:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
15480:                 }
15481:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
15482:                 my @crstypes;
15483:                 foreach my $type (sort(@posstypes)) {
15484:                     if ($posscrstype{$type}) {
15485:                         push(@crstypes,$type);
15486:                     }
15487:                 }
15488:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
15489:                 if ($env{'form.lti_storecrs_'.$idx}) {
15490:                     $confhash{$itemid}{'storecrs'} = 1;
15491:                 }       
15492:                 if ($env{'form.lti_makecrs_'.$idx}) {
15493:                     $confhash{$itemid}{'makecrs'} = 1;
15494:                 }
15495:                 foreach my $ltirole (@lticourseroles) {
15496:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
15497:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
15498:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
15499:                     }
15500:                 }
15501:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
15502:                 my @selfenroll;
15503:                 foreach my $type (sort(@possenroll)) {
15504:                     if ($posslticrs{$type}) {
15505:                         push(@selfenroll,$type);
15506:                     }
15507:                 }
15508:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
15509:                 if ($env{'form.lti_crssec_'.$idx}) {
15510:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
15511:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
15512:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
15513:                         my $section = $env{'form.lti_customsection_'.$idx};
15514:                         $section =~ s/(`)/'/g;
15515:                         $section =~ s/^\s+|\s+$//g;
15516:                         if ($section ne '') {
15517:                             $confhash{$itemid}{'section'} = $section;
15518:                         }
15519:                     }
15520:                 }
15521:                 foreach my $field ('passback','roster') {
15522:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
15523:                         $confhash{$itemid}{$field} = 1;
15524:                     }
15525:                 }
15526:                 if ($env{'form.lti_passback_'.$idx}) {
15527:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
15528:                         $confhash{$itemid}{'passbackformat'} = '1.0';
15529:                     } else {
15530:                         $confhash{$itemid}{'passbackformat'} = '1.1';
15531:                     }
15532:                 }
15533:             }
15534:             unless (($idx eq 'add') || ($changes{$itemid})) {
15535:                 if ($confhash{$itemid}{'crsinc'}) {
15536:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15537:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15538:                             $changes{$itemid} = 1;
15539:                         }
15540:                     }
15541:                     unless ($changes{$itemid}) {
15542:                         if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
15543:                             if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15544:                                 $changes{$itemid} = 1;
15545:                             }
15546:                         }
15547:                     }
15548:                     foreach my $field ('mapcrstype','selfenroll') {
15549:                         unless ($changes{$itemid}) {
15550:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15551:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15552:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15553:                                                                                    $confhash{$itemid}{$field});
15554:                                     if (@diffs) {
15555:                                         $changes{$itemid} = 1;
15556:                                     }
15557:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15558:                                     $changes{$itemid} = 1;
15559:                                 }
15560:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15561:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15562:                                     $changes{$itemid} = 1;
15563:                                 }
15564:                             }
15565:                         }
15566:                     }
15567:                     unless ($changes{$itemid}) {
15568:                         if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
15569:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15570:                                 foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
15571:                                     if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
15572:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15573:                                         $changes{$itemid} = 1;
15574:                                         last;
15575:                                     }
15576:                                 }
15577:                                 unless ($changes{$itemid}) {
15578:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15579:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15580:                                             $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
15581:                                             $changes{$itemid} = 1;
15582:                                             last;
15583:                                         }
15584:                                     }
15585:                                 }
15586:                             } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
15587:                                 $changes{$itemid} = 1;
15588:                             }
15589:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15590:                             unless ($changes{$itemid}) {
15591:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15592:                                     $changes{$itemid} = 1;
15593:                                 }
15594:                             }
15595:                         }
15596:                     }
15597:                 }
15598:                 unless ($changes{$itemid}) {
15599:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15600:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15601:                             $changes{$itemid} = 1;
15602:                         }
15603:                     }
15604:                     unless ($changes{$itemid}) {
15605:                         foreach my $field ('makeuser','lcmenu') {
15606:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15607:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15608:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15609:                                                                                    $confhash{$itemid}{$field});
15610:                                     if (@diffs) {
15611:                                         $changes{$itemid} = 1;
15612:                                     }
15613:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15614:                                     $changes{$itemid} = 1;
15615:                                 }
15616:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15617:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15618:                                     $changes{$itemid} = 1;
15619:                                 }
15620:                             }
15621:                         }
15622:                     }
15623:                 }
15624:             }
15625:         }
15626:     }
15627:     if (@allpos > 0) {
15628:         my $idx = 0;
15629:         foreach my $itemid (@allpos) {
15630:             if ($itemid ne '') {
15631:                 $confhash{$itemid}{'order'} = $idx;
15632:                 if (ref($domconfig{$action}) eq 'HASH') {
15633:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15634:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
15635:                             $changes{$itemid} = 1;
15636:                         }
15637:                     }
15638:                 }
15639:                 $idx ++;
15640:             }
15641:         }
15642:     }
15643:     my %ltihash = (
15644:                           $action => { %confhash }
15645:                        );
15646:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
15647:                                              $dom);
15648:     if ($putresult eq 'ok') {
15649:         my %ltienchash = (
15650:                              $action => { %encconfig }
15651:                          );
15652:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
15653:         if (keys(%changes) > 0) {
15654:             my $cachetime = 24*60*60;
15655:             my %ltiall = %confhash;
15656:             foreach my $id (keys(%ltiall)) {
15657:                 if (ref($encconfig{$id}) eq 'HASH') {
15658:                     foreach my $item ('key','secret') {
15659:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
15660:                     }
15661:                 }
15662:             }
15663:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
15664:             if (ref($lastactref) eq 'HASH') {
15665:                 $lastactref->{'lti'} = 1;
15666:             }
15667:             $resulttext = &mt('Changes made:').'<ul>';
15668:             my %bynum;
15669:             foreach my $itemid (sort(keys(%changes))) {
15670:                 my $position = $confhash{$itemid}{'order'};
15671:                 $bynum{$position} = $itemid;
15672:             }
15673:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15674:                 my $itemid = $bynum{$pos};
15675:                 if (ref($confhash{$itemid}) ne 'HASH') {
15676:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
15677:                 } else {
15678:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
15679:                     my $position = $pos + 1;
15680:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15681:                     foreach my $item ('version','lifetime') {
15682:                         if ($confhash{$itemid}{$item} ne '') {
15683:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
15684:                         }
15685:                     }
15686:                     if ($encconfig{$itemid}{'key'} ne '') {
15687:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
15688:                     }
15689:                     if ($encconfig{$itemid}{'secret'} ne '') {
15690:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15691:                         my $num = length($encconfig{$itemid}{'secret'});
15692:                         $resulttext .= ('*'x$num).'</li>';
15693:                     }
15694:                     if ($confhash{$itemid}{'requser'}) {
15695:                         if ($confhash{$itemid}{'callback'}) {
15696:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
15697:                         } else {
15698:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
15699:                         }
15700:                         if ($confhash{$itemid}{'mapuser'}) {
15701:                             my $shownmapuser;
15702:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
15703:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
15704:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
15705:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
15706:                             } else {
15707:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
15708:                             }
15709:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
15710:                         }
15711:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
15712:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
15713:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
15714:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
15715:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
15716:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
15717:                                 } else {
15718:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
15719:                                                        $confhash{$itemid}{'lcauth'});
15720:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
15721:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
15722:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
15723:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
15724:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
15725:                                         }
15726:                                     } else {
15727:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
15728:                                     }
15729:                                 }
15730:                                 $resulttext .= '</li>';
15731:                             } else {
15732:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
15733:                             }
15734:                         }
15735:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
15736:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
15737:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
15738:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
15739:                             } else {
15740:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
15741:                             }
15742:                         }
15743:                         foreach my $item ('topmenu','inlinemenu') {
15744:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15745:                             if ($confhash{$itemid}{$item}) {
15746:                                 $resulttext .= &mt('Yes');
15747:                             } else {
15748:                                 $resulttext .= &mt('No');
15749:                             }
15750:                             $resulttext .= '</li>';
15751:                         }
15752:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15753:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15754:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
15755:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
15756:                             } else {
15757:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
15758:                             }
15759:                         }
15760:                         if ($confhash{$itemid}{'crsinc'}) {
15761:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15762:                                 my $rolemaps;
15763:                                 foreach my $role (@ltiroles) {
15764:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
15765:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
15766:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
15767:                                                                                 'Course').',';
15768:                                     }
15769:                                 }
15770:                                 if ($rolemaps) {
15771:                                     $rolemaps =~ s/,$//;
15772:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15773:                                 }
15774:                             }
15775:                             if ($confhash{$itemid}{'mapcrs'}) {
15776:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
15777:                             }
15778:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
15779:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
15780:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
15781:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
15782:                                                    '</li>';
15783:                                 } else {
15784:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
15785:                                 }
15786:                             }
15787:                             if ($confhash{$itemid}{'storecrs'}) {
15788:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
15789:                             }
15790:                             if ($confhash{$itemid}{'makecrs'}) {
15791:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
15792:                             } else {
15793:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
15794:                             }
15795:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
15796:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
15797:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
15798:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
15799:                                                    '</li>';
15800:                                 } else {
15801:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
15802:                                 }
15803:                             }
15804:                             if ($confhash{$itemid}{'section'}) {
15805:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
15806:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
15807:                                                          ' (course_section_sourcedid)'.'</li>';  
15808:                                 } else {
15809:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
15810:                                                           $confhash{$itemid}{'section'}.'</li>';
15811:                                 }
15812:                             } else {
15813:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
15814:                             }
15815:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
15816:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15817:                                 if ($confhash{$itemid}{$item}) {
15818:                                     $resulttext .= &mt('Yes');
15819:                                     if ($item eq 'passback') {
15820:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
15821:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
15822:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
15823:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
15824:                                         }
15825:                                     }
15826:                                 } else {
15827:                                     $resulttext .= &mt('No');
15828:                                 }
15829:                                 $resulttext .= '</li>';
15830:                             }
15831:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15832:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15833:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
15834:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
15835:                                 } else {
15836:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
15837:                                 }
15838:                             }
15839:                         }
15840:                     }
15841:                     $resulttext .= '</ul></li>';
15842:                 }
15843:             }
15844:             $resulttext .= '</ul>';
15845:         } else {
15846:             $resulttext = &mt('No changes made.');
15847:         }
15848:     } else {
15849:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15850:     }
15851:     if ($errors) {
15852:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15853:                        $errors.'</ul>';
15854:     }
15855:     return $resulttext;
15856: }
15857: 
15858: sub get_lti_id {
15859:     my ($domain,$consumer) = @_;
15860:     # get lock on lti db
15861:     my $lockhash = {
15862:                       lock => $env{'user.name'}.
15863:                               ':'.$env{'user.domain'},
15864:                    };
15865:     my $tries = 0;
15866:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15867:     my ($id,$error);
15868: 
15869:     while (($gotlock ne 'ok') && ($tries<10)) {
15870:         $tries ++;
15871:         sleep (0.1);
15872:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15873:     }
15874:     if ($gotlock eq 'ok') {
15875:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
15876:         if ($currids{'lock'}) {
15877:             delete($currids{'lock'});
15878:             if (keys(%currids)) {
15879:                 my @curr = sort { $a <=> $b } keys(%currids);
15880:                 if ($curr[-1] =~ /^\d+$/) {
15881:                     $id = 1 + $curr[-1];
15882:                 }
15883:             } else {
15884:                 $id = 1;
15885:             }
15886:             if ($id) {
15887:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
15888:                     $error = 'nostore';
15889:                 }
15890:             } else {
15891:                 $error = 'nonumber';
15892:             }
15893:         }
15894:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
15895:     } else {
15896:         $error = 'nolock';
15897:     }
15898:     return ($id,$error);
15899: }
15900: 
15901: sub modify_autoenroll {
15902:     my ($dom,$lastactref,%domconfig) = @_;
15903:     my ($resulttext,%changes);
15904:     my %currautoenroll;
15905:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
15906:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
15907:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
15908:         }
15909:     }
15910:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
15911:     my %title = ( run => 'Auto-enrollment active',
15912:                   sender => 'Sender for notification messages',
15913:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
15914:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
15915:     my @offon = ('off','on');
15916:     my $sender_uname = $env{'form.sender_uname'};
15917:     my $sender_domain = $env{'form.sender_domain'};
15918:     if ($sender_domain eq '') {
15919:         $sender_uname = '';
15920:     } elsif ($sender_uname eq '') {
15921:         $sender_domain = '';
15922:     }
15923:     my $coowners = $env{'form.autoassign_coowners'};
15924:     my $failsafe = $env{'form.autoenroll_failsafe'};
15925:     $failsafe =~ s{^\s+|\s+$}{}g;
15926:     if ($failsafe =~ /\D/) {
15927:         undef($failsafe);
15928:     }
15929:     my %autoenrollhash =  (
15930:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
15931:                                        'sender_uname' => $sender_uname,
15932:                                        'sender_domain' => $sender_domain,
15933:                                        'co-owners' => $coowners,
15934:                                        'autofailsafe' => $failsafe,
15935:                                 }
15936:                      );
15937:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
15938:                                              $dom);
15939:     if ($putresult eq 'ok') {
15940:         if (exists($currautoenroll{'run'})) {
15941:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
15942:                  $changes{'run'} = 1;
15943:              }
15944:         } elsif ($autorun) {
15945:             if ($env{'form.autoenroll_run'} ne '1') {
15946:                  $changes{'run'} = 1;
15947:             }
15948:         }
15949:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
15950:             $changes{'sender'} = 1;
15951:         }
15952:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
15953:             $changes{'sender'} = 1;
15954:         }
15955:         if ($currautoenroll{'co-owners'} ne '') {
15956:             if ($currautoenroll{'co-owners'} ne $coowners) {
15957:                 $changes{'coowners'} = 1;
15958:             }
15959:         } elsif ($coowners) {
15960:             $changes{'coowners'} = 1;
15961:         }
15962:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
15963:             $changes{'autofailsafe'} = 1;
15964:         }
15965:         if (keys(%changes) > 0) {
15966:             $resulttext = &mt('Changes made:').'<ul>';
15967:             if ($changes{'run'}) {
15968:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
15969:             }
15970:             if ($changes{'sender'}) {
15971:                 if ($sender_uname eq '' || $sender_domain eq '') {
15972:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
15973:                 } else {
15974:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
15975:                 }
15976:             }
15977:             if ($changes{'coowners'}) {
15978:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
15979:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15980:                 if (ref($lastactref) eq 'HASH') {
15981:                     $lastactref->{'domainconfig'} = 1;
15982:                 }
15983:             }
15984:             if ($changes{'autofailsafe'}) {
15985:                 if ($failsafe ne '') {
15986:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
15987:                 } else {
15988:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
15989:                 }
15990:                 &Apache::lonnet::get_domain_defaults($dom,1);
15991:                 if (ref($lastactref) eq 'HASH') {
15992:                     $lastactref->{'domdefaults'} = 1;
15993:                 }
15994:             }
15995:             $resulttext .= '</ul>';
15996:         } else {
15997:             $resulttext = &mt('No changes made to auto-enrollment settings');
15998:         }
15999:     } else {
16000:         $resulttext = '<span class="LC_error">'.
16001: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16002:     }
16003:     return $resulttext;
16004: }
16005: 
16006: sub modify_autoupdate {
16007:     my ($dom,%domconfig) = @_;
16008:     my ($resulttext,%currautoupdate,%fields,%changes);
16009:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16010:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16011:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16012:         }
16013:     }
16014:     my @offon = ('off','on');
16015:     my %title = &Apache::lonlocal::texthash (
16016:                     run        => 'Auto-update:',
16017:                     classlists => 'Updates to user information in classlists?',
16018:                     unexpired  => 'Skip updates for users without active or future roles?',
16019:                     lastactive => 'Skip updates for inactive users?',
16020:                 );
16021:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16022:     my %fieldtitles = &Apache::lonlocal::texthash (
16023:                         id => 'Student/Employee ID',
16024:                         permanentemail => 'E-mail address',
16025:                         lastname => 'Last Name',
16026:                         firstname => 'First Name',
16027:                         middlename => 'Middle Name',
16028:                         generation => 'Generation',
16029:                       );
16030:     $othertitle = &mt('All users');
16031:     if (keys(%{$usertypes}) >  0) {
16032:         $othertitle = &mt('Other users');
16033:     }
16034:     foreach my $key (keys(%env)) {
16035:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16036:             my ($usertype,$item) = ($1,$2);
16037:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16038:                 if ($usertype eq 'default') {   
16039:                     push(@{$fields{$1}},$2);
16040:                 } elsif (ref($types) eq 'ARRAY') {
16041:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16042:                         push(@{$fields{$1}},$2);
16043:                     }
16044:                 }
16045:             }
16046:         }
16047:     }
16048:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16049:     @lockablenames = sort(@lockablenames);
16050:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16051:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16052:         if (@changed) {
16053:             $changes{'lockablenames'} = 1;
16054:         }
16055:     } else {
16056:         if (@lockablenames) {
16057:             $changes{'lockablenames'} = 1;
16058:         }
16059:     }
16060:     my %updatehash = (
16061:                       autoupdate => { run => $env{'form.autoupdate_run'},
16062:                                       classlists => $env{'form.classlists'},
16063:                                       unexpired  => $env{'form.unexpired'},
16064:                                       fields => {%fields},
16065:                                       lockablenames => \@lockablenames,
16066:                                     }
16067:                      );
16068:     my $lastactivedays;
16069:     if ($env{'form.lastactive'}) {
16070:         $lastactivedays = $env{'form.lastactivedays'};
16071:         $lastactivedays =~ s/^\s+|\s+$//g;
16072:         unless ($lastactivedays =~ /^\d+$/) {
16073:             undef($lastactivedays);
16074:             $env{'form.lastactive'} = 0;
16075:         }
16076:     }
16077:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16078:     foreach my $key (keys(%currautoupdate)) {
16079:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16080:             if (exists($updatehash{autoupdate}{$key})) {
16081:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16082:                     $changes{$key} = 1;
16083:                 }
16084:             }
16085:         } elsif ($key eq 'fields') {
16086:             if (ref($currautoupdate{$key}) eq 'HASH') {
16087:                 foreach my $item (@{$types},'default') {
16088:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16089:                         my $change = 0;
16090:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16091:                             if (!exists($fields{$item})) {
16092:                                 $change = 1;
16093:                                 last;
16094:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16095:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16096:                                     $change = 1;
16097:                                     last;
16098:                                 }
16099:                             }
16100:                         }
16101:                         if ($change) {
16102:                             push(@{$changes{$key}},$item);
16103:                         }
16104:                     } 
16105:                 }
16106:             }
16107:         } elsif ($key eq 'lockablenames') {
16108:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16109:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16110:                 if (@changed) {
16111:                     $changes{'lockablenames'} = 1;
16112:                 }
16113:             } else {
16114:                 if (@lockablenames) {
16115:                     $changes{'lockablenames'} = 1;
16116:                 }
16117:             }
16118:         }
16119:     }
16120:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16121:         if (@lockablenames) {
16122:             $changes{'lockablenames'} = 1;
16123:         }
16124:     }
16125:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16126:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16127:             $changes{'unexpired'} = 1;
16128:         }
16129:     }
16130:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16131:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16132:             $changes{'lastactive'} = 1;
16133:         }
16134:     }
16135:     foreach my $item (@{$types},'default') {
16136:         if (defined($fields{$item})) {
16137:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16138:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16139:                     my $change = 0;
16140:                     if (ref($fields{$item}) eq 'ARRAY') {
16141:                         foreach my $type (@{$fields{$item}}) {
16142:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16143:                                 $change = 1;
16144:                                 last;
16145:                             }
16146:                         }
16147:                     }
16148:                     if ($change) {
16149:                         push(@{$changes{'fields'}},$item);
16150:                     }
16151:                 } else {
16152:                     push(@{$changes{'fields'}},$item);
16153:                 }
16154:             } else {
16155:                 push(@{$changes{'fields'}},$item);
16156:             }
16157:         }
16158:     }
16159:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16160:                                              $dom);
16161:     if ($putresult eq 'ok') {
16162:         if (keys(%changes) > 0) {
16163:             $resulttext = &mt('Changes made:').'<ul>';
16164:             foreach my $key (sort(keys(%changes))) {
16165:                 if ($key eq 'lockablenames') {
16166:                     $resulttext .= '<li>';
16167:                     if (@lockablenames) {
16168:                         $usertypes->{'default'} = $othertitle;
16169:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16170:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16171:                     } else {
16172:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16173:                     }
16174:                     $resulttext .= '</li>';
16175:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16176:                     foreach my $item (@{$changes{$key}}) {
16177:                         my @newvalues;
16178:                         foreach my $type (@{$fields{$item}}) {
16179:                             push(@newvalues,$fieldtitles{$type});
16180:                         }
16181:                         my $newvaluestr;
16182:                         if (@newvalues > 0) {
16183:                             $newvaluestr = join(', ',@newvalues);
16184:                         } else {
16185:                             $newvaluestr = &mt('none');
16186:                         }
16187:                         if ($item eq 'default') {
16188:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16189:                         } else {
16190:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16191:                         }
16192:                     }
16193:                 } else {
16194:                     my $newvalue;
16195:                     if ($key eq 'run') {
16196:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16197:                     } elsif ($key eq 'lastactive') {
16198:                         $newvalue = $offon[$env{'form.lastactive'}];
16199:                         unless ($lastactivedays eq '') {
16200:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16201:                         }
16202:                     } else {
16203:                         $newvalue = $offon[$env{'form.'.$key}];
16204:                     }
16205:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16206:                 }
16207:             }
16208:             $resulttext .= '</ul>';
16209:         } else {
16210:             $resulttext = &mt('No changes made to autoupdates');
16211:         }
16212:     } else {
16213:         $resulttext = '<span class="LC_error">'.
16214: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16215:     }
16216:     return $resulttext;
16217: }
16218: 
16219: sub modify_autocreate {
16220:     my ($dom,%domconfig) = @_;
16221:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16222:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16223:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16224:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16225:         }
16226:     }
16227:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16228:                  req => 'Auto-creation of validated requests for official courses',
16229:                  xmldc => 'Identity of course creator of courses from XML files',
16230:                );
16231:     my @types = ('xml','req');
16232:     foreach my $item (@types) {
16233:         $newvals{$item} = $env{'form.autocreate_'.$item};
16234:         $newvals{$item} =~ s/\D//g;
16235:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16236:     }
16237:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16238:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16239:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16240:         $newvals{'xmldc'} = '';
16241:     } 
16242:     %autocreatehash =  (
16243:                         autocreate => { xml => $newvals{'xml'},
16244:                                         req => $newvals{'req'},
16245:                                       }
16246:                        );
16247:     if ($newvals{'xmldc'} ne '') {
16248:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
16249:     }
16250:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
16251:                                              $dom);
16252:     if ($putresult eq 'ok') {
16253:         my @items = @types;
16254:         if ($newvals{'xml'}) {
16255:             push(@items,'xmldc');
16256:         }
16257:         foreach my $item (@items) {
16258:             if (exists($currautocreate{$item})) {
16259:                 if ($currautocreate{$item} ne $newvals{$item}) {
16260:                     $changes{$item} = 1;
16261:                 }
16262:             } elsif ($newvals{$item}) {
16263:                 $changes{$item} = 1;
16264:             }
16265:         }
16266:         if (keys(%changes) > 0) {
16267:             my @offon = ('off','on'); 
16268:             $resulttext = &mt('Changes made:').'<ul>';
16269:             foreach my $item (@types) {
16270:                 if ($changes{$item}) {
16271:                     my $newtxt = $offon[$newvals{$item}];
16272:                     $resulttext .= '<li>'.
16273:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
16274:                                        '<b>','</b>').
16275:                                    '</li>';
16276:                 }
16277:             }
16278:             if ($changes{'xmldc'}) {
16279:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
16280:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
16281:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
16282:             }
16283:             $resulttext .= '</ul>';
16284:         } else {
16285:             $resulttext = &mt('No changes made to auto-creation settings');
16286:         }
16287:     } else {
16288:         $resulttext = '<span class="LC_error">'.
16289:             &mt('An error occurred: [_1]',$putresult).'</span>';
16290:     }
16291:     return $resulttext;
16292: }
16293: 
16294: sub modify_directorysrch {
16295:     my ($dom,$lastactref,%domconfig) = @_;
16296:     my ($resulttext,%changes);
16297:     my %currdirsrch;
16298:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
16299:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
16300:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
16301:         }
16302:     }
16303:     my %title = ( available => 'Institutional directory search available',
16304:                   localonly => 'Other domains can search institution',
16305:                   lcavailable => 'LON-CAPA directory search available',
16306:                   lclocalonly => 'Other domains can search LON-CAPA domain',
16307:                   searchby => 'Search types',
16308:                   searchtypes => 'Search latitude');
16309:     my @offon = ('off','on');
16310:     my @otherdoms = ('Yes','No');
16311: 
16312:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
16313:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
16314:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
16315: 
16316:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16317:     if (keys(%{$usertypes}) == 0) {
16318:         @cansearch = ('default');
16319:     } else {
16320:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
16321:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
16322:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
16323:                     push(@{$changes{'cansearch'}},$type);
16324:                 }
16325:             }
16326:             foreach my $type (@cansearch) {
16327:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
16328:                     push(@{$changes{'cansearch'}},$type);
16329:                 }
16330:             }
16331:         } else {
16332:             push(@{$changes{'cansearch'}},@cansearch);
16333:         }
16334:     }
16335: 
16336:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
16337:         foreach my $by (@{$currdirsrch{'searchby'}}) {
16338:             if (!grep(/^\Q$by\E$/,@searchby)) {
16339:                 push(@{$changes{'searchby'}},$by);
16340:             }
16341:         }
16342:         foreach my $by (@searchby) {
16343:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
16344:                 push(@{$changes{'searchby'}},$by);
16345:             }
16346:         }
16347:     } else {
16348:         push(@{$changes{'searchby'}},@searchby);
16349:     }
16350: 
16351:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
16352:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
16353:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
16354:                 push(@{$changes{'searchtypes'}},$type);
16355:             }
16356:         }
16357:         foreach my $type (@searchtypes) {
16358:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
16359:                 push(@{$changes{'searchtypes'}},$type);
16360:             }
16361:         }
16362:     } else {
16363:         if (exists($currdirsrch{'searchtypes'})) {
16364:             foreach my $type (@searchtypes) {  
16365:                 if ($type ne $currdirsrch{'searchtypes'}) { 
16366:                     push(@{$changes{'searchtypes'}},$type);
16367:                 }
16368:             }
16369:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
16370:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
16371:             }   
16372:         } else {
16373:             push(@{$changes{'searchtypes'}},@searchtypes); 
16374:         }
16375:     }
16376: 
16377:     my %dirsrch_hash =  (
16378:             directorysrch => { available => $env{'form.dirsrch_available'},
16379:                                cansearch => \@cansearch,
16380:                                localonly => $env{'form.dirsrch_instlocalonly'},
16381:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
16382:                                lcavailable => $env{'form.dirsrch_domavailable'},
16383:                                searchby => \@searchby,
16384:                                searchtypes => \@searchtypes,
16385:                              }
16386:             );
16387:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
16388:                                              $dom);
16389:     if ($putresult eq 'ok') {
16390:         if (exists($currdirsrch{'available'})) {
16391:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
16392:                  $changes{'available'} = 1;
16393:              }
16394:         } else {
16395:             if ($env{'form.dirsrch_available'} eq '1') {
16396:                 $changes{'available'} = 1;
16397:             }
16398:         }
16399:         if (exists($currdirsrch{'lcavailable'})) {
16400:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
16401:                 $changes{'lcavailable'} = 1;
16402:             }
16403:         } else {
16404:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
16405:                 $changes{'lcavailable'} = 1;
16406:             }
16407:         }
16408:         if (exists($currdirsrch{'localonly'})) {
16409:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
16410:                 $changes{'localonly'} = 1;
16411:             }
16412:         } else {
16413:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
16414:                 $changes{'localonly'} = 1;
16415:             }
16416:         }
16417:         if (exists($currdirsrch{'lclocalonly'})) {
16418:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
16419:                 $changes{'lclocalonly'} = 1;
16420:             }
16421:         } else {
16422:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
16423:                 $changes{'lclocalonly'} = 1;
16424:             }
16425:         }
16426:         if (keys(%changes) > 0) {
16427:             $resulttext = &mt('Changes made:').'<ul>';
16428:             if ($changes{'available'}) {
16429:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
16430:             }
16431:             if ($changes{'lcavailable'}) {
16432:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
16433:             }
16434:             if ($changes{'localonly'}) {
16435:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
16436:             }
16437:             if ($changes{'lclocalonly'}) {
16438:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
16439:             }
16440:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
16441:                 my $chgtext;
16442:                 if (ref($usertypes) eq 'HASH') {
16443:                     if (keys(%{$usertypes}) > 0) {
16444:                         foreach my $type (@{$types}) {
16445:                             if (grep(/^\Q$type\E$/,@cansearch)) {
16446:                                 $chgtext .= $usertypes->{$type}.'; ';
16447:                             }
16448:                         }
16449:                         if (grep(/^default$/,@cansearch)) {
16450:                             $chgtext .= $othertitle;
16451:                         } else {
16452:                             $chgtext =~ s/\; $//;
16453:                         }
16454:                         $resulttext .=
16455:                             '<li>'.
16456:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
16457:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
16458:                             '</li>';
16459:                     }
16460:                 }
16461:             }
16462:             if (ref($changes{'searchby'}) eq 'ARRAY') {
16463:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
16464:                 my $chgtext;
16465:                 foreach my $type (@{$titleorder}) {
16466:                     if (grep(/^\Q$type\E$/,@searchby)) {
16467:                         if (defined($searchtitles->{$type})) {
16468:                             $chgtext .= $searchtitles->{$type}.'; ';
16469:                         }
16470:                     }
16471:                 }
16472:                 $chgtext =~ s/\; $//;
16473:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
16474:             }
16475:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
16476:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
16477:                 my $chgtext;
16478:                 foreach my $type (@{$srchtypeorder}) {
16479:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
16480:                         if (defined($srchtypes_desc->{$type})) {
16481:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
16482:                         }
16483:                     }
16484:                 }
16485:                 $chgtext =~ s/\; $//;
16486:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
16487:             }
16488:             $resulttext .= '</ul>';
16489:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
16490:             if (ref($lastactref) eq 'HASH') {
16491:                 $lastactref->{'directorysrch'} = 1;
16492:             }
16493:         } else {
16494:             $resulttext = &mt('No changes made to directory search settings');
16495:         }
16496:     } else {
16497:         $resulttext = '<span class="LC_error">'.
16498:                       &mt('An error occurred: [_1]',$putresult).'</span>';
16499:     }
16500:     return $resulttext;
16501: }
16502: 
16503: sub modify_contacts {
16504:     my ($dom,$lastactref,%domconfig) = @_;
16505:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
16506:     if (ref($domconfig{'contacts'}) eq 'HASH') {
16507:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
16508:             $currsetting{$key} = $domconfig{'contacts'}{$key};
16509:         }
16510:     }
16511:     my (%others,%to,%bcc,%includestr,%includeloc);
16512:     my @contacts = ('supportemail','adminemail');
16513:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
16514:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
16515:     my @toggles = ('reporterrors','reportupdates','reportstatus');
16516:     my @lonstatus = ('threshold','sysmail','weights','excluded');
16517:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
16518:     foreach my $type (@mailings) {
16519:         @{$newsetting{$type}} = 
16520:             &Apache::loncommon::get_env_multiple('form.'.$type);
16521:         foreach my $item (@contacts) {
16522:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
16523:                 $contacts_hash{contacts}{$type}{$item} = 1;
16524:             } else {
16525:                 $contacts_hash{contacts}{$type}{$item} = 0;
16526:             }
16527:         }
16528:         $others{$type} = $env{'form.'.$type.'_others'};
16529:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
16530:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16531:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
16532:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
16533:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
16534:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
16535:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
16536:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16537:             }
16538:         }
16539:     }
16540:     foreach my $item (@contacts) {
16541:         $to{$item} = $env{'form.'.$item};
16542:         $contacts_hash{'contacts'}{$item} = $to{$item};
16543:     }
16544:     foreach my $item (@toggles) {
16545:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
16546:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
16547:         }
16548:     }
16549:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
16550:     foreach my $item (@lonstatus) {
16551:         if ($item eq 'excluded') {
16552:             my (%serverhomes,@excluded);
16553:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
16554:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
16555:             if (@possexcluded) {
16556:                 foreach my $id (sort(@possexcluded)) {
16557:                     if ($serverhomes{$id}) {
16558:                         push(@excluded,$id);
16559:                     }
16560:                 }
16561:             }
16562:             if (@excluded) {
16563:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
16564:             }
16565:         } elsif ($item eq 'weights') {
16566:             foreach my $type ('E','W','N','U') {
16567:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
16568:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
16569:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
16570:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
16571:                             $env{'form.error'.$item.'_'.$type};
16572:                     }
16573:                 }
16574:             }
16575:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
16576:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
16577:             if ($env{'form.error'.$item} =~ /^\d+$/) {
16578:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
16579:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
16580:                 }
16581:             }
16582:         }
16583:     }
16584:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
16585:         foreach my $field (@{$fields}) {
16586:             if (ref($possoptions->{$field}) eq 'ARRAY') {
16587:                 my $value = $env{'form.helpform_'.$field};
16588:                 $value =~ s/^\s+|\s+$//g;
16589:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
16590:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
16591:                     if ($field eq 'screenshot') {
16592:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
16593:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
16594:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
16595:                         }
16596:                     }
16597:                 }
16598:             }
16599:         }
16600:     }
16601:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16602:     my (@statuses,%usertypeshash,@overrides);
16603:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
16604:         @statuses = @{$types};
16605:         if (ref($usertypes) eq 'HASH') {
16606:             %usertypeshash = %{$usertypes};
16607:         }
16608:     }
16609:     if (@statuses) {
16610:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
16611:         foreach my $type (@possoverrides) {
16612:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
16613:                 push(@overrides,$type);
16614:             }
16615:         }
16616:         if (@overrides) {
16617:             foreach my $type (@overrides) {
16618:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
16619:                 foreach my $item (@contacts) {
16620:                     if (grep(/^\Q$item\E$/,@standard)) {
16621:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
16622:                         $newsetting{'override_'.$type}{$item} = 1;
16623:                     } else {
16624:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
16625:                         $newsetting{'override_'.$type}{$item} = 0;
16626:                     }
16627:                 }
16628:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
16629:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16630:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
16631:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16632:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
16633:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
16634:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
16635:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16636:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
16637:                 }
16638:             }     
16639:         }
16640:     }
16641:     if (keys(%currsetting) > 0) {
16642:         foreach my $item (@contacts) {
16643:             if ($to{$item} ne $currsetting{$item}) {
16644:                 $changes{$item} = 1;
16645:             }
16646:         }
16647:         foreach my $type (@mailings) {
16648:             foreach my $item (@contacts) {
16649:                 if (ref($currsetting{$type}) eq 'HASH') {
16650:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
16651:                         push(@{$changes{$type}},$item);
16652:                     }
16653:                 } else {
16654:                     push(@{$changes{$type}},@{$newsetting{$type}});
16655:                 }
16656:             }
16657:             if ($others{$type} ne $currsetting{$type}{'others'}) {
16658:                 push(@{$changes{$type}},'others');
16659:             }
16660:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16661:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
16662:                     push(@{$changes{$type}},'bcc'); 
16663:                 }
16664:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
16665:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
16666:                     push(@{$changes{$type}},'include');
16667:                 }
16668:             }
16669:         }
16670:         if (ref($fields) eq 'ARRAY') {
16671:             if (ref($currsetting{'helpform'}) eq 'HASH') {
16672:                 foreach my $field (@{$fields}) {
16673:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
16674:                         push(@{$changes{'helpform'}},$field);
16675:                     }
16676:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16677:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
16678:                             push(@{$changes{'helpform'}},'maxsize');
16679:                         }
16680:                     }
16681:                 }
16682:             } else {
16683:                 foreach my $field (@{$fields}) {
16684:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16685:                         push(@{$changes{'helpform'}},$field);
16686:                     }
16687:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16688:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16689:                             push(@{$changes{'helpform'}},'maxsize');
16690:                         }
16691:                     }
16692:                 }
16693:             }
16694:         }
16695:         if (@statuses) {
16696:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
16697:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
16698:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
16699:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
16700:                             foreach my $item (@contacts,'bcc','others','include') {
16701:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
16702:                                     push(@{$changes{'overrides'}},$key);
16703:                                     last;
16704:                                 }
16705:                             }
16706:                         } else {
16707:                             push(@{$changes{'overrides'}},$key);
16708:                         }
16709:                     }
16710:                 }
16711:                 foreach my $key (@overrides) {
16712:                     unless (exists($currsetting{'overrides'}{$key})) {
16713:                         push(@{$changes{'overrides'}},$key);
16714:                     }
16715:                 }
16716:             } else {
16717:                 foreach my $key (@overrides) {
16718:                     push(@{$changes{'overrides'}},$key); 
16719:                 }
16720:             }
16721:         }
16722:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
16723:             foreach my $key ('excluded','weights','threshold','sysmail') {
16724:                 if ($key eq 'excluded') {
16725:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16726:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
16727:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16728:                             (@{$currsetting{'lonstatus'}{$key}})) {
16729:                             my @diffs =
16730:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
16731:                                                                    $currsetting{'lonstatus'}{$key});
16732:                             if (@diffs) {
16733:                                 push(@{$changes{'lonstatus'}},$key);
16734:                             }
16735:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
16736:                             push(@{$changes{'lonstatus'}},$key);
16737:                         }
16738:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16739:                              (@{$currsetting{'lonstatus'}{$key}})) {
16740:                         push(@{$changes{'lonstatus'}},$key);
16741:                     }
16742:                 } elsif ($key eq 'weights') {
16743:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16744:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
16745:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16746:                             foreach my $type ('E','W','N','U') {
16747:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
16748:                                         $currsetting{'lonstatus'}{$key}{$type}) {
16749:                                     push(@{$changes{'lonstatus'}},$key);
16750:                                     last;
16751:                                 }
16752:                             }
16753:                         } else {
16754:                             foreach my $type ('E','W','N','U') {
16755:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
16756:                                     push(@{$changes{'lonstatus'}},$key);
16757:                                     last;
16758:                                 }
16759:                             }
16760:                         }
16761:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16762:                         foreach my $type ('E','W','N','U') {
16763:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
16764:                                 push(@{$changes{'lonstatus'}},$key);
16765:                                 last;
16766:                             }
16767:                         }
16768:                     }
16769:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
16770:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16771:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16772:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
16773:                                 push(@{$changes{'lonstatus'}},$key);
16774:                             }
16775:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
16776:                             push(@{$changes{'lonstatus'}},$key);
16777:                         }
16778:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16779:                         push(@{$changes{'lonstatus'}},$key);
16780:                     }
16781:                 }
16782:             }
16783:         } else {
16784:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16785:                 foreach my $key ('excluded','weights','threshold','sysmail') {
16786:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16787:                         push(@{$changes{'lonstatus'}},$key);
16788:                     }
16789:                 }
16790:             }
16791:         }
16792:     } else {
16793:         my %default;
16794:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
16795:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
16796:         $default{'errormail'} = 'adminemail';
16797:         $default{'packagesmail'} = 'adminemail';
16798:         $default{'helpdeskmail'} = 'supportemail';
16799:         $default{'otherdomsmail'} = 'supportemail';
16800:         $default{'lonstatusmail'} = 'adminemail';
16801:         $default{'requestsmail'} = 'adminemail';
16802:         $default{'updatesmail'} = 'adminemail';
16803:         $default{'hostipmail'} = 'adminemail';
16804:         foreach my $item (@contacts) {
16805:            if ($to{$item} ne $default{$item}) {
16806:                $changes{$item} = 1;
16807:            }
16808:         }
16809:         foreach my $type (@mailings) {
16810:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
16811:                 push(@{$changes{$type}},@{$newsetting{$type}});
16812:             }
16813:             if ($others{$type} ne '') {
16814:                 push(@{$changes{$type}},'others');
16815:             }
16816:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16817:                 if ($bcc{$type} ne '') {
16818:                     push(@{$changes{$type}},'bcc');
16819:                 }
16820:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
16821:                     push(@{$changes{$type}},'include');
16822:                 }
16823:             }
16824:         }
16825:         if (ref($fields) eq 'ARRAY') {
16826:             foreach my $field (@{$fields}) {
16827:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16828:                     push(@{$changes{'helpform'}},$field);
16829:                 }
16830:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16831:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16832:                         push(@{$changes{'helpform'}},'maxsize');
16833:                     }
16834:                 }
16835:             }
16836:         }
16837:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16838:             foreach my $key ('excluded','weights','threshold','sysmail') {
16839:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16840:                     push(@{$changes{'lonstatus'}},$key);
16841:                 }
16842:             }
16843:         }
16844:     }
16845:     foreach my $item (@toggles) {
16846:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
16847:             $changes{$item} = 1;
16848:         } elsif ((!$env{'form.'.$item}) &&
16849:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
16850:             $changes{$item} = 1;
16851:         }
16852:     }
16853:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
16854:                                              $dom);
16855:     if ($putresult eq 'ok') {
16856:         if (keys(%changes) > 0) {
16857:             &Apache::loncommon::devalidate_domconfig_cache($dom);
16858:             if (ref($lastactref) eq 'HASH') {
16859:                 $lastactref->{'domainconfig'} = 1;
16860:             }
16861:             my ($titles,$short_titles)  = &contact_titles();
16862:             $resulttext = &mt('Changes made:').'<ul>';
16863:             foreach my $item (@contacts) {
16864:                 if ($changes{$item}) {
16865:                     $resulttext .= '<li>'.$titles->{$item}.
16866:                                     &mt(' set to: ').
16867:                                     '<span class="LC_cusr_emph">'.
16868:                                     $to{$item}.'</span></li>';
16869:                 }
16870:             }
16871:             foreach my $type (@mailings) {
16872:                 if (ref($changes{$type}) eq 'ARRAY') {
16873:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16874:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
16875:                     } else {
16876:                         $resulttext .= '<li>'.$titles->{$type}.': ';
16877:                     }
16878:                     my @text;
16879:                     foreach my $item (@{$newsetting{$type}}) {
16880:                         push(@text,$short_titles->{$item});
16881:                     }
16882:                     if ($others{$type} ne '') {
16883:                         push(@text,$others{$type});
16884:                     }
16885:                     if (@text) {
16886:                         $resulttext .= '<span class="LC_cusr_emph">'.
16887:                                        join(', ',@text).'</span>';
16888:                     }
16889:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16890:                         if ($bcc{$type} ne '') {
16891:                             my $bcctext;
16892:                             if (@text) {
16893:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
16894:                             } else {
16895:                                 $bcctext = '(Bcc)';
16896:                             }
16897:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
16898:                         } elsif (!@text) {
16899:                             $resulttext .= &mt('No one');
16900:                         }   
16901:                         if ($includestr{$type} ne '') {
16902:                             if ($includeloc{$type} eq 'b') {
16903:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
16904:                             } elsif ($includeloc{$type} eq 's') {
16905:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
16906:                             }
16907:                         }
16908:                     } elsif (!@text) {
16909:                         $resulttext .= &mt('No recipients');
16910:                     }
16911:                     $resulttext .= '</li>';
16912:                 }
16913:             }
16914:             if (ref($changes{'overrides'}) eq 'ARRAY') {
16915:                 my @deletions;
16916:                 foreach my $type (@{$changes{'overrides'}}) {
16917:                     if ($usertypeshash{$type}) {
16918:                         if (grep(/^\Q$type\E/,@overrides)) {
16919:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
16920:                                                       $usertypeshash{$type}).'<ul><li>';
16921:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
16922:                                 my @text;
16923:                                 foreach my $item (@contacts) {
16924:                                     if ($newsetting{'override_'.$type}{$item}) { 
16925:                                         push(@text,$short_titles->{$item});
16926:                                     }
16927:                                 }
16928:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
16929:                                     push(@text,$newsetting{'override_'.$type}{'others'});
16930:                                 }
16931:   
16932:                                 if (@text) {
16933:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
16934:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
16935:                                 }
16936:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
16937:                                     my $bcctext;
16938:                                     if (@text) {
16939:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
16940:                                     } else {
16941:                                         $bcctext = '(Bcc)';
16942:                                     }
16943:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
16944:                                 } elsif (!@text) {
16945:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
16946:                                 }
16947:                                 $resulttext .= '</li>';
16948:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
16949:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
16950:                                     if ($loc eq 'b') {
16951:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
16952:                                     } elsif ($loc eq 's') {
16953:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
16954:                                     }
16955:                                 }
16956:                             }
16957:                             $resulttext .= '</li></ul></li>';
16958:                         } else {
16959:                             push(@deletions,$usertypeshash{$type});
16960:                         }
16961:                     }
16962:                 }
16963:                 if (@deletions) {
16964:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
16965:                                               join(', ',@deletions)).'</li>';
16966:                 }
16967:             }
16968:             my @offon = ('off','on');
16969:             my $corelink = &core_link_msu();
16970:             if ($changes{'reporterrors'}) {
16971:                 $resulttext .= '<li>'.
16972:                                &mt('E-mail error reports to [_1] set to "'.
16973:                                    $offon[$env{'form.reporterrors'}].'".',
16974:                                    $corelink).
16975:                                '</li>';
16976:             }
16977:             if ($changes{'reportupdates'}) {
16978:                 $resulttext .= '<li>'.
16979:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
16980:                                     $offon[$env{'form.reportupdates'}].'".',
16981:                                     $corelink).
16982:                                 '</li>';
16983:             }
16984:             if ($changes{'reportstatus'}) {
16985:                 $resulttext .= '<li>'.
16986:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
16987:                                     $offon[$env{'form.reportstatus'}].'".',
16988:                                     $corelink).
16989:                                 '</li>';
16990:             }
16991:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
16992:                 $resulttext .= '<li>'.
16993:                                &mt('Nightly status check e-mail settings').':<ul>';
16994:                 my (%defval,%use_def,%shown);
16995:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
16996:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
16997:                 $defval{'weights'} =
16998:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
16999:                 $defval{'excluded'} = &mt('None');
17000:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17001:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17002:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17003:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17004:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17005:                             } elsif ($item eq 'weights') {
17006:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17007:                                     foreach my $type ('E','W','N','U') {
17008:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17009:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17010:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17011:                                         } else {
17012:                                             $shown{$item} .= $lonstatus_defs->{$type};
17013:                                         }
17014:                                         $shown{$item} .= ', ';
17015:                                     }
17016:                                     $shown{$item} =~ s/, $//;
17017:                                 } else {
17018:                                     $shown{$item} = $defval{$item};
17019:                                 }
17020:                             } elsif ($item eq 'excluded') {
17021:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17022:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17023:                                 } else {
17024:                                     $shown{$item} = $defval{$item};
17025:                                 }
17026:                             }
17027:                         } else {
17028:                             $shown{$item} = $defval{$item};
17029:                         }
17030:                     }
17031:                 } else {
17032:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17033:                         $shown{$item} = $defval{$item};
17034:                     }
17035:                 }
17036:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17037:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17038:                                           $shown{$item}).'</li>';
17039:                 }
17040:                 $resulttext .= '</ul></li>';
17041:             }
17042:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17043:                 my (@optional,@required,@unused,$maxsizechg);
17044:                 foreach my $field (@{$changes{'helpform'}}) {
17045:                     if ($field eq 'maxsize') {
17046:                         $maxsizechg = 1;
17047:                         next;
17048:                     }
17049:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17050:                         push(@optional,$field);
17051:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17052:                         push(@unused,$field);
17053:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17054:                         push(@required,$field);
17055:                     }
17056:                 }
17057:                 if (@optional) {
17058:                     $resulttext .= '<li>'.
17059:                                    &mt('Help form fields changed to "Optional": [_1].',
17060:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17061:                                    '</li>';
17062:                 }
17063:                 if (@required) {
17064:                     $resulttext .= '<li>'.
17065:                                    &mt('Help form fields changed to "Required": [_1].',
17066:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17067:                                    '</li>';
17068:                 }
17069:                 if (@unused) {
17070:                     $resulttext .= '<li>'.
17071:                                    &mt('Help form fields changed to "Not shown": [_1].',
17072:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17073:                                    '</li>';
17074:                 }
17075:                 if ($maxsizechg) {
17076:                     $resulttext .= '<li>'.
17077:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17078:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17079:                                    '</li>';
17080:                 }
17081:             }
17082:             $resulttext .= '</ul>';
17083:         } else {
17084:             $resulttext = &mt('No changes made to contacts and form settings');
17085:         }
17086:     } else {
17087:         $resulttext = '<span class="LC_error">'.
17088:             &mt('An error occurred: [_1].',$putresult).'</span>';
17089:     }
17090:     return $resulttext;
17091: }
17092: 
17093: sub modify_privacy {
17094:     my ($dom,%domconfig) = @_;
17095:     my ($resulttext,%current,%changes);
17096:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17097:         %current = %{$domconfig{'privacy'}};
17098:     }
17099:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17100:     my @items = ('domain','author','course','community');
17101:     my %names = &Apache::lonlocal::texthash (
17102:                    domain => 'Assigned domain role(s)',
17103:                    author => 'Assigned co-author role(s)',
17104:                    course => 'Assigned course role(s)',
17105:                    community => 'Assigned community role',
17106:                 );
17107:     my %roles = &Apache::lonlocal::texthash (
17108:                    domain => 'Domain role',
17109:                    author => 'Co-author role',
17110:                    course => 'Course role',
17111:                    community => 'Community role',
17112:                 );
17113:     my %titles = &Apache::lonlocal::texthash (
17114:                   approval => 'Approval for role in different domain',
17115:                   othdom   => 'User information available in other domain',
17116:                   priv     => 'Information viewable by privileged user in same domain',
17117:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17118:                   instdom  => 'Other domain shares institution/provider',
17119:                   extdom   => 'Other domain has different institution/provider',
17120:                   none     => 'Not allowed',
17121:                   user     => 'User authorizes',
17122:                   domain   => 'Domain Coordinator authorizes',
17123:                   auto     => 'Unrestricted',
17124:     );
17125:     my %fieldnames = &Apache::lonlocal::texthash (
17126:                         id => 'Student/Employee ID',
17127:                         permanentemail => 'E-mail address',
17128:                         lastname => 'Last Name',
17129:                         firstname => 'First Name',
17130:                         middlename => 'Middle Name',
17131:                         generation => 'Generation',
17132:     );
17133:     my ($othertitle,$usertypes,$types) =
17134:         &Apache::loncommon::sorted_inst_types($dom);
17135:     my (%by_ip,%by_location,@intdoms,@instdoms);
17136:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17137: 
17138:     my %privacyhash = (
17139:                        'approval' => {
17140:                                        instdom => {},
17141:                                        extdom  => {},
17142:                                      },
17143:                        'othdom'   => {},
17144:                        'priv'     => {},
17145:                        'unpriv'   => {},
17146:                       );
17147:     foreach my $item (@items) {
17148:         if (@instdoms > 1) {
17149:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
17150:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17151:             }
17152:             if (ref($current{'approval'}) eq 'HASH') {
17153:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17154:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17155:                         $changes{'approval'} = 1;
17156:                     }
17157:                 }
17158:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17159:                 $changes{'approval'} = 1;
17160:             }
17161:         }
17162:         if (keys(%by_location) > 0) {
17163:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17164:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17165:             }
17166:             if (ref($current{'approval'}) eq 'HASH') {
17167:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17168:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17169:                         $changes{'approval'} = 1;
17170:                     }
17171:                 }
17172:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17173:                 $changes{'approval'} = 1;
17174:             }
17175:         }
17176:         foreach my $status ('priv','unpriv') {
17177:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17178:             my @newvalues;
17179:             foreach my $field (@possibles) {
17180:                 if (grep(/^\Q$field\E$/,@fields)) {
17181:                     $privacyhash{$status}{$item}{$field} = 1;
17182:                     push(@newvalues,$field);
17183:                 }
17184:             }
17185:             @newvalues = sort(@newvalues);
17186:             if (ref($current{$status}) eq 'HASH') {
17187:                 if (ref($current{$status}{$item}) eq 'HASH') {
17188:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17189:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17190:                     if (@diffs > 0) {
17191:                         $changes{$status} = 1;
17192:                     }
17193:                 }
17194:             } else {
17195:                 my @stdfields;
17196:                 foreach my $field (@fields) {
17197:                     if ($field eq 'id') {
17198:                         next if ($status eq 'unpriv');
17199:                         next if (($status eq 'priv') && ($item eq 'community'));
17200:                     }
17201:                     push(@stdfields,$field);
17202:                 }
17203:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17204:                 if (@diffs > 0) {
17205:                     $changes{$status} = 1;
17206:                 }
17207:             }
17208:         }
17209:     }
17210:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17211:         my @statuses;
17212:         if (ref($types) eq 'ARRAY') {
17213:             @statuses = @{$types};
17214:         }
17215:         foreach my $type (@statuses,'default') {
17216:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17217:             my @newvalues;
17218:             foreach my $field (sort(@possfields)) {
17219:                 if (grep(/^\Q$field\E$/,@fields)) {
17220:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17221:                     push(@newvalues,$field);
17222:                 }
17223:             }
17224:             @newvalues = sort(@newvalues);
17225:             if (ref($current{'othdom'}) eq 'HASH') {
17226:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17227:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17228:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17229:                     if (@diffs > 0) {
17230:                         $changes{'othdom'} = 1;
17231:                     }
17232:                 }
17233:             } else {
17234:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17235:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17236:                 if (@diffs > 0) {
17237:                     $changes{'othdom'} = 1;
17238:                 }
17239:             }
17240:         }
17241:     }
17242:     my %confighash = (
17243:                         privacy => \%privacyhash,
17244:                      );
17245:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17246:     if ($putresult eq 'ok') {
17247:         if (keys(%changes) > 0) {
17248:             $resulttext = &mt('Changes made: ').'<ul>';
17249:             foreach my $key ('approval','othdom','priv','unpriv') {
17250:                 if ($changes{$key}) {
17251:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17252:                     if ($key eq 'approval') {
17253:                         if (keys(%{$privacyhash{$key}{instdom}})) {
17254:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
17255:                             foreach my $item (@items) {
17256:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
17257:                             }
17258:                             $resulttext .= '</ul></li>';
17259:                         }
17260:                         if (keys(%{$privacyhash{$key}{extdom}})) {
17261:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
17262:                             foreach my $item (@items) {
17263:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
17264:                             }
17265:                             $resulttext .= '</ul></li>';
17266:                         }
17267:                     } elsif ($key eq 'othdom') {
17268:                         my @statuses;
17269:                         if (ref($types) eq 'ARRAY') {
17270:                             @statuses = @{$types};
17271:                         }
17272:                         if (ref($privacyhash{$key}) eq 'HASH') {
17273:                             foreach my $status (@statuses,'default') {
17274:                                 if ($status eq 'default') {
17275:                                     $resulttext .= '<li>'.$othertitle.': ';
17276:                                 } elsif (ref($usertypes) eq 'HASH') {
17277:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
17278:                                 } else {
17279:                                     next;
17280:                                 }
17281:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
17282:                                     if (keys(%{$privacyhash{$key}{$status}})) {
17283:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
17284:                                     } else {
17285:                                         $resulttext .= &mt('none');
17286:                                     }
17287:                                 }
17288:                                 $resulttext .= '</li>';
17289:                             }
17290:                         }
17291:                     } else {
17292:                         foreach my $item (@items) {
17293:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
17294:                                 $resulttext .= '<li>'.$names{$item}.': ';
17295:                                 if (keys(%{$privacyhash{$key}{$item}})) {
17296:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
17297:                                 } else {
17298:                                     $resulttext .= &mt('none');
17299:                                 }
17300:                                 $resulttext .= '</li>';
17301:                             }
17302:                         }
17303:                     }
17304:                     $resulttext .= '</ul></li>';
17305:                 }
17306:             }
17307:         } else {
17308:             $resulttext = &mt('No changes made to user information settings');
17309:         }
17310:     } else {
17311:         $resulttext = '<span class="LC_error">'.
17312:             &mt('An error occurred: [_1]',$putresult).'</span>';
17313:     }
17314:     return $resulttext;
17315: }
17316: 
17317: sub modify_passwords {
17318:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17319:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
17320:         $updatedefaults,$updateconf);
17321:     my $customfn = 'resetpw.html';
17322:     if (ref($domconfig{'passwords'}) eq 'HASH') {
17323:         %current = %{$domconfig{'passwords'}};
17324:     }
17325:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17326:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17327:     if (ref($types) eq 'ARRAY') {
17328:         @oktypes = @{$types};
17329:     }
17330:     push(@oktypes,'default');
17331: 
17332:     my %titles = &Apache::lonlocal::texthash (
17333:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
17334:         intauth_check  => 'Check bcrypt cost if authenticated',
17335:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
17336:         permanent      => 'Permanent e-mail address',
17337:         critical       => 'Critical notification address',
17338:         notify         => 'Notification address',
17339:         min            => 'Minimum password length',
17340:         max            => 'Maximum password length',
17341:         chars          => 'Required characters',
17342:         expire         => 'Password expiration (days)',
17343:         numsaved       => 'Number of previous passwords to save',
17344:         reset          => 'Resetting Forgotten Password',
17345:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
17346:         rules          => 'Rules for LON-CAPA Passwords',
17347:         crsownerchg    => 'Course Owner Changing Student Passwords',
17348:         username       => 'Username',
17349:         email          => 'E-mail address',
17350:     );
17351: 
17352: #
17353: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
17354: #
17355:     my (%curr_defaults,%save_defaults);
17356:     if (ref($domconfig{'defaults'}) eq 'HASH') {
17357:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
17358:             if ($key =~ /^intauth_(cost|check|switch)$/) {
17359:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
17360:             } else {
17361:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
17362:             }
17363:         }
17364:     }
17365:     my %staticdefaults = (
17366:         'resetlink'      => 2,
17367:         'resetcase'      => \@oktypes,
17368:         'resetprelink'   => 'both',
17369:         'resetemail'     => ['critical','notify','permanent'],
17370:         'intauth_cost'   => 10,
17371:         'intauth_check'  => 0,
17372:         'intauth_switch' => 0,
17373:     );
17374:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17375:     foreach my $type (@oktypes) {
17376:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
17377:     }
17378:     my $linklife = $env{'form.passwords_link'};
17379:     $linklife =~ s/^\s+|\s+$//g;
17380:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
17381:         $newvalues{'resetlink'} = $linklife;
17382:         if ($current{'resetlink'}) {
17383:             if ($current{'resetlink'} ne $linklife) {
17384:                 $changes{'reset'} = 1;
17385:             }
17386:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17387:             if ($staticdefaults{'resetlink'} ne $linklife) {
17388:                 $changes{'reset'} = 1;
17389:             }
17390:         }
17391:     } elsif ($current{'resetlink'}) {
17392:         $changes{'reset'} = 1;
17393:     }
17394:     my @casesens;
17395:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
17396:     foreach my $case (sort(@posscase)) {
17397:         if (grep(/^\Q$case\E$/,@oktypes)) {
17398:             push(@casesens,$case);
17399:         }
17400:     }
17401:     $newvalues{'resetcase'} = \@casesens;
17402:     if (ref($current{'resetcase'}) eq 'ARRAY') {
17403:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
17404:         if (@diffs > 0) {
17405:             $changes{'reset'} = 1;
17406:         }
17407:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17408:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
17409:         if (@diffs > 0) {
17410:             $changes{'reset'} = 1;
17411:         }
17412:     }
17413:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
17414:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
17415:         if (exists($current{'resetprelink'})) {
17416:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
17417:                 $changes{'reset'} = 1;
17418:             }
17419:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17420:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
17421:                 $changes{'reset'} = 1;
17422:             }
17423:         }
17424:     } elsif ($current{'resetprelink'}) {
17425:         $changes{'reset'} = 1;
17426:     }
17427:     foreach my $type (@oktypes) {
17428:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
17429:         my @postlink;
17430:         foreach my $item (sort(@possplink)) {
17431:             if ($item =~ /^(email|username)$/) {
17432:                 push(@postlink,$item);
17433:             }
17434:         }
17435:         $newvalues{'resetpostlink'}{$type} = \@postlink;
17436:         unless ($changes{'reset'}) {
17437:             if (ref($current{'resetpostlink'}) eq 'HASH') {
17438:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
17439:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
17440:                     if (@diffs > 0) {
17441:                         $changes{'reset'} = 1;
17442:                     }
17443:                 } else {
17444:                     $changes{'reset'} = 1;
17445:                 }
17446:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17447:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
17448:                 if (@diffs > 0) {
17449:                     $changes{'reset'} = 1;
17450:                 }
17451:             }
17452:         }
17453:     }
17454:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
17455:     my @resetemail;
17456:     foreach my $item (sort(@possemailsrc)) {
17457:         if ($item =~ /^(permanent|critical|notify)$/) {
17458:             push(@resetemail,$item);
17459:         }
17460:     }
17461:     $newvalues{'resetemail'} = \@resetemail;
17462:     unless ($changes{'reset'}) {
17463:         if (ref($current{'resetemail'}) eq 'ARRAY') {
17464:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
17465:             if (@diffs > 0) {
17466:                 $changes{'reset'} = 1;
17467:             }
17468:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17469:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
17470:             if (@diffs > 0) {
17471:                 $changes{'reset'} = 1;
17472:             }
17473:         }
17474:     }
17475:     if ($env{'form.passwords_stdtext'} == 0) {
17476:         $newvalues{'resetremove'} = 1;
17477:         unless ($current{'resetremove'}) {
17478:             $changes{'reset'} = 1;
17479:         }
17480:     } elsif ($current{'resetremove'}) {
17481:         $changes{'reset'} = 1;
17482:     }
17483:     if ($env{'form.passwords_customfile.filename'} ne '') {
17484:         my $servadm = $r->dir_config('lonAdmEMail');
17485:         my ($configuserok,$author_ok,$switchserver) =
17486:             &config_check($dom,$confname,$servadm);
17487:         my $error;
17488:         if ($configuserok eq 'ok') {
17489:             if ($switchserver) {
17490:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
17491:             } else {
17492:                 if ($author_ok eq 'ok') {
17493:                     my ($result,$customurl) =
17494:                         &publishlogo($r,'upload','passwords_customfile',$dom,
17495:                                      $confname,'customtext/resetpw','','',$customfn);
17496:                     if ($result eq 'ok') {
17497:                         $newvalues{'resetcustom'} = $customurl;
17498:                         $changes{'reset'} = 1;
17499:                     } else {
17500:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
17501:                     }
17502:                 } else {
17503:                     $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);
17504:                 }
17505:             }
17506:         } else {
17507:             $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);
17508:         }
17509:         if ($error) {
17510:             &Apache::lonnet::logthis($error);
17511:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17512:         }
17513:     } elsif ($current{'resetcustom'}) {
17514:         if ($env{'form.passwords_custom_del'}) {
17515:             $changes{'reset'} = 1;
17516:         } else {
17517:             $newvalues{'resetcustom'} = $current{'resetcustom'};
17518:         }
17519:     }
17520:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
17521:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
17522:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
17523:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
17524:             $changes{'intauth'} = 1;
17525:         }
17526:     } else {
17527:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
17528:     }
17529:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
17530:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
17531:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
17532:             $changes{'intauth'} = 1;
17533:         }
17534:     } else {
17535:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17536:     }
17537:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
17538:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
17539:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
17540:             $changes{'intauth'} = 1;
17541:         }
17542:     } else {
17543:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17544:     }
17545:     foreach my $item ('cost','check','switch') {
17546:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
17547:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
17548:             $updatedefaults = 1;
17549:         }
17550:     }
17551:     foreach my $rule ('min','max','expire','numsaved') {
17552:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
17553:         my $ruleok;
17554:         if ($rule eq 'expire') {
17555:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
17556:                 ($env{'form.passwords_'.$rule} ne '0')) {
17557:                 $ruleok = 1;
17558:             }
17559:         } elsif ($rule eq 'min') {
17560:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
17561:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
17562:                     $ruleok = 1;
17563:                 }
17564:             }
17565:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
17566:                  ($env{'form.passwords_'.$rule} ne '0')) {
17567:             $ruleok = 1;
17568:         }
17569:         if ($ruleok) {
17570:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
17571:             if (exists($current{$rule})) {
17572:                 if ($newvalues{$rule} ne $current{$rule}) {
17573:                     $changes{'rules'} = 1;
17574:                 }
17575:             } elsif ($rule eq 'min') {
17576:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
17577:                     $changes{'rules'} = 1;
17578:                 }
17579:             } else {
17580:                 $changes{'rules'} = 1;
17581:             }
17582:         } elsif (exists($current{$rule})) {
17583:             $changes{'rules'} = 1;
17584:         }
17585:     }
17586:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
17587:     my @chars;
17588:     foreach my $item (sort(@posschars)) {
17589:         if ($item =~ /^(uc|lc|num|spec)$/) {
17590:             push(@chars,$item);
17591:         }
17592:     }
17593:     $newvalues{'chars'} = \@chars;
17594:     unless ($changes{'rules'}) {
17595:         if (ref($current{'chars'}) eq 'ARRAY') {
17596:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
17597:             if (@diffs > 0) {
17598:                 $changes{'rules'} = 1;
17599:             }
17600:         } else {
17601:             if (@chars > 0) {
17602:                 $changes{'rules'} = 1;
17603:             }
17604:         }
17605:     }
17606:     my %crsownerchg = (
17607:                         by => [],
17608:                         for => [],
17609:                       );
17610:     foreach my $item ('by','for') {
17611:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
17612:         foreach my $type (sort(@posstypes)) {
17613:             if (grep(/^\Q$type\E$/,@oktypes)) {
17614:                 push(@{$crsownerchg{$item}},$type);
17615:             }
17616:         }
17617:     }
17618:     $newvalues{'crsownerchg'} = \%crsownerchg;
17619:     if (ref($current{'crsownerchg'}) eq 'HASH') {
17620:         foreach my $item ('by','for') {
17621:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
17622:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
17623:                 if (@diffs > 0) {
17624:                     $changes{'crsownerchg'} = 1;
17625:                     last;
17626:                 }
17627:             }
17628:         }
17629:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
17630:         foreach my $item ('by','for') {
17631:             if (@{$crsownerchg{$item}} > 0) {
17632:                 $changes{'crsownerchg'} = 1;
17633:                 last;
17634:             }
17635:         }
17636:     }
17637: 
17638:     my %confighash = (
17639:                         defaults  => \%save_defaults,
17640:                         passwords => \%newvalues,
17641:                      );
17642:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
17643: 
17644:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17645:     if ($putresult eq 'ok') {
17646:         if (keys(%changes) > 0) {
17647:             $resulttext = &mt('Changes made: ').'<ul>';
17648:             foreach my $key ('reset','intauth','rules','crsownerchg') {
17649:                 if ($changes{$key}) {
17650:                     unless ($key eq 'intauth') {
17651:                         $updateconf = 1;
17652:                     }
17653:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17654:                     if ($key eq 'reset') {
17655:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
17656:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
17657:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
17658:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
17659:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
17660:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
17661:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
17662:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
17663:                             }
17664:                         } else {
17665:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
17666:                         }
17667:                         if ($confighash{'passwords'}{'resetlink'}) {
17668:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
17669:                         } else {
17670:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
17671:                                                   &mt('Will default to 2 hours').'</li>';
17672:                         }
17673:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
17674:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
17675:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
17676:                             } else {
17677:                                 my $casesens;
17678:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
17679:                                     if ($type eq 'default') {
17680:                                         $casesens .= $othertitle.', ';
17681:                                     } elsif ($usertypes->{$type} ne '') {
17682:                                         $casesens .= $usertypes->{$type}.', ';
17683:                                     }
17684:                                 }
17685:                                 $casesens =~ s/\Q, \E$//;
17686:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
17687:                             }
17688:                         } else {
17689:                             $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>';
17690:                         }
17691:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
17692:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
17693:                         } else {
17694:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
17695:                         }
17696:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
17697:                             my $output;
17698:                             if (ref($types) eq 'ARRAY') {
17699:                                 foreach my $type (@{$types}) {
17700:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
17701:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
17702:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
17703:                                         } else {
17704:                                             $output .= $usertypes->{$type}.' -- '.
17705:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
17706:                                         }
17707:                                     }
17708:                                 }
17709:                             }
17710:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
17711:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
17712:                                     $output .= $othertitle.' -- '.&mt('none');
17713:                                 } else {
17714:                                     $output .= $othertitle.' -- '.
17715:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
17716:                                 }
17717:                             }
17718:                             if ($output) {
17719:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
17720:                             } else {
17721:                                 $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>';
17722:                             }
17723:                         } else {
17724:                             $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>';
17725:                         }
17726:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
17727:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
17728:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
17729:                             } else {
17730:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17731:                             }
17732:                         } else {
17733:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17734:                         }
17735:                         if ($confighash{'passwords'}{'resetremove'}) {
17736:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
17737:                         } else {
17738:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
17739:                         }
17740:                         if ($confighash{'passwords'}{'resetcustom'}) {
17741:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
17742:                                                                             &mt('custom text'),600,500,undef,undef,
17743:                                                                             undef,undef,'background-color:#ffffff');
17744:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
17745:                         } else {
17746:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
17747:                         }
17748:                     } elsif ($key eq 'intauth') {
17749:                         foreach my $item ('cost','switch','check') {
17750:                             my $value = $save_defaults{$key.'_'.$item};
17751:                             if ($item eq 'switch') {
17752:                                 my %optiondesc = &Apache::lonlocal::texthash (
17753:                                                      0 => 'No',
17754:                                                      1 => 'Yes',
17755:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
17756:                                                  );
17757:                                 if ($value =~ /^(0|1|2)$/) {
17758:                                     $value = $optiondesc{$value};
17759:                                 } else {
17760:                                     $value = &mt('none -- defaults to No');
17761:                                 }
17762:                             } elsif ($item eq 'check') {
17763:                                 my %optiondesc = &Apache::lonlocal::texthash (
17764:                                                      0 => 'No',
17765:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
17766:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
17767:                                                  );
17768:                                 if ($value =~ /^(0|1|2)$/) {
17769:                                     $value = $optiondesc{$value};
17770:                                 } else {
17771:                                     $value = &mt('none -- defaults to No');
17772:                                 }
17773:                             }
17774:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
17775:                         }
17776:                     } elsif ($key eq 'rules') {
17777:                         foreach my $rule ('min','max','expire','numsaved') {
17778:                             if ($confighash{'passwords'}{$rule} eq '') {
17779:                                 if ($rule eq 'min') {
17780:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
17781:                                                    ' '.&mt('Default of [_1] will be used',
17782:                                                            $Apache::lonnet::passwdmin).'</li>';
17783:                                 } else {
17784:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
17785:                                 }
17786:                             } else {
17787:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
17788:                             }
17789:                         }
17790:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
17791:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
17792:                                 my %rulenames = &Apache::lonlocal::texthash(
17793:                                                      uc => 'At least one upper case letter',
17794:                                                      lc => 'At least one lower case letter',
17795:                                                      num => 'At least one number',
17796:                                                      spec => 'At least one non-alphanumeric',
17797:                                                    );
17798:                                 my $needed = '<ul><li>'.
17799:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
17800:                                              '</li></ul>'; 
17801:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
17802:                             } else {
17803:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17804:                             }
17805:                         } else {
17806:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17807:                         }
17808:                     } elsif ($key eq 'crsownerchg') {
17809:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
17810:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
17811:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
17812:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17813:                             } else {
17814:                                 my %crsownerstr;
17815:                                 foreach my $item ('by','for') {
17816:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
17817:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
17818:                                             if ($type eq 'default') {
17819:                                                 $crsownerstr{$item} .= $othertitle.', ';
17820:                                             } elsif ($usertypes->{$type} ne '') {
17821:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
17822:                                             }
17823:                                         }
17824:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
17825:                                     }
17826:                                 }
17827:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
17828:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
17829:                             }
17830:                         } else {
17831:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17832:                         }
17833:                     }
17834:                     $resulttext .= '</ul></li>';
17835:                 }
17836:             }
17837:             $resulttext .= '</ul>';
17838:         } else {
17839:             $resulttext = &mt('No changes made to password settings');
17840:         }
17841:         my $cachetime = 24*60*60;
17842:         if ($updatedefaults) {
17843:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17844:             if (ref($lastactref) eq 'HASH') {
17845:                 $lastactref->{'domdefaults'} = 1;
17846:             }
17847:         }
17848:         if ($updateconf) {
17849:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
17850:             if (ref($lastactref) eq 'HASH') {
17851:                 $lastactref->{'passwdconf'} = 1;
17852:             }
17853:         }
17854:     } else {
17855:         $resulttext = '<span class="LC_error">'.
17856:             &mt('An error occurred: [_1]',$putresult).'</span>';
17857:     }
17858:     if ($errors) {
17859:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17860:                        $errors.'</ul></p>';
17861:     }
17862:     return $resulttext;
17863: }
17864: 
17865: sub modify_usercreation {
17866:     my ($dom,%domconfig) = @_;
17867:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
17868:     my $warningmsg;
17869:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17870:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17871:             if ($key eq 'cancreate') {
17872:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17873:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17874:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
17875:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17876:                         } else {
17877:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17878:                         }
17879:                     }
17880:                 }
17881:             } elsif ($key eq 'email_rule') {
17882:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17883:             } else {
17884:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17885:             }
17886:         }
17887:     }
17888:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
17889:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
17890:     my @contexts = ('author','course','requestcrs');
17891:     foreach my $item(@contexts) {
17892:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
17893:     }
17894:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17895:         foreach my $item (@contexts) {
17896:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
17897:                 push(@{$changes{'cancreate'}},$item);
17898:             }
17899:         }
17900:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
17901:         foreach my $item (@contexts) {
17902:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
17903:                 if ($cancreate{$item} ne 'any') {
17904:                     push(@{$changes{'cancreate'}},$item);
17905:                 }
17906:             } else {
17907:                 if ($cancreate{$item} ne 'none') {
17908:                     push(@{$changes{'cancreate'}},$item);
17909:                 }
17910:             }
17911:         }
17912:     } else {
17913:         foreach my $item (@contexts)  {
17914:             push(@{$changes{'cancreate'}},$item);
17915:         }
17916:     }
17917: 
17918:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
17919:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
17920:             if (!grep(/^\Q$type\E$/,@username_rule)) {
17921:                 push(@{$changes{'username_rule'}},$type);
17922:             }
17923:         }
17924:         foreach my $type (@username_rule) {
17925:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
17926:                 push(@{$changes{'username_rule'}},$type);
17927:             }
17928:         }
17929:     } else {
17930:         push(@{$changes{'username_rule'}},@username_rule);
17931:     }
17932: 
17933:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
17934:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
17935:             if (!grep(/^\Q$type\E$/,@id_rule)) {
17936:                 push(@{$changes{'id_rule'}},$type);
17937:             }
17938:         }
17939:         foreach my $type (@id_rule) {
17940:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
17941:                 push(@{$changes{'id_rule'}},$type);
17942:             }
17943:         }
17944:     } else {
17945:         push(@{$changes{'id_rule'}},@id_rule);
17946:     }
17947: 
17948:     my @authen_contexts = ('author','course','domain');
17949:     my @authtypes = ('int','krb4','krb5','loc','lti');
17950:     my %authhash;
17951:     foreach my $item (@authen_contexts) {
17952:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
17953:         foreach my $auth (@authtypes) {
17954:             if (grep(/^\Q$auth\E$/,@authallowed)) {
17955:                 $authhash{$item}{$auth} = 1;
17956:             } else {
17957:                 $authhash{$item}{$auth} = 0;
17958:             }
17959:         }
17960:     }
17961:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
17962:         foreach my $item (@authen_contexts) {
17963:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
17964:                 foreach my $auth (@authtypes) {
17965:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
17966:                         push(@{$changes{'authtypes'}},$item);
17967:                         last;
17968:                     }
17969:                 }
17970:             }
17971:         }
17972:     } else {
17973:         foreach my $item (@authen_contexts) {
17974:             push(@{$changes{'authtypes'}},$item);
17975:         }
17976:     }
17977: 
17978:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
17979:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
17980:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
17981:     $save_usercreate{'id_rule'} = \@id_rule;
17982:     $save_usercreate{'username_rule'} = \@username_rule,
17983:     $save_usercreate{'authtypes'} = \%authhash;
17984: 
17985:     my %usercreation_hash =  (
17986:         usercreation     => \%save_usercreate,
17987:     );
17988: 
17989:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
17990:                                              $dom);
17991: 
17992:     if ($putresult eq 'ok') {
17993:         if (keys(%changes) > 0) {
17994:             $resulttext = &mt('Changes made:').'<ul>';
17995:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17996:                 my %lt = &usercreation_types();
17997:                 foreach my $type (@{$changes{'cancreate'}}) {
17998:                     my $chgtext = $lt{$type}.', ';
17999:                     if ($cancreate{$type} eq 'none') {
18000:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18001:                     } elsif ($cancreate{$type} eq 'any') {
18002:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18003:                     } elsif ($cancreate{$type} eq 'official') {
18004:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18005:                     } elsif ($cancreate{$type} eq 'unofficial') {
18006:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18007:                     }
18008:                     $resulttext .= '<li>'.$chgtext.'</li>';
18009:                 }
18010:             }
18011:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18012:                 my ($rules,$ruleorder) = 
18013:                     &Apache::lonnet::inst_userrules($dom,'username');
18014:                 my $chgtext = '<ul>';
18015:                 foreach my $type (@username_rule) {
18016:                     if (ref($rules->{$type}) eq 'HASH') {
18017:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18018:                     }
18019:                 }
18020:                 $chgtext .= '</ul>';
18021:                 if (@username_rule > 0) {
18022:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18023:                 } else {
18024:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18025:                 }
18026:             }
18027:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18028:                 my ($idrules,$idruleorder) = 
18029:                     &Apache::lonnet::inst_userrules($dom,'id');
18030:                 my $chgtext = '<ul>';
18031:                 foreach my $type (@id_rule) {
18032:                     if (ref($idrules->{$type}) eq 'HASH') {
18033:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18034:                     }
18035:                 }
18036:                 $chgtext .= '</ul>';
18037:                 if (@id_rule > 0) {
18038:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18039:                 } else {
18040:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18041:                 }
18042:             }
18043:             my %authname = &authtype_names();
18044:             my %context_title = &context_names();
18045:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18046:                 my $chgtext = '<ul>';
18047:                 foreach my $type (@{$changes{'authtypes'}}) {
18048:                     my @allowed;
18049:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18050:                     foreach my $auth (@authtypes) {
18051:                         if ($authhash{$type}{$auth}) {
18052:                             push(@allowed,$authname{$auth});
18053:                         }
18054:                     }
18055:                     if (@allowed > 0) {
18056:                         $chgtext .= join(', ',@allowed).'</li>';
18057:                     } else {
18058:                         $chgtext .= &mt('none').'</li>';
18059:                     }
18060:                 }
18061:                 $chgtext .= '</ul>';
18062:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18063:                 $resulttext .= '</li>';
18064:             }
18065:             $resulttext .= '</ul>';
18066:         } else {
18067:             $resulttext = &mt('No changes made to user creation settings');
18068:         }
18069:     } else {
18070:         $resulttext = '<span class="LC_error">'.
18071:             &mt('An error occurred: [_1]',$putresult).'</span>';
18072:     }
18073:     if ($warningmsg ne '') {
18074:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18075:     }
18076:     return $resulttext;
18077: }
18078: 
18079: sub modify_selfcreation {
18080:     my ($dom,$lastactref,%domconfig) = @_;
18081:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18082:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18083:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18084:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18085:     if (ref($typesref) eq 'ARRAY') {
18086:         @types = @{$typesref};
18087:     }
18088:     if (ref($usertypesref) eq 'HASH') {
18089:         %usertypes = %{$usertypesref};
18090:     }
18091:     $usertypes{'default'} = $othertitle;
18092: #
18093: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18094: #
18095:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18096:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18097:             if ($key eq 'cancreate') {
18098:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18099:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18100:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18101:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18102:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18103:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18104:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18105:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18106:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18107:                         } else {
18108:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18109:                         }
18110:                     }
18111:                 }
18112:             } elsif ($key eq 'email_rule') {
18113:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18114:             } else {
18115:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18116:             }
18117:         }
18118:     }
18119: #
18120: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18121: #
18122:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18123:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18124:             if ($key eq 'selfcreate') {
18125:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18126:             } else {
18127:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18128:             }
18129:         }
18130:     }
18131: #
18132: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18133: #
18134:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18135:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18136:             if ($key eq 'inststatusguest') {
18137:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18138:             } else {
18139:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18140:             }
18141:         }
18142:     }
18143: 
18144:     my @contexts = ('selfcreate');
18145:     @{$cancreate{'selfcreate'}} = ();
18146:     %{$cancreate{'emailusername'}} = ();
18147:     if (@types) {
18148:         @{$cancreate{'statustocreate'}} = ();
18149:     }
18150:     %{$cancreate{'selfcreateprocessing'}} = ();
18151:     %{$cancreate{'shibenv'}} = ();
18152:     %{$cancreate{'emailverified'}} = ();
18153:     %{$cancreate{'emailoptions'}} = ();
18154:     %{$cancreate{'emaildomain'}} = ();
18155:     my %selfcreatetypes = (
18156:                              sso   => 'users authenticated by institutional single sign on',
18157:                              login => 'users authenticated by institutional log-in',
18158:                              email => 'users verified by e-mail',
18159:                           );
18160: #
18161: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18162: # is permitted.
18163: #
18164:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18165: 
18166:     my (@statuses,%email_rule);
18167:     foreach my $item ('login','sso','email') {
18168:         if ($item eq 'email') {
18169:             if ($env{'form.cancreate_email'}) {
18170:                 if (@types) {
18171:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18172:                     foreach my $status (@poss_statuses) {
18173:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18174:                             push(@statuses,$status);
18175:                         }
18176:                     }
18177:                     $save_inststatus{'inststatusguest'} = \@statuses;
18178:                 } else {
18179:                     push(@statuses,'default');
18180:                 }
18181:                 if (@statuses) {
18182:                     my %curr_rule;
18183:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
18184:                         foreach my $type (@statuses) {
18185:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
18186:                         }
18187:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
18188:                         foreach my $type (@statuses) {
18189:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
18190:                         }
18191:                     }
18192:                     push(@{$cancreate{'selfcreate'}},'email');
18193:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
18194:                     my %curremaildom;
18195:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
18196:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
18197:                     }
18198:                     foreach my $type (@statuses) {
18199:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
18200:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
18201:                         }
18202:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
18203:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
18204:                         }
18205:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
18206: #
18207: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
18208: #
18209:                             my $chosen = $1;
18210:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
18211:                                 my $emaildom;
18212:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
18213:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
18214:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
18215:                                     if (ref($curremaildom{$type}) eq 'HASH') {
18216:                                         if (exists($curremaildom{$type}{$chosen})) {
18217:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
18218:                                                 push(@{$changes{'cancreate'}},'emaildomain');
18219:                                             }
18220:                                         } elsif ($emaildom ne '') {
18221:                                             push(@{$changes{'cancreate'}},'emaildomain');
18222:                                         }
18223:                                     } elsif ($emaildom ne '') {
18224:                                         push(@{$changes{'cancreate'}},'emaildomain');
18225:                                     } 
18226:                                 }
18227:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18228:                             } elsif ($chosen eq 'custom') {
18229:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
18230:                                 $email_rule{$type} = [];
18231:                                 if (ref($emailrules) eq 'HASH') {
18232:                                     foreach my $rule (@possemail_rules) {
18233:                                         if (exists($emailrules->{$rule})) {
18234:                                             push(@{$email_rule{$type}},$rule);
18235:                                         }
18236:                                     }
18237:                                 }
18238:                                 if (@{$email_rule{$type}}) {
18239:                                     $cancreate{'emailoptions'}{$type} = 'custom';
18240:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
18241:                                         if (@{$curr_rule{$type}} > 0) {
18242:                                             foreach my $rule (@{$curr_rule{$type}}) {
18243:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
18244:                                                     push(@{$changes{'email_rule'}},$type);
18245:                                                 }
18246:                                             }
18247:                                         }
18248:                                         foreach my $type (@{$email_rule{$type}}) {
18249:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
18250:                                                 push(@{$changes{'email_rule'}},$type);
18251:                                             }
18252:                                         }
18253:                                     } else {
18254:                                         push(@{$changes{'email_rule'}},$type);
18255:                                     }
18256:                                 }
18257:                             } else {
18258:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18259:                             }
18260:                         }
18261:                     }
18262:                     if (@types) {
18263:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18264:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
18265:                             if (@changed) {
18266:                                 push(@{$changes{'inststatus'}},'inststatusguest');
18267:                             }
18268:                         } else {
18269:                             push(@{$changes{'inststatus'}},'inststatusguest');
18270:                         }
18271:                     }
18272:                 } else {
18273:                     delete($env{'form.cancreate_email'});
18274:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18275:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18276:                             push(@{$changes{'inststatus'}},'inststatusguest');
18277:                         }
18278:                     }
18279:                 }
18280:             } else {
18281:                 $save_inststatus{'inststatusguest'} = [];
18282:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18283:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18284:                         push(@{$changes{'inststatus'}},'inststatusguest');
18285:                     }
18286:                 }
18287:             }
18288:         } else {
18289:             if ($env{'form.cancreate_'.$item}) {
18290:                 push(@{$cancreate{'selfcreate'}},$item);
18291:             }
18292:         }
18293:     }
18294:     my (%userinfo,%savecaptcha);
18295:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
18296: #
18297: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
18298: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
18299: #
18300: 
18301:     if ($env{'form.cancreate_email'}) {
18302:         push(@contexts,'emailusername');
18303:         if (@statuses) {
18304:             foreach my $type (@statuses) {
18305:                 if (ref($infofields) eq 'ARRAY') {
18306:                     foreach my $field (@{$infofields}) {
18307:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
18308:                             $cancreate{'emailusername'}{$type}{$field} = $1;
18309:                         }
18310:                     }
18311:                 }
18312:             }
18313:         }
18314: #
18315: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
18316: # queued requests for self-creation of account verified by e-mail.
18317: #
18318: 
18319:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
18320:         @approvalnotify = sort(@approvalnotify);
18321:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
18322:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18323:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
18324:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
18325:                     push(@{$changes{'cancreate'}},'notify');
18326:                 }
18327:             } else {
18328:                 if ($cancreate{'notify'}{'approval'}) {
18329:                     push(@{$changes{'cancreate'}},'notify');
18330:                 }
18331:             }
18332:         } elsif ($cancreate{'notify'}{'approval'}) {
18333:             push(@{$changes{'cancreate'}},'notify');
18334:         }
18335: 
18336:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
18337:     }
18338: #  
18339: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
18340: # institutional log-in.
18341: #
18342:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
18343:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
18344:                ($domdefaults{'auth_def'} eq 'localauth'))) {
18345:             $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.').' '.
18346:                           &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.');
18347:         }
18348:     }
18349:     my @fields = ('lastname','firstname','middlename','generation',
18350:                   'permanentemail','id');
18351:     my @shibfields = (@fields,'inststatus');
18352:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18353: #
18354: # Where usernames may created for institutional log-in and/or institutional single sign on:
18355: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
18356: # may self-create accounts 
18357: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
18358: # which the user may supply, if institutional data is unavailable.
18359: #
18360:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
18361:         if (@types) {
18362:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
18363:             push(@contexts,'statustocreate');
18364:             foreach my $type (@types) {
18365:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
18366:                 foreach my $field (@fields) {
18367:                     if (grep(/^\Q$field\E$/,@modifiable)) {
18368:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
18369:                     } else {
18370:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
18371:                     }
18372:                 }
18373:             }
18374:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
18375:                 foreach my $type (@types) {
18376:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
18377:                         foreach my $field (@fields) {
18378:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
18379:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
18380:                                 push(@{$changes{'selfcreate'}},$type);
18381:                                 last;
18382:                             }
18383:                         }
18384:                     }
18385:                 }
18386:             } else {
18387:                 foreach my $type (@types) {
18388:                     push(@{$changes{'selfcreate'}},$type);
18389:                 }
18390:             }
18391:         }
18392:         foreach my $field (@shibfields) {
18393:             if ($env{'form.shibenv_'.$field} ne '') {
18394:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
18395:             }
18396:         }
18397:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18398:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
18399:                 foreach my $field (@shibfields) {
18400:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
18401:                         push(@{$changes{'cancreate'}},'shibenv');
18402:                     }
18403:                 }
18404:             } else {
18405:                 foreach my $field (@shibfields) {
18406:                     if ($env{'form.shibenv_'.$field}) {
18407:                         push(@{$changes{'cancreate'}},'shibenv');
18408:                         last;
18409:                     }
18410:                 }
18411:             }
18412:         }
18413:     }
18414:     foreach my $item (@contexts) {
18415:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
18416:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
18417:                 if (ref($cancreate{$item}) eq 'ARRAY') {
18418:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
18419:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18420:                             push(@{$changes{'cancreate'}},$item);
18421:                         }
18422:                     }
18423:                 }
18424:             }
18425:             if (ref($cancreate{$item}) eq 'ARRAY') {
18426:                 foreach my $type (@{$cancreate{$item}}) {
18427:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
18428:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18429:                             push(@{$changes{'cancreate'}},$item);
18430:                         }
18431:                     }
18432:                 }
18433:             }
18434:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
18435:             if (ref($cancreate{$item}) eq 'HASH') {
18436:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
18437:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18438:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
18439:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18440:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18441:                                     push(@{$changes{'cancreate'}},$item);
18442:                                 }
18443:                             }
18444:                         }
18445:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18446:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18447:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18448:                                 push(@{$changes{'cancreate'}},$item);
18449:                             }
18450:                         }
18451:                     }
18452:                 }
18453:                 foreach my $type (keys(%{$cancreate{$item}})) {
18454:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18455:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18456:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18457:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18458:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18459:                                         push(@{$changes{'cancreate'}},$item);
18460:                                     }
18461:                                 }
18462:                             } else {
18463:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18464:                                     push(@{$changes{'cancreate'}},$item);
18465:                                 }
18466:                             }
18467:                         }
18468:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18469:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18470:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18471:                                 push(@{$changes{'cancreate'}},$item);
18472:                             }
18473:                         }
18474:                     }
18475:                 }
18476:             }
18477:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
18478:             if (ref($cancreate{$item}) eq 'ARRAY') {
18479:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
18480:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18481:                         push(@{$changes{'cancreate'}},$item);
18482:                     }
18483:                 }
18484:             }
18485:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18486:             if (ref($cancreate{$item}) eq 'HASH') {
18487:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18488:                     push(@{$changes{'cancreate'}},$item);
18489:                 }
18490:             }
18491:         } elsif ($item eq 'emailusername') {
18492:             if (ref($cancreate{$item}) eq 'HASH') {
18493:                 foreach my $type (keys(%{$cancreate{$item}})) {
18494:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18495:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18496:                             if ($cancreate{$item}{$type}{$field}) {
18497:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18498:                                     push(@{$changes{'cancreate'}},$item);
18499:                                 }
18500:                                 last;
18501:                             }
18502:                         }
18503:                     }
18504:                 }
18505:             }
18506:         }
18507:     }
18508: #
18509: # Populate %save_usercreate hash with updates to self-creation configuration.
18510: #
18511:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
18512:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
18513:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
18514:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
18515:     if (ref($cancreate{'notify'}) eq 'HASH') {
18516:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
18517:     }
18518:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
18519:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
18520:     }
18521:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
18522:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
18523:     }
18524:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
18525:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
18526:     }
18527:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
18528:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
18529:     }
18530:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18531:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
18532:     }
18533:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
18534:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
18535:     }
18536:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
18537:     $save_usercreate{'email_rule'} = \%email_rule;
18538: 
18539:     my %userconfig_hash = (
18540:             usercreation     => \%save_usercreate,
18541:             usermodification => \%save_usermodify,
18542:             inststatus       => \%save_inststatus,
18543:     );
18544: 
18545:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
18546:                                              $dom);
18547: #
18548: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
18549: #
18550:     if ($putresult eq 'ok') {
18551:         if (keys(%changes) > 0) {
18552:             $resulttext = &mt('Changes made:').'<ul>';
18553:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18554:                 my %lt = &selfcreation_types();
18555:                 foreach my $type (@{$changes{'cancreate'}}) {
18556:                     my $chgtext = '';
18557:                     if ($type eq 'selfcreate') {
18558:                         if (@{$cancreate{$type}} == 0) {
18559:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
18560:                         } else {
18561:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
18562:                                         '<ul>';
18563:                             foreach my $case (@{$cancreate{$type}}) {
18564:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
18565:                             }
18566:                             $chgtext .= '</ul>';
18567:                             if (ref($cancreate{$type}) eq 'ARRAY') {
18568:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
18569:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18570:                                         if (@{$cancreate{'statustocreate'}} == 0) {
18571:                                             $chgtext .= '<span class="LC_warning">'.
18572:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
18573:                                                         '</span><br />';
18574:                                         }
18575:                                     }
18576:                                 }
18577:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
18578:                                     if (!@statuses) {
18579:                                         $chgtext .= '<span class="LC_warning">'.
18580:                                                     &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.").
18581:                                                     '</span><br />';
18582: 
18583:                                     }
18584:                                 }
18585:                             }
18586:                         }
18587:                     } elsif ($type eq 'shibenv') {
18588:                         if (keys(%{$cancreate{$type}}) == 0) {
18589:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
18590:                         } else {
18591:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
18592:                                         '<ul>';
18593:                             foreach my $field (@shibfields) {
18594:                                 next if ($cancreate{$type}{$field} eq '');
18595:                                 if ($field eq 'inststatus') {
18596:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
18597:                                 } else {
18598:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
18599:                                 }
18600:                             }
18601:                             $chgtext .= '</ul>';
18602:                         }
18603:                     } elsif ($type eq 'statustocreate') {
18604:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
18605:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
18606:                             if (@{$cancreate{'selfcreate'}} > 0) {
18607:                                 if (@{$cancreate{'statustocreate'}} == 0) {
18608:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
18609:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
18610:                                         $chgtext .= '<br />'.
18611:                                                     '<span class="LC_warning">'.
18612:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
18613:                                                     '</span>';
18614:                                     }
18615:                                 } elsif (keys(%usertypes) > 0) {
18616:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18617:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
18618:                                     } else {
18619:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
18620:                                     }
18621:                                     $chgtext .= '<ul>';
18622:                                     foreach my $case (@{$cancreate{$type}}) {
18623:                                         if ($case eq 'default') {
18624:                                             $chgtext .= '<li>'.$othertitle.'</li>';
18625:                                         } else {
18626:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
18627:                                         }
18628:                                     }
18629:                                     $chgtext .= '</ul>';
18630:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18631:                                         $chgtext .= '<span class="LC_warning">'.
18632:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
18633:                                                     '</span>';
18634:                                     }
18635:                                 }
18636:                             } else {
18637:                                 if (@{$cancreate{$type}} == 0) {
18638:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
18639:                                 } else {
18640:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
18641:                                 }
18642:                             }
18643:                             $chgtext .= '<br />';
18644:                         }
18645:                     } elsif ($type eq 'selfcreateprocessing') {
18646:                         my %choices = &Apache::lonlocal::texthash (
18647:                                                                     automatic => 'Automatic approval',
18648:                                                                     approval  => 'Queued for approval',
18649:                                                                   );
18650:                         if (@types) {
18651:                             if (@statuses) {
18652:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
18653:                                             '<ul>';
18654:                                 foreach my $status (@statuses) {
18655:                                     if ($status eq 'default') {
18656:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18657:                                     } else {
18658:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18659:                                     }
18660:                                 }
18661:                                 $chgtext .= '</ul>';
18662:                             }
18663:                         } else {
18664:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
18665:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
18666:                         }
18667:                     } elsif ($type eq 'emailverified') {
18668:                         my %options = &Apache::lonlocal::texthash (
18669:                                                                     all   => 'Same as e-mail',
18670:                                                                     first => 'Omit @domain',
18671:                                                                     free  => 'Free to choose',
18672:                                                                   );
18673:                         if (@types) {
18674:                             if (@statuses) {
18675:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
18676:                                             '<ul>';
18677:                                 foreach my $status (@statuses) {
18678:                                     if ($status eq 'default') {
18679:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18680:                                     } else {
18681:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18682:                                     }
18683:                                 }
18684:                                 $chgtext .= '</ul>';
18685:                             }
18686:                         } else {
18687:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
18688:                                             $options{$cancreate{'emailverified'}{'default'}});
18689:                         }
18690:                     } elsif ($type eq 'emailoptions') {
18691:                         my %options = &Apache::lonlocal::texthash (
18692:                                                                     any     => 'Any e-mail',
18693:                                                                     inst    => 'Institutional only',
18694:                                                                     noninst => 'Non-institutional only',
18695:                                                                     custom  => 'Custom restrictions',
18696:                                                                   );
18697:                         if (@types) {
18698:                             if (@statuses) {
18699:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
18700:                                             '<ul>';
18701:                                 foreach my $status (@statuses) {
18702:                                     if ($type eq 'default') {
18703:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18704:                                     } else {
18705:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18706:                                     }
18707:                                 }
18708:                                 $chgtext .= '</ul>';
18709:                             }
18710:                         } else {
18711:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
18712:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
18713:                             } else {
18714:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
18715:                                                 $options{$cancreate{'emailoptions'}{'default'}});
18716:                             }
18717:                         }
18718:                     } elsif ($type eq 'emaildomain') {
18719:                         my $output;
18720:                         if (@statuses) {
18721:                             foreach my $type (@statuses) {
18722:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
18723:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
18724:                                         if ($type eq 'default') {
18725:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18726:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18727:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18728:                                             } else {
18729:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
18730:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18731:                                             }
18732:                                         } else {
18733:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18734:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18735:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18736:                                             } else {
18737:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
18738:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
18739:                                             }
18740:                                         }
18741:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
18742:                                         if ($type eq 'default') {
18743:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18744:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18745:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18746:                                             } else {
18747:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
18748:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18749:                                             }
18750:                                         } else {
18751:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18752:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18753:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18754:                                             } else {
18755:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
18756:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
18757:                                             }
18758:                                         }
18759:                                     }
18760:                                 }
18761:                             }
18762:                         }
18763:                         if ($output ne '') {
18764:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
18765:                                         '<ul>'.$output.'</ul>';
18766:                         }
18767:                     } elsif ($type eq 'captcha') {
18768:                         if ($savecaptcha{$type} eq 'notused') {
18769:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
18770:                         } else {
18771:                             my %captchas = &captcha_phrases();
18772:                             if ($captchas{$savecaptcha{$type}}) {
18773:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
18774:                             } else {
18775:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
18776:                             }
18777:                         }
18778:                     } elsif ($type eq 'recaptchakeys') {
18779:                         my ($privkey,$pubkey);
18780:                         if (ref($savecaptcha{$type}) eq 'HASH') {
18781:                             $pubkey = $savecaptcha{$type}{'public'};
18782:                             $privkey = $savecaptcha{$type}{'private'};
18783:                         }
18784:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
18785:                         if (!$pubkey) {
18786:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
18787:                         } else {
18788:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
18789:                         }
18790:                         if (!$privkey) {
18791:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
18792:                         } else {
18793:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
18794:                         }
18795:                         $chgtext .= '</ul>';
18796:                     } elsif ($type eq 'recaptchaversion') {
18797:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
18798:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
18799:                         }
18800:                     } elsif ($type eq 'emailusername') {
18801:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
18802:                             if (@statuses) {
18803:                                 foreach my $type (@statuses) {
18804:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
18805:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
18806:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
18807:                                                     '<ul>';
18808:                                             foreach my $field (@{$infofields}) {
18809:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
18810:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
18811:                                                 }
18812:                                             }
18813:                                             $chgtext .= '</ul>';
18814:                                         } else {
18815:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18816:                                         }
18817:                                     } else {
18818:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18819:                                     }
18820:                                 }
18821:                             }
18822:                         }
18823:                     } elsif ($type eq 'notify') {
18824:                         my $numapprove = 0;
18825:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
18826:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
18827:                                 if ($cancreate{'notify'}{'approval'}) {
18828:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
18829:                                     $numapprove ++;
18830:                                 }
18831:                             }
18832:                         }
18833:                         unless ($numapprove) {
18834:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
18835:                         }
18836:                     }
18837:                     if ($chgtext) {
18838:                         $resulttext .= '<li>'.$chgtext.'</li>';
18839:                     }
18840:                 }
18841:             }
18842:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
18843:                 my ($emailrules,$emailruleorder) =
18844:                     &Apache::lonnet::inst_userrules($dom,'email');
18845:                 foreach my $type (@{$changes{'email_rule'}}) {
18846:                     if (ref($email_rule{$type}) eq 'ARRAY') {
18847:                         my $chgtext = '<ul>';
18848:                         foreach my $rule (@{$email_rule{$type}}) {
18849:                             if (ref($emailrules->{$rule}) eq 'HASH') {
18850:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
18851:                             }
18852:                         }
18853:                         $chgtext .= '</ul>';
18854:                         my $typename;
18855:                         if (@types) {
18856:                             if ($type eq 'default') {
18857:                                 $typename = $othertitle;
18858:                             } else {
18859:                                 $typename = $usertypes{$type};
18860:                             } 
18861:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
18862:                         }
18863:                         if (@{$email_rule{$type}} > 0) {
18864:                             $resulttext .= '<li>'.
18865:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
18866:                                                $usertypes{$type}).
18867:                                            $chgtext.
18868:                                            '</li>';
18869:                         } else {
18870:                             $resulttext .= '<li>'.
18871:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
18872:                                            '</li>'.
18873:                                            &mt('(Affiliation: [_1])',$typename);
18874:                         }
18875:                     }
18876:                 }
18877:             }
18878:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
18879:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
18880:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
18881:                         my $chgtext = '<ul>';
18882:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
18883:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
18884:                         }
18885:                         $chgtext .= '</ul>';
18886:                         $resulttext .= '<li>'.
18887:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
18888:                                           $chgtext.
18889:                                        '</li>';
18890:                     } else {
18891:                         $resulttext .= '<li>'.
18892:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
18893:                                        '</li>';
18894:                     }
18895:                 }
18896:             }
18897:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
18898:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
18899:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18900:                 foreach my $type (@{$changes{'selfcreate'}}) {
18901:                     my $typename = $type;
18902:                     if (keys(%usertypes) > 0) {
18903:                         if ($usertypes{$type} ne '') {
18904:                             $typename = $usertypes{$type};
18905:                         }
18906:                     }
18907:                     my @modifiable;
18908:                     $resulttext .= '<li>'.
18909:                                     &mt('Self-creation of account by users with status: [_1]',
18910:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
18911:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
18912:                     foreach my $field (@fields) {
18913:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
18914:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
18915:                         }
18916:                     }
18917:                     if (@modifiable > 0) {
18918:                         $resulttext .= join(', ',@modifiable);
18919:                     } else {
18920:                         $resulttext .= &mt('none');
18921:                     }
18922:                     $resulttext .= '</li>';
18923:                 }
18924:                 $resulttext .= '</ul></li>';
18925:             }
18926:             $resulttext .= '</ul>';
18927:             my $cachetime = 24*60*60;
18928:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
18929:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18930:             if (ref($lastactref) eq 'HASH') {
18931:                 $lastactref->{'domdefaults'} = 1;
18932:             }
18933:         } else {
18934:             $resulttext = &mt('No changes made to self-creation settings');
18935:         }
18936:     } else {
18937:         $resulttext = '<span class="LC_error">'.
18938:             &mt('An error occurred: [_1]',$putresult).'</span>';
18939:     }
18940:     if ($warningmsg ne '') {
18941:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18942:     }
18943:     return $resulttext;
18944: }
18945: 
18946: sub process_captcha {
18947:     my ($container,$changes,$newsettings,$currsettings) = @_;
18948:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
18949:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
18950:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
18951:         $newsettings->{'captcha'} = 'original';
18952:     }
18953:     my %current;
18954:     if (ref($currsettings) eq 'HASH') {
18955:         %current = %{$currsettings};
18956:     }
18957:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
18958:         if ($container eq 'cancreate') {
18959:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18960:                 push(@{$changes->{'cancreate'}},'captcha');
18961:             } elsif (!defined($changes->{'cancreate'})) {
18962:                 $changes->{'cancreate'} = ['captcha'];
18963:             }
18964:         } elsif ($container eq 'passwords') {
18965:             $changes->{'reset'} = 1;
18966:         } else {
18967:             $changes->{'captcha'} = 1;
18968:         }
18969:     }
18970:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
18971:     if ($newsettings->{'captcha'} eq 'recaptcha') {
18972:         $newpub = $env{'form.'.$container.'_recaptchapub'};
18973:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
18974:         $newpub =~ s/[^\w\-]//g;
18975:         $newpriv =~ s/[^\w\-]//g;
18976:         $newsettings->{'recaptchakeys'} = {
18977:                                              public  => $newpub,
18978:                                              private => $newpriv,
18979:                                           };
18980:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
18981:         $newversion =~ s/\D//g;
18982:         if ($newversion ne '2') {
18983:             $newversion = 1;
18984:         }
18985:         $newsettings->{'recaptchaversion'} = $newversion;
18986:     }
18987:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
18988:         $currpub = $current{'recaptchakeys'}{'public'};
18989:         $currpriv = $current{'recaptchakeys'}{'private'};
18990:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
18991:             $newsettings->{'recaptchakeys'} = {
18992:                                                  public  => '',
18993:                                                  private => '',
18994:                                               }
18995:         }
18996:     }
18997:     if ($current{'captcha'} eq 'recaptcha') {
18998:         $currversion = $current{'recaptchaversion'};
18999:         if ($currversion ne '2') {
19000:             $currversion = 1;
19001:         }
19002:     }
19003:     if ($currversion ne $newversion) {
19004:         if ($container eq 'cancreate') {
19005:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19006:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19007:             } elsif (!defined($changes->{'cancreate'})) {
19008:                 $changes->{'cancreate'} = ['recaptchaversion'];
19009:             }
19010:         } elsif ($container eq 'passwords') {
19011:             $changes->{'reset'} = 1;
19012:         } else {
19013:             $changes->{'recaptchaversion'} = 1;
19014:         }
19015:     }
19016:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19017:         if ($container eq 'cancreate') {
19018:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19019:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19020:             } elsif (!defined($changes->{'cancreate'})) {
19021:                 $changes->{'cancreate'} = ['recaptchakeys'];
19022:             }
19023:         } elsif ($container eq 'passwords') {
19024:             $changes->{'reset'} = 1;
19025:         } else {
19026:             $changes->{'recaptchakeys'} = 1;
19027:         }
19028:     }
19029:     return;
19030: }
19031: 
19032: sub modify_usermodification {
19033:     my ($dom,%domconfig) = @_;
19034:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19035:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19036:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19037:             if ($key eq 'selfcreate') {
19038:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19039:             } else {  
19040:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19041:             }
19042:         }
19043:     }
19044:     my @contexts = ('author','course');
19045:     my %context_title = (
19046:                            author => 'In author context',
19047:                            course => 'In course context',
19048:                         );
19049:     my @fields = ('lastname','firstname','middlename','generation',
19050:                   'permanentemail','id');
19051:     my %roles = (
19052:                   author => ['ca','aa'],
19053:                   course => ['st','ep','ta','in','cr'],
19054:                 );
19055:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19056:     foreach my $context (@contexts) {
19057:         foreach my $role (@{$roles{$context}}) {
19058:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19059:             foreach my $item (@fields) {
19060:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19061:                     $modifyhash{$context}{$role}{$item} = 1;
19062:                 } else {
19063:                     $modifyhash{$context}{$role}{$item} = 0;
19064:                 }
19065:             }
19066:         }
19067:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19068:             foreach my $role (@{$roles{$context}}) {
19069:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19070:                     foreach my $field (@fields) {
19071:                         if ($modifyhash{$context}{$role}{$field} ne 
19072:                                 $curr_usermodification{$context}{$role}{$field}) {
19073:                             push(@{$changes{$context}},$role);
19074:                             last;
19075:                         }
19076:                     }
19077:                 }
19078:             }
19079:         } else {
19080:             foreach my $context (@contexts) {
19081:                 foreach my $role (@{$roles{$context}}) {
19082:                     push(@{$changes{$context}},$role);
19083:                 }
19084:             }
19085:         }
19086:     }
19087:     my %usermodification_hash =  (
19088:                                    usermodification => \%modifyhash,
19089:                                  );
19090:     my $putresult = &Apache::lonnet::put_dom('configuration',
19091:                                              \%usermodification_hash,$dom);
19092:     if ($putresult eq 'ok') {
19093:         if (keys(%changes) > 0) {
19094:             $resulttext = &mt('Changes made: ').'<ul>';
19095:             foreach my $context (@contexts) {
19096:                 if (ref($changes{$context}) eq 'ARRAY') {
19097:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19098:                     if (ref($changes{$context}) eq 'ARRAY') {
19099:                         foreach my $role (@{$changes{$context}}) {
19100:                             my $rolename;
19101:                             if ($role eq 'cr') {
19102:                                 $rolename = &mt('Custom');
19103:                             } else {
19104:                                 $rolename = &Apache::lonnet::plaintext($role);
19105:                             }
19106:                             my @modifiable;
19107:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19108:                             foreach my $field (@fields) {
19109:                                 if ($modifyhash{$context}{$role}{$field}) {
19110:                                     push(@modifiable,$fieldtitles{$field});
19111:                                 }
19112:                             }
19113:                             if (@modifiable > 0) {
19114:                                 $resulttext .= join(', ',@modifiable);
19115:                             } else {
19116:                                 $resulttext .= &mt('none'); 
19117:                             }
19118:                             $resulttext .= '</li>';
19119:                         }
19120:                         $resulttext .= '</ul></li>';
19121:                     }
19122:                 }
19123:             }
19124:             $resulttext .= '</ul>';
19125:         } else {
19126:             $resulttext = &mt('No changes made to user modification settings');
19127:         }
19128:     } else {
19129:         $resulttext = '<span class="LC_error">'.
19130:             &mt('An error occurred: [_1]',$putresult).'</span>';
19131:     }
19132:     return $resulttext;
19133: }
19134: 
19135: sub modify_defaults {
19136:     my ($dom,$lastactref,%domconfig) = @_;
19137:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19138:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19139:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19140:                  'portal_def');
19141:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19142:     foreach my $item (@items) {
19143:         $newvalues{$item} = $env{'form.'.$item};
19144:         if ($item eq 'auth_def') {
19145:             if ($newvalues{$item} ne '') {
19146:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19147:                     push(@errors,$item);
19148:                 }
19149:             }
19150:         } elsif ($item eq 'lang_def') {
19151:             if ($newvalues{$item} ne '') {
19152:                 if ($newvalues{$item} =~ /^(\w+)/) {
19153:                     my $langcode = $1;
19154:                     if ($langcode ne 'x_chef') {
19155:                         if (code2language($langcode) eq '') {
19156:                             push(@errors,$item);
19157:                         }
19158:                     }
19159:                 } else {
19160:                     push(@errors,$item);
19161:                 }
19162:             }
19163:         } elsif ($item eq 'timezone_def') {
19164:             if ($newvalues{$item} ne '') {
19165:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19166:                     push(@errors,$item);   
19167:                 }
19168:             }
19169:         } elsif ($item eq 'datelocale_def') {
19170:             if ($newvalues{$item} ne '') {
19171:                 my @datelocale_ids = DateTime::Locale->ids();
19172:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19173:                     push(@errors,$item);
19174:                 }
19175:             }
19176:         } elsif ($item eq 'portal_def') {
19177:             if ($newvalues{$item} ne '') {
19178:                 unless ($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])\/?$/) {
19179:                     push(@errors,$item);
19180:                 }
19181:             }
19182:         }
19183:         if (grep(/^\Q$item\E$/,@errors)) {
19184:             $newvalues{$item} = $domdefaults{$item};
19185:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
19186:             $changes{$item} = 1;
19187:         }
19188:         $domdefaults{$item} = $newvalues{$item};
19189:     }
19190:     my %staticdefaults = (
19191:                            'intauth_cost'   => 10,
19192:                            'intauth_check'  => 0,
19193:                            'intauth_switch' => 0,
19194:                          );
19195:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
19196:         if (exists($domdefaults{$item})) {
19197:             $newvalues{$item} = $domdefaults{$item};
19198:         } else {
19199:             $newvalues{$item} = $staticdefaults{$item};
19200:         }
19201:     }
19202:     my %defaults_hash = (
19203:                          defaults => \%newvalues,
19204:                         );
19205:     my $title = &defaults_titles();
19206: 
19207:     my $currinststatus;
19208:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
19209:         $currinststatus = $domconfig{'inststatus'};
19210:     } else {
19211:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19212:         $currinststatus = {
19213:                              inststatustypes => $usertypes,
19214:                              inststatusorder => $types,
19215:                              inststatusguest => [],
19216:                           };
19217:     }
19218:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
19219:     my @allpos;
19220:     my %alltypes;
19221:     my @inststatusguest;
19222:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
19223:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
19224:             unless (grep(/^\Q$type\E$/,@todelete)) {
19225:                 push(@inststatusguest,$type);
19226:             }
19227:         }
19228:     }
19229:     my ($currtitles,$currorder);
19230:     if (ref($currinststatus) eq 'HASH') {
19231:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
19232:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
19233:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
19234:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
19235:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
19236:                     }
19237:                 }
19238:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
19239:                     my $position = $env{'form.inststatus_pos_'.$type};
19240:                     $position =~ s/\D+//g;
19241:                     $allpos[$position] = $type;
19242:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
19243:                     $alltypes{$type} =~ s/`//g;
19244:                 }
19245:             }
19246:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
19247:             $currtitles =~ s/,$//;
19248:         }
19249:     }
19250:     if ($env{'form.addinststatus'}) {
19251:         my $newtype = $env{'form.addinststatus'};
19252:         $newtype =~ s/\W//g;
19253:         unless (exists($alltypes{$newtype})) {
19254:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
19255:             $alltypes{$newtype} =~ s/`//g; 
19256:             my $position = $env{'form.addinststatus_pos'};
19257:             $position =~ s/\D+//g;
19258:             if ($position ne '') {
19259:                 $allpos[$position] = $newtype;
19260:             }
19261:         }
19262:     }
19263:     my @orderedstatus;
19264:     foreach my $type (@allpos) {
19265:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
19266:             push(@orderedstatus,$type);
19267:         }
19268:     }
19269:     foreach my $type (keys(%alltypes)) {
19270:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
19271:             delete($alltypes{$type});
19272:         }
19273:     }
19274:     $defaults_hash{'inststatus'} = {
19275:                                      inststatustypes => \%alltypes,
19276:                                      inststatusorder => \@orderedstatus,
19277:                                      inststatusguest => \@inststatusguest,
19278:                                    };
19279:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
19280:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
19281:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
19282:         }
19283:     }
19284:     if ($currorder ne join(',',@orderedstatus)) {
19285:         $changes{'inststatus'}{'inststatusorder'} = 1;
19286:     }
19287:     my $newtitles;
19288:     foreach my $item (@orderedstatus) {
19289:         $newtitles .= $alltypes{$item}.',';
19290:     }
19291:     $newtitles =~ s/,$//;
19292:     if ($currtitles ne $newtitles) {
19293:         $changes{'inststatus'}{'inststatustypes'} = 1;
19294:     }
19295:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
19296:                                              $dom);
19297:     if ($putresult eq 'ok') {
19298:         if (keys(%changes) > 0) {
19299:             $resulttext = &mt('Changes made:').'<ul>';
19300:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
19301:             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";
19302:             foreach my $item (sort(keys(%changes))) {
19303:                 if ($item eq 'inststatus') {
19304:                     if (ref($changes{'inststatus'}) eq 'HASH') {
19305:                         if (@orderedstatus) {
19306:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
19307:                             foreach my $type (@orderedstatus) { 
19308:                                 $resulttext .= $alltypes{$type}.', ';
19309:                             }
19310:                             $resulttext =~ s/, $//;
19311:                             $resulttext .= '</li>';
19312:                         } else {
19313:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
19314:                         }
19315:                     }
19316:                 } else {
19317:                     my $value = $env{'form.'.$item};
19318:                     if ($value eq '') {
19319:                         $value = &mt('none');
19320:                     } elsif ($item eq 'auth_def') {
19321:                         my %authnames = &authtype_names();
19322:                         my %shortauth = (
19323:                                           internal   => 'int',
19324:                                           krb4       => 'krb4',
19325:                                           krb5       => 'krb5',
19326:                                           localauth  => 'loc',
19327:                                           lti        => 'lti',
19328:                         );
19329:                         $value = $authnames{$shortauth{$value}};
19330:                     }
19331:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
19332:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
19333:                 }
19334:             }
19335:             $resulttext .= '</ul>';
19336:             $mailmsgtext .= "\n";
19337:             my $cachetime = 24*60*60;
19338:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19339:             if (ref($lastactref) eq 'HASH') {
19340:                 $lastactref->{'domdefaults'} = 1;
19341:             }
19342:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
19343:                 my $notify = 1;
19344:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
19345:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
19346:                         $notify = 0;
19347:                     }
19348:                 }
19349:                 if ($notify) {
19350:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
19351:                                                "LON-CAPA Domain Settings Change - $dom",
19352:                                                $mailmsgtext);
19353:                 }
19354:             }
19355:         } else {
19356:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
19357:         }
19358:     } else {
19359:         $resulttext = '<span class="LC_error">'.
19360:             &mt('An error occurred: [_1]',$putresult).'</span>';
19361:     }
19362:     if (@errors > 0) {
19363:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
19364:         foreach my $item (@errors) {
19365:             $resulttext .= ' "'.$title->{$item}.'",';
19366:         }
19367:         $resulttext =~ s/,$//;
19368:     }
19369:     return $resulttext;
19370: }
19371: 
19372: sub modify_scantron {
19373:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19374:     my ($resulttext,%confhash,%changes,$errors);
19375:     my $custom = 'custom.tab';
19376:     my $default = 'default.tab';
19377:     my $servadm = $r->dir_config('lonAdmEMail');
19378:     my ($configuserok,$author_ok,$switchserver) =
19379:         &config_check($dom,$confname,$servadm);
19380:     if ($env{'form.scantronformat.filename'} ne '') {
19381:         my $error;
19382:         if ($configuserok eq 'ok') {
19383:             if ($switchserver) {
19384:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
19385:             } else {
19386:                 if ($author_ok eq 'ok') {
19387:                     my ($result,$scantronurl) =
19388:                         &publishlogo($r,'upload','scantronformat',$dom,
19389:                                      $confname,'scantron','','',$custom);
19390:                     if ($result eq 'ok') {
19391:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
19392:                         $changes{'scantronformat'} = 1;
19393:                     } else {
19394:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
19395:                     }
19396:                 } else {
19397:                     $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);
19398:                 }
19399:             }
19400:         } else {
19401:             $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);
19402:         }
19403:         if ($error) {
19404:             &Apache::lonnet::logthis($error);
19405:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
19406:         }
19407:     }
19408:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19409:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
19410:             if ($env{'form.scantronformat_del'}) {
19411:                 $confhash{'scantron'}{'scantronformat'} = '';
19412:                 $changes{'scantronformat'} = 1;
19413:             } else {
19414:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
19415:             }
19416:         }
19417:     }
19418:     my @options = ('hdr','pad','rem');
19419:     my @fields = &scantroncsv_fields();
19420:     my %titles = &scantronconfig_titles();
19421:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
19422:     my ($newdat,$currdat,%newcol,%currcol);
19423:     if (grep(/^dat$/,@formats)) {
19424:         $confhash{'scantron'}{config}{dat} = 1;
19425:         $newdat = 1;
19426:     } else {
19427:         $newdat = 0;
19428:     }
19429:     if (grep(/^csv$/,@formats)) {
19430:         my %bynum;
19431:         foreach my $field (@fields) {
19432:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
19433:                 my $posscol = $1;
19434:                 if (($posscol < 20) && (!$bynum{$posscol})) {
19435:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
19436:                     $bynum{$posscol} = $field;
19437:                     $newcol{$field} = $posscol;
19438:                 }
19439:             }
19440:         }
19441:         if (keys(%newcol)) {
19442:             foreach my $option (@options) {
19443:                 if ($env{'form.scantroncsv_'.$option}) {
19444:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
19445:                 }
19446:             }
19447:         }
19448:     }
19449:     $currdat = 1;
19450:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19451:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
19452:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
19453:                 $currdat = 0;
19454:             }
19455:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19456:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19457:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
19458:                 }
19459:             }
19460:         }
19461:     }
19462:     if ($currdat != $newdat) {
19463:         $changes{'config'} = 1;
19464:     } else {
19465:         foreach my $field (@fields) {
19466:             if ($currcol{$field} ne '') {
19467:                 if ($currcol{$field} ne $newcol{$field}) {
19468:                     $changes{'config'} = 1;
19469:                     last;
19470:                 }
19471:             } elsif ($newcol{$field} ne '') {
19472:                 $changes{'config'} = 1;
19473:                 last;
19474:             }
19475:         }
19476:     }
19477:     if (keys(%confhash) > 0) {
19478:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
19479:                                                  $dom);
19480:         if ($putresult eq 'ok') {
19481:             if (keys(%changes) > 0) {
19482:                 if (ref($confhash{'scantron'}) eq 'HASH') {
19483:                     $resulttext = &mt('Changes made:').'<ul>';
19484:                     if ($changes{'scantronformat'}) {
19485:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
19486:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
19487:                         } else {
19488:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
19489:                         }
19490:                     }
19491:                     if ($changes{'config'}) {
19492:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
19493:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
19494:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
19495:                             }
19496:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19497:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19498:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
19499:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
19500:                                         foreach my $field (@fields) {
19501:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
19502:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
19503:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
19504:                                             }
19505:                                         }
19506:                                         $resulttext .= '</ul></li>';
19507:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
19508:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
19509:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
19510:                                                 foreach my $option (@options) {
19511:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
19512:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
19513:                                                     }
19514:                                                 }
19515:                                                 $resulttext .= '</ul></li>';
19516:                                             }
19517:                                         }
19518:                                     }
19519:                                 }
19520:                             }
19521:                         } else {
19522:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
19523:                         }
19524:                     }
19525:                     $resulttext .= '</ul>';
19526:                 } else {
19527:                     $resulttext = &mt('Changes made to bubblesheet format file.');
19528:                 }
19529:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
19530:                 if (ref($lastactref) eq 'HASH') {
19531:                     $lastactref->{'domainconfig'} = 1;
19532:                 }
19533:             } else {
19534:                 $resulttext = &mt('No changes made to bubblesheet format settings');
19535:             }
19536:         } else {
19537:             $resulttext = '<span class="LC_error">'.
19538:                 &mt('An error occurred: [_1]',$putresult).'</span>';
19539:         }
19540:     } else {
19541:         $resulttext = &mt('No changes made to bubblesheet format file'); 
19542:     }
19543:     if ($errors) {
19544:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
19545:                        $errors.'</ul></p>';
19546:     }
19547:     return $resulttext;
19548: }
19549: 
19550: sub modify_coursecategories {
19551:     my ($dom,$lastactref,%domconfig) = @_;
19552:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
19553:         $cathash);
19554:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
19555:     my @catitems = ('unauth','auth');
19556:     my @cattypes = ('std','domonly','codesrch','none');
19557:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19558:         $cathash = $domconfig{'coursecategories'}{'cats'};
19559:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
19560:             $changes{'togglecats'} = 1;
19561:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
19562:         }
19563:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
19564:             $changes{'categorize'} = 1;
19565:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
19566:         }
19567:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
19568:             $changes{'togglecatscomm'} = 1;
19569:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
19570:         }
19571:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
19572:             $changes{'categorizecomm'} = 1;
19573:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
19574: 
19575:         }
19576:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
19577:             $changes{'togglecatsplace'} = 1;
19578:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
19579:         }
19580:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
19581:             $changes{'categorizeplace'} = 1;
19582:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
19583:         }
19584:         foreach my $item (@catitems) {
19585:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19586:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
19587:                     $changes{$item} = 1;
19588:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19589:                 }
19590:             }
19591:         }
19592:     } else {
19593:         $changes{'togglecats'} = 1;
19594:         $changes{'categorize'} = 1;
19595:         $changes{'togglecatscomm'} = 1;
19596:         $changes{'categorizecomm'} = 1;
19597:         $changes{'togglecatsplace'} = 1;
19598:         $changes{'categorizeplace'} = 1;
19599:         $domconfig{'coursecategories'} = {
19600:                                              togglecats => $env{'form.togglecats'},
19601:                                              categorize => $env{'form.categorize'},
19602:                                              togglecatscomm => $env{'form.togglecatscomm'},
19603:                                              categorizecomm => $env{'form.categorizecomm'},
19604:                                              togglecatsplace => $env{'form.togglecatsplace'},
19605:                                              categorizeplace => $env{'form.categorizeplace'},
19606:                                          };
19607:         foreach my $item (@catitems) {
19608:             if ($env{'form.coursecat_'.$item} ne 'std') {
19609:                 $changes{$item} = 1;
19610:             }
19611:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19612:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19613:             }
19614:         }
19615:     }
19616:     if (ref($cathash) eq 'HASH') {
19617:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
19618:             push (@deletecategory,'instcode::0');
19619:         }
19620:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
19621:             push(@deletecategory,'communities::0');
19622:         }
19623:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
19624:             push(@deletecategory,'placement::0');
19625:         }
19626:     }
19627:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
19628:     if (ref($cathash) eq 'HASH') {
19629:         if (@deletecategory > 0) {
19630:             #FIXME Need to remove category from all courses using a deleted category 
19631:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
19632:             foreach my $item (@deletecategory) {
19633:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
19634:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
19635:                     $deletions{$item} = 1;
19636:                     &recurse_cat_deletes($item,$cathash,\%deletions);
19637:                 }
19638:             }
19639:         }
19640:         foreach my $item (keys(%{$cathash})) {
19641:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19642:             if ($cathash->{$item} ne $env{'form.'.$item}) {
19643:                 $reorderings{$item} = 1;
19644:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
19645:             }
19646:             if ($env{'form.addcategory_name_'.$item} ne '') {
19647:                 my $newcat = $env{'form.addcategory_name_'.$item};
19648:                 my $newdepth = $depth+1;
19649:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19650:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
19651:                 $adds{$newitem} = 1; 
19652:             }
19653:             if ($env{'form.subcat_'.$item} ne '') {
19654:                 my $newcat = $env{'form.subcat_'.$item};
19655:                 my $newdepth = $depth+1;
19656:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19657:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
19658:                 $adds{$newitem} = 1;
19659:             }
19660:         }
19661:     }
19662:     if ($env{'form.instcode'} eq '1') {
19663:         if (ref($cathash) eq 'HASH') {
19664:             my $newitem = 'instcode::0';
19665:             if ($cathash->{$newitem} eq '') {  
19666:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19667:                 $adds{$newitem} = 1;
19668:             }
19669:         } else {
19670:             my $newitem = 'instcode::0';
19671:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19672:             $adds{$newitem} = 1;
19673:         }
19674:     }
19675:     if ($env{'form.communities'} eq '1') {
19676:         if (ref($cathash) eq 'HASH') {
19677:             my $newitem = 'communities::0';
19678:             if ($cathash->{$newitem} eq '') {
19679:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19680:                 $adds{$newitem} = 1;
19681:             }
19682:         } else {
19683:             my $newitem = 'communities::0';
19684:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19685:             $adds{$newitem} = 1;
19686:         }
19687:     }
19688:     if ($env{'form.placement'} eq '1') {
19689:         if (ref($cathash) eq 'HASH') {
19690:             my $newitem = 'placement::0';
19691:             if ($cathash->{$newitem} eq '') {
19692:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19693:                 $adds{$newitem} = 1;
19694:             }
19695:         } else {
19696:             my $newitem = 'placement::0';
19697:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19698:             $adds{$newitem} = 1;
19699:         }
19700:     }
19701:     if ($env{'form.addcategory_name'} ne '') {
19702:         if (($env{'form.addcategory_name'} ne 'instcode') &&
19703:             ($env{'form.addcategory_name'} ne 'communities') &&
19704:             ($env{'form.addcategory_name'} ne 'placement')) {
19705:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
19706:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
19707:             $adds{$newitem} = 1;
19708:         }
19709:     }
19710:     my $putresult;
19711:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19712:         if (keys(%deletions) > 0) {
19713:             foreach my $key (keys(%deletions)) {
19714:                 if ($predelallitems{$key} ne '') {
19715:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
19716:                 }
19717:             }
19718:         }
19719:         my (@chkcats,@chktrails,%chkallitems);
19720:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
19721:         if (ref($chkcats[0]) eq 'ARRAY') {
19722:             my $depth = 0;
19723:             my $chg = 0;
19724:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
19725:                 my $name = $chkcats[0][$i];
19726:                 my $item;
19727:                 if ($name eq '') {
19728:                     $chg ++;
19729:                 } else {
19730:                     $item = &escape($name).'::0';
19731:                     if ($chg) {
19732:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
19733:                     }
19734:                     $depth ++; 
19735:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
19736:                     $depth --;
19737:                 }
19738:             }
19739:         }
19740:     }
19741:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19742:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
19743:         if ($putresult eq 'ok') {
19744:             my %title = (
19745:                          togglecats     => 'Show/Hide a course in catalog',
19746:                          categorize     => 'Assign a category to a course',
19747:                          togglecatscomm => 'Show/Hide a community in catalog',
19748:                          categorizecomm => 'Assign a category to a community',
19749:                         );
19750:             my %level = (
19751:                          dom  => 'set in Domain ("Modify Course/Community")',
19752:                          crs  => 'set in Course ("Course Configuration")',
19753:                          comm => 'set in Community ("Community Configuration")',
19754:                          none     => 'No catalog',
19755:                          std      => 'Standard catalog',
19756:                          domonly  => 'Domain-only catalog',
19757:                          codesrch => 'Code search form',
19758:                         );
19759:             $resulttext = &mt('Changes made:').'<ul>';
19760:             if ($changes{'togglecats'}) {
19761:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
19762:             }
19763:             if ($changes{'categorize'}) {
19764:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
19765:             }
19766:             if ($changes{'togglecatscomm'}) {
19767:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
19768:             }
19769:             if ($changes{'categorizecomm'}) {
19770:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
19771:             }
19772:             if ($changes{'unauth'}) {
19773:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
19774:             }
19775:             if ($changes{'auth'}) {
19776:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
19777:             }
19778:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19779:                 my $cathash;
19780:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19781:                     $cathash = $domconfig{'coursecategories'}{'cats'};
19782:                 } else {
19783:                     $cathash = {};
19784:                 } 
19785:                 my (@cats,@trails,%allitems);
19786:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
19787:                 if (keys(%deletions) > 0) {
19788:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
19789:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
19790:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
19791:                     }
19792:                     $resulttext .= '</ul></li>';
19793:                 }
19794:                 if (keys(%reorderings) > 0) {
19795:                     my %sort_by_trail;
19796:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
19797:                     foreach my $key (keys(%reorderings)) {
19798:                         if ($allitems{$key} ne '') {
19799:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19800:                         }
19801:                     }
19802:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19803:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19804:                     }
19805:                     $resulttext .= '</ul></li>';
19806:                 }
19807:                 if (keys(%adds) > 0) {
19808:                     my %sort_by_trail;
19809:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
19810:                     foreach my $key (keys(%adds)) {
19811:                         if ($allitems{$key} ne '') {
19812:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19813:                         }
19814:                     }
19815:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19816:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19817:                     }
19818:                     $resulttext .= '</ul></li>';
19819:                 }
19820:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
19821:                 if (ref($lastactref) eq 'HASH') {
19822:                     $lastactref->{'cats'} = 1;
19823:                 }
19824:             }
19825:             $resulttext .= '</ul>';
19826:             if ($changes{'unauth'} || $changes{'auth'}) {
19827:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19828:                 if ($changes{'auth'}) {
19829:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
19830:                 }
19831:                 if ($changes{'unauth'}) {
19832:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
19833:                 }
19834:                 my $cachetime = 24*60*60;
19835:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19836:                 if (ref($lastactref) eq 'HASH') {
19837:                     $lastactref->{'domdefaults'} = 1;
19838:                 }
19839:             }
19840:         } else {
19841:             $resulttext = '<span class="LC_error">'.
19842:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19843:         }
19844:     } else {
19845:         $resulttext = &mt('No changes made to course and community categories');
19846:     }
19847:     return $resulttext;
19848: }
19849: 
19850: sub modify_serverstatuses {
19851:     my ($dom,%domconfig) = @_;
19852:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
19853:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
19854:         %currserverstatus = %{$domconfig{'serverstatuses'}};
19855:     }
19856:     my @pages = &serverstatus_pages();
19857:     foreach my $type (@pages) {
19858:         $newserverstatus{$type}{'namedusers'} = '';
19859:         $newserverstatus{$type}{'machines'} = '';
19860:         if (defined($env{'form.'.$type.'_namedusers'})) {
19861:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
19862:             my @okusers;
19863:             foreach my $user (@users) {
19864:                 my ($uname,$udom) = split(/:/,$user);
19865:                 if (($udom =~ /^$match_domain$/) &&   
19866:                     (&Apache::lonnet::domain($udom)) &&
19867:                     ($uname =~ /^$match_username$/)) {
19868:                     if (!grep(/^\Q$user\E/,@okusers)) {
19869:                         push(@okusers,$user);
19870:                     }
19871:                 }
19872:             }
19873:             if (@okusers > 0) {
19874:                  @okusers = sort(@okusers);
19875:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
19876:             }
19877:         }
19878:         if (defined($env{'form.'.$type.'_machines'})) {
19879:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
19880:             my @okmachines;
19881:             foreach my $ip (@machines) {
19882:                 my @parts = split(/\./,$ip);
19883:                 next if (@parts < 4);
19884:                 my $badip = 0;
19885:                 for (my $i=0; $i<4; $i++) {
19886:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
19887:                         $badip = 1;
19888:                         last;
19889:                     }
19890:                 }
19891:                 if (!$badip) {
19892:                     push(@okmachines,$ip);     
19893:                 }
19894:             }
19895:             @okmachines = sort(@okmachines);
19896:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
19897:         }
19898:     }
19899:     my %serverstatushash =  (
19900:                                 serverstatuses => \%newserverstatus,
19901:                             );
19902:     foreach my $type (@pages) {
19903:         foreach my $setting ('namedusers','machines') {
19904:             my (@current,@new);
19905:             if (ref($currserverstatus{$type}) eq 'HASH') {
19906:                 if ($currserverstatus{$type}{$setting} ne '') { 
19907:                     @current = split(/,/,$currserverstatus{$type}{$setting});
19908:                 }
19909:             }
19910:             if ($newserverstatus{$type}{$setting} ne '') {
19911:                 @new = split(/,/,$newserverstatus{$type}{$setting});
19912:             }
19913:             if (@current > 0) {
19914:                 if (@new > 0) {
19915:                     foreach my $item (@current) {
19916:                         if (!grep(/^\Q$item\E$/,@new)) {
19917:                             $changes{$type}{$setting} = 1;
19918:                             last;
19919:                         }
19920:                     }
19921:                     foreach my $item (@new) {
19922:                         if (!grep(/^\Q$item\E$/,@current)) {
19923:                             $changes{$type}{$setting} = 1;
19924:                             last;
19925:                         }
19926:                     }
19927:                 } else {
19928:                     $changes{$type}{$setting} = 1;
19929:                 }
19930:             } elsif (@new > 0) {
19931:                 $changes{$type}{$setting} = 1;
19932:             }
19933:         }
19934:     }
19935:     if (keys(%changes) > 0) {
19936:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
19937:         my $putresult = &Apache::lonnet::put_dom('configuration',
19938:                                                  \%serverstatushash,$dom);
19939:         if ($putresult eq 'ok') {
19940:             $resulttext .= &mt('Changes made:').'<ul>';
19941:             foreach my $type (@pages) {
19942:                 if (ref($changes{$type}) eq 'HASH') {
19943:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
19944:                     if ($changes{$type}{'namedusers'}) {
19945:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
19946:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
19947:                         } else {
19948:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
19949:                         }
19950:                     }
19951:                     if ($changes{$type}{'machines'}) {
19952:                         if ($newserverstatus{$type}{'machines'} eq '') {
19953:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
19954:                         } else {
19955:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
19956:                         }
19957: 
19958:                     }
19959:                     $resulttext .= '</ul></li>';
19960:                 }
19961:             }
19962:             $resulttext .= '</ul>';
19963:         } else {
19964:             $resulttext = '<span class="LC_error">'.
19965:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
19966: 
19967:         }
19968:     } else {
19969:         $resulttext = &mt('No changes made to access to server status pages');
19970:     }
19971:     return $resulttext;
19972: }
19973: 
19974: sub modify_helpsettings {
19975:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19976:     my ($resulttext,$errors,%changes,%helphash);
19977:     my %defaultchecked = ('submitbugs' => 'on');
19978:     my @offon = ('off','on');
19979:     my @toggles = ('submitbugs');
19980:     my %current = ('submitbugs' => '',
19981:                    'adhoc'      => {},
19982:                   );
19983:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
19984:         %current = %{$domconfig{'helpsettings'}};
19985:     }
19986:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19987:     foreach my $item (@toggles) {
19988:         if ($defaultchecked{$item} eq 'on') { 
19989:             if ($current{$item} eq '') {
19990:                 if ($env{'form.'.$item} eq '0') {
19991:                     $changes{$item} = 1;
19992:                 }
19993:             } elsif ($current{$item} ne $env{'form.'.$item}) {
19994:                 $changes{$item} = 1;
19995:             }
19996:         } elsif ($defaultchecked{$item} eq 'off') {
19997:             if ($current{$item} eq '') {
19998:                 if ($env{'form.'.$item} eq '1') {
19999:                     $changes{$item} = 1;
20000:                 }
20001:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20002:                 $changes{$item} = 1;
20003:             }
20004:         }
20005:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20006:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20007:         }
20008:     }
20009:     my $maxnum = $env{'form.helproles_maxnum'};
20010:     my $confname = $dom.'-domainconfig';
20011:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20012:     my (@allpos,%newsettings,%changedprivs,$newrole);
20013:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20014:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20015:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20016:     my %lt = &Apache::lonlocal::texthash(
20017:                     s      => 'system',
20018:                     d      => 'domain',
20019:                     order  => 'Display order',
20020:                     access => 'Role usage',
20021:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20022:                     dh     => 'All with domain helpdesk role',
20023:                     da     => 'All with domain helpdesk assistant role',
20024:                     none   => 'None',
20025:                     status => 'Determined based on institutional status',
20026:                     inc    => 'Include all, but exclude specific personnel',
20027:                     exc    => 'Exclude all, but include specific personnel',
20028:     );
20029:     for (my $num=0; $num<=$maxnum; $num++) {
20030:         my ($prefix,$identifier,$rolename,%curr);
20031:         if ($num == $maxnum) {
20032:             next unless ($env{'form.newcusthelp'} == $maxnum);
20033:             $identifier = 'custhelp'.$num;
20034:             $prefix = 'helproles_'.$num;
20035:             $rolename = $env{'form.custhelpname'.$num};
20036:             $rolename=~s/[^A-Za-z0-9]//gs;
20037:             next if ($rolename eq '');
20038:             next if (exists($existing{'rolesdef_'.$rolename}));
20039:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20040:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20041:                                                      $newprivs{'c'},$confname,$dom);
20042:             if ($result ne 'ok') {
20043:                 $errors .= '<li><span class="LC_error">'.
20044:                            &mt('An error occurred storing the new custom role: [_1]',
20045:                            $result).'</span></li>';
20046:                 next;
20047:             } else {
20048:                 $changedprivs{$rolename} = \%newprivs;
20049:                 $newrole = $rolename;
20050:             }
20051:         } else {
20052:             $prefix = 'helproles_'.$num;
20053:             $rolename = $env{'form.'.$prefix};
20054:             next if ($rolename eq '');
20055:             next unless (exists($existing{'rolesdef_'.$rolename}));
20056:             $identifier = 'custhelp'.$num;
20057:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20058:             my %currprivs;
20059:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20060:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20061:             foreach my $level ('c','d','s') {
20062:                 if ($newprivs{$level} ne $currprivs{$level}) {
20063:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20064:                                                              $newprivs{'c'},$confname,$dom);
20065:                     if ($result ne 'ok') {
20066:                         $errors .= '<li><span class="LC_error">'.
20067:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
20068:                                        $rolename,$result).'</span></li>';
20069:                     } else {
20070:                         $changedprivs{$rolename} = \%newprivs;
20071:                     }
20072:                     last;
20073:                 }
20074:             }
20075:             if (ref($current{'adhoc'}) eq 'HASH') {
20076:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20077:                     %curr = %{$current{'adhoc'}{$rolename}};
20078:                 }
20079:             }
20080:         }
20081:         my $newpos = $env{'form.'.$prefix.'_pos'};
20082:         $newpos =~ s/\D+//g;
20083:         $allpos[$newpos] = $rolename;
20084:         my $newdesc = $env{'form.'.$prefix.'_desc'};
20085:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
20086:         if ($curr{'desc'}) {
20087:             if ($curr{'desc'} ne $newdesc) {
20088:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
20089:                 $newsettings{$rolename}{'desc'} = $newdesc;
20090:             }
20091:         } elsif ($newdesc ne '') {
20092:             $changes{'customrole'}{$rolename}{'desc'} = 1;
20093:             $newsettings{$rolename}{'desc'} = $newdesc;
20094:         }
20095:         my $access = $env{'form.'.$prefix.'_access'};
20096:         if (grep(/^\Q$access\E$/,@accesstypes)) {
20097:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
20098:             if ($access eq 'status') {
20099:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
20100:                 if (scalar(@statuses) == 0) {
20101:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
20102:                 } else {
20103:                     my (@shownstatus,$numtypes);
20104:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20105:                     if (ref($types) eq 'ARRAY') {
20106:                         $numtypes = scalar(@{$types});
20107:                         foreach my $type (sort(@statuses)) {
20108:                             if ($type eq 'default') {
20109:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20110:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
20111:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20112:                                 push(@shownstatus,$usertypes->{$type});
20113:                             }
20114:                         }
20115:                     }
20116:                     if (grep(/^default$/,@statuses)) {
20117:                         push(@shownstatus,$othertitle);
20118:                     }
20119:                     if (scalar(@shownstatus) == 1+$numtypes) {
20120:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
20121:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
20122:                     } else {
20123:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
20124:                         if (ref($curr{'status'}) eq 'ARRAY') {
20125:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20126:                             if (@diffs) {
20127:                                 $changes{'customrole'}{$rolename}{$access} = 1;
20128:                             }
20129:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20130:                             $changes{'customrole'}{$rolename}{$access} = 1;
20131:                         }
20132:                     }
20133:                 }
20134:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
20135:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
20136:                 my @newspecstaff;
20137:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20138:                 foreach my $person (sort(@personnel)) {
20139:                     if ($domhelpdesk{$person}) {
20140:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
20141:                     }
20142:                 }
20143:                 if (ref($curr{$access}) eq 'ARRAY') {
20144:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20145:                     if (@diffs) {
20146:                         $changes{'customrole'}{$rolename}{$access} = 1;
20147:                     }
20148:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20149:                     $changes{'customrole'}{$rolename}{$access} = 1;
20150:                 }
20151:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20152:                     my ($uname,$udom) = split(/:/,$person);
20153:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
20154:                 }
20155:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
20156:             }
20157:         } else {
20158:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
20159:         }
20160:         unless ($curr{'access'} eq $access) {
20161:             $changes{'customrole'}{$rolename}{'access'} = 1;
20162:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
20163:         }
20164:     }
20165:     if (@allpos > 0) {
20166:         my $idx = 0;
20167:         foreach my $rolename (@allpos) {
20168:             if ($rolename ne '') {
20169:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
20170:                 if (ref($current{'adhoc'}) eq 'HASH') {
20171:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20172:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
20173:                             $changes{'customrole'}{$rolename}{'order'} = 1;
20174:                             $newsettings{$rolename}{'order'} = $idx+1;
20175:                         }
20176:                     }
20177:                 }
20178:                 $idx ++;
20179:             }
20180:         }
20181:     }
20182:     my $putresult;
20183:     if (keys(%changes) > 0) {
20184:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
20185:         if ($putresult eq 'ok') {
20186:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
20187:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
20188:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
20189:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
20190:                 }
20191:             }
20192:             my $cachetime = 24*60*60;
20193:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20194:             if (ref($lastactref) eq 'HASH') {
20195:                 $lastactref->{'domdefaults'} = 1;
20196:             }
20197:         } else {
20198:             $errors .= '<li><span class="LC_error">'.
20199:                        &mt('An error occurred storing the settings: [_1]',
20200:                            $putresult).'</span></li>';
20201:         }
20202:     }
20203:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
20204:         $resulttext = &mt('Changes made:').'<ul>';
20205:         my (%shownprivs,@levelorder);
20206:         @levelorder = ('c','d','s');
20207:         if ((keys(%changes)) && ($putresult eq 'ok')) {
20208:             foreach my $item (sort(keys(%changes))) {
20209:                 if ($item eq 'submitbugs') {
20210:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
20211:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
20212:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
20213:                 } elsif ($item eq 'customrole') {
20214:                     if (ref($changes{'customrole'}) eq 'HASH') {
20215:                         my @keyorder = ('order','desc','access','status','exc','inc');
20216:                         my %keytext = &Apache::lonlocal::texthash(
20217:                                                                    order  => 'Order',
20218:                                                                    desc   => 'Role description',
20219:                                                                    access => 'Role usage',
20220:                                                                    status => 'Allowed institutional types',
20221:                                                                    exc    => 'Allowed personnel',
20222:                                                                    inc    => 'Disallowed personnel',
20223:                         );
20224:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
20225:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
20226:                                 if ($role eq $newrole) {
20227:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
20228:                                                               $role).'<ul>';
20229:                                 } else {
20230:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20231:                                                               $role).'<ul>';
20232:                                 }
20233:                                 foreach my $key (@keyorder) {
20234:                                     if ($changes{'customrole'}{$role}{$key}) {
20235:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
20236:                                                                   $keytext{$key},$newsettings{$role}{$key}).
20237:                                                        '</li>';
20238:                                     }
20239:                                 }
20240:                                 if (ref($changedprivs{$role}) eq 'HASH') {
20241:                                     $shownprivs{$role} = 1;
20242:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
20243:                                     foreach my $level (@levelorder) {
20244:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20245:                                             next if ($item eq '');
20246:                                             my ($priv) = split(/\&/,$item,2);
20247:                                             if (&Apache::lonnet::plaintext($priv)) {
20248:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20249:                                                 unless ($level eq 'c') {
20250:                                                     $resulttext .= ' ('.$lt{$level}.')';
20251:                                                 }
20252:                                                 $resulttext .= '</li>';
20253:                                             }
20254:                                         }
20255:                                     }
20256:                                     $resulttext .= '</ul>';
20257:                                 }
20258:                                 $resulttext .= '</ul></li>';
20259:                             }
20260:                         }
20261:                     }
20262:                 }
20263:             }
20264:         }
20265:         if (keys(%changedprivs)) {
20266:             foreach my $role (sort(keys(%changedprivs))) {
20267:                 unless ($shownprivs{$role}) {
20268:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20269:                                               $role).'<ul>'.
20270:                                    '<li>'.&mt('Privileges set to :').'<ul>';
20271:                     foreach my $level (@levelorder) {
20272:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20273:                             next if ($item eq '');
20274:                             my ($priv) = split(/\&/,$item,2);
20275:                             if (&Apache::lonnet::plaintext($priv)) {
20276:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20277:                                 unless ($level eq 'c') {
20278:                                     $resulttext .= ' ('.$lt{$level}.')';
20279:                                 }
20280:                                 $resulttext .= '</li>';
20281:                             }
20282:                         }
20283:                     }
20284:                     $resulttext .= '</ul></li></ul></li>';
20285:                 }
20286:             }
20287:         }
20288:         $resulttext .= '</ul>';
20289:     } else {
20290:         $resulttext = &mt('No changes made to help settings');
20291:     }
20292:     if ($errors) {
20293:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
20294:                                     $errors.'</ul>';
20295:     }
20296:     return $resulttext;
20297: }
20298: 
20299: sub modify_coursedefaults {
20300:     my ($dom,$lastactref,%domconfig) = @_;
20301:     my ($resulttext,$errors,%changes,%defaultshash);
20302:     my %defaultchecked = (
20303:                            'canuse_pdfforms' => 'off',
20304:                            'uselcmath'       => 'on',
20305:                            'usejsme'         => 'on'
20306:                          );
20307:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
20308:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
20309:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
20310:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
20311:                    'mysqltables_textbook','mysqltables_placement');
20312:     my @types = ('official','unofficial','community','textbook','placement');
20313:     my %staticdefaults = (
20314:                            anonsurvey_threshold => 10,
20315:                            uploadquota          => 500,
20316:                            postsubmit           => 60,
20317:                            mysqltables          => 172800,
20318:                          );
20319:     my %texoptions = (
20320:                         MathJax  => 'MathJax',
20321:                         mimetex  => &mt('Convert to Images'),
20322:                         tth      => &mt('TeX to HTML'),
20323:                      );
20324:     $defaultshash{'coursedefaults'} = {};
20325: 
20326:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
20327:         if ($domconfig{'coursedefaults'} eq '') {
20328:             $domconfig{'coursedefaults'} = {};
20329:         }
20330:     }
20331: 
20332:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
20333:         foreach my $item (@toggles) {
20334:             if ($defaultchecked{$item} eq 'on') {
20335:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20336:                     ($env{'form.'.$item} eq '0')) {
20337:                     $changes{$item} = 1;
20338:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20339:                     $changes{$item} = 1;
20340:                 }
20341:             } elsif ($defaultchecked{$item} eq 'off') {
20342:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20343:                     ($env{'form.'.$item} eq '1')) {
20344:                     $changes{$item} = 1;
20345:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20346:                     $changes{$item} = 1;
20347:                 }
20348:             }
20349:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
20350:         }
20351:         foreach my $item (@numbers) {
20352:             my ($currdef,$newdef);
20353:             $newdef = $env{'form.'.$item};
20354:             if ($item eq 'anonsurvey_threshold') {
20355:                 $currdef = $domconfig{'coursedefaults'}{$item};
20356:                 $newdef =~ s/\D//g;
20357:                 if ($newdef eq '' || $newdef < 1) {
20358:                     $newdef = 1;
20359:                 }
20360:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
20361:             } else {
20362:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
20363:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
20364:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
20365:                 }
20366:                 $newdef =~ s/[^\w.\-]//g;
20367:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
20368:             }
20369:             if ($currdef ne $newdef) {
20370:                 if ($item eq 'anonsurvey_threshold') {
20371:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
20372:                         $changes{$item} = 1;
20373:                     }
20374:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
20375:                     my $setting = $1;
20376:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
20377:                         $changes{$setting} = 1;
20378:                     }
20379:                 }
20380:             }
20381:         }
20382:         my $texengine;
20383:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
20384:             $texengine = $env{'form.texengine'};
20385:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
20386:             if ($currdef eq '') {
20387:                 unless ($texengine eq $Apache::lonnet::deftex) {
20388:                     $changes{'texengine'} = 1;
20389:                 }
20390:             } elsif ($currdef ne $texengine) {
20391:                 $changes{'texengine'} = 1;
20392:             }
20393:         }
20394:         if ($texengine ne '') {
20395:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
20396:         }
20397:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
20398:         my @currclonecode;
20399:         if (ref($currclone) eq 'HASH') {
20400:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
20401:                 @currclonecode = @{$currclone->{'instcode'}};
20402:             }
20403:         }
20404:         my $newclone;
20405:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
20406:             $newclone = $env{'form.canclone'};
20407:         }
20408:         if ($newclone eq 'instcode') {
20409:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
20410:             my (%codedefaults,@code_order,@clonecode);
20411:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
20412:                                                     \@code_order);
20413:             foreach my $item (@code_order) {
20414:                 if (grep(/^\Q$item\E$/,@newcodes)) {
20415:                     push(@clonecode,$item);
20416:                 }
20417:             }
20418:             if (@clonecode) {
20419:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
20420:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
20421:                 if (@diffs) {
20422:                     $changes{'canclone'} = 1;
20423:                 }
20424:             } else {
20425:                 $newclone eq '';
20426:             }
20427:         } elsif ($newclone ne '') {
20428:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
20429:         }
20430:         if ($newclone ne $currclone) {
20431:             $changes{'canclone'} = 1;
20432:         }
20433:         my %credits;
20434:         foreach my $type (@types) {
20435:             unless ($type eq 'community') {
20436:                 $credits{$type} = $env{'form.'.$type.'_credits'};
20437:                 $credits{$type} =~ s/[^\d.]+//g;
20438:             }
20439:         }
20440:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
20441:             ($env{'form.coursecredits'} eq '1')) {
20442:             $changes{'coursecredits'} = 1;
20443:             foreach my $type (keys(%credits)) {
20444:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20445:             }
20446:         } else {
20447:             if ($env{'form.coursecredits'} eq '1') {
20448:                 foreach my $type (@types) {
20449:                     unless ($type eq 'community') {
20450:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
20451:                             $changes{'coursecredits'} = 1;
20452:                         }
20453:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20454:                     }
20455:                 }
20456:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20457:                 foreach my $type (@types) {
20458:                     unless ($type eq 'community') {
20459:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
20460:                             $changes{'coursecredits'} = 1;
20461:                             last;
20462:                         }
20463:                     }
20464:                 }
20465:             }
20466:         }
20467:         if ($env{'form.postsubmit'} eq '1') {
20468:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
20469:             my %currtimeout;
20470:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20471:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
20472:                     $changes{'postsubmit'} = 1;
20473:                 }
20474:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20475:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
20476:                 }
20477:             } else {
20478:                 $changes{'postsubmit'} = 1;
20479:             }
20480:             foreach my $type (@types) {
20481:                 my $timeout = $env{'form.'.$type.'_timeout'};
20482:                 $timeout =~ s/\D//g;
20483:                 if ($timeout == $staticdefaults{'postsubmit'}) {
20484:                     $timeout = '';
20485:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
20486:                     $timeout = '0';
20487:                 }
20488:                 unless ($timeout eq '') {
20489:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
20490:                 }
20491:                 if (exists($currtimeout{$type})) {
20492:                     if ($timeout ne $currtimeout{$type}) {
20493:                         $changes{'postsubmit'} = 1;
20494:                     }
20495:                 } elsif ($timeout ne '') {
20496:                     $changes{'postsubmit'} = 1;
20497:                 }
20498:             }
20499:         } else {
20500:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
20501:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20502:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
20503:                     $changes{'postsubmit'} = 1;
20504:                 }
20505:             } else {
20506:                 $changes{'postsubmit'} = 1;
20507:             }
20508:         }
20509:     }
20510:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20511:                                              $dom);
20512:     if ($putresult eq 'ok') {
20513:         if (keys(%changes) > 0) {
20514:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20515:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
20516:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
20517:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
20518:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
20519:                     if ($changes{$item}) {
20520:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
20521:                     }
20522:                 }
20523:                 if ($changes{'coursecredits'}) {
20524:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20525:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
20526:                             $domdefaults{$type.'credits'} =
20527:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
20528:                         }
20529:                     }
20530:                 }
20531:                 if ($changes{'postsubmit'}) {
20532:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20533:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
20534:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20535:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
20536:                                 $domdefaults{$type.'postsubtimeout'} =
20537:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20538:                             }
20539:                         }
20540:                     }
20541:                 }
20542:                 if ($changes{'uploadquota'}) {
20543:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20544:                         foreach my $type (@types) {
20545:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
20546:                         }
20547:                     }
20548:                 }
20549:                 if ($changes{'canclone'}) {
20550:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20551:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20552:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
20553:                             if (@clonecodes) {
20554:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
20555:                             }
20556:                         }
20557:                     } else {
20558:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
20559:                     }
20560:                 }
20561:                 my $cachetime = 24*60*60;
20562:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20563:                 if (ref($lastactref) eq 'HASH') {
20564:                     $lastactref->{'domdefaults'} = 1;
20565:                 }
20566:             }
20567:             $resulttext = &mt('Changes made:').'<ul>';
20568:             foreach my $item (sort(keys(%changes))) {
20569:                 if ($item eq 'canuse_pdfforms') {
20570:                     if ($env{'form.'.$item} eq '1') {
20571:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
20572:                     } else {
20573:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
20574:                     }
20575:                 } elsif ($item eq 'uselcmath') {
20576:                     if ($env{'form.'.$item} eq '1') {
20577:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
20578:                     } else {
20579:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
20580:                     }
20581:                 } elsif ($item eq 'usejsme') {
20582:                     if ($env{'form.'.$item} eq '1') {
20583:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
20584:                     } else {
20585:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
20586:                     }
20587:                 } elsif ($item eq 'texengine') {
20588:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
20589:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
20590:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
20591:                     }
20592:                 } elsif ($item eq 'anonsurvey_threshold') {
20593:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
20594:                 } elsif ($item eq 'uploadquota') {
20595:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20596:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
20597:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
20598:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
20599:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
20600:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
20601:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
20602:                                        '</ul>'.
20603:                                        '</li>';
20604:                     } else {
20605:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
20606:                     }
20607:                 } elsif ($item eq 'mysqltables') {
20608:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
20609:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
20610:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
20611:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
20612:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
20613:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
20614:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
20615:                                        '</ul>'.
20616:                                        '</li>';
20617:                     } else {
20618:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
20619:                     }
20620:                 } elsif ($item eq 'postsubmit') {
20621:                     if ($domdefaults{'postsubmit'} eq 'off') {
20622:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
20623:                     } else {
20624:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
20625:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20626:                             $resulttext .= &mt('durations:').'<ul>';
20627:                             foreach my $type (@types) {
20628:                                 $resulttext .= '<li>';
20629:                                 my $timeout;
20630:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20631:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20632:                                 }
20633:                                 my $display;
20634:                                 if ($timeout eq '0') {
20635:                                     $display = &mt('unlimited');
20636:                                 } elsif ($timeout eq '') {
20637:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
20638:                                 } else {
20639:                                     $display = &mt('[quant,_1,second]',$timeout);
20640:                                 }
20641:                                 if ($type eq 'community') {
20642:                                     $resulttext .= &mt('Communities');
20643:                                 } elsif ($type eq 'official') {
20644:                                     $resulttext .= &mt('Official courses');
20645:                                 } elsif ($type eq 'unofficial') {
20646:                                     $resulttext .= &mt('Unofficial courses');
20647:                                 } elsif ($type eq 'textbook') {
20648:                                     $resulttext .= &mt('Textbook courses');
20649:                                 } elsif ($type eq 'placement') {
20650:                                     $resulttext .= &mt('Placement tests');
20651:                                 }
20652:                                 $resulttext .= ' -- '.$display.'</li>';
20653:                             }
20654:                             $resulttext .= '</ul>';
20655:                         }
20656:                         $resulttext .= '</li>';
20657:                     }
20658:                 } elsif ($item eq 'coursecredits') {
20659:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20660:                         if (($domdefaults{'officialcredits'} eq '') &&
20661:                             ($domdefaults{'unofficialcredits'} eq '') &&
20662:                             ($domdefaults{'textbookcredits'} eq '')) {
20663:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20664:                         } else {
20665:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
20666:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
20667:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
20668:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
20669:                                            '</ul>'.
20670:                                            '</li>';
20671:                         }
20672:                     } else {
20673:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20674:                     }
20675:                 } elsif ($item eq 'canclone') {
20676:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20677:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20678:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
20679:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
20680:                         }
20681:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
20682:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
20683:                     } else {
20684:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
20685:                     }
20686:                 }
20687:             }
20688:             $resulttext .= '</ul>';
20689:         } else {
20690:             $resulttext = &mt('No changes made to course defaults');
20691:         }
20692:     } else {
20693:         $resulttext = '<span class="LC_error">'.
20694:             &mt('An error occurred: [_1]',$putresult).'</span>';
20695:     }
20696:     return $resulttext;
20697: }
20698: 
20699: sub modify_selfenrollment {
20700:     my ($dom,$lastactref,%domconfig) = @_;
20701:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
20702:     my @types = ('official','unofficial','community','textbook','placement');
20703:     my %titles = &tool_titles();
20704:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
20705:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
20706:     $ordered{'default'} = ['types','registered','approval','limit'];
20707: 
20708:     my (%roles,%shown,%toplevel);
20709:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
20710: 
20711:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
20712:         if ($domconfig{'selfenrollment'} eq '') {
20713:             $domconfig{'selfenrollment'} = {};
20714:         }
20715:     }
20716:     %toplevel = (
20717:                   admin      => 'Configuration Rights',
20718:                   default    => 'Default settings',
20719:                   validation => 'Validation of self-enrollment requests',
20720:                 );
20721:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
20722: 
20723:     if (ref($ordered{'admin'}) eq 'ARRAY') {
20724:         foreach my $item (@{$ordered{'admin'}}) {
20725:             foreach my $type (@types) {
20726:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
20727:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
20728:                 } else {
20729:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
20730:                 }
20731:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
20732:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
20733:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
20734:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
20735:                             push(@{$changes{'admin'}{$type}},$item);
20736:                         }
20737:                     } else {
20738:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
20739:                             push(@{$changes{'admin'}{$type}},$item);
20740:                         }
20741:                     }
20742:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
20743:                     push(@{$changes{'admin'}{$type}},$item);
20744:                 }
20745:             }
20746:         }
20747:     }
20748: 
20749:     foreach my $item (@{$ordered{'default'}}) {
20750:         foreach my $type (@types) {
20751:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
20752:             if ($item eq 'types') {
20753:                 unless (($value eq 'all') || ($value eq 'dom')) {
20754:                     $value = '';
20755:                 }
20756:             } elsif ($item eq 'registered') {
20757:                 unless ($value eq '1') {
20758:                     $value = 0;
20759:                 }
20760:             } elsif ($item eq 'approval') {
20761:                 unless ($value =~ /^[012]$/) {
20762:                     $value = 0;
20763:                 }
20764:             } else {
20765:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20766:                     $value = 'none';
20767:                 }
20768:             }
20769:             $selfenrollhash{'default'}{$type}{$item} = $value;
20770:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
20771:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20772:                     if ($selfenrollhash{'default'}{$type}{$item} ne
20773:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
20774:                          push(@{$changes{'default'}{$type}},$item);
20775:                     }
20776:                 } else {
20777:                     push(@{$changes{'default'}{$type}},$item);
20778:                 }
20779:             } else {
20780:                 push(@{$changes{'default'}{$type}},$item);
20781:             }
20782:             if ($item eq 'limit') {
20783:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20784:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
20785:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
20786:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
20787:                     }
20788:                 } else {
20789:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
20790:                 }
20791:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20792:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
20793:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
20794:                          push(@{$changes{'default'}{$type}},'cap');
20795:                     }
20796:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
20797:                     push(@{$changes{'default'}{$type}},'cap');
20798:                 }
20799:             }
20800:         }
20801:     }
20802: 
20803:     foreach my $item (@{$itemsref}) {
20804:         if ($item eq 'fields') {
20805:             my @changed;
20806:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
20807:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
20808:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
20809:             }
20810:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20811:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
20812:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
20813:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
20814:                 } else {
20815:                     @changed = @{$selfenrollhash{'validation'}{$item}};
20816:                 }
20817:             } else {
20818:                 @changed = @{$selfenrollhash{'validation'}{$item}};
20819:             }
20820:             if (@changed) {
20821:                 if ($selfenrollhash{'validation'}{$item}) { 
20822:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
20823:                 } else {
20824:                     $changes{'validation'}{$item} = &mt('None');
20825:                 }
20826:             }
20827:         } else {
20828:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
20829:             if ($item eq 'markup') {
20830:                if ($env{'form.selfenroll_validation_'.$item}) {
20831:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
20832:                }
20833:             }
20834:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20835:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
20836:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
20837:                 }
20838:             }
20839:         }
20840:     }
20841: 
20842:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
20843:                                              $dom);
20844:     if ($putresult eq 'ok') {
20845:         if (keys(%changes) > 0) {
20846:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20847:             $resulttext = &mt('Changes made:').'<ul>';
20848:             foreach my $key ('admin','default','validation') {
20849:                 if (ref($changes{$key}) eq 'HASH') {
20850:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
20851:                     if ($key eq 'validation') {
20852:                         foreach my $item (@{$itemsref}) {
20853:                             if (exists($changes{$key}{$item})) {
20854:                                 if ($item eq 'markup') {
20855:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20856:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
20857:                                 } else {  
20858:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20859:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
20860:                                 }
20861:                             }
20862:                         }
20863:                     } else {
20864:                         foreach my $type (@types) {
20865:                             if ($type eq 'community') {
20866:                                 $roles{'1'} = &mt('Community personnel');
20867:                             } else {
20868:                                 $roles{'1'} = &mt('Course personnel');
20869:                             }
20870:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
20871:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
20872:                                     if ($key eq 'admin') {
20873:                                         my @mgrdc = ();
20874:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20875:                                             foreach my $item (@{$ordered{'admin'}}) {
20876:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
20877:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
20878:                                                         push(@mgrdc,$item);
20879:                                                     }
20880:                                                 }
20881:                                             }
20882:                                             if (@mgrdc) {
20883:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
20884:                                             } else {
20885:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
20886:                                             }
20887:                                         }
20888:                                     } else {
20889:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20890:                                             foreach my $item (@{$ordered{$key}}) {
20891:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20892:                                                     $domdefaults{$type.'selfenroll'.$item} =
20893:                                                         $selfenrollhash{$key}{$type}{$item};
20894:                                                 }
20895:                                             }
20896:                                         }
20897:                                     }
20898:                                 }
20899:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
20900:                                 foreach my $item (@{$ordered{$key}}) {
20901:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20902:                                         $resulttext .= '<li>';
20903:                                         if ($key eq 'admin') {
20904:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
20905:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
20906:                                         } else {
20907:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
20908:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
20909:                                         }
20910:                                         $resulttext .= '</li>';
20911:                                     }
20912:                                 }
20913:                                 $resulttext .= '</ul></li>';
20914:                             }
20915:                         }
20916:                         $resulttext .= '</ul></li>'; 
20917:                     }
20918:                 }
20919:             }
20920:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
20921:                 my $cachetime = 24*60*60;
20922:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20923:                 if (ref($lastactref) eq 'HASH') {
20924:                     $lastactref->{'domdefaults'} = 1;
20925:                 }
20926:             }
20927:             $resulttext .= '</ul>';
20928:         } else {
20929:             $resulttext = &mt('No changes made to self-enrollment settings');
20930:         }
20931:     } else {
20932:         $resulttext = '<span class="LC_error">'.
20933:             &mt('An error occurred: [_1]',$putresult).'</span>';
20934:     }
20935:     return $resulttext;
20936: }
20937: 
20938: sub modify_wafproxy {
20939:     my ($dom,$action,$lastactref,%domconfig) = @_;
20940:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20941:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
20942:         %wafproxy,%changes,%expirecache,%expiresaml);
20943:     foreach my $server (sort(keys(%servers))) {
20944:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20945:         if ($serverhome eq $server) {
20946:             my $serverdom = &Apache::lonnet::host_domain($server);
20947:             if ($serverdom eq $dom) {
20948:                 $canset{$server} = 1;
20949:             }
20950:         }
20951:     }
20952:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
20953:         %{$values{$dom}} = ();
20954:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
20955:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
20956:         }
20957:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
20958:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
20959:         }
20960:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20961:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
20962:         }
20963:     }
20964:     my $output;
20965:     if (keys(%canset)) {
20966:         %{$wafproxy{'alias'}} = ();
20967:         %{$wafproxy{'saml'}} = ();
20968:         foreach my $key (sort(keys(%canset))) {
20969:             if ($env{'form.wafproxy_'.$dom}) {
20970:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
20971:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
20972:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
20973:                     $changes{'alias'} = 1;
20974:                 }
20975:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
20976:                     $wafproxy{'saml'}{$key} = 1;
20977:                 }
20978:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
20979:                     $changes{'saml'} = 1;
20980:                 }
20981:             } else {
20982:                 $wafproxy{'alias'}{$key} = '';
20983:                 $wafproxy{'saml'}{$key} = '';
20984:                 if ($curralias{$key}) {
20985:                     $changes{'alias'} = 1;
20986:                 }
20987:                 if ($currsaml{$key}) {
20988:                     $changes{'saml'} = 1;
20989:                 }
20990:             }
20991:             if ($wafproxy{'alias'}{$key} eq '') {
20992:                 if ($curralias{$key}) {
20993:                     $expirecache{$key} = 1;
20994:                 }
20995:                 delete($wafproxy{'alias'}{$key});
20996:             }
20997:             if ($wafproxy{'saml'}{$key} eq '') {
20998:                 if ($currsaml{$key}) {
20999:                     $expiresaml{$key} = 1;
21000:                 }
21001:                 delete($wafproxy{'saml'}{$key});
21002:             }
21003:         }
21004:         unless (keys(%{$wafproxy{'alias'}})) {
21005:             delete($wafproxy{'alias'});
21006:         }
21007:         unless (keys(%{$wafproxy{'saml'}})) {
21008:             delete($wafproxy{'saml'});
21009:         }
21010:         # Localization for values in %warn occurs in &mt() calls separately.
21011:         my %warn = (
21012:                      trusted => 'trusted IP range(s)',
21013:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
21014:                      vpnext => 'IP range(s) for backend WAF connections',
21015:                    );
21016:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21017:             my $possible = $env{'form.wafproxy_'.$item};
21018:             $possible =~ s/^\s+|\s+$//g;
21019:             if ($possible ne '') {
21020:                 if ($item eq 'remoteip') {
21021:                     if ($possible =~ /^[mhn]$/) {
21022:                         $wafproxy{$item} = $possible;
21023:                     }
21024:                 } elsif ($item eq 'ipheader') {
21025:                     if ($wafproxy{'remoteip'} eq 'h') {
21026:                         $wafproxy{$item} = $possible;
21027:                     }
21028:                 } elsif ($item eq 'sslopt') {
21029:                     if ($possible =~ /^0|1$/) {
21030:                         $wafproxy{$item} = $possible;
21031:                     }
21032:                 } else {
21033:                     my (@ok,$count);
21034:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
21035:                         unless ($env{'form.wafproxy_vpnaccess'}) {
21036:                             $possible = '';
21037:                         }
21038:                     } elsif ($item eq 'trusted') {
21039:                         unless ($wafproxy{'remoteip'} eq 'h') {
21040:                             $possible = '';
21041:                         }
21042:                     }
21043:                     unless ($possible eq '') {
21044:                         $possible =~ s/[\r\n]+/\s/g;
21045:                         $possible =~ s/\s*-\s*/-/g;
21046:                         $possible =~ s/\s+/,/g;
21047:                         $possible =~ s/,+/,/g;
21048:                     }
21049:                     $count = 0;
21050:                     if ($possible ne '') {
21051:                         foreach my $poss (split(/\,/,$possible)) {
21052:                             $count ++;
21053:                             $poss = &validate_ip_pattern($poss);
21054:                             if ($poss ne '') {
21055:                                 push(@ok,$poss);
21056:                             }
21057:                         }
21058:                         my $diff = $count - scalar(@ok);
21059:                         if ($diff) {
21060:                             push(@warnings,'<li>'.
21061:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
21062:                                      $diff,$warn{$item}).
21063:                                  '</li>');
21064:                         }
21065:                         if (@ok) {
21066:                             my @cidr_list;
21067:                             foreach my $item (@ok) {
21068:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
21069:                             }
21070:                             $wafproxy{$item} = join(',',@cidr_list);
21071:                         }
21072:                     }
21073:                 }
21074:                 if ($wafproxy{$item} ne $currvalue{$item}) {
21075:                     $changes{$item} = 1;
21076:                 }
21077:             } elsif ($currvalue{$item}) {
21078:                 $changes{$item} = 1;
21079:             } 
21080:         }
21081:     } else {
21082:         if (keys(%curralias)) {
21083:             $changes{'alias'} = 1;
21084:         }
21085:         if (keys(%currsaml)) {
21086:             $changes{'saml'} = 1;
21087:         } 
21088:         if (keys(%currvalue)) {
21089:             foreach my $key (keys(%currvalue)) {
21090:                 $changes{$key} = 1;
21091:             }
21092:         }
21093:     }
21094:     if (keys(%changes)) {
21095:         my %defaultshash = (
21096:                               wafproxy => \%wafproxy,
21097:                            ); 
21098:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21099:                                                  $dom);
21100:         if ($putresult eq 'ok') {
21101:             my $cachetime = 24*60*60;
21102:             my (%domdefaults,$updatedomdefs);
21103:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
21104:                 if ($changes{$item}) {
21105:                     unless ($updatedomdefs) {
21106:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
21107:                         $updatedomdefs = 1;
21108:                     }
21109:                     if ($wafproxy{$item}) {
21110:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
21111:                     } elsif (exists($domdefaults{'waf_'.$item})) {
21112:                         delete($domdefaults{'waf_'.$item});
21113:                     } 
21114:                 }
21115:             }
21116:             if ($updatedomdefs) {
21117:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21118:                 if (ref($lastactref) eq 'HASH') {
21119:                     $lastactref->{'domdefaults'} = 1;
21120:                 }
21121:             }
21122:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
21123:                 my %updates = %expirecache;
21124:                 foreach my $key (keys(%expirecache)) {
21125:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
21126:                 }
21127:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
21128:                     my $cachetime = 24*60*60;
21129:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
21130:                         $updates{$key} = 1;
21131:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
21132:                                                       $cachetime);
21133:                     }
21134:                 }
21135:                 if (ref($lastactref) eq 'HASH') {
21136:                     $lastactref->{'proxyalias'} = \%updates;
21137:                 }
21138:             }
21139:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
21140:                 my %samlupdates = %expiresaml;
21141:                 foreach my $key (keys(%expiresaml)) {
21142:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
21143:                 }
21144:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
21145:                     my $cachetime = 24*60*60;
21146:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
21147:                         $samlupdates{$key} = 1;
21148:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
21149:                                                       $cachetime);
21150:                     }
21151:                 }
21152:                 if (ref($lastactref) eq 'HASH') {
21153:                     $lastactref->{'proxysaml'} = \%samlupdates;
21154:                 }
21155:             }
21156:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
21157:             foreach my $item ('alias','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21158:                 if ($changes{$item}) {
21159:                     if ($item eq 'alias') {
21160:                         my $numaliased = 0;
21161:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
21162:                             my $shown;
21163:                             if (keys(%{$wafproxy{'alias'}})) {
21164:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
21165:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
21166:                                                          &Apache::lonnet::hostname($server),
21167:                                                          $wafproxy{'alias'}{$server}).'</li>';
21168:                                     $numaliased ++;
21169:                                 }
21170:                                 if ($numaliased) {
21171:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
21172:                                                           '<ul>'.$shown.'</ul>').'</li>';
21173:                                 }
21174:                             }
21175:                         }
21176:                         unless ($numaliased) {
21177:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
21178:                         }
21179:                     } elsif ($item eq 'saml') {
21180:                         my $shown; 
21181:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
21182:                             if (keys(%{$wafproxy{'saml'}})) {
21183:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
21184:                             }
21185:                         }
21186:                         if ($shown) {
21187:                             $output .= '<li>'.&mt('Alias used by Shibboleth for: [_1]',
21188:                                                   $shown).'</li>';
21189:                         } else {
21190:                             $output .= '<li>'.&mt('No alias used for Shibboleth').'</li>';
21191:                         }
21192:                     } else {
21193:                         if ($item eq 'remoteip') {
21194:                             my %ip_methods = &remoteip_methods();
21195:                             if ($wafproxy{$item} =~ /^[mh]$/) {
21196:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
21197:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
21198:                             } else {
21199:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
21200:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
21201:                                                '</li>';
21202:                                 } else {
21203:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
21204:                                 }
21205:                             }
21206:                         } elsif ($item eq 'ipheader') {
21207:                             if ($wafproxy{$item}) {
21208:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
21209:                                                       $wafproxy{$item}).'</li>';
21210:                             } else {
21211:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
21212:                             }
21213:                         } elsif ($item eq 'trusted') {
21214:                             if ($wafproxy{$item}) {
21215:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
21216:                                                       $wafproxy{$item}).'</li>';
21217:                             } else {
21218:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
21219:                             }
21220:                         } elsif ($item eq 'vpnint') {
21221:                             if ($wafproxy{$item}) {
21222:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
21223:                                                        $wafproxy{$item}).'</li>';
21224:                             } else {
21225:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
21226:                             }
21227:                         } elsif ($item eq 'vpnext') {
21228:                             if ($wafproxy{$item}) {
21229:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
21230:                                                        $wafproxy{$item}).'</li>';
21231:                             } else {
21232:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
21233:                             }
21234:                         } elsif ($item eq 'sslopt') {
21235:                             if ($wafproxy{$item}) {
21236:                                 $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>';
21237:                             } else {
21238:                                 $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>';
21239:                             }
21240:                         }
21241:                     }
21242:                 }
21243:             }
21244:         } else {
21245:             $output = '<span class="LC_error">'.
21246:                       &mt('An error occurred: [_1]',$putresult).'</span>';
21247:         }
21248:     } elsif (keys(%canset)) {
21249:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
21250:     }
21251:     if (@warnings) {
21252:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
21253:                        join("\n",@warnings).'</ul>';
21254:     }
21255:     return $output;
21256: }
21257: 
21258: sub validate_ip_pattern {
21259:     my ($pattern) = @_;
21260:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
21261:         my ($start,$end) = ($1,$2);
21262:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
21263:             if (($start !~ m{/}) && ($end !~ m{/})) {
21264:                 return $start.'-'.$end;
21265:             }
21266:         }
21267:     } elsif ($pattern ne '') {
21268:         $pattern = &Net::CIDR::cidrvalidate($pattern);
21269:         if ($pattern ne '') {
21270:             return $pattern;
21271:         }
21272:     }
21273:     return;
21274: }
21275: 
21276: sub modify_usersessions {
21277:     my ($dom,$lastactref,%domconfig) = @_;
21278:     my @hostingtypes = ('version','excludedomain','includedomain');
21279:     my @offloadtypes = ('primary','default');
21280:     my %types = (
21281:                   remote => \@hostingtypes,
21282:                   hosted => \@hostingtypes,
21283:                   spares => \@offloadtypes,
21284:                 );
21285:     my @prefixes = ('remote','hosted','spares');
21286:     my @lcversions = &Apache::lonnet::all_loncaparevs();
21287:     my (%by_ip,%by_location,@intdoms,@instdoms);
21288:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21289:     my @locations = sort(keys(%by_location));
21290:     my (%defaultshash,%changes);
21291:     foreach my $prefix (@prefixes) {
21292:         $defaultshash{'usersessions'}{$prefix} = {};
21293:     }
21294:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21295:     my $resulttext;
21296:     my %iphost = &Apache::lonnet::get_iphost();
21297:     foreach my $prefix (@prefixes) {
21298:         next if ($prefix eq 'spares');
21299:         foreach my $type (@{$types{$prefix}}) {
21300:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21301:             if ($type eq 'version') {
21302:                 my $value = $env{'form.'.$prefix.'_'.$type};
21303:                 my $okvalue;
21304:                 if ($value ne '') {
21305:                     if (grep(/^\Q$value\E$/,@lcversions)) {
21306:                         $okvalue = $value;
21307:                     }
21308:                 }
21309:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21310:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21311:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
21312:                             if ($inuse == 0) {
21313:                                 $changes{$prefix}{$type} = 1;
21314:                             } else {
21315:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
21316:                                     $changes{$prefix}{$type} = 1;
21317:                                 }
21318:                                 if ($okvalue ne '') {
21319:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21320:                                 } 
21321:                             }
21322:                         } else {
21323:                             if (($inuse == 1) && ($okvalue ne '')) {
21324:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21325:                                 $changes{$prefix}{$type} = 1;
21326:                             }
21327:                         }
21328:                     } else {
21329:                         if (($inuse == 1) && ($okvalue ne '')) {
21330:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21331:                             $changes{$prefix}{$type} = 1;
21332:                         }
21333:                     }
21334:                 } else {
21335:                     if (($inuse == 1) && ($okvalue ne '')) {
21336:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21337:                         $changes{$prefix}{$type} = 1;
21338:                     }
21339:                 }
21340:             } else {
21341:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21342:                 my @okvals;
21343:                 foreach my $val (@vals) {
21344:                     if ($val =~ /:/) {
21345:                         my @items = split(/:/,$val);
21346:                         foreach my $item (@items) {
21347:                             if (ref($by_location{$item}) eq 'ARRAY') {
21348:                                 push(@okvals,$item);
21349:                             }
21350:                         }
21351:                     } else {
21352:                         if (ref($by_location{$val}) eq 'ARRAY') {
21353:                             push(@okvals,$val);
21354:                         }
21355:                     }
21356:                 }
21357:                 @okvals = sort(@okvals);
21358:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21359:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21360:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21361:                             if ($inuse == 0) {
21362:                                 $changes{$prefix}{$type} = 1; 
21363:                             } else {
21364:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21365:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
21366:                                 if (@changed > 0) {
21367:                                     $changes{$prefix}{$type} = 1;
21368:                                 }
21369:                             }
21370:                         } else {
21371:                             if ($inuse == 1) {
21372:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21373:                                 $changes{$prefix}{$type} = 1;
21374:                             }
21375:                         } 
21376:                     } else {
21377:                         if ($inuse == 1) {
21378:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21379:                             $changes{$prefix}{$type} = 1;
21380:                         }
21381:                     }
21382:                 } else {
21383:                     if ($inuse == 1) {
21384:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21385:                         $changes{$prefix}{$type} = 1;
21386:                     }
21387:                 }
21388:             }
21389:         }
21390:     }
21391: 
21392:     my @alldoms = &Apache::lonnet::all_domains();
21393:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21394:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
21395:     my $savespares;
21396: 
21397:     foreach my $lonhost (sort(keys(%servers))) {
21398:         my $serverhomeID =
21399:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
21400:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
21401:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
21402:         my %spareschg;
21403:         foreach my $type (@{$types{'spares'}}) {
21404:             my @okspares;
21405:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
21406:             foreach my $server (@checked) {
21407:                 if (&Apache::lonnet::hostname($server) ne '') {
21408:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
21409:                         unless (grep(/^\Q$server\E$/,@okspares)) {
21410:                             push(@okspares,$server);
21411:                         }
21412:                     }
21413:                 }
21414:             }
21415:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
21416:             my $newspare;
21417:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
21418:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
21419:                     $newspare = $new;
21420:                 }
21421:             }
21422:             my @spares;
21423:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
21424:                 @spares = sort(@okspares,$newspare);
21425:             } else {
21426:                 @spares = sort(@okspares);
21427:             }
21428:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
21429:             if (ref($spareid{$lonhost}) eq 'HASH') {
21430:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
21431:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
21432:                     if (@diffs > 0) {
21433:                         $spareschg{$type} = 1;
21434:                     }
21435:                 }
21436:             }
21437:         }
21438:         if (keys(%spareschg) > 0) {
21439:             $changes{'spares'}{$lonhost} = \%spareschg;
21440:         }
21441:     }
21442:     $defaultshash{'usersessions'}{'offloadnow'} = {};
21443:     $defaultshash{'usersessions'}{'offloadoth'} = {};
21444:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
21445:     my @okoffload;
21446:     if (@offloadnow) {
21447:         foreach my $server (@offloadnow) {
21448:             if (&Apache::lonnet::hostname($server) ne '') {
21449:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
21450:                     push(@okoffload,$server);
21451:                 }
21452:             }
21453:         }
21454:         if (@okoffload) {
21455:             foreach my $lonhost (@okoffload) {
21456:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
21457:             }
21458:         }
21459:     }
21460:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
21461:     my @okoffloadoth;
21462:     if (@offloadoth) {
21463:         foreach my $server (@offloadoth) {
21464:             if (&Apache::lonnet::hostname($server) ne '') {
21465:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
21466:                     push(@okoffloadoth,$server);
21467:                 }
21468:             }
21469:         }
21470:         if (@okoffloadoth) {
21471:             foreach my $lonhost (@okoffloadoth) {
21472:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
21473:             }
21474:         }
21475:     }
21476:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
21477:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
21478:             if (ref($changes{'spares'}) eq 'HASH') {
21479:                 if (keys(%{$changes{'spares'}}) > 0) {
21480:                     $savespares = 1;
21481:                 }
21482:             }
21483:         } else {
21484:             $savespares = 1;
21485:         }
21486:         foreach my $offload ('offloadnow','offloadoth') {
21487:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
21488:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
21489:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
21490:                         $changes{$offload} = 1;
21491:                         last;
21492:                     }
21493:                 }
21494:                 unless ($changes{$offload}) {
21495:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
21496:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
21497:                             $changes{$offload} = 1;
21498:                             last;
21499:                         }
21500:                     }
21501:                 }
21502:             } else {
21503:                 if (($offload eq 'offloadnow') && (@okoffload)) {
21504:                      $changes{'offloadnow'} = 1;
21505:                 }
21506:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
21507:                     $changes{'offloadoth'} = 1;
21508:                 }
21509:             } 
21510:         }
21511:     } else {
21512:         if (@okoffload) {
21513:             $changes{'offloadnow'} = 1;
21514:         }
21515:         if (@okoffloadoth) {
21516:             $changes{'offloadoth'} = 1;
21517:         }
21518:     }
21519:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
21520:     if ((keys(%changes) > 0) || ($savespares)) {
21521:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21522:                                                  $dom);
21523:         if ($putresult eq 'ok') {
21524:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21525:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
21526:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
21527:                 }
21528:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
21529:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
21530:                 }
21531:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21532:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
21533:                 }
21534:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21535:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
21536:                 }
21537:             }
21538:             my $cachetime = 24*60*60;
21539:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21540:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
21541:             if (ref($lastactref) eq 'HASH') {
21542:                 $lastactref->{'domdefaults'} = 1;
21543:                 $lastactref->{'usersessions'} = 1;
21544:             }
21545:             if (keys(%changes) > 0) {
21546:                 my %lt = &usersession_titles();
21547:                 $resulttext = &mt('Changes made:').'<ul>';
21548:                 foreach my $prefix (@prefixes) {
21549:                     if (ref($changes{$prefix}) eq 'HASH') {
21550:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21551:                         if ($prefix eq 'spares') {
21552:                             if (ref($changes{$prefix}) eq 'HASH') {
21553:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
21554:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
21555:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
21556:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
21557:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21558:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
21559:                                         foreach my $type (@{$types{$prefix}}) {
21560:                                             if ($changes{$prefix}{$lonhost}{$type}) {
21561:                                                 my $offloadto = &mt('None');
21562:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
21563:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
21564:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
21565:                                                     }
21566:                                                 }
21567:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
21568:                                             }
21569:                                         }
21570:                                     }
21571:                                     $resulttext .= '</li>';
21572:                                 }
21573:                             }
21574:                         } else {
21575:                             foreach my $type (@{$types{$prefix}}) {
21576:                                 if (defined($changes{$prefix}{$type})) {
21577:                                     my ($newvalue,$notinuse);
21578:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21579:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
21580:                                             if ($type eq 'version') {
21581:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
21582:                                             } else {
21583:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21584:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
21585:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
21586:                                                     }
21587:                                                 } else {
21588:                                                     $notinuse = 1;
21589:                                                 }
21590:                                             }
21591:                                         }
21592:                                     }
21593:                                     if ($newvalue eq '') {
21594:                                         if ($type eq 'version') {
21595:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
21596:                                         } elsif ($notinuse) {
21597:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21598:                                         } else {
21599:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21600:                                         }
21601:                                     } else {
21602:                                         if ($type eq 'version') {
21603:                                             $newvalue .= ' '.&mt('(or later)');
21604:                                         }
21605:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21606:                                     }
21607:                                 }
21608:                             }
21609:                         }
21610:                         $resulttext .= '</ul>';
21611:                     }
21612:                 }
21613:                 if ($changes{'offloadnow'}) {
21614:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21615:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
21616:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
21617:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
21618:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21619:                             }
21620:                             $resulttext .= '</ul>';
21621:                         } else {
21622:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
21623:                         }
21624:                     } else {
21625:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
21626:                     }
21627:                 }
21628:                 if ($changes{'offloadoth'}) {
21629:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21630:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
21631:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
21632:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
21633:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21634:                             }
21635:                             $resulttext .= '</ul>';
21636:                         } else {
21637:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
21638:                         }
21639:                     } else {
21640:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
21641:                     }
21642:                 }
21643:                 $resulttext .= '</ul>';
21644:             } else {
21645:                 $resulttext = $nochgmsg;
21646:             }
21647:         } else {
21648:             $resulttext = '<span class="LC_error">'.
21649:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21650:         }
21651:     } else {
21652:         $resulttext = $nochgmsg;
21653:     }
21654:     return $resulttext;
21655: }
21656: 
21657: sub modify_ssl {
21658:     my ($dom,$lastactref,%domconfig) = @_;
21659:     my (%by_ip,%by_location,@intdoms,@instdoms);
21660:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21661:     my @locations = sort(keys(%by_location));
21662:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21663:     my (%defaultshash,%changes);
21664:     my $action = 'ssl';
21665:     my @prefixes = ('connto','connfrom','replication');
21666:     foreach my $prefix (@prefixes) {
21667:         $defaultshash{$action}{$prefix} = {};
21668:     }
21669:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21670:     my $resulttext;
21671:     my %iphost = &Apache::lonnet::get_iphost();
21672:     my @reptypes = ('certreq','nocertreq');
21673:     my @connecttypes = ('dom','intdom','other');
21674:     my %types = (
21675:                   connto      => \@connecttypes,
21676:                   connfrom    => \@connecttypes,
21677:                   replication => \@reptypes,
21678:                 );
21679:     foreach my $prefix (sort(keys(%types))) {
21680:         foreach my $type (@{$types{$prefix}}) {
21681:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21682:                 my $value = 'yes';
21683:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
21684:                     $value = $env{'form.'.$prefix.'_'.$type};
21685:                 }
21686:                 if (ref($domconfig{$action}) eq 'HASH') {
21687:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21688:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
21689:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
21690:                                 $changes{$prefix}{$type} = 1;
21691:                             }
21692:                             $defaultshash{$action}{$prefix}{$type} = $value;
21693:                         } else {
21694:                             $defaultshash{$action}{$prefix}{$type} = $value;
21695:                             $changes{$prefix}{$type} = 1;
21696:                         }
21697:                     } else {
21698:                         $defaultshash{$action}{$prefix}{$type} = $value;
21699:                         $changes{$prefix}{$type} = 1;
21700:                     }
21701:                 } else {
21702:                     $defaultshash{$action}{$prefix}{$type} = $value;
21703:                     $changes{$prefix}{$type} = 1;
21704:                 }
21705:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
21706:                     delete($changes{$prefix}{$type});
21707:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
21708:                     delete($changes{$prefix}{$type});
21709:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
21710:                     delete($changes{$prefix}{$type});
21711:                 }
21712:             } elsif ($prefix eq 'replication') {
21713:                 if (@locations > 0) {
21714:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21715:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21716:                     my @okvals;
21717:                     foreach my $val (@vals) {
21718:                         if ($val =~ /:/) {
21719:                             my @items = split(/:/,$val);
21720:                             foreach my $item (@items) {
21721:                                 if (ref($by_location{$item}) eq 'ARRAY') {
21722:                                     push(@okvals,$item);
21723:                                 }
21724:                             }
21725:                         } else {
21726:                             if (ref($by_location{$val}) eq 'ARRAY') {
21727:                                 push(@okvals,$val);
21728:                             }
21729:                         }
21730:                     }
21731:                     @okvals = sort(@okvals);
21732:                     if (ref($domconfig{$action}) eq 'HASH') {
21733:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21734:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
21735:                                 if ($inuse == 0) {
21736:                                     $changes{$prefix}{$type} = 1;
21737:                                 } else {
21738:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21739:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
21740:                                     if (@changed > 0) {
21741:                                         $changes{$prefix}{$type} = 1;
21742:                                     }
21743:                                 }
21744:                             } else {
21745:                                 if ($inuse == 1) {
21746:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21747:                                     $changes{$prefix}{$type} = 1;
21748:                                 }
21749:                             }
21750:                         } else {
21751:                             if ($inuse == 1) {
21752:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
21753:                                 $changes{$prefix}{$type} = 1;
21754:                             }
21755:                         }
21756:                     } else {
21757:                         if ($inuse == 1) {
21758:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
21759:                             $changes{$prefix}{$type} = 1;
21760:                         }
21761:                     }
21762:                 }
21763:             }
21764:         }
21765:     }
21766:     if (keys(%changes)) {
21767:         foreach my $prefix (keys(%changes)) {
21768:             if (ref($changes{$prefix}) eq 'HASH') {
21769:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
21770:                     delete($changes{$prefix});
21771:                 }
21772:             } else {
21773:                 delete($changes{$prefix});
21774:             }
21775:         }
21776:     }
21777:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
21778:     if (keys(%changes) > 0) {
21779:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21780:                                                  $dom);
21781:         if ($putresult eq 'ok') {
21782:             if (ref($defaultshash{$action}) eq 'HASH') {
21783:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
21784:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
21785:                 }
21786:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
21787:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
21788:                 }
21789:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
21790:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
21791:                 }
21792:             }
21793:             my $cachetime = 24*60*60;
21794:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21795:             if (ref($lastactref) eq 'HASH') {
21796:                 $lastactref->{'domdefaults'} = 1;
21797:             }
21798:             if (keys(%changes) > 0) {
21799:                 my %titles = &ssl_titles();
21800:                 $resulttext = &mt('Changes made:').'<ul>';
21801:                 foreach my $prefix (@prefixes) {
21802:                     if (ref($changes{$prefix}) eq 'HASH') {
21803:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
21804:                         foreach my $type (@{$types{$prefix}}) {
21805:                             if (defined($changes{$prefix}{$type})) {
21806:                                 my ($newvalue,$notinuse);
21807:                                 if (ref($defaultshash{$action}) eq 'HASH') {
21808:                                     if (ref($defaultshash{$action}{$prefix})) {
21809:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21810:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
21811:                                         } else {
21812:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
21813:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
21814:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
21815:                                                 }
21816:                                             } else {
21817:                                                 $notinuse = 1;
21818:                                             }
21819:                                         }
21820:                                     }
21821:                                     if ($notinuse) {
21822:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
21823:                                     } elsif ($newvalue eq '') {
21824:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
21825:                                     } else {
21826:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
21827:                                     }
21828:                                 }
21829:                             }
21830:                         }
21831:                         $resulttext .= '</ul>';
21832:                     }
21833:                 }
21834:             } else {
21835:                 $resulttext = $nochgmsg;
21836:             }
21837:         } else {
21838:             $resulttext = '<span class="LC_error">'.
21839:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21840:         }
21841:     } else {
21842:         $resulttext = $nochgmsg;
21843:     }
21844:     return $resulttext;
21845: }
21846: 
21847: sub modify_trust {
21848:     my ($dom,$lastactref,%domconfig) = @_;
21849:     my (%by_ip,%by_location,@intdoms,@instdoms);
21850:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21851:     my @locations = sort(keys(%by_location));
21852:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
21853:     my @types = ('exc','inc');
21854:     my (%defaultshash,%changes);
21855:     foreach my $prefix (@prefixes) {
21856:         $defaultshash{'trust'}{$prefix} = {};
21857:     }
21858:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21859:     my $resulttext;
21860:     foreach my $prefix (@prefixes) {
21861:         foreach my $type (@types) {
21862:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21863:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21864:             my @okvals;
21865:             foreach my $val (@vals) {
21866:                 if ($val =~ /:/) {
21867:                     my @items = split(/:/,$val);
21868:                     foreach my $item (@items) {
21869:                         if (ref($by_location{$item}) eq 'ARRAY') {
21870:                             push(@okvals,$item);
21871:                         }
21872:                     }
21873:                 } else {
21874:                     if (ref($by_location{$val}) eq 'ARRAY') {
21875:                         push(@okvals,$val);
21876:                     }
21877:                 }
21878:             }
21879:             @okvals = sort(@okvals);
21880:             if (ref($domconfig{'trust'}) eq 'HASH') {
21881:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
21882:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21883:                         if ($inuse == 0) {
21884:                             $changes{$prefix}{$type} = 1;
21885:                         } else {
21886:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21887:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
21888:                             if (@changed > 0) {
21889:                                 $changes{$prefix}{$type} = 1;
21890:                             }
21891:                         }
21892:                     } else {
21893:                         if ($inuse == 1) {
21894:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21895:                             $changes{$prefix}{$type} = 1;
21896:                         }
21897:                     }
21898:                 } else {
21899:                     if ($inuse == 1) {
21900:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21901:                         $changes{$prefix}{$type} = 1;
21902:                     }
21903:                 }
21904:             } else {
21905:                 if ($inuse == 1) {
21906:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21907:                     $changes{$prefix}{$type} = 1;
21908:                 }
21909:             }
21910:         }
21911:     }
21912:     my $nochgmsg = &mt('No changes made to trust settings.');
21913:     if (keys(%changes) > 0) {
21914:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21915:                                                  $dom);
21916:         if ($putresult eq 'ok') {
21917:             if (ref($defaultshash{'trust'}) eq 'HASH') {
21918:                 foreach my $prefix (@prefixes) {
21919:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
21920:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
21921:                     }
21922:                 }
21923:             }
21924:             my $cachetime = 24*60*60;
21925:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21926:             if (ref($lastactref) eq 'HASH') {
21927:                 $lastactref->{'domdefaults'} = 1;
21928:             }
21929:             if (keys(%changes) > 0) {
21930:                 my %lt = &trust_titles();
21931:                 $resulttext = &mt('Changes made:').'<ul>';
21932:                 foreach my $prefix (@prefixes) {
21933:                     if (ref($changes{$prefix}) eq 'HASH') {
21934:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21935:                         foreach my $type (@types) {
21936:                             if (defined($changes{$prefix}{$type})) {
21937:                                 my ($newvalue,$notinuse);
21938:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
21939:                                     if (ref($defaultshash{'trust'}{$prefix})) {
21940:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21941:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
21942:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
21943:                                             }
21944:                                         } else {
21945:                                             $notinuse = 1;
21946:                                         }
21947:                                     }
21948:                                 }
21949:                                 if ($notinuse) {
21950:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21951:                                 } elsif ($newvalue eq '') {
21952:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21953:                                 } else {
21954:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21955:                                 }
21956:                             }
21957:                         }
21958:                         $resulttext .= '</ul>';
21959:                     }
21960:                 }
21961:                 $resulttext .= '</ul>';
21962:             } else {
21963:                 $resulttext = $nochgmsg;
21964:             }
21965:         } else {
21966:             $resulttext = '<span class="LC_error">'.
21967:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21968:         }
21969:     } else {
21970:         $resulttext = $nochgmsg;
21971:     }
21972:     return $resulttext;
21973: }
21974: 
21975: sub modify_loadbalancing {
21976:     my ($dom,%domconfig) = @_;
21977:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
21978:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
21979:     my ($othertitle,$usertypes,$types) =
21980:         &Apache::loncommon::sorted_inst_types($dom);
21981:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21982:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
21983:     my @sparestypes = ('primary','default');
21984:     my %typetitles = &sparestype_titles();
21985:     my $resulttext;
21986:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
21987:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
21988:         %existing = %{$domconfig{'loadbalancing'}};
21989:     }
21990:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
21991:                               \%currtargets,\%currrules,\%currcookies);
21992:     my ($saveloadbalancing,%defaultshash,%changes);
21993:     my ($alltypes,$othertypes,$titles) =
21994:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
21995:     my %ruletitles = &offloadtype_text();
21996:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
21997:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
21998:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
21999:         if ($balancer eq '') {
22000:             next;
22001:         }
22002:         if (!exists($servers{$balancer})) {
22003:             if (exists($currbalancer{$balancer})) {
22004:                 push(@{$changes{'delete'}},$balancer);
22005:             }
22006:             next;
22007:         }
22008:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
22009:             push(@{$changes{'delete'}},$balancer);
22010:             next;
22011:         }
22012:         if (!exists($currbalancer{$balancer})) {
22013:             push(@{$changes{'add'}},$balancer);
22014:         }
22015:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
22016:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
22017:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
22018:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22019:             $saveloadbalancing = 1;
22020:         }
22021:         foreach my $sparetype (@sparestypes) {
22022:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
22023:             my @offloadto;
22024:             foreach my $target (@targets) {
22025:                 if (($servers{$target}) && ($target ne $balancer)) {
22026:                     if ($sparetype eq 'default') {
22027:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
22028:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
22029:                         }
22030:                     }
22031:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
22032:                         push(@offloadto,$target);
22033:                     }
22034:                 }
22035:             }
22036:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
22037:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
22038:                     push(@offloadto,$balancer);
22039:                 }
22040:             }
22041:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
22042:         }
22043:         if ($env{'form.loadbalancing_cookie_'.$i}) {
22044:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
22045:             if (exists($currbalancer{$balancer})) { 
22046:                 unless ($currcookies{$balancer}) {
22047:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
22048:                 }
22049:             }
22050:         } elsif (exists($currbalancer{$balancer})) {
22051:             if ($currcookies{$balancer}) {
22052:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
22053:             }
22054:         }
22055:         if (ref($currtargets{$balancer}) eq 'HASH') {
22056:             foreach my $sparetype (@sparestypes) {
22057:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
22058:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
22059:                     if (@targetdiffs > 0) {
22060:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22061:                     }
22062:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22063:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22064:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22065:                     }
22066:                 }
22067:             }
22068:         } else {
22069:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
22070:                 foreach my $sparetype (@sparestypes) {
22071:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22072:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22073:                             $changes{'curr'}{$balancer}{'targets'} = 1;
22074:                         }
22075:                     }
22076:                 }
22077:             }
22078:         }
22079:         my $ishomedom;
22080:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
22081:             $ishomedom = 1;
22082:         }
22083:         if (ref($alltypes) eq 'ARRAY') {
22084:             foreach my $type (@{$alltypes}) {
22085:                 my $rule;
22086:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
22087:                          (!$ishomedom)) {
22088:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
22089:                 }
22090:                 if ($rule eq 'specific') {
22091:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
22092:                     if (exists($servers{$specifiedhost})) {
22093:                         $rule = $specifiedhost;
22094:                     }
22095:                 }
22096:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
22097:                 if (ref($currrules{$balancer}) eq 'HASH') {
22098:                     if ($rule ne $currrules{$balancer}{$type}) {
22099:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22100:                     }
22101:                 } elsif ($rule ne '') {
22102:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22103:                 }
22104:             }
22105:         }
22106:     }
22107:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
22108:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
22109:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
22110:             $defaultshash{'loadbalancing'} = {};
22111:         }
22112:         my $putresult = &Apache::lonnet::put_dom('configuration',
22113:                                                  \%defaultshash,$dom);
22114:         if ($putresult eq 'ok') {
22115:             if (keys(%changes) > 0) {
22116:                 my %toupdate;
22117:                 if (ref($changes{'delete'}) eq 'ARRAY') {
22118:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
22119:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
22120:                         $toupdate{$balancer} = 1;
22121:                     }
22122:                 }
22123:                 if (ref($changes{'add'}) eq 'ARRAY') {
22124:                     foreach my $balancer (sort(@{$changes{'add'}})) {
22125:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
22126:                         $toupdate{$balancer} = 1;
22127:                     }
22128:                 }
22129:                 if (ref($changes{'curr'}) eq 'HASH') {
22130:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
22131:                         $toupdate{$balancer} = 1;
22132:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
22133:                             if ($changes{'curr'}{$balancer}{'targets'}) {
22134:                                 my %offloadstr;
22135:                                 foreach my $sparetype (@sparestypes) {
22136:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22137:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22138:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22139:                                         }
22140:                                     }
22141:                                 }
22142:                                 if (keys(%offloadstr) == 0) {
22143:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
22144:                                 } else {
22145:                                     my $showoffload;
22146:                                     foreach my $sparetype (@sparestypes) {
22147:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
22148:                                         if (defined($offloadstr{$sparetype})) {
22149:                                             $showoffload .= $offloadstr{$sparetype};
22150:                                         } else {
22151:                                             $showoffload .= &mt('None');
22152:                                         }
22153:                                         $showoffload .= ('&nbsp;'x3);
22154:                                     }
22155:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
22156:                                 }
22157:                             }
22158:                         }
22159:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
22160:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
22161:                                 foreach my $type (@{$alltypes}) {
22162:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
22163:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22164:                                         my $balancetext;
22165:                                         if ($rule eq '') {
22166:                                             $balancetext =  $ruletitles{'default'};
22167:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
22168:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22169:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22170:                                                 foreach my $sparetype (@sparestypes) {
22171:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22172:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22173:                                                     }
22174:                                                 }
22175:                                                 foreach my $item (@{$alltypes}) {
22176:                                                     next if ($item =~  /^_LC_ipchange/);
22177:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
22178:                                                     if ($hasrule eq 'homeserver') {
22179:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
22180:                                                     } else {
22181:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
22182:                                                             if ($servers{$hasrule}) {
22183:                                                                 $toupdate{$hasrule} = 1;
22184:                                                             }
22185:                                                         }
22186:                                                     }
22187:                                                 }
22188:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
22189:                                                     $balancetext =  $ruletitles{$rule};
22190:                                                 } else {
22191:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22192:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
22193:                                                     if ($receiver) {
22194:                                                         $toupdate{$receiver};
22195:                                                     }
22196:                                                 }
22197:                                             } else {
22198:                                                 $balancetext =  $ruletitles{$rule};
22199:                                             }
22200:                                         } else {
22201:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
22202:                                         }
22203:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
22204:                                     }
22205:                                 }
22206:                             }
22207:                         }
22208:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
22209:                             if ($currcookies{$balancer}) {
22210:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
22211:                                                           $balancer).'</li>';
22212:                             } else {
22213:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
22214:                                                           $balancer).'</li>';
22215:                             }
22216:                         }
22217:                     }
22218:                 }
22219:                 if (keys(%toupdate)) {
22220:                     my %thismachine;
22221:                     my $updatedhere;
22222:                     my $cachetime = 60*60*24;
22223:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22224:                     foreach my $lonhost (keys(%toupdate)) {
22225:                         if ($thismachine{$lonhost}) {
22226:                             unless ($updatedhere) {
22227:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
22228:                                                               $defaultshash{'loadbalancing'},
22229:                                                               $cachetime);
22230:                                 $updatedhere = 1;
22231:                             }
22232:                         } else {
22233:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
22234:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22235:                         }
22236:                     }
22237:                 }
22238:                 if ($resulttext ne '') {
22239:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
22240:                 } else {
22241:                     $resulttext = $nochgmsg;
22242:                 }
22243:             } else {
22244:                 $resulttext = $nochgmsg;
22245:             }
22246:         } else {
22247:             $resulttext = '<span class="LC_error">'.
22248:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22249:         }
22250:     } else {
22251:         $resulttext = $nochgmsg;
22252:     }
22253:     return $resulttext;
22254: }
22255: 
22256: sub recurse_check {
22257:     my ($chkcats,$categories,$depth,$name) = @_;
22258:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
22259:         my $chg = 0;
22260:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
22261:             my $category = $chkcats->[$depth]{$name}[$j];
22262:             my $item;
22263:             if ($category eq '') {
22264:                 $chg ++;
22265:             } else {
22266:                 my $deeper = $depth + 1;
22267:                 $item = &escape($category).':'.&escape($name).':'.$depth;
22268:                 if ($chg) {
22269:                     $categories->{$item} -= $chg;
22270:                 }
22271:                 &recurse_check($chkcats,$categories,$deeper,$category);
22272:                 $deeper --;
22273:             }
22274:         }
22275:     }
22276:     return;
22277: }
22278: 
22279: sub recurse_cat_deletes {
22280:     my ($item,$coursecategories,$deletions) = @_;
22281:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
22282:     my $subdepth = $depth + 1;
22283:     if (ref($coursecategories) eq 'HASH') {
22284:         foreach my $subitem (keys(%{$coursecategories})) {
22285:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
22286:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
22287:                 delete($coursecategories->{$subitem});
22288:                 $deletions->{$subitem} = 1;
22289:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
22290:             }
22291:         }
22292:     }
22293:     return;
22294: }
22295: 
22296: sub active_dc_picker {
22297:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
22298:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
22299:     my @domcoord = keys(%domcoords);
22300:     if (keys(%currhash)) {
22301:         foreach my $dc (keys(%currhash)) {
22302:             unless (exists($domcoords{$dc})) {
22303:                 push(@domcoord,$dc);
22304:             }
22305:         }
22306:     }
22307:     @domcoord = sort(@domcoord);
22308:     my $numdcs = scalar(@domcoord);
22309:     my $rows = 0;
22310:     my $table;
22311:     if ($numdcs > 1) {
22312:         $table = '<table>';
22313:         for (my $i=0; $i<@domcoord; $i++) {
22314:             my $rem = $i%($numinrow);
22315:             if ($rem == 0) {
22316:                 if ($i > 0) {
22317:                     $table .= '</tr>';
22318:                 }
22319:                 $table .= '<tr>';
22320:                 $rows ++;
22321:             }
22322:             my $check = '';
22323:             if ($inputtype eq 'radio') {
22324:                 if (keys(%currhash) == 0) {
22325:                     if (!$i) {
22326:                         $check = ' checked="checked"';
22327:                     }
22328:                 } elsif (exists($currhash{$domcoord[$i]})) {
22329:                     $check = ' checked="checked"';
22330:                 }
22331:             } else {
22332:                 if (exists($currhash{$domcoord[$i]})) {
22333:                     $check = ' checked="checked"';
22334:                 }
22335:             }
22336:             if ($i == @domcoord - 1) {
22337:                 my $colsleft = $numinrow - $rem;
22338:                 if ($colsleft > 1) {
22339:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
22340:                 } else {
22341:                     $table .= '<td class="LC_left_item">';
22342:                 }
22343:             } else {
22344:                 $table .= '<td class="LC_left_item">';
22345:             }
22346:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
22347:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22348:             $table .= '<span class="LC_nobreak"><label>'.
22349:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
22350:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
22351:             if ($user ne $dcname.':'.$dcdom) {
22352:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22353:             }
22354:             $table .= '</label></span></td>';
22355:         }
22356:         $table .= '</tr></table>';
22357:     } elsif ($numdcs == 1) {
22358:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
22359:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22360:         if ($inputtype eq 'radio') {
22361:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
22362:             if ($user ne $dcname.':'.$dcdom) {
22363:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22364:             }
22365:         } else {
22366:             my $check;
22367:             if (exists($currhash{$domcoord[0]})) {
22368:                 $check = ' checked="checked"';
22369:             }
22370:             $table = '<span class="LC_nobreak"><label>'.
22371:                      '<input type="checkbox" name="'.$name.'" '.
22372:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
22373:             if ($user ne $dcname.':'.$dcdom) {
22374:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22375:             }
22376:             $table .= '</label></span>';
22377:             $rows ++;
22378:         }
22379:     }
22380:     return ($numdcs,$table,$rows);
22381: }
22382: 
22383: sub usersession_titles {
22384:     return &Apache::lonlocal::texthash(
22385:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
22386:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
22387:                spares => 'Servers offloaded to, when busy',
22388:                version => 'LON-CAPA version requirement',
22389:                excludedomain => 'Allow all, but exclude specific domains',
22390:                includedomain => 'Deny all, but include specific domains',
22391:                primary => 'Primary (checked first)',
22392:                default => 'Default',
22393:            );
22394: }
22395: 
22396: sub id_for_thisdom {
22397:     my (%servers) = @_;
22398:     my %altids;
22399:     foreach my $server (keys(%servers)) {
22400:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22401:         if ($serverhome ne $server) {
22402:             $altids{$serverhome} = $server;
22403:         }
22404:     }
22405:     return %altids;
22406: }
22407: 
22408: sub count_servers {
22409:     my ($currbalancer,%servers) = @_;
22410:     my (@spares,$numspares);
22411:     foreach my $lonhost (sort(keys(%servers))) {
22412:         next if ($currbalancer eq $lonhost);
22413:         push(@spares,$lonhost);
22414:     }
22415:     if ($currbalancer) {
22416:         $numspares = scalar(@spares);
22417:     } else {
22418:         $numspares = scalar(@spares) - 1;
22419:     }
22420:     return ($numspares,@spares);
22421: }
22422: 
22423: sub lonbalance_targets_js {
22424:     my ($dom,$types,$servers,$settings) = @_;
22425:     my $select = &mt('Select');
22426:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
22427:     if (ref($servers) eq 'HASH') {
22428:         $alltargets = join("','",sort(keys(%{$servers})));
22429:         my @homedoms;
22430:         foreach my $server (sort(keys(%{$servers}))) {
22431:             if (&Apache::lonnet::host_domain($server) eq $dom) {
22432:                 push(@homedoms,'1');
22433:             } else {
22434:                 push(@homedoms,'0');
22435:             }
22436:         }
22437:         $allishome = join("','",@homedoms);
22438:     }
22439:     if (ref($types) eq 'ARRAY') {
22440:         if (@{$types} > 0) {
22441:             @alltypes = @{$types};
22442:         }
22443:     }
22444:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
22445:     $allinsttypes = join("','",@alltypes);
22446:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22447:     if (ref($settings) eq 'HASH') {
22448:         %existing = %{$settings};
22449:     }
22450:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
22451:                               \%currtargets,\%currrules,\%currcookies);
22452:     my $balancers = join("','",sort(keys(%currbalancer)));
22453:     return <<"END";
22454: 
22455: <script type="text/javascript">
22456: // <![CDATA[
22457: 
22458: currBalancers = new Array('$balancers');
22459: 
22460: function toggleTargets(balnum) {
22461:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22462:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
22463:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
22464:     var prevbalancer = prevhostitem.value;
22465:     var baltotal = document.getElementById('loadbalancing_total').value;
22466:     prevhostitem.value = balancer;
22467:     if (prevbalancer != '') {
22468:         var prevIdx = currBalancers.indexOf(prevbalancer);
22469:         if (prevIdx != -1) {
22470:             currBalancers.splice(prevIdx,1);
22471:         }
22472:     }
22473:     if (balancer == '') {
22474:         hideSpares(balnum);
22475:     } else {
22476:         var currIdx = currBalancers.indexOf(balancer);
22477:         if (currIdx == -1) {
22478:             currBalancers.push(balancer);
22479:         }
22480:         var homedoms = new Array('$allishome');
22481:         var ishomedom = homedoms[lonhostitem.selectedIndex];
22482:         showSpares(balancer,ishomedom,balnum);
22483:     }
22484:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
22485:     return;
22486: }
22487: 
22488: function showSpares(balancer,ishomedom,balnum) {
22489:     var alltargets = new Array('$alltargets');
22490:     var insttypes = new Array('$allinsttypes');
22491:     var offloadtypes = new Array('primary','default');
22492: 
22493:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
22494:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
22495:  
22496:     for (var i=0; i<offloadtypes.length; i++) {
22497:         var count = 0;
22498:         for (var j=0; j<alltargets.length; j++) {
22499:             if (alltargets[j] != balancer) {
22500:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
22501:                 item.value = alltargets[j];
22502:                 item.style.textAlign='left';
22503:                 item.style.textFace='normal';
22504:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
22505:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
22506:                     item.disabled = '';
22507:                 } else {
22508:                     item.disabled = 'disabled';
22509:                     item.checked = false;
22510:                 }
22511:                 count ++;
22512:             }
22513:         }
22514:     }
22515:     for (var k=0; k<insttypes.length; k++) {
22516:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
22517:             if (ishomedom == 1) {
22518:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22519:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22520:             } else {
22521:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22522:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22523:             }
22524:         } else {
22525:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22526:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22527:         }
22528:         if ((insttypes[k] != '_LC_external') && 
22529:             ((insttypes[k] != '_LC_internetdom') ||
22530:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
22531:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
22532:             item.options.length = 0;
22533:             item.options[0] = new Option("","",true,true);
22534:             var idx = 0;
22535:             for (var m=0; m<alltargets.length; m++) {
22536:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
22537:                     idx ++;
22538:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22539:                 }
22540:             }
22541:         }
22542:     }
22543:     return;
22544: }
22545: 
22546: function hideSpares(balnum) {
22547:     var alltargets = new Array('$alltargets');
22548:     var insttypes = new Array('$allinsttypes');
22549:     var offloadtypes = new Array('primary','default');
22550: 
22551:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
22552:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
22553: 
22554:     var total = alltargets.length - 1;
22555:     for (var i=0; i<offloadtypes; i++) {
22556:         for (var j=0; j<total; j++) {
22557:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
22558:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
22559:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
22560:         }
22561:     }
22562:     for (var k=0; k<insttypes.length; k++) {
22563:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22564:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22565:         if (insttypes[k] != '_LC_external') {
22566:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
22567:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
22568:         }
22569:     }
22570:     return;
22571: }
22572: 
22573: function checkOffloads(item,balnum,type) {
22574:     var alltargets = new Array('$alltargets');
22575:     var offloadtypes = new Array('primary','default');
22576:     if (item.checked) {
22577:         var total = alltargets.length - 1;
22578:         var other;
22579:         if (type == offloadtypes[0]) {
22580:             other = offloadtypes[1];
22581:         } else {
22582:             other = offloadtypes[0];
22583:         }
22584:         for (var i=0; i<total; i++) {
22585:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
22586:             if (server == item.value) {
22587:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
22588:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
22589:                 }
22590:             }
22591:         }
22592:     }
22593:     return;
22594: }
22595: 
22596: function singleServerToggle(balnum,type) {
22597:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
22598:     if (offloadtoSelIdx == 0) {
22599:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
22600:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22601: 
22602:     } else {
22603:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
22604:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22605:     }
22606:     return;
22607: }
22608: 
22609: function balanceruleChange(formname,balnum,type) {
22610:     if (type == '_LC_external') {
22611:         return;
22612:     }
22613:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
22614:     for (var i=0; i<typesRules.length; i++) {
22615:         if (formname.elements[typesRules[i]].checked) {
22616:             if (formname.elements[typesRules[i]].value != 'specific') {
22617:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
22618:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22619:             } else {
22620:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22621:             }
22622:         }
22623:     }
22624:     return;
22625: }
22626: 
22627: function balancerDeleteChange(balnum) {
22628:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22629:     var baltotal = document.getElementById('loadbalancing_total').value;
22630:     var addtarget;
22631:     var removetarget;
22632:     var action = 'delete';
22633:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
22634:         var lonhost = hostitem.value;
22635:         var currIdx = currBalancers.indexOf(lonhost);
22636:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
22637:             if (currIdx != -1) {
22638:                 currBalancers.splice(currIdx,1);
22639:             }
22640:             addtarget = lonhost;
22641:         } else {
22642:             if (currIdx == -1) {
22643:                 currBalancers.push(lonhost);
22644:             }
22645:             removetarget = lonhost;
22646:             action = 'undelete';
22647:         }
22648:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
22649:     }
22650:     return;
22651: }
22652: 
22653: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
22654:     if (baltotal > 1) {
22655:         var offloadtypes = new Array('primary','default');
22656:         var alltargets = new Array('$alltargets');
22657:         var insttypes = new Array('$allinsttypes');
22658:         for (var i=0; i<baltotal; i++) {
22659:             if (i != balnum) {
22660:                 for (var j=0; j<offloadtypes.length; j++) {
22661:                     var total = alltargets.length - 1;
22662:                     for (var k=0; k<total; k++) {
22663:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
22664:                         var server = serveritem.value;
22665:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
22666:                             if (server == addtarget) {
22667:                                 serveritem.disabled = '';
22668:                             }
22669:                         }
22670:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22671:                             if (server == removetarget) {
22672:                                 serveritem.disabled = 'disabled';
22673:                                 serveritem.checked = false;
22674:                             }
22675:                         }
22676:                     }
22677:                 }
22678:                 for (var j=0; j<insttypes.length; j++) {
22679:                     if (insttypes[j] != '_LC_external') {
22680:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
22681:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
22682:                             var currSel = singleserver.selectedIndex;
22683:                             var currVal = singleserver.options[currSel].value;
22684:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
22685:                                 var numoptions = singleserver.options.length;
22686:                                 var needsnew = 1;
22687:                                 for (var k=0; k<numoptions; k++) {
22688:                                     if (singleserver.options[k] == addtarget) {
22689:                                         needsnew = 0;
22690:                                         break;
22691:                                     }
22692:                                 }
22693:                                 if (needsnew == 1) {
22694:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
22695:                                 }
22696:                             }
22697:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22698:                                 singleserver.options.length = 0;
22699:                                 if ((currVal) && (currVal != removetarget)) {
22700:                                     singleserver.options[0] = new Option("","",false,false);
22701:                                 } else {
22702:                                     singleserver.options[0] = new Option("","",true,true);
22703:                                 }
22704:                                 var idx = 0;
22705:                                 for (var m=0; m<alltargets.length; m++) {
22706:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
22707:                                         idx ++;
22708:                                         if (currVal == alltargets[m]) {
22709:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
22710:                                         } else {
22711:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22712:                                         }
22713:                                     }
22714:                                 }
22715:                             }
22716:                         }
22717:                     }
22718:                 }
22719:             }
22720:         }
22721:     }
22722:     return;
22723: }
22724: 
22725: // ]]>
22726: </script>
22727: 
22728: END
22729: }
22730: 
22731: 
22732: sub new_spares_js {
22733:     my @sparestypes = ('primary','default');
22734:     my $types = join("','",@sparestypes);
22735:     my $select = &mt('Select');
22736:     return <<"END";
22737: 
22738: <script type="text/javascript">
22739: // <![CDATA[
22740: 
22741: function updateNewSpares(formname,lonhost) {
22742:     var types = new Array('$types');
22743:     var include = new Array();
22744:     var exclude = new Array();
22745:     for (var i=0; i<types.length; i++) {
22746:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
22747:         for (var j=0; j<spareboxes.length; j++) {
22748:             if (formname.elements[spareboxes[j]].checked) {
22749:                 exclude.push(formname.elements[spareboxes[j]].value);
22750:             } else {
22751:                 include.push(formname.elements[spareboxes[j]].value);
22752:             }
22753:         }
22754:     }
22755:     for (var i=0; i<types.length; i++) {
22756:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
22757:         var selIdx = newSpare.selectedIndex;
22758:         var currnew = newSpare.options[selIdx].value;
22759:         var okSpares = new Array();
22760:         for (var j=0; j<newSpare.options.length; j++) {
22761:             var possible = newSpare.options[j].value;
22762:             if (possible != '') {
22763:                 if (exclude.indexOf(possible) == -1) {
22764:                     okSpares.push(possible);
22765:                 } else {
22766:                     if (currnew == possible) {
22767:                         selIdx = 0;
22768:                     }
22769:                 }
22770:             }
22771:         }
22772:         for (var k=0; k<include.length; k++) {
22773:             if (okSpares.indexOf(include[k]) == -1) {
22774:                 okSpares.push(include[k]);
22775:             }
22776:         }
22777:         okSpares.sort();
22778:         newSpare.options.length = 0;
22779:         if (selIdx == 0) {
22780:             newSpare.options[0] = new Option("$select","",true,true);
22781:         } else {
22782:             newSpare.options[0] = new Option("$select","",false,false);
22783:         }
22784:         for (var m=0; m<okSpares.length; m++) {
22785:             var idx = m+1;
22786:             var selThis = 0;
22787:             if (selIdx != 0) {
22788:                 if (okSpares[m] == currnew) {
22789:                     selThis = 1;
22790:                 }
22791:             }
22792:             if (selThis == 1) {
22793:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
22794:             } else {
22795:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
22796:             }
22797:         }
22798:     }
22799:     return;
22800: }
22801: 
22802: function checkNewSpares(lonhost,type) {
22803:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
22804:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
22805:     if (chosen != '') {
22806:         var othertype;
22807:         var othernewSpare;
22808:         if (type == 'primary') {
22809:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
22810:         }
22811:         if (type == 'default') {
22812:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
22813:         }
22814:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
22815:             othernewSpare.selectedIndex = 0;
22816:         }
22817:     }
22818:     return;
22819: }
22820: 
22821: // ]]>
22822: </script>
22823: 
22824: END
22825: 
22826: }
22827: 
22828: sub common_domprefs_js {
22829:     return <<"END";
22830: 
22831: <script type="text/javascript">
22832: // <![CDATA[
22833: 
22834: function getIndicesByName(formname,item) {
22835:     var group = new Array();
22836:     for (var i=0;i<formname.elements.length;i++) {
22837:         if (formname.elements[i].name == item) {
22838:             group.push(formname.elements[i].id);
22839:         }
22840:     }
22841:     return group;
22842: }
22843: 
22844: // ]]>
22845: </script>
22846: 
22847: END
22848: 
22849: }
22850: 
22851: sub recaptcha_js {
22852:     my %lt = &captcha_phrases();
22853:     return <<"END";
22854: 
22855: <script type="text/javascript">
22856: // <![CDATA[
22857: 
22858: function updateCaptcha(caller,context) {
22859:     var privitem;
22860:     var pubitem;
22861:     var privtext;
22862:     var pubtext;
22863:     var versionitem;
22864:     var versiontext;
22865:     if (document.getElementById(context+'_recaptchapub')) {
22866:         pubitem = document.getElementById(context+'_recaptchapub');
22867:     } else {
22868:         return;
22869:     }
22870:     if (document.getElementById(context+'_recaptchapriv')) {
22871:         privitem = document.getElementById(context+'_recaptchapriv');
22872:     } else {
22873:         return;
22874:     }
22875:     if (document.getElementById(context+'_recaptchapubtxt')) {
22876:         pubtext = document.getElementById(context+'_recaptchapubtxt');
22877:     } else {
22878:         return;
22879:     }
22880:     if (document.getElementById(context+'_recaptchaprivtxt')) {
22881:         privtext = document.getElementById(context+'_recaptchaprivtxt');
22882:     } else {
22883:         return;
22884:     }
22885:     if (document.getElementById(context+'_recaptchaversion')) {
22886:         versionitem = document.getElementById(context+'_recaptchaversion');
22887:     } else {
22888:         return;
22889:     }
22890:     if (document.getElementById(context+'_recaptchavertxt')) {
22891:         versiontext = document.getElementById(context+'_recaptchavertxt');
22892:     } else {
22893:         return;
22894:     }
22895:     if (caller.checked) {
22896:         if (caller.value == 'recaptcha') {
22897:             pubitem.type = 'text';
22898:             privitem.type = 'text';
22899:             pubitem.size = '40';
22900:             privitem.size = '40';
22901:             pubtext.innerHTML = "$lt{'pub'}";
22902:             privtext.innerHTML = "$lt{'priv'}";
22903:             versionitem.type = 'text';
22904:             versionitem.size = '3';
22905:             versiontext.innerHTML = "$lt{'ver'}";
22906:         } else {
22907:             pubitem.type = 'hidden';
22908:             privitem.type = 'hidden';
22909:             versionitem.type = 'hidden';
22910:             pubtext.innerHTML = '';
22911:             privtext.innerHTML = '';
22912:             versiontext.innerHTML = '';
22913:         }
22914:     }
22915:     return;
22916: }
22917: 
22918: // ]]>
22919: </script>
22920: 
22921: END
22922: 
22923: }
22924: 
22925: sub toggle_display_js {
22926:     return <<"END";
22927: 
22928: <script type="text/javascript">
22929: // <![CDATA[
22930: 
22931: function toggleDisplay(domForm,caller) {
22932:     if (document.getElementById(caller)) {
22933:         var divitem = document.getElementById(caller);
22934:         var optionsElement = domForm.coursecredits;
22935:         var checkval = 1;
22936:         var dispval = 'block';
22937:         var selfcreateRegExp = /^cancreate_emailverified/;
22938:         if (caller == 'emailoptions') {
22939:             optionsElement = domForm.cancreate_email;
22940:         }
22941:         if (caller == 'studentsubmission') {
22942:             optionsElement = domForm.postsubmit;
22943:         }
22944:         if (caller == 'cloneinstcode') {
22945:             optionsElement = domForm.canclone;
22946:             checkval = 'instcode';
22947:         }
22948:         if (selfcreateRegExp.test(caller)) {
22949:             optionsElement = domForm.elements[caller];
22950:             checkval = 'other';
22951:             dispval = 'inline'
22952:         }
22953:         if (optionsElement.length) {
22954:             var currval;
22955:             for (var i=0; i<optionsElement.length; i++) {
22956:                 if (optionsElement[i].checked) {
22957:                    currval = optionsElement[i].value;
22958:                 }
22959:             }
22960:             if (currval == checkval) {
22961:                 divitem.style.display = dispval;
22962:             } else {
22963:                 divitem.style.display = 'none';
22964:             }
22965:         }
22966:     }
22967:     return;
22968: }
22969: 
22970: // ]]>
22971: </script>
22972: 
22973: END
22974: 
22975: }
22976: 
22977: sub captcha_phrases {
22978:     return &Apache::lonlocal::texthash (
22979:                  priv => 'Private key',
22980:                  pub  => 'Public key',
22981:                  original  => 'original (CAPTCHA)',
22982:                  recaptcha => 'successor (ReCAPTCHA)',
22983:                  notused   => 'unused',
22984:                  ver => 'ReCAPTCHA version (1 or 2)',
22985:     );
22986: }
22987: 
22988: sub devalidate_remote_domconfs {
22989:     my ($dom,$cachekeys) = @_;
22990:     return unless (ref($cachekeys) eq 'HASH');
22991:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22992:     my %thismachine;
22993:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22994:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
22995:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
22996:                       'ipaccess');
22997:     my %cache_by_lonhost;
22998:     if (exists($cachekeys->{'samllanding'})) {
22999:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
23000:             my %landing = %{$cachekeys->{'samllanding'}};
23001:             my %domservers = &Apache::lonnet::get_servers($dom);
23002:             if (keys(%domservers)) {
23003:                 foreach my $server (keys(%domservers)) {
23004:                     my @cached;
23005:                     next if ($thismachine{$server});
23006:                     if ($landing{$server}) {
23007:                         push(@cached,&escape('samllanding').':'.&escape($server));
23008:                     }
23009:                     if (@cached) {
23010:                         $cache_by_lonhost{$server} = \@cached;
23011:                     }
23012:                 }
23013:             }
23014:         }
23015:     }
23016:     if (keys(%servers)) {
23017:         foreach my $server (keys(%servers)) {
23018:             next if ($thismachine{$server});
23019:             my @cached;
23020:             foreach my $name (@posscached) {
23021:                 if ($cachekeys->{$name}) {
23022:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
23023:                         if (ref($cachekeys->{$name}) eq 'HASH') {
23024:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
23025:                                 push(@cached,&escape($name).':'.&escape($key));
23026:                             }
23027:                         }
23028:                     } else {
23029:                         push(@cached,&escape($name).':'.&escape($dom));
23030:                     }
23031:                 }
23032:             }
23033:             if ((exists($cache_by_lonhost{$server})) &&
23034:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
23035:                 push(@cached,@{$cache_by_lonhost{$server}});
23036:             }
23037:             if (@cached) {
23038:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
23039:             }
23040:         }
23041:     }
23042:     return;
23043: }
23044: 
23045: 1;

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