File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.393: download - view: text, annotated - select for diffs
Sun Nov 28 18:43:37 2021 UTC (2 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
  - Sanity checking for IP address(es) and IP ranges, and store as
    IP netblock(s) in CIDR notation.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.393 2021/11/28 18:43:37 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'],$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','defaults','wafproxy','passwords','quotas',
  264:                        '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:     );
  628:     if (keys(%servers) > 1) {
  629:         $prefs{'login'}  = { text   => 'Log-in page options',
  630:                              help   => 'Domain_Configuration_Login_Page',
  631:                             header => [{col1 => 'Log-in Service',
  632:                                         col2 => 'Server Setting',},
  633:                                        {col1 => 'Log-in Page Items',
  634:                                         col2 => ''},
  635:                                        {col1 => 'Log-in Help',
  636:                                         col2 => 'Value'},
  637:                                        {col1 => 'Custom HTML in document head',
  638:                                         col2 => 'Value'},
  639:                                        {col1 => 'SSO',
  640:                                         col2 => 'Dual login: SSO and non-SSO options'},
  641:                                       ],
  642:                             print => \&print_login,
  643:                             modify => \&modify_login,
  644:                            };
  645:     }
  646: 
  647:     my @roles = ('student','coordinator','author','admin');
  648:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  649:     &Apache::lonhtmlcommon::add_breadcrumb
  650:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  651:       text=>"Settings to display/modify"});
  652:     my $confname = $dom.'-domainconfig';
  653: 
  654:     if ($phase eq 'process') {
  655:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  656:                                                               \%prefs,\%domconfig,$confname,\@roles);
  657:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  658:             $r->rflush();
  659:             &devalidate_remote_domconfs($dom,$result);
  660:         }
  661:     } elsif ($phase eq 'display') {
  662:         my $js = &recaptcha_js().
  663:                  &toggle_display_js();
  664:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  665:             my ($othertitle,$usertypes,$types) =
  666:                 &Apache::loncommon::sorted_inst_types($dom);
  667:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  668:                                           $domconfig{'loadbalancing'}).
  669:                    &new_spares_js().
  670:                    &common_domprefs_js().
  671:                    &Apache::loncommon::javascript_array_indexof();
  672:         }
  673:         if (grep(/^requestcourses$/,@actions)) {
  674:             my $javascript_validations;
  675:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  676:             $js .= <<END;
  677: <script type="text/javascript">
  678: $javascript_validations
  679: </script>
  680: $coursebrowserjs
  681: END
  682:         }
  683:         if (grep(/^selfcreation$/,@actions)) {
  684:             $js .= &selfcreate_javascript();
  685:         }
  686:         if (grep(/^contacts$/,@actions)) {
  687:             $js .= &contacts_javascript();
  688:         }
  689:         if (grep(/^scantron$/,@actions)) {
  690:             $js .= &scantron_javascript();
  691:         }
  692:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  693:     } else {
  694: # check if domconfig user exists for the domain.
  695:         my $servadm = $r->dir_config('lonAdmEMail');
  696:         my ($configuserok,$author_ok,$switchserver) =
  697:             &config_check($dom,$confname,$servadm);
  698:         unless ($configuserok eq 'ok') {
  699:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  700:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  701:                           $confname).
  702:                       '<br />'
  703:             );
  704:             if ($switchserver) {
  705:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  706:                           '<br />'.
  707:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  708:                           '<br />'.
  709:                           &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).
  710:                           '<br />'.
  711:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  712:                 );
  713:             } else {
  714:                 $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.').
  715:                           '<br />'.
  716:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  717:                 );
  718:             }
  719:             $r->print(&Apache::loncommon::end_page());
  720:             return OK;
  721:         }
  722:         if (keys(%domconfig) == 0) {
  723:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  724:             my @ids=&Apache::lonnet::current_machine_ids();
  725:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  726:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  727:                 my @loginimages = ('img','logo','domlogo','login');
  728:                 my $custom_img_count = 0;
  729:                 foreach my $img (@loginimages) {
  730:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  731:                         $custom_img_count ++;
  732:                     }
  733:                 }
  734:                 foreach my $role (@roles) {
  735:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  736:                         $custom_img_count ++;
  737:                     }
  738:                 }
  739:                 if ($custom_img_count > 0) {
  740:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  741:                     my $switch_server = &check_switchserver($dom,$confname);
  742:                     $r->print(
  743:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  744:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  745:     &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 />'.
  746:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  747:                     if ($switch_server) {
  748:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  749:                     }
  750:                     $r->print(&Apache::loncommon::end_page());
  751:                     return OK;
  752:                 }
  753:             }
  754:         }
  755:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  756:     }
  757:     return OK;
  758: }
  759: 
  760: sub process_changes {
  761:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  762:     my %domconfig;
  763:     if (ref($values) eq 'HASH') {
  764:         %domconfig = %{$values};
  765:     }
  766:     my $output;
  767:     if ($action eq 'login') {
  768:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  769:     } elsif ($action eq 'rolecolors') {
  770:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  771:                                      $lastactref,%domconfig);
  772:     } elsif ($action eq 'quotas') {
  773:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  774:     } elsif ($action eq 'autoenroll') {
  775:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  776:     } elsif ($action eq 'autoupdate') {
  777:         $output = &modify_autoupdate($dom,%domconfig);
  778:     } elsif ($action eq 'autocreate') {
  779:         $output = &modify_autocreate($dom,%domconfig);
  780:     } elsif ($action eq 'directorysrch') {
  781:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  782:     } elsif ($action eq 'usercreation') {
  783:         $output = &modify_usercreation($dom,%domconfig);
  784:     } elsif ($action eq 'selfcreation') {
  785:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  786:     } elsif ($action eq 'usermodification') {
  787:         $output = &modify_usermodification($dom,%domconfig);
  788:     } elsif ($action eq 'contacts') {
  789:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  790:     } elsif ($action eq 'defaults') {
  791:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  792:     } elsif ($action eq 'scantron') {
  793:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  794:     } elsif ($action eq 'coursecategories') {
  795:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  796:     } elsif ($action eq 'serverstatuses') {
  797:         $output = &modify_serverstatuses($dom,%domconfig);
  798:     } elsif ($action eq 'requestcourses') {
  799:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  800:     } elsif ($action eq 'requestauthor') {
  801:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  802:     } elsif ($action eq 'helpsettings') {
  803:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  804:     } elsif ($action eq 'coursedefaults') {
  805:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  806:     } elsif ($action eq 'selfenrollment') {
  807:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  808:     } elsif ($action eq 'usersessions') {
  809:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  810:     } elsif ($action eq 'loadbalancing') {
  811:         $output = &modify_loadbalancing($dom,%domconfig);
  812:     } elsif ($action eq 'ltitools') {
  813:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  814:     } elsif ($action eq 'proctoring') {
  815:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  816:     } elsif ($action eq 'ssl') {
  817:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  818:     } elsif ($action eq 'trust') {
  819:         $output = &modify_trust($dom,$lastactref,%domconfig);
  820:     } elsif ($action eq 'lti') {
  821:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  822:     } elsif ($action eq 'privacy') {
  823:         $output = &modify_privacy($dom,%domconfig);
  824:     } elsif ($action eq 'passwords') {
  825:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  826:     } elsif ($action eq 'wafproxy') {
  827:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  828:     }
  829:     return $output;
  830: }
  831: 
  832: sub print_config_box {
  833:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  834:     my $rowtotal = 0;
  835:     my $output;
  836:     if ($action eq 'coursecategories') {
  837:         $output = &coursecategories_javascript($settings);
  838:     } elsif ($action eq 'defaults') {
  839:         $output = &defaults_javascript($settings); 
  840:     } elsif ($action eq 'passwords') {
  841:         $output = &passwords_javascript();
  842:     } elsif ($action eq 'helpsettings') {
  843:         my (%privs,%levelscurrent);
  844:         my %full=();
  845:         my %levels=(
  846:                      course => {},
  847:                      domain => {},
  848:                      system => {},
  849:                    );
  850:         my $context = 'domain';
  851:         my $crstype = 'Course';
  852:         my $formname = 'display';
  853:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  854:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  855:         $output =
  856:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  857:                                                       \@templateroles);
  858:     } elsif ($action eq 'ltitools') {
  859:         $output .= &ltitools_javascript($settings);
  860:     } elsif ($action eq 'lti') {
  861:         $output .= &lti_javascript($settings);
  862:     } elsif ($action eq 'proctoring') {
  863:         $output .= &proctoring_javascript($settings);
  864:     } elsif ($action eq 'wafproxy') {
  865:         $output .= &wafproxy_javascript($dom);
  866:     } elsif ($action eq 'autoupdate') {
  867:         $output .= &autoupdate_javascript();
  868:     } elsif ($action eq 'login') {
  869:         $output .= &saml_javascript();
  870:     }
  871:     $output .=
  872:          '<table class="LC_nested_outer">
  873:           <tr>
  874:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  875:            &mt($item->{text}).'&nbsp;'.
  876:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  877:           '</tr>';
  878:     $rowtotal ++;
  879:     my $numheaders = 1;
  880:     if (ref($item->{'header'}) eq 'ARRAY') {
  881:         $numheaders = scalar(@{$item->{'header'}});
  882:     }
  883:     if ($numheaders > 1) {
  884:         my $colspan = '';
  885:         my $rightcolspan = '';
  886:         my $leftnobr = '';
  887:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  888:             ($action eq 'directorysrch') ||
  889:             (($action eq 'login') && ($numheaders < 5))) {
  890:             $colspan = ' colspan="2"';
  891:         }
  892:         if ($action eq 'usersessions') {
  893:             $rightcolspan = ' colspan="3"'; 
  894:         }
  895:         if ($action eq 'passwords') {
  896:             $leftnobr = ' LC_nobreak';
  897:         }
  898:         $output .= '
  899:           <tr>
  900:            <td>
  901:             <table class="LC_nested">
  902:              <tr class="LC_info_row">
  903:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  904:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  905:              </tr>';
  906:         $rowtotal ++;
  907:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  908:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  909:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  910:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  911:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy')) {
  912:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  913:         } elsif ($action eq 'passwords') {
  914:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  915:         } elsif ($action eq 'coursecategories') {
  916:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  917:         } elsif ($action eq 'scantron') {
  918:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  919:         } elsif ($action eq 'login') {
  920:             if ($numheaders == 5) {
  921:                 $colspan = ' colspan="2"';
  922:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  923:             } else {
  924:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  925:             }
  926:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  927:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  928:         } elsif ($action eq 'rolecolors') {
  929:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  930:         }
  931:         $output .= '
  932:            </table>
  933:           </td>
  934:          </tr>
  935:          <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  940:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  941:              </tr>';
  942:             $rowtotal ++;
  943:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  944:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  945:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  946:             ($action eq 'trust') || ($action eq 'contacts') ||
  947:             ($action eq 'privacy') || ($action eq 'passwords')) {
  948:             if ($action eq 'coursecategories') {
  949:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  950:                 $colspan = ' colspan="2"';
  951:             } elsif ($action eq 'trust') {
  952:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  953:             } elsif ($action eq 'passwords') {
  954:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  955:             } else {
  956:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  957:             }
  958:             if ($action eq 'trust') {
  959:                 $output .= '
  960:             </table>
  961:           </td>
  962:          </tr>';
  963:                 my @trusthdrs = qw(2 3 4 5 6 7);
  964:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  965:                 for (my $i=0; $i<@trusthdrs; $i++) {
  966:                     $output .= '
  967:          <tr>
  968:            <td>
  969:             <table class="LC_nested">
  970:              <tr class="LC_info_row">
  971:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  972:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  973:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  974:             </table>
  975:           </td>
  976:          </tr>';
  977:                 }
  978:                 $output .= '
  979:          <tr>
  980:            <td>
  981:             <table class="LC_nested">
  982:              <tr class="LC_info_row">
  983:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  984:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  985:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  986:             } else {
  987:                 $output .= '
  988:            </table>
  989:           </td>
  990:          </tr>
  991:          <tr>
  992:            <td>
  993:             <table class="LC_nested">
  994:              <tr class="LC_info_row">
  995:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  996:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  997:              </tr>'."\n";
  998:                 if ($action eq 'coursecategories') {
  999:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1000:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
 1001:                     if ($action eq 'passwords') {
 1002:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1003:                     } else {
 1004:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1005:                     }
 1006:                     $output .= '
 1007:              </tr>
 1008:             </table>
 1009:            </td>
 1010:           </tr>
 1011:           <tr>
 1012:            <td>
 1013:             <table class="LC_nested">
 1014:              <tr class="LC_info_row">
 1015:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1016:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1017:                     if ($action eq 'passwords') {
 1018:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1019:                     } else {
 1020:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1021:                     }
 1022:                     $output .= '
 1023:             </table>
 1024:           </td>
 1025:          </tr>
 1026:          <tr>';
 1027:                 } else {
 1028:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1029:                 }
 1030:             }
 1031:             $rowtotal ++;
 1032:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1033:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1034:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
 1035:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1036:         } elsif ($action eq 'scantron') {
 1037:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1038:         } elsif ($action eq 'ssl') {
 1039:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1040:             </table>
 1041:           </td>
 1042:          </tr>
 1043:          <tr>
 1044:            <td>
 1045:             <table class="LC_nested">
 1046:              <tr class="LC_info_row">
 1047:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1048:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1049:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1050:             </table>
 1051:           </td>
 1052:          </tr>
 1053:          <tr>
 1054:            <td>
 1055:             <table class="LC_nested">
 1056:              <tr class="LC_info_row">
 1057:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1058:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1059:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1060:         } elsif ($action eq 'login') {
 1061:             if ($numheaders == 5) {
 1062:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1063:            </table>
 1064:           </td>
 1065:          </tr>
 1066:          <tr>
 1067:            <td>
 1068:             <table class="LC_nested">
 1069:              <tr class="LC_info_row">
 1070:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1071:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1072:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1073:                 $rowtotal ++;
 1074:             } else {
 1075:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1076:             }
 1077:             $output .= '
 1078:            </table>
 1079:           </td>
 1080:          </tr>
 1081:          <tr>
 1082:            <td>
 1083:             <table class="LC_nested">
 1084:              <tr class="LC_info_row">';
 1085:             if ($numheaders == 5) {
 1086:                 $output .= '
 1087:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1088:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1089:              </tr>';
 1090:             } else {
 1091:                 $output .= '
 1092:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1093:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1094:              </tr>';
 1095:             }
 1096:             $rowtotal ++;
 1097:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1098:            </table>
 1099:           </td>
 1100:          </tr>
 1101:          <tr>
 1102:            <td>
 1103:             <table class="LC_nested">
 1104:              <tr class="LC_info_row">';
 1105:             if ($numheaders == 5) {
 1106:                 $output .= '
 1107:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1108:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1109:              </tr>';
 1110:             } else {
 1111:                 $output .= '
 1112:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1113:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1114:              </tr>';
 1115:             }
 1116:             $rowtotal ++;
 1117:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1118:         } elsif ($action eq 'requestcourses') {
 1119:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1120:             $rowtotal ++;
 1121:             $output .= &print_studentcode($settings,\$rowtotal).'
 1122:            </table>
 1123:           </td>
 1124:          </tr>
 1125:          <tr>
 1126:            <td>
 1127:             <table class="LC_nested">
 1128:              <tr class="LC_info_row">
 1129:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1130:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1131:                        &textbookcourses_javascript($settings).
 1132:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1133:             </table>
 1134:            </td>
 1135:           </tr>
 1136:          <tr>
 1137:            <td>
 1138:             <table class="LC_nested">
 1139:              <tr class="LC_info_row">
 1140:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1141:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1142:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1143:             </table>
 1144:            </td>
 1145:           </tr>
 1146:           <tr>
 1147:            <td>
 1148:             <table class="LC_nested">
 1149:              <tr class="LC_info_row">
 1150:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1151:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1152:              </tr>'.
 1153:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1154:         } elsif ($action eq 'requestauthor') {
 1155:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1156:             $rowtotal ++;
 1157:         } elsif ($action eq 'rolecolors') {
 1158:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1159:            </table>
 1160:           </td>
 1161:          </tr>
 1162:          <tr>
 1163:            <td>
 1164:             <table class="LC_nested">
 1165:              <tr class="LC_info_row">
 1166:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1167:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1168:               <td class="LC_right_item" style="vertical-align: top">'.
 1169:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1170:              </tr>'.
 1171:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1172:            </table>
 1173:           </td>
 1174:          </tr>
 1175:          <tr>
 1176:            <td>
 1177:             <table class="LC_nested">
 1178:              <tr class="LC_info_row">
 1179:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1180:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1181:              </tr>'.
 1182:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1183:             $rowtotal += 2;
 1184:         }
 1185:     } else {
 1186:         $output .= '
 1187:           <tr>
 1188:            <td>
 1189:             <table class="LC_nested">
 1190:              <tr class="LC_info_row">';
 1191:         if ($action eq 'login') {
 1192:             $output .= '  
 1193:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1194:         } elsif ($action eq 'serverstatuses') {
 1195:             $output .= '
 1196:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1197:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1198: 
 1199:         } else {
 1200:             $output .= '
 1201:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1202:         }
 1203:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1204:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1205:                        &mt($item->{'header'}->[0]->{'col2'});
 1206:             if ($action eq 'serverstatuses') {
 1207:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1208:             } 
 1209:         } else {
 1210:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1211:                        &mt($item->{'header'}->[0]->{'col2'});
 1212:         }
 1213:         $output .= '</td>';
 1214:         if ($item->{'header'}->[0]->{'col3'}) {
 1215:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1216:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1217:                             &mt($item->{'header'}->[0]->{'col3'});
 1218:             } else {
 1219:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1220:                            &mt($item->{'header'}->[0]->{'col3'});
 1221:             }
 1222:             if ($action eq 'serverstatuses') {
 1223:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1224:             }
 1225:             $output .= '</td>';
 1226:         }
 1227:         if ($item->{'header'}->[0]->{'col4'}) {
 1228:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1229:                        &mt($item->{'header'}->[0]->{'col4'});
 1230:         }
 1231:         $output .= '</tr>';
 1232:         $rowtotal ++;
 1233:         if ($action eq 'quotas') {
 1234:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1235:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1236:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1237:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1238:                  ($action eq 'proctoring')) {
 1239:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1240:         }
 1241:     }
 1242:     $output .= '
 1243:    </table>
 1244:   </td>
 1245:  </tr>
 1246: </table><br />';
 1247:     return ($output,$rowtotal);
 1248: }
 1249: 
 1250: sub print_login {
 1251:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1252:     my ($css_class,$datatable,$switchserver,%lt);
 1253:     my %choices = &login_choices();
 1254:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1255:         %lt = &login_file_options();
 1256:         $switchserver = &check_switchserver($dom,$confname);
 1257:     }
 1258:     if ($caller eq 'service') {
 1259:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1260:         my $choice = $choices{'disallowlogin'};
 1261:         $css_class = ' class="LC_odd_row"';
 1262:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1263:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1264:                       '<th>'.$choices{'server'}.'</th>'.
 1265:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1266:                       '<th>'.$choices{'custompath'}.'</th>'.
 1267:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1268:         my %disallowed;
 1269:         if (ref($settings) eq 'HASH') {
 1270:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1271:                %disallowed = %{$settings->{'loginvia'}};
 1272:             }
 1273:         }
 1274:         foreach my $lonhost (sort(keys(%servers))) {
 1275:             my $direct = 'selected="selected"';
 1276:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1277:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1278:                     $direct = '';
 1279:                 }
 1280:             }
 1281:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1282:                           '<td><select name="'.$lonhost.'_server">'.
 1283:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1284:                           '</option>';
 1285:             foreach my $hostid (sort(keys(%servers))) {
 1286:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1287:                 my $selected = '';
 1288:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1289:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1290:                         $selected = 'selected="selected"';
 1291:                     }
 1292:                 }
 1293:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1294:                               $servers{$hostid}.'</option>';
 1295:             }
 1296:             $datatable .= '</select></td>'.
 1297:                           '<td><select name="'.$lonhost.'_serverpath">';
 1298:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1299:                 my $pathname = $path;
 1300:                 if ($path eq 'custom') {
 1301:                     $pathname = &mt('Custom Path').' ->';
 1302:                 }
 1303:                 my $selected = '';
 1304:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1305:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1306:                         $selected = 'selected="selected"';
 1307:                     }
 1308:                 } elsif ($path eq '') {
 1309:                     $selected = 'selected="selected"';
 1310:                 }
 1311:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1312:             }
 1313:             $datatable .= '</select></td>';
 1314:             my ($custom,$exempt);
 1315:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1316:                 $custom = $disallowed{$lonhost}{'custompath'};
 1317:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1318:             }
 1319:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1320:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1321:                           '</tr>';
 1322:         }
 1323:         $datatable .= '</table></td></tr>';
 1324:         return $datatable;
 1325:     } elsif ($caller eq 'page') {
 1326:         my %defaultchecked = ( 
 1327:                                'coursecatalog' => 'on',
 1328:                                'helpdesk'      => 'on',
 1329:                                'adminmail'     => 'off',
 1330:                                'newuser'       => 'off',
 1331:                              );
 1332:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1333:         my (%checkedon,%checkedoff);
 1334:         foreach my $item (@toggles) {
 1335:             if ($defaultchecked{$item} eq 'on') { 
 1336:                 $checkedon{$item} = ' checked="checked" ';
 1337:                 $checkedoff{$item} = ' ';
 1338:             } elsif ($defaultchecked{$item} eq 'off') {
 1339:                 $checkedoff{$item} = ' checked="checked" ';
 1340:                 $checkedon{$item} = ' ';
 1341:             }
 1342:         }
 1343:         my @images = ('img','logo','domlogo','login');
 1344:         my @logintext = ('textcol','bgcol');
 1345:         my @bgs = ('pgbg','mainbg','sidebg');
 1346:         my @links = ('link','alink','vlink');
 1347:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1348:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1349:         my (%is_custom,%designs);
 1350:         my %defaults = (
 1351:                        font => $defaultdesign{'login.font'},
 1352:                        );
 1353:         foreach my $item (@images) {
 1354:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1355:             $defaults{'showlogo'}{$item} = 1;
 1356:         }
 1357:         foreach my $item (@bgs) {
 1358:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1359:         }
 1360:         foreach my $item (@logintext) {
 1361:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1362:         }
 1363:         foreach my $item (@links) {
 1364:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1365:         }
 1366:         if (ref($settings) eq 'HASH') {
 1367:             foreach my $item (@toggles) {
 1368:                 if ($settings->{$item} eq '1') {
 1369:                     $checkedon{$item} =  ' checked="checked" ';
 1370:                     $checkedoff{$item} = ' ';
 1371:                 } elsif ($settings->{$item} eq '0') {
 1372:                     $checkedoff{$item} =  ' checked="checked" ';
 1373:                     $checkedon{$item} = ' ';
 1374:                 }
 1375:             }
 1376:             foreach my $item (@images) {
 1377:                 if (defined($settings->{$item})) {
 1378:                     $designs{$item} = $settings->{$item};
 1379:                     $is_custom{$item} = 1;
 1380:                 }
 1381:                 if (defined($settings->{'showlogo'}{$item})) {
 1382:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1383:                 }
 1384:             }
 1385:             foreach my $item (@logintext) {
 1386:                 if ($settings->{$item} ne '') {
 1387:                     $designs{'logintext'}{$item} = $settings->{$item};
 1388:                     $is_custom{$item} = 1;
 1389:                 }
 1390:             }
 1391:             if ($settings->{'font'} ne '') {
 1392:                 $designs{'font'} = $settings->{'font'};
 1393:                 $is_custom{'font'} = 1;
 1394:             }
 1395:             foreach my $item (@bgs) {
 1396:                 if ($settings->{$item} ne '') {
 1397:                     $designs{'bgs'}{$item} = $settings->{$item};
 1398:                     $is_custom{$item} = 1;
 1399:                 }
 1400:             }
 1401:             foreach my $item (@links) {
 1402:                 if ($settings->{$item} ne '') {
 1403:                     $designs{'links'}{$item} = $settings->{$item};
 1404:                     $is_custom{$item} = 1;
 1405:                 }
 1406:             }
 1407:         } else {
 1408:             if ($designhash{$dom.'.login.font'} ne '') {
 1409:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1410:                 $is_custom{'font'} = 1;
 1411:             }
 1412:             foreach my $item (@images) {
 1413:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1414:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1415:                     $is_custom{$item} = 1;
 1416:                 }
 1417:             }
 1418:             foreach my $item (@bgs) {
 1419:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1420:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1421:                     $is_custom{$item} = 1;
 1422:                 }
 1423:             }
 1424:             foreach my $item (@links) {
 1425:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1426:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1427:                     $is_custom{$item} = 1;
 1428:                 }
 1429:             }
 1430:         }
 1431:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1432:                                                       logo => 'Institution Logo',
 1433:                                                       domlogo => 'Domain Logo',
 1434:                                                       login => 'Login box');
 1435:         my $itemcount = 1;
 1436:         foreach my $item (@toggles) {
 1437:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1438:             $datatable .=  
 1439:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1440:                 '</td><td>'.
 1441:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1442:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1443:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1444:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1445:                 '</tr>';
 1446:             $itemcount ++;
 1447:         }
 1448:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1449:         $datatable .= '</tr></table></td></tr>';
 1450:     } elsif ($caller eq 'help') {
 1451:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1452:         my $itemcount = 1;
 1453:         $defaulturl = '/adm/loginproblems.html';
 1454:         $defaulttype = 'default';
 1455:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1456:         my @currlangs;
 1457:         if (ref($settings) eq 'HASH') {
 1458:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1459:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1460:                     next if ($settings->{'helpurl'}{$key} eq '');
 1461:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1462:                     $type{$key} = 'custom';
 1463:                     unless ($key eq 'nolang') {
 1464:                         push(@currlangs,$key);
 1465:                     }
 1466:                 }
 1467:             } elsif ($settings->{'helpurl'} ne '') {
 1468:                 $type{'nolang'} = 'custom';
 1469:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1470:             }
 1471:         }
 1472:         foreach my $lang ('nolang',sort(@currlangs)) {
 1473:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1474:             $datatable .= '<tr'.$css_class.'>';
 1475:             if ($url{$lang} eq '') {
 1476:                 $url{$lang} = $defaulturl;
 1477:             }
 1478:             if ($type{$lang} eq '') {
 1479:                 $type{$lang} = $defaulttype;
 1480:             }
 1481:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1482:             if ($lang eq 'nolang') {
 1483:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1484:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1485:             } else {
 1486:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1487:                                   $langchoices{$lang},
 1488:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1489:             }
 1490:             $datatable .= '</span></td>'."\n".
 1491:                           '<td class="LC_left_item">';
 1492:             if ($type{$lang} eq 'custom') {
 1493:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1494:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1495:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1496:             } else {
 1497:                 $datatable .= $lt{'upl'};
 1498:             }
 1499:             $datatable .='<br />';
 1500:             if ($switchserver) {
 1501:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1502:             } else {
 1503:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1504:             }
 1505:             $datatable .= '</td></tr>';
 1506:             $itemcount ++;
 1507:         }
 1508:         my @addlangs;
 1509:         foreach my $lang (sort(keys(%langchoices))) {
 1510:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1511:             push(@addlangs,$lang);
 1512:         }
 1513:         if (@addlangs > 0) {
 1514:             my %toadd;
 1515:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1516:             $toadd{''} = &mt('Select');
 1517:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1518:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1519:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1520:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1521:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1522:             if ($switchserver) {
 1523:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1524:             } else {
 1525:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1526:             }
 1527:             $datatable .= '</td></tr>';
 1528:             $itemcount ++;
 1529:         }
 1530:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1531:     } elsif ($caller eq 'headtag') {
 1532:         my %domservers = &Apache::lonnet::get_servers($dom);
 1533:         my $choice = $choices{'headtag'};
 1534:         $css_class = ' class="LC_odd_row"';
 1535:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1536:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1537:                       '<th>'.$choices{'current'}.'</th>'.
 1538:                       '<th>'.$choices{'action'}.'</th>'.
 1539:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1540:         my (%currurls,%currexempt);
 1541:         if (ref($settings) eq 'HASH') {
 1542:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1543:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1544:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1545:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1546:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1547:                     }
 1548:                 }
 1549:             }
 1550:         }
 1551:         foreach my $lonhost (sort(keys(%domservers))) {
 1552:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1553:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1554:             if ($currurls{$lonhost}) {
 1555:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1556:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1557:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1558:                               '">'.$lt{'curr'}.'</a></td>'.
 1559:                               '<td><span class="LC_nobreak"><label>'.
 1560:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1561:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1562:             } else {
 1563:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1564:             }
 1565:             $datatable .='<br />';
 1566:             if ($switchserver) {
 1567:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1568:             } else {
 1569:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1570:             }
 1571:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1572:         }
 1573:         $datatable .= '</table></td></tr>';
 1574:     } elsif ($caller eq 'saml') {
 1575:         my %domservers = &Apache::lonnet::get_servers($dom);
 1576:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1577:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1578:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1579:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1580:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso,%styleon,%styleoff);
 1581:         foreach my $lonhost (keys(%domservers)) {
 1582:             $samlurl{$lonhost} = '/adm/sso';
 1583:             $styleon{$lonhost} = 'display:none';
 1584:             $styleoff{$lonhost} = '';
 1585:         }
 1586:         if (ref($settings->{'saml'}) eq 'HASH') {
 1587:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1588:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1589:                     $saml{$lonhost} = 1;
 1590:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1591:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1592:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1593:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1594:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1595:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1596:                     $styleon{$lonhost} = '';
 1597:                     $styleoff{$lonhost} = 'display:none';
 1598:                 } else {
 1599:                     $styleon{$lonhost} = 'display:none';
 1600:                     $styleoff{$lonhost} = '';
 1601:                 }
 1602:             }
 1603:         }
 1604:         my $itemcount = 1;
 1605:         foreach my $lonhost (sort(keys(%domservers))) {
 1606:             my $samlon = ' ';
 1607:             my $samloff = ' checked="checked" ';
 1608:             if ($saml{$lonhost}) {
 1609:                 $samlon = $samloff;
 1610:                 $samloff = ' ';
 1611:             }
 1612:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1613:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1614:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1615:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1616:                           &mt('No').'</label>'.('&nbsp;'x2).
 1617:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1618:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1619:                           &mt('Yes').'</label></span></td>'.
 1620:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1621:                           '<table><tr><th colspan="5" align="center">'.&mt('SSO').'</th><th align="center">'.
 1622:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1623:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1624:                           '<th>'.&mt('Alt Text').'</th><th>'.&mt('URL').'</th>'.
 1625:                           '<th>'.&mt('Tool Tip').'</th><th>'.&mt('Text').'</th></tr>'.
 1626:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="8" value="'.
 1627:                           $samltext{$lonhost}.'" /></td><td>';
 1628:             if ($samlimg{$lonhost}) {
 1629:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1630:                               '<span class="LC_nobreak"><label>'.
 1631:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1632:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1633:             } else {
 1634:                 $datatable .= $lt{'upl'};
 1635:             }
 1636:             $datatable .='<br />';
 1637:             if ($switchserver) {
 1638:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1639:             } else {
 1640:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1641:             }
 1642:             $datatable .= '</td>'.
 1643:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="20" '.
 1644:                           'value="'.$samlalt{$lonhost}.'" /></td>'.
 1645:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="8" '.
 1646:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1647:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="15">'.
 1648:                           $samltitle{$lonhost}.'</textarea></td>'.
 1649:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="8" '.
 1650:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1651:                           '</table></td>'.
 1652:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1653:            $itemcount ++;
 1654:         }
 1655:         $datatable .= '</table></td></tr>';
 1656:     }
 1657:     return $datatable;
 1658: }
 1659: 
 1660: sub login_choices {
 1661:     my %choices =
 1662:         &Apache::lonlocal::texthash (
 1663:             coursecatalog => 'Display Course/Community Catalog link?',
 1664:             adminmail     => "Display Administrator's E-mail Address?",
 1665:             helpdesk      => 'Display "Contact Helpdesk" link',
 1666:             disallowlogin => "Login page requests redirected",
 1667:             hostid        => "Server",
 1668:             server        => "Redirect to:",
 1669:             serverpath    => "Path",
 1670:             custompath    => "Custom", 
 1671:             exempt        => "Exempt IP(s)",
 1672:             directlogin   => "No redirect",
 1673:             newuser       => "Link to create a user account",
 1674:             img           => "Header",
 1675:             logo          => "Main Logo",
 1676:             domlogo       => "Domain Logo",
 1677:             login         => "Log-in Header", 
 1678:             textcol       => "Text color",
 1679:             bgcol         => "Box color",
 1680:             bgs           => "Background colors",
 1681:             links         => "Link colors",
 1682:             font          => "Font color",
 1683:             pgbg          => "Header",
 1684:             mainbg        => "Page",
 1685:             sidebg        => "Login box",
 1686:             link          => "Link",
 1687:             alink         => "Active link",
 1688:             vlink         => "Visited link",
 1689:             headtag       => "Custom markup",
 1690:             action        => "Action",
 1691:             current       => "Current",
 1692:             samllanding   => "Dual login?",
 1693:             samloptions   => "Options",
 1694:         );
 1695:     return %choices;
 1696: }
 1697: 
 1698: sub login_file_options {
 1699:       return &Apache::lonlocal::texthash(
 1700:                                            del     => 'Delete?',
 1701:                                            rep     => 'Replace:',
 1702:                                            upl     => 'Upload:',
 1703:                                            curr    => 'View contents',
 1704:                                            default => 'Default',
 1705:                                            custom  => 'Custom',
 1706:                                            none    => 'None',
 1707:       );
 1708: }
 1709: 
 1710: sub print_rolecolors {
 1711:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1712:     my %choices = &color_font_choices();
 1713:     my @bgs = ('pgbg','tabbg','sidebg');
 1714:     my @links = ('link','alink','vlink');
 1715:     my @images = ('img');
 1716:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1717:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1718:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1719:     my (%is_custom,%designs);
 1720:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1721:     if (ref($settings) eq 'HASH') {
 1722:         if (ref($settings->{$role}) eq 'HASH') {
 1723:             if ($settings->{$role}->{'img'} ne '') {
 1724:                 $designs{'img'} = $settings->{$role}->{'img'};
 1725:                 $is_custom{'img'} = 1;
 1726:             }
 1727:             if ($settings->{$role}->{'font'} ne '') {
 1728:                 $designs{'font'} = $settings->{$role}->{'font'};
 1729:                 $is_custom{'font'} = 1;
 1730:             }
 1731:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1732:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1733:                 $is_custom{'fontmenu'} = 1;
 1734:             }
 1735:             foreach my $item (@bgs) {
 1736:                 if ($settings->{$role}->{$item} ne '') {
 1737:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1738:                     $is_custom{$item} = 1;
 1739:                 }
 1740:             }
 1741:             foreach my $item (@links) {
 1742:                 if ($settings->{$role}->{$item} ne '') {
 1743:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1744:                     $is_custom{$item} = 1;
 1745:                 }
 1746:             }
 1747:         }
 1748:     } else {
 1749:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1750:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1751:             $is_custom{'img'} = 1;
 1752:         }
 1753:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1754:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1755:             $is_custom{'fontmenu'} = 1; 
 1756:         }
 1757:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1758:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1759:             $is_custom{'font'} = 1;
 1760:         }
 1761:         foreach my $item (@bgs) {
 1762:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1763:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1764:                 $is_custom{$item} = 1;
 1765:             
 1766:             }
 1767:         }
 1768:         foreach my $item (@links) {
 1769:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1770:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1771:                 $is_custom{$item} = 1;
 1772:             }
 1773:         }
 1774:     }
 1775:     my $itemcount = 1;
 1776:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1777:     $datatable .= '</tr></table></td></tr>';
 1778:     return $datatable;
 1779: }
 1780: 
 1781: sub role_defaults {
 1782:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1783:     my %defaults;
 1784:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1785:         return %defaults;
 1786:     }
 1787:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1788:     if ($role eq 'login') {
 1789:         %defaults = (
 1790:                        font => $defaultdesign{$role.'.font'},
 1791:                     );
 1792:         if (ref($logintext) eq 'ARRAY') {
 1793:             foreach my $item (@{$logintext}) {
 1794:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1795:             }
 1796:         }
 1797:         foreach my $item (@{$images}) {
 1798:             $defaults{'showlogo'}{$item} = 1;
 1799:         }
 1800:     } else {
 1801:         %defaults = (
 1802:                        img => $defaultdesign{$role.'.img'},
 1803:                        font => $defaultdesign{$role.'.font'},
 1804:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1805:                     );
 1806:     }
 1807:     foreach my $item (@{$bgs}) {
 1808:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1809:     }
 1810:     foreach my $item (@{$links}) {
 1811:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1812:     }
 1813:     foreach my $item (@{$images}) {
 1814:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1815:     }
 1816:     return %defaults;
 1817: }
 1818: 
 1819: sub display_color_options {
 1820:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1821:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1822:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1823:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1824:     my $datatable = '<tr'.$css_class.'>'.
 1825:         '<td>'.$choices->{'font'}.'</td>';
 1826:     if (!$is_custom->{'font'}) {
 1827:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1828:     } else {
 1829:         $datatable .= '<td>&nbsp;</td>';
 1830:     }
 1831:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1832: 
 1833:     $datatable .= '<td><span class="LC_nobreak">'.
 1834:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1835:                   ' value="'.$current_color.'" />&nbsp;'.
 1836:                   '&nbsp;</span></td></tr>';
 1837:     unless ($role eq 'login') { 
 1838:         $datatable .= '<tr'.$css_class.'>'.
 1839:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1840:         if (!$is_custom->{'fontmenu'}) {
 1841:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1842:         } else {
 1843:             $datatable .= '<td>&nbsp;</td>';
 1844:         }
 1845: 	$current_color = $designs->{'fontmenu'} ?
 1846: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1847:         $datatable .= '<td><span class="LC_nobreak">'.
 1848:                       '<input class="colorchooser" type="text" size="10" name="'
 1849: 		      .$role.'_fontmenu"'.
 1850:                       ' value="'.$current_color.'" />&nbsp;'.
 1851:                       '&nbsp;</span></td></tr>';
 1852:     }
 1853:     my $switchserver = &check_switchserver($dom,$confname);
 1854:     foreach my $img (@{$images}) {
 1855: 	$itemcount ++;
 1856:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1857:         $datatable .= '<tr'.$css_class.'>'.
 1858:                       '<td>'.$choices->{$img};
 1859:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1860:         if ($role eq 'login') {
 1861:             if ($img eq 'login') {
 1862:                 $login_hdr_pick =
 1863:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1864:                 $logincolors =
 1865:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1866:                                        $designs,$defaults);
 1867:             } elsif ($img ne 'domlogo') {
 1868:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1869:             }
 1870:         }
 1871:         $datatable .= '</td>';
 1872:         if ($designs->{$img} ne '') {
 1873:             $imgfile = $designs->{$img};
 1874: 	    $img_import = ($imgfile =~ m{^/adm/});
 1875:         } else {
 1876:             $imgfile = $defaults->{$img};
 1877:         }
 1878:         if ($imgfile) {
 1879:             my ($showfile,$fullsize);
 1880:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1881:                 my $urldir = $1;
 1882:                 my $filename = $2;
 1883:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1884:                 if (@info) {
 1885:                     my $thumbfile = 'tn-'.$filename;
 1886:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1887:                     if (@thumb) {
 1888:                         $showfile = $urldir.'/'.$thumbfile;
 1889:                     } else {
 1890:                         $showfile = $imgfile;
 1891:                     }
 1892:                 } else {
 1893:                     $showfile = '';
 1894:                 }
 1895:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1896:                 $showfile = $imgfile;
 1897:                 my $imgdir = $1;
 1898:                 my $filename = $2;
 1899:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1900:                     $showfile = "/$imgdir/tn-".$filename;
 1901:                 } else {
 1902:                     my $input = $londocroot.$imgfile;
 1903:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1904:                     if (!-e $output) {
 1905:                         my ($width,$height) = &thumb_dimensions();
 1906:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1907:                         if ($fullwidth ne '' && $fullheight ne '') {
 1908:                             if ($fullwidth > $width && $fullheight > $height) { 
 1909:                                 my $size = $width.'x'.$height;
 1910:                                 my @args = ('convert','-sample',$size,$input,$output);
 1911:                                 system({$args[0]} @args);
 1912:                                 $showfile = "/$imgdir/tn-".$filename;
 1913:                             }
 1914:                         }
 1915:                     }
 1916:                 }
 1917:             }
 1918:             if ($showfile) {
 1919:                 if ($showfile =~ m{^/(adm|res)/}) {
 1920:                     if ($showfile =~ m{^/res/}) {
 1921:                         my $local_showfile =
 1922:                             &Apache::lonnet::filelocation('',$showfile);
 1923:                         &Apache::lonnet::repcopy($local_showfile);
 1924:                     }
 1925:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1926:                 }
 1927:                 if ($imgfile) {
 1928:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1929:                         if ($imgfile =~ m{^/res/}) {
 1930:                             my $local_imgfile =
 1931:                                 &Apache::lonnet::filelocation('',$imgfile);
 1932:                             &Apache::lonnet::repcopy($local_imgfile);
 1933:                         }
 1934:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1935:                     } else {
 1936:                         $fullsize = $imgfile;
 1937:                     }
 1938:                 }
 1939:                 $datatable .= '<td>';
 1940:                 if ($img eq 'login') {
 1941:                     $datatable .= $login_hdr_pick;
 1942:                 } 
 1943:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1944:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1945:             } else {
 1946:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1947:                               &mt('Upload:').'<br />';
 1948:             }
 1949:         } else {
 1950:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1951:                           &mt('Upload:').'<br />';
 1952:         }
 1953:         if ($switchserver) {
 1954:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1955:         } else {
 1956:             if ($img ne 'login') { # suppress file selection for Log-in header
 1957:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1958:             }
 1959:         }
 1960:         $datatable .= '</td></tr>';
 1961:     }
 1962:     $itemcount ++;
 1963:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1964:     $datatable .= '<tr'.$css_class.'>'.
 1965:                   '<td>'.$choices->{'bgs'}.'</td>';
 1966:     my $bgs_def;
 1967:     foreach my $item (@{$bgs}) {
 1968:         if (!$is_custom->{$item}) {
 1969:             $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>';
 1970:         }
 1971:     }
 1972:     if ($bgs_def) {
 1973:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1974:     } else {
 1975:         $datatable .= '<td>&nbsp;</td>';
 1976:     }
 1977:     $datatable .= '<td class="LC_right_item">'.
 1978:                   '<table border="0"><tr>';
 1979: 
 1980:     foreach my $item (@{$bgs}) {
 1981:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1982: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1983:         if ($designs->{'bgs'}{$item}) {
 1984:             $datatable .= '&nbsp;';
 1985:         }
 1986:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1987:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1988:     }
 1989:     $datatable .= '</tr></table></td></tr>';
 1990:     $itemcount ++;
 1991:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1992:     $datatable .= '<tr'.$css_class.'>'.
 1993:                   '<td>'.$choices->{'links'}.'</td>';
 1994:     my $links_def;
 1995:     foreach my $item (@{$links}) {
 1996:         if (!$is_custom->{$item}) {
 1997:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1998:         }
 1999:     }
 2000:     if ($links_def) {
 2001:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2002:     } else {
 2003:         $datatable .= '<td>&nbsp;</td>';
 2004:     }
 2005:     $datatable .= '<td class="LC_right_item">'.
 2006:                   '<table border="0"><tr>';
 2007:     foreach my $item (@{$links}) {
 2008: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2009:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2010:         if ($designs->{'links'}{$item}) {
 2011:             $datatable.='&nbsp;';
 2012:         }
 2013:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2014:                       '" /></td>';
 2015:     }
 2016:     $$rowtotal += $itemcount;
 2017:     return $datatable;
 2018: }
 2019: 
 2020: sub logo_display_options {
 2021:     my ($img,$defaults,$designs) = @_;
 2022:     my $checkedon;
 2023:     if (ref($defaults) eq 'HASH') {
 2024:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2025:             if ($defaults->{'showlogo'}{$img}) {
 2026:                 $checkedon = 'checked="checked" ';     
 2027:             }
 2028:         } 
 2029:     }
 2030:     if (ref($designs) eq 'HASH') {
 2031:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2032:             if (defined($designs->{'showlogo'}{$img})) {
 2033:                 if ($designs->{'showlogo'}{$img} == 0) {
 2034:                     $checkedon = '';
 2035:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2036:                     $checkedon = 'checked="checked" ';
 2037:                 }
 2038:             }
 2039:         }
 2040:     }
 2041:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2042:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2043:            &mt('show').'</label>'."\n";
 2044: }
 2045: 
 2046: sub login_header_options  {
 2047:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2048:     my $output = '';
 2049:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2050:         $output .= &mt('Text default(s):').'<br />';
 2051:         if (!$is_custom->{'textcol'}) {
 2052:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2053:                        '&nbsp;&nbsp;&nbsp;';
 2054:         }
 2055:         if (!$is_custom->{'bgcol'}) {
 2056:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2057:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2058:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2059:         }
 2060:         $output .= '<br />';
 2061:     }
 2062:     $output .='<br />';
 2063:     return $output;
 2064: }
 2065: 
 2066: sub login_text_colors {
 2067:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2068:     my $color_menu = '<table border="0"><tr>';
 2069:     foreach my $item (@{$logintext}) {
 2070:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2071:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2072:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2073:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2074:     }
 2075:     $color_menu .= '</tr></table><br />';
 2076:     return $color_menu;
 2077: }
 2078: 
 2079: sub image_changes {
 2080:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2081:     my $output;
 2082:     if ($img eq 'login') {
 2083:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2084:     } elsif (!$is_custom) {
 2085:         if ($img ne 'domlogo') {
 2086:             $output = &mt('Default image:').'<br />';
 2087:         } else {
 2088:             $output = &mt('Default in use:').'<br />';
 2089:         }
 2090:     }
 2091:     if ($img ne 'login') {
 2092:         if ($img_import) {
 2093:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2094:         }
 2095:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2096:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2097:         if ($is_custom) {
 2098:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2099:                        '<input type="checkbox" name="'.
 2100:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2101:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2102:         } else {
 2103:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2104:         }
 2105:     }
 2106:     return $output;
 2107: }
 2108: 
 2109: sub print_quotas {
 2110:     my ($dom,$settings,$rowtotal,$action) = @_;
 2111:     my $context;
 2112:     if ($action eq 'quotas') {
 2113:         $context = 'tools';
 2114:     } else {
 2115:         $context = $action;
 2116:     }
 2117:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2118:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2119:     my $typecount = 0;
 2120:     my ($css_class,%titles);
 2121:     if ($context eq 'requestcourses') {
 2122:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2123:         @options =('norequest','approval','validate','autolimit');
 2124:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2125:         %titles = &courserequest_titles();
 2126:     } elsif ($context eq 'requestauthor') {
 2127:         @usertools = ('author');
 2128:         @options = ('norequest','approval','automatic');
 2129:         %titles = &authorrequest_titles();
 2130:     } else {
 2131:         @usertools = ('aboutme','blog','webdav','portfolio');
 2132:         %titles = &tool_titles();
 2133:     }
 2134:     if (ref($types) eq 'ARRAY') {
 2135:         foreach my $type (@{$types}) {
 2136:             my ($currdefquota,$currauthorquota);
 2137:             unless (($context eq 'requestcourses') ||
 2138:                     ($context eq 'requestauthor')) {
 2139:                 if (ref($settings) eq 'HASH') {
 2140:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2141:                         $currdefquota = $settings->{defaultquota}->{$type};
 2142:                     } else {
 2143:                         $currdefquota = $settings->{$type};
 2144:                     }
 2145:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2146:                         $currauthorquota = $settings->{authorquota}->{$type};
 2147:                     }
 2148:                 }
 2149:             }
 2150:             if (defined($usertypes->{$type})) {
 2151:                 $typecount ++;
 2152:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2153:                 $datatable .= '<tr'.$css_class.'>'.
 2154:                               '<td>'.$usertypes->{$type}.'</td>'.
 2155:                               '<td class="LC_left_item">';
 2156:                 if ($context eq 'requestcourses') {
 2157:                     $datatable .= '<table><tr>';
 2158:                 }
 2159:                 my %cell;  
 2160:                 foreach my $item (@usertools) {
 2161:                     if ($context eq 'requestcourses') {
 2162:                         my ($curroption,$currlimit);
 2163:                         if (ref($settings) eq 'HASH') {
 2164:                             if (ref($settings->{$item}) eq 'HASH') {
 2165:                                 $curroption = $settings->{$item}->{$type};
 2166:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2167:                                     $currlimit = $1; 
 2168:                                 }
 2169:                             }
 2170:                         }
 2171:                         if (!$curroption) {
 2172:                             $curroption = 'norequest';
 2173:                         }
 2174:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2175:                         foreach my $option (@options) {
 2176:                             my $val = $option;
 2177:                             if ($option eq 'norequest') {
 2178:                                 $val = 0;  
 2179:                             }
 2180:                             if ($option eq 'validate') {
 2181:                                 my $canvalidate = 0;
 2182:                                 if (ref($validations{$item}) eq 'HASH') { 
 2183:                                     if ($validations{$item}{$type}) {
 2184:                                         $canvalidate = 1;
 2185:                                     }
 2186:                                 }
 2187:                                 next if (!$canvalidate);
 2188:                             }
 2189:                             my $checked = '';
 2190:                             if ($option eq $curroption) {
 2191:                                 $checked = ' checked="checked"';
 2192:                             } elsif ($option eq 'autolimit') {
 2193:                                 if ($curroption =~ /^autolimit/) {
 2194:                                     $checked = ' checked="checked"';
 2195:                                 }                       
 2196:                             } 
 2197:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2198:                                   '<input type="radio" name="crsreq_'.$item.
 2199:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2200:                                   $titles{$option}.'</label>';
 2201:                             if ($option eq 'autolimit') {
 2202:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2203:                                                 $item.'_limit_'.$type.'" size="1" '.
 2204:                                                 'value="'.$currlimit.'" />';
 2205:                             }
 2206:                             $cell{$item} .= '</span> ';
 2207:                             if ($option eq 'autolimit') {
 2208:                                 $cell{$item} .= $titles{'unlimited'};
 2209:                             }
 2210:                         }
 2211:                     } elsif ($context eq 'requestauthor') {
 2212:                         my $curroption;
 2213:                         if (ref($settings) eq 'HASH') {
 2214:                             $curroption = $settings->{$type};
 2215:                         }
 2216:                         if (!$curroption) {
 2217:                             $curroption = 'norequest';
 2218:                         }
 2219:                         foreach my $option (@options) {
 2220:                             my $val = $option;
 2221:                             if ($option eq 'norequest') {
 2222:                                 $val = 0;
 2223:                             }
 2224:                             my $checked = '';
 2225:                             if ($option eq $curroption) {
 2226:                                 $checked = ' checked="checked"';
 2227:                             }
 2228:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2229:                                   '<input type="radio" name="authorreq_'.$type.
 2230:                                   '" value="'.$val.'"'.$checked.' />'.
 2231:                                   $titles{$option}.'</label></span>&nbsp; ';
 2232:                         }
 2233:                     } else {
 2234:                         my $checked = 'checked="checked" ';
 2235:                         if (ref($settings) eq 'HASH') {
 2236:                             if (ref($settings->{$item}) eq 'HASH') {
 2237:                                 if ($settings->{$item}->{$type} == 0) {
 2238:                                     $checked = '';
 2239:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2240:                                     $checked =  'checked="checked" ';
 2241:                                 }
 2242:                             }
 2243:                         }
 2244:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2245:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2246:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2247:                                       '</label></span>&nbsp; ';
 2248:                     }
 2249:                 }
 2250:                 if ($context eq 'requestcourses') {
 2251:                     $datatable .= '</tr><tr>';
 2252:                     foreach my $item (@usertools) {
 2253:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2254:                     }
 2255:                     $datatable .= '</tr></table>';
 2256:                 }
 2257:                 $datatable .= '</td>';
 2258:                 unless (($context eq 'requestcourses') ||
 2259:                         ($context eq 'requestauthor')) {
 2260:                     $datatable .= 
 2261:                               '<td class="LC_right_item">'.
 2262:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2263:                               '<input type="text" name="quota_'.$type.
 2264:                               '" value="'.$currdefquota.
 2265:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2266:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2267:                               '<input type="text" name="authorquota_'.$type.
 2268:                               '" value="'.$currauthorquota.
 2269:                               '" size="5" /></span></td>';
 2270:                 }
 2271:                 $datatable .= '</tr>';
 2272:             }
 2273:         }
 2274:     }
 2275:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2276:         $defaultquota = '20';
 2277:         $authorquota = '500';
 2278:         if (ref($settings) eq 'HASH') {
 2279:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2280:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2281:             } elsif (defined($settings->{'default'})) {
 2282:                 $defaultquota = $settings->{'default'};
 2283:             }
 2284:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2285:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2286:             }
 2287:         }
 2288:     }
 2289:     $typecount ++;
 2290:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2291:     $datatable .= '<tr'.$css_class.'>'.
 2292:                   '<td>'.$othertitle.'</td>'.
 2293:                   '<td class="LC_left_item">';
 2294:     if ($context eq 'requestcourses') {
 2295:         $datatable .= '<table><tr>';
 2296:     }
 2297:     my %defcell;
 2298:     foreach my $item (@usertools) {
 2299:         if ($context eq 'requestcourses') {
 2300:             my ($curroption,$currlimit);
 2301:             if (ref($settings) eq 'HASH') {
 2302:                 if (ref($settings->{$item}) eq 'HASH') {
 2303:                     $curroption = $settings->{$item}->{'default'};
 2304:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2305:                         $currlimit = $1;
 2306:                     }
 2307:                 }
 2308:             }
 2309:             if (!$curroption) {
 2310:                 $curroption = 'norequest';
 2311:             }
 2312:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2313:             foreach my $option (@options) {
 2314:                 my $val = $option;
 2315:                 if ($option eq 'norequest') {
 2316:                     $val = 0;
 2317:                 }
 2318:                 if ($option eq 'validate') {
 2319:                     my $canvalidate = 0;
 2320:                     if (ref($validations{$item}) eq 'HASH') {
 2321:                         if ($validations{$item}{'default'}) {
 2322:                             $canvalidate = 1;
 2323:                         }
 2324:                     }
 2325:                     next if (!$canvalidate);
 2326:                 }
 2327:                 my $checked = '';
 2328:                 if ($option eq $curroption) {
 2329:                     $checked = ' checked="checked"';
 2330:                 } elsif ($option eq 'autolimit') {
 2331:                     if ($curroption =~ /^autolimit/) {
 2332:                         $checked = ' checked="checked"';
 2333:                     }
 2334:                 }
 2335:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2336:                                   '<input type="radio" name="crsreq_'.$item.
 2337:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2338:                                   $titles{$option}.'</label>';
 2339:                 if ($option eq 'autolimit') {
 2340:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2341:                                        $item.'_limit_default" size="1" '.
 2342:                                        'value="'.$currlimit.'" />';
 2343:                 }
 2344:                 $defcell{$item} .= '</span> ';
 2345:                 if ($option eq 'autolimit') {
 2346:                     $defcell{$item} .= $titles{'unlimited'};
 2347:                 }
 2348:             }
 2349:         } elsif ($context eq 'requestauthor') {
 2350:             my $curroption;
 2351:             if (ref($settings) eq 'HASH') {
 2352:                 $curroption = $settings->{'default'};
 2353:             }
 2354:             if (!$curroption) {
 2355:                 $curroption = 'norequest';
 2356:             }
 2357:             foreach my $option (@options) {
 2358:                 my $val = $option;
 2359:                 if ($option eq 'norequest') {
 2360:                     $val = 0;
 2361:                 }
 2362:                 my $checked = '';
 2363:                 if ($option eq $curroption) {
 2364:                     $checked = ' checked="checked"';
 2365:                 }
 2366:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2367:                               '<input type="radio" name="authorreq_default"'.
 2368:                               ' value="'.$val.'"'.$checked.' />'.
 2369:                               $titles{$option}.'</label></span>&nbsp; ';
 2370:             }
 2371:         } else {
 2372:             my $checked = 'checked="checked" ';
 2373:             if (ref($settings) eq 'HASH') {
 2374:                 if (ref($settings->{$item}) eq 'HASH') {
 2375:                     if ($settings->{$item}->{'default'} == 0) {
 2376:                         $checked = '';
 2377:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2378:                         $checked = 'checked="checked" ';
 2379:                     }
 2380:                 }
 2381:             }
 2382:             $datatable .= '<span class="LC_nobreak"><label>'.
 2383:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2384:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2385:                           '</label></span>&nbsp; ';
 2386:         }
 2387:     }
 2388:     if ($context eq 'requestcourses') {
 2389:         $datatable .= '</tr><tr>';
 2390:         foreach my $item (@usertools) {
 2391:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2392:         }
 2393:         $datatable .= '</tr></table>';
 2394:     }
 2395:     $datatable .= '</td>';
 2396:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2397:         $datatable .= '<td class="LC_right_item">'.
 2398:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2399:                       '<input type="text" name="defaultquota" value="'.
 2400:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2401:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2402:                       '<input type="text" name="authorquota" value="'.
 2403:                       $authorquota.'" size="5" /></span></td>';
 2404:     }
 2405:     $datatable .= '</tr>';
 2406:     $typecount ++;
 2407:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2408:     $datatable .= '<tr'.$css_class.'>'.
 2409:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2410:     if ($context eq 'requestcourses') {
 2411:         $datatable .= &mt('(overrides affiliation, if set)').
 2412:                       '</td>'.
 2413:                       '<td class="LC_left_item">'.
 2414:                       '<table><tr>';
 2415:     } else {
 2416:         $datatable .= &mt('(overrides affiliation, if checked)').
 2417:                       '</td>'.
 2418:                       '<td class="LC_left_item" colspan="2">'.
 2419:                       '<br />';
 2420:     }
 2421:     my %advcell;
 2422:     foreach my $item (@usertools) {
 2423:         if ($context eq 'requestcourses') {
 2424:             my ($curroption,$currlimit);
 2425:             if (ref($settings) eq 'HASH') {
 2426:                 if (ref($settings->{$item}) eq 'HASH') {
 2427:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2428:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2429:                         $currlimit = $1;
 2430:                     }
 2431:                 }
 2432:             }
 2433:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2434:             my $checked = '';
 2435:             if ($curroption eq '') {
 2436:                 $checked = ' checked="checked"';
 2437:             }
 2438:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2439:                                '<input type="radio" name="crsreq_'.$item.
 2440:                                '__LC_adv" value=""'.$checked.' />'.
 2441:                                &mt('No override set').'</label></span>&nbsp; ';
 2442:             foreach my $option (@options) {
 2443:                 my $val = $option;
 2444:                 if ($option eq 'norequest') {
 2445:                     $val = 0;
 2446:                 }
 2447:                 if ($option eq 'validate') {
 2448:                     my $canvalidate = 0;
 2449:                     if (ref($validations{$item}) eq 'HASH') {
 2450:                         if ($validations{$item}{'_LC_adv'}) {
 2451:                             $canvalidate = 1;
 2452:                         }
 2453:                     }
 2454:                     next if (!$canvalidate);
 2455:                 }
 2456:                 my $checked = '';
 2457:                 if ($val eq $curroption) {
 2458:                     $checked = ' checked="checked"';
 2459:                 } elsif ($option eq 'autolimit') {
 2460:                     if ($curroption =~ /^autolimit/) {
 2461:                         $checked = ' checked="checked"';
 2462:                     }
 2463:                 }
 2464:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2465:                                   '<input type="radio" name="crsreq_'.$item.
 2466:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2467:                                   $titles{$option}.'</label>';
 2468:                 if ($option eq 'autolimit') {
 2469:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2470:                                        $item.'_limit__LC_adv" size="1" '.
 2471:                                        'value="'.$currlimit.'" />';
 2472:                 }
 2473:                 $advcell{$item} .= '</span> ';
 2474:                 if ($option eq 'autolimit') {
 2475:                     $advcell{$item} .= $titles{'unlimited'};
 2476:                 }
 2477:             }
 2478:         } elsif ($context eq 'requestauthor') {
 2479:             my $curroption;
 2480:             if (ref($settings) eq 'HASH') {
 2481:                 $curroption = $settings->{'_LC_adv'};
 2482:             }
 2483:             my $checked = '';
 2484:             if ($curroption eq '') {
 2485:                 $checked = ' checked="checked"';
 2486:             }
 2487:             $datatable .= '<span class="LC_nobreak"><label>'.
 2488:                           '<input type="radio" name="authorreq__LC_adv"'.
 2489:                           ' value=""'.$checked.' />'.
 2490:                           &mt('No override set').'</label></span>&nbsp; ';
 2491:             foreach my $option (@options) {
 2492:                 my $val = $option;
 2493:                 if ($option eq 'norequest') {
 2494:                     $val = 0;
 2495:                 }
 2496:                 my $checked = '';
 2497:                 if ($val eq $curroption) {
 2498:                     $checked = ' checked="checked"';
 2499:                 }
 2500:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2501:                               '<input type="radio" name="authorreq__LC_adv"'.
 2502:                               ' value="'.$val.'"'.$checked.' />'.
 2503:                               $titles{$option}.'</label></span>&nbsp; ';
 2504:             }
 2505:         } else {
 2506:             my $checked = 'checked="checked" ';
 2507:             if (ref($settings) eq 'HASH') {
 2508:                 if (ref($settings->{$item}) eq 'HASH') {
 2509:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2510:                         $checked = '';
 2511:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2512:                         $checked = 'checked="checked" ';
 2513:                     }
 2514:                 }
 2515:             }
 2516:             $datatable .= '<span class="LC_nobreak"><label>'.
 2517:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2518:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2519:                           '</label></span>&nbsp; ';
 2520:         }
 2521:     }
 2522:     if ($context eq 'requestcourses') {
 2523:         $datatable .= '</tr><tr>';
 2524:         foreach my $item (@usertools) {
 2525:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2526:         }
 2527:         $datatable .= '</tr></table>';
 2528:     }
 2529:     $datatable .= '</td></tr>';
 2530:     $$rowtotal += $typecount;
 2531:     return $datatable;
 2532: }
 2533: 
 2534: sub print_requestmail {
 2535:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2536:     my ($now,$datatable,%currapp);
 2537:     $now = time;
 2538:     if (ref($settings) eq 'HASH') {
 2539:         if (ref($settings->{'notify'}) eq 'HASH') {
 2540:             if ($settings->{'notify'}{'approval'} ne '') {
 2541:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2542:             }
 2543:         }
 2544:     }
 2545:     my $numinrow = 2;
 2546:     my $css_class;
 2547:     if ($$rowtotal%2) {
 2548:         $css_class = 'LC_odd_row';
 2549:     }
 2550:     if ($customcss) {
 2551:         $css_class .= " $customcss";
 2552:     }
 2553:     $css_class =~ s/^\s+//;
 2554:     if ($css_class) {
 2555:         $css_class = ' class="'.$css_class.'"';
 2556:     }
 2557:     if ($rowstyle) {
 2558:         $css_class .= ' style="'.$rowstyle.'"';
 2559:     }
 2560:     my $text;
 2561:     if ($action eq 'requestcourses') {
 2562:         $text = &mt('Receive notification of course requests requiring approval');
 2563:     } elsif ($action eq 'requestauthor') {
 2564:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2565:     } else {
 2566:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2567:     }
 2568:     $datatable = '<tr'.$css_class.'>'.
 2569:                  ' <td>'.$text.'</td>'.
 2570:                  ' <td class="LC_left_item">';
 2571:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2572:                                                  $action.'notifyapproval',%currapp);
 2573:     if ($numdc > 0) {
 2574:         $datatable .= $table;
 2575:     } else {
 2576:         $datatable .= &mt('There are no active Domain Coordinators');
 2577:     }
 2578:     $datatable .='</td></tr>';
 2579:     return $datatable;
 2580: }
 2581: 
 2582: sub print_studentcode {
 2583:     my ($settings,$rowtotal) = @_;
 2584:     my $rownum = 0; 
 2585:     my ($output,%current);
 2586:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2587:     if (ref($settings) eq 'HASH') {
 2588:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2589:             foreach my $type (@crstypes) {
 2590:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2591:             }
 2592:         }
 2593:     }
 2594:     $output .= '<tr>'.
 2595:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2596:                '<td class="LC_left_item">';
 2597:     foreach my $type (@crstypes) {
 2598:         my $check = ' ';
 2599:         if ($current{$type}) {
 2600:             $check = ' checked="checked" ';
 2601:         }
 2602:         $output .= '<span class="LC_nobreak"><label>'.
 2603:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2604:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2605:     }
 2606:     $output .= '</td></tr>';
 2607:     $$rowtotal ++;
 2608:     return $output;
 2609: }
 2610: 
 2611: sub print_textbookcourses {
 2612:     my ($dom,$type,$settings,$rowtotal) = @_;
 2613:     my $rownum = 0;
 2614:     my $css_class;
 2615:     my $itemcount = 1;
 2616:     my $maxnum = 0;
 2617:     my $bookshash;
 2618:     if (ref($settings) eq 'HASH') {
 2619:         $bookshash = $settings->{$type};
 2620:     }
 2621:     my %ordered;
 2622:     if (ref($bookshash) eq 'HASH') {
 2623:         foreach my $item (keys(%{$bookshash})) {
 2624:             if (ref($bookshash->{$item}) eq 'HASH') {
 2625:                 my $num = $bookshash->{$item}{'order'};
 2626:                 $ordered{$num} = $item;
 2627:             }
 2628:         }
 2629:     }
 2630:     my $confname = $dom.'-domainconfig';
 2631:     my $switchserver = &check_switchserver($dom,$confname);
 2632:     my $maxnum = scalar(keys(%ordered));
 2633:     my $datatable;
 2634:     if (keys(%ordered)) {
 2635:         my @items = sort { $a <=> $b } keys(%ordered);
 2636:         for (my $i=0; $i<@items; $i++) {
 2637:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2638:             my $key = $ordered{$items[$i]};
 2639:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2640:             my $coursetitle = $coursehash{'description'};
 2641:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2642:             if (ref($bookshash->{$key}) eq 'HASH') {
 2643:                 $subject = $bookshash->{$key}->{'subject'};
 2644:                 $title = $bookshash->{$key}->{'title'};
 2645:                 if ($type eq 'textbooks') {
 2646:                     $publisher = $bookshash->{$key}->{'publisher'};
 2647:                     $author = $bookshash->{$key}->{'author'};
 2648:                     $image = $bookshash->{$key}->{'image'};
 2649:                     if ($image ne '') {
 2650:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2651:                         my $imagethumb = "$path/tn-".$imagefile;
 2652:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2653:                     }
 2654:                 }
 2655:             }
 2656:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2657:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2658:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2659:             for (my $k=0; $k<=$maxnum; $k++) {
 2660:                 my $vpos = $k+1;
 2661:                 my $selstr;
 2662:                 if ($k == $i) {
 2663:                     $selstr = ' selected="selected" ';
 2664:                 }
 2665:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2666:             }
 2667:             $datatable .= '</select>'.('&nbsp;'x2).
 2668:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2669:                 &mt('Delete?').'</label></span></td>'.
 2670:                 '<td colspan="2">'.
 2671:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2672:                 ('&nbsp;'x2).
 2673:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2674:             if ($type eq 'textbooks') {
 2675:                 $datatable .= ('&nbsp;'x2).
 2676:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2677:                               ('&nbsp;'x2).
 2678:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2679:                               ('&nbsp;'x2).
 2680:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2681:                 if ($image) {
 2682:                     $datatable .= $imgsrc.
 2683:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2684:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2685:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2686:                 }
 2687:                 if ($switchserver) {
 2688:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2689:                 } else {
 2690:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2691:                 }
 2692:             }
 2693:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2694:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2695:                           $coursetitle.'</span></td></tr>'."\n";
 2696:             $itemcount ++;
 2697:         }
 2698:     }
 2699:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2700:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2701:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2702:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2703:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2704:     for (my $k=0; $k<$maxnum+1; $k++) {
 2705:         my $vpos = $k+1;
 2706:         my $selstr;
 2707:         if ($k == $maxnum) {
 2708:             $selstr = ' selected="selected" ';
 2709:         }
 2710:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2711:     }
 2712:     $datatable .= '</select>&nbsp;'."\n".
 2713:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2714:                   '<td colspan="2">'.
 2715:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2716:                   ('&nbsp;'x2).
 2717:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2718:                   ('&nbsp;'x2);
 2719:     if ($type eq 'textbooks') {
 2720:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2721:                       ('&nbsp;'x2).
 2722:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2723:                       ('&nbsp;'x2).
 2724:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2725:         if ($switchserver) {
 2726:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2727:         } else {
 2728:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2729:         }
 2730:         $datatable .= '</span>'."\n";
 2731:     }
 2732:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2733:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2734:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2735:                   &Apache::loncommon::selectcourse_link
 2736:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2737:                   '</span></td>'."\n".
 2738:                   '</tr>'."\n";
 2739:     $itemcount ++;
 2740:     return $datatable;
 2741: }
 2742: 
 2743: sub textbookcourses_javascript {
 2744:     my ($settings) = @_;
 2745:     return unless(ref($settings) eq 'HASH');
 2746:     my (%ordered,%total,%jstext);
 2747:     foreach my $type ('textbooks','templates') {
 2748:         $total{$type} = 0;
 2749:         if (ref($settings->{$type}) eq 'HASH') {
 2750:             foreach my $item (keys(%{$settings->{$type}})) {
 2751:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2752:                     my $num = $settings->{$type}->{$item}{'order'};
 2753:                     $ordered{$type}{$num} = $item;
 2754:                 }
 2755:             }
 2756:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2757:         }
 2758:         my @jsarray = ();
 2759:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2760:             push(@jsarray,$ordered{$type}{$item});
 2761:         }
 2762:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2763:     }
 2764:     return <<"ENDSCRIPT";
 2765: <script type="text/javascript">
 2766: // <![CDATA[
 2767: function reorderBooks(form,item,caller) {
 2768:     var changedVal;
 2769: $jstext{'textbooks'};
 2770: $jstext{'templates'};
 2771:     var newpos;
 2772:     var maxh;
 2773:     if (caller == 'textbooks') {  
 2774:         newpos = 'textbooks_addbook_pos';
 2775:         maxh = 1 + $total{'textbooks'};
 2776:     } else {
 2777:         newpos = 'templates_addbook_pos';
 2778:         maxh = 1 + $total{'templates'};
 2779:     }
 2780:     var current = new Array;
 2781:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2782:     if (item == newpos) {
 2783:         changedVal = newitemVal;
 2784:     } else {
 2785:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2786:         current[newitemVal] = newpos;
 2787:     }
 2788:     if (caller == 'textbooks') {
 2789:         for (var i=0; i<textbooks.length; i++) {
 2790:             var elementName = 'textbooks_'+textbooks[i];
 2791:             if (elementName != item) {
 2792:                 if (form.elements[elementName]) {
 2793:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2794:                     current[currVal] = elementName;
 2795:                 }
 2796:             }
 2797:         }
 2798:     }
 2799:     if (caller == 'templates') {
 2800:         for (var i=0; i<templates.length; i++) {
 2801:             var elementName = 'templates_'+templates[i];
 2802:             if (elementName != item) {
 2803:                 if (form.elements[elementName]) {
 2804:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2805:                     current[currVal] = elementName;
 2806:                 }
 2807:             }
 2808:         }
 2809:     }
 2810:     var oldVal;
 2811:     for (var j=0; j<maxh; j++) {
 2812:         if (current[j] == undefined) {
 2813:             oldVal = j;
 2814:         }
 2815:     }
 2816:     if (oldVal < changedVal) {
 2817:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2818:            var elementName = current[k];
 2819:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2820:         }
 2821:     } else {
 2822:         for (var k=changedVal; k<oldVal; k++) {
 2823:             var elementName = current[k];
 2824:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2825:         }
 2826:     }
 2827:     return;
 2828: }
 2829: 
 2830: // ]]>
 2831: </script>
 2832: 
 2833: ENDSCRIPT
 2834: }
 2835: 
 2836: sub ltitools_javascript {
 2837:     my ($settings) = @_;
 2838:     my $togglejs = &ltitools_toggle_js();
 2839:     unless (ref($settings) eq 'HASH') {
 2840:         return $togglejs;
 2841:     }
 2842:     my (%ordered,$total,%jstext);
 2843:     $total = 0;
 2844:     foreach my $item (keys(%{$settings})) {
 2845:         if (ref($settings->{$item}) eq 'HASH') {
 2846:             my $num = $settings->{$item}{'order'};
 2847:             $ordered{$num} = $item;
 2848:         }
 2849:     }
 2850:     $total = scalar(keys(%{$settings}));
 2851:     my @jsarray = ();
 2852:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2853:         push(@jsarray,$ordered{$item});
 2854:     }
 2855:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2856:     return <<"ENDSCRIPT";
 2857: <script type="text/javascript">
 2858: // <![CDATA[
 2859: function reorderLTITools(form,item) {
 2860:     var changedVal;
 2861: $jstext
 2862:     var newpos = 'ltitools_add_pos';
 2863:     var maxh = 1 + $total;
 2864:     var current = new Array;
 2865:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2866:     if (item == newpos) {
 2867:         changedVal = newitemVal;
 2868:     } else {
 2869:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2870:         current[newitemVal] = newpos;
 2871:     }
 2872:     for (var i=0; i<ltitools.length; i++) {
 2873:         var elementName = 'ltitools_'+ltitools[i];
 2874:         if (elementName != item) {
 2875:             if (form.elements[elementName]) {
 2876:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2877:                 current[currVal] = elementName;
 2878:             }
 2879:         }
 2880:     }
 2881:     var oldVal;
 2882:     for (var j=0; j<maxh; j++) {
 2883:         if (current[j] == undefined) {
 2884:             oldVal = j;
 2885:         }
 2886:     }
 2887:     if (oldVal < changedVal) {
 2888:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2889:            var elementName = current[k];
 2890:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2891:         }
 2892:     } else {
 2893:         for (var k=changedVal; k<oldVal; k++) {
 2894:             var elementName = current[k];
 2895:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2896:         }
 2897:     }
 2898:     return;
 2899: }
 2900: 
 2901: // ]]>
 2902: </script>
 2903: 
 2904: $togglejs
 2905: 
 2906: ENDSCRIPT
 2907: }
 2908: 
 2909: sub ltitools_toggle_js {
 2910:     return <<"ENDSCRIPT";
 2911: <script type="text/javascript">
 2912: // <![CDATA[
 2913: 
 2914: function toggleLTITools(form,setting,item) {
 2915:     var radioname = '';
 2916:     var divid = '';
 2917:     if ((setting == 'passback') || (setting == 'roster')) {
 2918:         radioname = 'ltitools_'+setting+'_'+item;
 2919:         divid = 'ltitools_'+setting+'time_'+item;
 2920:         var num = form.elements[radioname].length;
 2921:         if (num) {
 2922:             var setvis = '';
 2923:             for (var i=0; i<num; i++) {
 2924:                 if (form.elements[radioname][i].checked) {
 2925:                     if (form.elements[radioname][i].value == '1') {
 2926:                         if (document.getElementById(divid)) {
 2927:                             document.getElementById(divid).style.display = 'inline-block';
 2928:                         }
 2929:                         setvis = 1;
 2930:                     }
 2931:                     break;
 2932:                 }
 2933:             }
 2934:         }
 2935:         if (!setvis) {
 2936:             if (document.getElementById(divid)) {
 2937:                 document.getElementById(divid).style.display = 'none';
 2938:             }
 2939:         }
 2940:     }
 2941:     if (setting == 'user') {
 2942:         divid = 'ltitools_'+setting+'_div_'+item;
 2943:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2944:         if (document.getElementById(divid)) {
 2945:             if (document.getElementById(checkid)) {
 2946:                 if (document.getElementById(checkid).checked) {
 2947:                     document.getElementById(divid).style.display = 'inline-block';
 2948:                 } else {
 2949:                     document.getElementById(divid).style.display = 'none';
 2950:                 }
 2951:             }
 2952:         }
 2953:     }
 2954:     return;
 2955: }
 2956: // ]]>
 2957: </script>
 2958: 
 2959: ENDSCRIPT
 2960: }
 2961: 
 2962: sub wafproxy_javascript {
 2963:     my ($dom) = @_;
 2964:     return <<"ENDSCRIPT";
 2965: <script type="text/javascript">
 2966: // <![CDATA[
 2967: function updateWAF() {
 2968:     if (document.getElementById('wafproxy_remoteip')) {
 2969:         var wafremote = 0;
 2970:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 2971:             wafremote = 1;
 2972:         }
 2973:         var fields = new Array('header','trust');
 2974:         for (var i=0; i<fields.length; i++) {
 2975:             if (document.getElementById('wafproxy_'+fields[i])) {
 2976:                 if (wafremote == 1) {
 2977:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 2978:                 }
 2979:                 else {
 2980:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 2981:                 }
 2982:             }
 2983:         }
 2984:         if (document.getElementById('wafproxyranges_$dom')) {
 2985:             if (wafremote == 1) {
 2986:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 2987:             } else {
 2988:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 2989:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 2990:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 2991:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 2992:                         }
 2993:                     }
 2994:                 }
 2995:             }
 2996:         }
 2997:     }
 2998:     return;
 2999: }
 3000: 
 3001: function checkWAF() {
 3002:     if (document.getElementById('wafproxy_remoteip')) {
 3003:         var wafvpn = 0;
 3004:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3005:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3006:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3007:                     wafvpn = 1;
 3008:                 }
 3009:                 break;
 3010:             }
 3011:         }
 3012:         var vpn = new Array('vpnint','vpnext');
 3013:         for (var i=0; i<vpn.length; i++) {
 3014:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3015:                 if (wafvpn == 1) {
 3016:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3017:                 }
 3018:                 else {
 3019:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3020:                 }
 3021:             }
 3022:         }
 3023:         if (document.getElementById('wafproxyranges_$dom')) {
 3024:             if (wafvpn == 1) {
 3025:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3026:             }
 3027:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3028:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3029:             }
 3030:         }
 3031:     }
 3032:     return;
 3033: }
 3034: 
 3035: function toggleWAF() {
 3036:     if (document.getElementById('wafproxy_table')) {
 3037:         var wafproxy = 0;
 3038:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3039:              if (document.display.wafproxy_${dom}[i].checked) {
 3040:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3041:                      wafproxy = 1;
 3042:                      break;
 3043:                 }
 3044:             }
 3045:         }
 3046:         if (wafproxy == 1) {
 3047:             document.getElementById('wafproxy_table').style.display='inline';
 3048:         }
 3049:         else {
 3050:            document.getElementById('wafproxy_table').style.display='none';
 3051:         }
 3052:         if (document.getElementById('wafproxyrow_${dom}')) {
 3053:             if (wafproxy == 1) {
 3054:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3055:             }
 3056:             else {
 3057:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3058:             }
 3059:         }
 3060:         if (document.getElementById('nowafproxyrow_$dom')) {
 3061:             if (wafproxy == 1) {
 3062:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3063:             }
 3064:             else {
 3065:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3066:             }
 3067:         }
 3068:     }
 3069:     return;
 3070: }
 3071: // ]]>
 3072: </script>
 3073: 
 3074: ENDSCRIPT
 3075: }
 3076: 
 3077: sub proctoring_javascript {
 3078:     my ($settings) = @_;
 3079:     my (%ordered,$total,%jstext);
 3080:     $total = 0;
 3081:     if (ref($settings) eq 'HASH') {
 3082:         foreach my $item (keys(%{$settings})) {
 3083:             if (ref($settings->{$item}) eq 'HASH') {
 3084:                 my $num = $settings->{$item}{'order'};
 3085:                 $ordered{$num} = $item;
 3086:             }
 3087:         }
 3088:         $total = scalar(keys(%{$settings}));
 3089:     } else {
 3090:         %ordered = (
 3091:                        0 => 'proctorio',
 3092:                        1 => 'examity',
 3093:                    );
 3094:         $total = 2; 
 3095:     }
 3096:     my @jsarray = ();
 3097:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3098:         push(@jsarray,$ordered{$item});
 3099:     }
 3100:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3101:     return <<"ENDSCRIPT";
 3102: <script type="text/javascript">
 3103: // <![CDATA[
 3104: function reorderProctoring(form,item) {
 3105:     var changedVal;
 3106: $jstext
 3107:     var maxh = $total;
 3108:     var current = new Array;
 3109:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3110:     for (var i=0; i<proctors.length; i++) {
 3111:         var elementName = 'proctoring_pos_'+proctors[i];
 3112:         if (elementName != item) {
 3113:             if (form.elements[elementName]) {
 3114:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3115:                 current[currVal] = elementName;
 3116:             }
 3117:         }
 3118:     }
 3119:     var oldVal;
 3120:     for (var j=0; j<maxh; j++) {
 3121:         if (current[j] == undefined) {
 3122:             oldVal = j;
 3123:         }
 3124:     }
 3125:     if (oldVal < changedVal) {
 3126:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3127:            var elementName = current[k];
 3128:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3129:         }
 3130:     } else {
 3131:         for (var k=changedVal; k<oldVal; k++) {
 3132:             var elementName = current[k];
 3133:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3134:         }
 3135:     }
 3136:     return;
 3137: }
 3138: 
 3139: function toggleProctoring(form,item) {
 3140:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3141:     if (fieldsets.length) {
 3142:         var radioname = 'proctoring_available_'+item;
 3143:         var num = form.elements[radioname].length;
 3144:         if (num) {
 3145:             var setvis = '';
 3146:             for (var i=0; i<num; i++) {
 3147:                 if (form.elements[radioname][i].checked) {
 3148:                     if (form.elements[radioname][i].value == '1') {
 3149:                        setvis = 1;
 3150:                        break;
 3151:                     }
 3152:                 }
 3153:             }
 3154:             for (var j=0; j<fieldsets.length; j++) {
 3155:                 if (setvis) {
 3156:                     fieldsets[j].style.display = 'block';
 3157:                 } else {
 3158:                     fieldsets[j].style.display = 'none';
 3159:                 }
 3160:             }
 3161:         }
 3162:     }
 3163:     return;
 3164: }
 3165: 
 3166: // ]]>
 3167: </script>
 3168: 
 3169: ENDSCRIPT
 3170: }
 3171: 
 3172: 
 3173: sub lti_javascript {
 3174:     my ($settings) = @_;
 3175:     my $togglejs = &lti_toggle_js();
 3176:     unless (ref($settings) eq 'HASH') {
 3177:         return $togglejs;
 3178:     }
 3179:     my (%ordered,$total,%jstext);
 3180:     $total = scalar(keys(%{$settings}));
 3181:     foreach my $item (keys(%{$settings})) {
 3182:         if (ref($settings->{$item}) eq 'HASH') {
 3183:             my $num = $settings->{$item}{'order'};
 3184:             if ($num eq '') {
 3185:                 $num = $total - 1;
 3186:             }
 3187:             $ordered{$num} = $item;
 3188:         }
 3189:     }
 3190:     my @jsarray = ();
 3191:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3192:         push(@jsarray,$ordered{$item});
 3193:     }
 3194:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3195:     return <<"ENDSCRIPT";
 3196: <script type="text/javascript">
 3197: // <![CDATA[
 3198: function reorderLTI(form,item) {
 3199:     var changedVal;
 3200: $jstext
 3201:     var newpos = 'lti_pos_add';
 3202:     var maxh = 1 + $total;
 3203:     var current = new Array;
 3204:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3205:     if (item == newpos) {
 3206:         changedVal = newitemVal;
 3207:     } else {
 3208:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3209:         current[newitemVal] = newpos;
 3210:     }
 3211:     for (var i=0; i<lti.length; i++) {
 3212:         var elementName = 'lti_pos_'+lti[i];
 3213:         if (elementName != item) {
 3214:             if (form.elements[elementName]) {
 3215:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3216:                 current[currVal] = elementName;
 3217:             }
 3218:         }
 3219:     }
 3220:     var oldVal;
 3221:     for (var j=0; j<maxh; j++) {
 3222:         if (current[j] == undefined) {
 3223:             oldVal = j;
 3224:         }
 3225:     }
 3226:     if (oldVal < changedVal) {
 3227:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3228:            var elementName = current[k];
 3229:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3230:         }
 3231:     } else {
 3232:         for (var k=changedVal; k<oldVal; k++) {
 3233:             var elementName = current[k];
 3234:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3235:         }
 3236:     }
 3237:     return;
 3238: }
 3239: // ]]>
 3240: </script>
 3241: 
 3242: $togglejs
 3243: 
 3244: ENDSCRIPT
 3245: }
 3246: 
 3247: sub lti_toggle_js {
 3248:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3249:                             localauth => 'Local auth argument',
 3250:                             krb       => 'Kerberos domain',
 3251:                          );
 3252:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3253:     &js_escape(\$crsincalert);
 3254:     return <<"ENDSCRIPT";
 3255: <script type="text/javascript">
 3256: // <![CDATA[
 3257: 
 3258: function toggleLTI(form,setting,item) {
 3259:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3260:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3261:         var setvis = '';
 3262:         var radioname = 'lti_requser_'+item;
 3263:         var num = form.elements[radioname].length;
 3264:         if (num) {
 3265:             for (var i=0; i<num; i++) {
 3266:                 if (form.elements[radioname][i].checked) {
 3267:                     if (form.elements[radioname][i].value == '1') {
 3268:                         setvis = 1;
 3269:                         break;
 3270:                     }
 3271:                 }
 3272:             }
 3273:         }
 3274:         if (usrfieldsets.length) {
 3275:             for (var j=0; j<usrfieldsets.length; j++) {
 3276:                 if (setvis) {
 3277:                     usrfieldsets[j].style.display = 'block';
 3278:                 } else {
 3279:                     usrfieldsets[j].style.display = 'none';
 3280:                 }
 3281:             }
 3282:         }
 3283:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3284:         if (crsfieldsets.length) {
 3285:             radioname = 'lti_crsinc_'+item;
 3286:             var num = form.elements[radioname].length;
 3287:             if (num) {
 3288:                 var crsvis = '';
 3289:                 for (var i=0; i<num; i++) {
 3290:                     if (form.elements[radioname][i].checked) {
 3291:                         if (form.elements[radioname][i].value == '1') {
 3292:                             if (setvis == '') {
 3293:                                 if (setting == 'crsinc'){
 3294:                                     alert("$crsincalert");
 3295:                                     form.elements[radioname][0].checked = true;
 3296:                                 }
 3297:                             } else {
 3298:                                 crsvis = 1;
 3299:                             }
 3300:                             break;
 3301:                         }
 3302:                     }
 3303:                 }
 3304:                 setvis = crsvis;
 3305:             }
 3306:             for (var j=0; j<crsfieldsets.length; j++) {
 3307:                 if (setvis) {
 3308:                     crsfieldsets[j].style.display = 'block';
 3309:                 } else {
 3310:                     crsfieldsets[j].style.display = 'none';
 3311:                 }
 3312:             }
 3313:         }
 3314:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3315:         var radioname = '';
 3316:         var divid = '';
 3317:         if (setting == 'user') {
 3318:             radioname = 'lti_mapuser_'+item;
 3319:             divid = 'lti_userfield_'+item;
 3320:         } else if (setting == 'crs') {
 3321:             radioname = 'lti_mapcrs_'+item;
 3322:             divid = 'lti_crsfield_'+item;
 3323:         } else if (setting == 'callback') {
 3324:             radioname = 'lti_callback_'+item;
 3325:             divid = 'lti_callbackfield_'+item;
 3326:         } else {
 3327:             radioname = 'lti_passback_'+item;
 3328:             divid =  'lti_passback_'+item;
 3329:         }
 3330:         var num = form.elements[radioname].length;
 3331:         if (num) {
 3332:             var setvis = '';
 3333:             for (var i=0; i<num; i++) {
 3334:                if (form.elements[radioname][i].checked) {
 3335:                    if ((setting == 'passback') || (setting == 'callback')) {
 3336:                        if (form.elements[radioname][i].value == '1') {
 3337:                            if (document.getElementById(divid)) {
 3338:                                document.getElementById(divid).style.display = 'inline-block';
 3339:                            }
 3340:                            setvis = 1;
 3341:                            break;
 3342:                        }
 3343:                    } else {
 3344:                        if (form.elements[radioname][i].value == 'other') {
 3345:                            if (document.getElementById(divid)) {
 3346:                                document.getElementById(divid).style.display = 'inline-block';
 3347:                            }
 3348:                            setvis = 1;
 3349:                            break;
 3350:                        }
 3351:                    }
 3352:                } 
 3353:             }
 3354:             if (!setvis) {
 3355:                 if (document.getElementById(divid)) {
 3356:                     document.getElementById(divid).style.display = 'none';
 3357:                 }
 3358:             }
 3359:         }
 3360:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3361:         var numsec = form.elements['lti_crssec_'+item].length;
 3362:         if (numsec) {
 3363:             var setvis = '';
 3364:             for (var i=0; i<numsec; i++) {
 3365:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3366:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3367:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3368:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3369:                             setvis = 1;
 3370:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3371:                             if (numsrcsec) {
 3372:                                 var setsrcvis = '';
 3373:                                 for (var j=0; j<numsrcsec; j++) {
 3374:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3375:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3376:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3377:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3378:                                                 setsrcvis = 1;
 3379:                                             }
 3380:                                         }
 3381:                                     }
 3382:                                 }
 3383:                                 if (!setsrcvis) {
 3384:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3385:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3386:                                     }
 3387:                                 }
 3388:                             }
 3389:                         }
 3390:                     }
 3391:                 }
 3392:             }
 3393:             if (!setvis) {
 3394:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3395:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3396:                 }
 3397:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3398:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3399:                 }
 3400:             }
 3401:         }
 3402:     } else if (setting == 'lcauth') {
 3403:         var numauth = form.elements['lti_lcauth_'+item].length;
 3404:         if (numauth) {
 3405:             for (var i=0; i<numauth; i++) {
 3406:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3407:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3408:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3409:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3410:                         } else {
 3411:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3412:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3413:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3414:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3415:                                 } else {
 3416:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3417:                                 }
 3418:                             }
 3419:                         }
 3420:                     }
 3421:                 }
 3422:             }
 3423:         }
 3424:     } else if (setting == 'lcmenu') {
 3425:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3426:         var divid = 'lti_menufield_'+item;
 3427:         var setvis = '';
 3428:         for (var i=0; i<menus.length; i++) {
 3429:             var radioname = menus[i];  
 3430:             var num = form.elements[radioname].length;
 3431:             if (num) {
 3432:                 for (var j=0; j<num; j++) {
 3433:                     if (form.elements[radioname][j].checked) {
 3434:                         if (form.elements[radioname][j].value == '1') {
 3435:                             if (document.getElementById(divid)) {
 3436:                                 document.getElementById(divid).style.display = 'inline-block';
 3437:                             }
 3438:                             setvis = 1;
 3439:                             break;
 3440:                         }
 3441:                     }
 3442:                 }
 3443:             }
 3444:             if (setvis == 1) {
 3445:                 break;
 3446:             }
 3447:         }
 3448:         if (!setvis) {
 3449:             if (document.getElementById(divid)) {
 3450:                 document.getElementById(divid).style.display = 'none';
 3451:             }
 3452:         }
 3453:     }
 3454:     return;
 3455: }
 3456: // ]]>
 3457: </script>
 3458: 
 3459: ENDSCRIPT
 3460: }
 3461: 
 3462: sub autoupdate_javascript {
 3463:     return <<"ENDSCRIPT";
 3464: <script type="text/javascript">
 3465: // <![CDATA[
 3466: function toggleLastActiveDays(form) {
 3467:     var radioname = 'lastactive';
 3468:     var divid = 'lastactive_div';
 3469:     var num = form.elements[radioname].length;
 3470:     if (num) {
 3471:         var setvis = '';
 3472:         for (var i=0; i<num; i++) {
 3473:             if (form.elements[radioname][i].checked) {
 3474:                 if (form.elements[radioname][i].value == '1') {
 3475:                     if (document.getElementById(divid)) {
 3476:                         document.getElementById(divid).style.display = 'inline-block';
 3477:                     }
 3478:                     setvis = 1;
 3479:                 }
 3480:                 break;
 3481:             }
 3482:         }
 3483:         if (!setvis) {
 3484:             if (document.getElementById(divid)) {
 3485:                 document.getElementById(divid).style.display = 'none';
 3486:             }
 3487:         }
 3488:     }
 3489:     return;
 3490: }
 3491: // ]]>
 3492: </script>
 3493: 
 3494: ENDSCRIPT
 3495: }
 3496: 
 3497: sub saml_javascript {
 3498:     return <<"ENDSCRIPT";
 3499: <script type="text/javascript">
 3500: // <![CDATA[
 3501: function toggleSamlOptions(form,hostid) { 
 3502:     var radioname = 'saml_'+hostid;
 3503:     var tablecellon = 'samloptionson_'+hostid;
 3504:     var tablecelloff = 'samloptionsoff_'+hostid;
 3505:     var num = form.elements[radioname].length;
 3506:     if (num) {
 3507:         var setvis = ''; 
 3508:         for (var i=0; i<num; i++) {
 3509:             if (form.elements[radioname][i].checked) {
 3510:                 if (form.elements[radioname][i].value == '1') { 
 3511:                     if (document.getElementById(tablecellon)) {
 3512:                         document.getElementById(tablecellon).style.display='';
 3513:                     }
 3514:                     if (document.getElementById(tablecelloff)) {
 3515:                         document.getElementById(tablecelloff).style.display='none';
 3516:                     }
 3517:                     setvis = 1;
 3518:                 }
 3519:                 break;
 3520:             }
 3521:         }
 3522:         if (!setvis) {
 3523:             if (document.getElementById(tablecellon)) {
 3524:                 document.getElementById(tablecellon).style.display='none';
 3525:             }
 3526:             if (document.getElementById(tablecelloff)) {
 3527:                 document.getElementById(tablecelloff).style.display='';
 3528:             }
 3529:         }
 3530:     }
 3531:     return;
 3532: }
 3533: // ]]>
 3534: </script>
 3535: 
 3536: ENDSCRIPT
 3537: }
 3538: 
 3539: sub print_autoenroll {
 3540:     my ($dom,$settings,$rowtotal) = @_;
 3541:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3542:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3543:     if (ref($settings) eq 'HASH') {
 3544:         if (exists($settings->{'run'})) {
 3545:             if ($settings->{'run'} eq '0') {
 3546:                 $runoff = ' checked="checked" ';
 3547:                 $runon = ' ';
 3548:             } else {
 3549:                 $runon = ' checked="checked" ';
 3550:                 $runoff = ' ';
 3551:             }
 3552:         } else {
 3553:             if ($autorun) {
 3554:                 $runon = ' checked="checked" ';
 3555:                 $runoff = ' ';
 3556:             } else {
 3557:                 $runoff = ' checked="checked" ';
 3558:                 $runon = ' ';
 3559:             }
 3560:         }
 3561:         if (exists($settings->{'co-owners'})) {
 3562:             if ($settings->{'co-owners'} eq '0') {
 3563:                 $coownersoff = ' checked="checked" ';
 3564:                 $coownerson = ' ';
 3565:             } else {
 3566:                 $coownerson = ' checked="checked" ';
 3567:                 $coownersoff = ' ';
 3568:             }
 3569:         } else {
 3570:             $coownersoff = ' checked="checked" ';
 3571:             $coownerson = ' ';
 3572:         }
 3573:         if (exists($settings->{'sender_domain'})) {
 3574:             $defdom = $settings->{'sender_domain'};
 3575:         }
 3576:         if (exists($settings->{'autofailsafe'})) {
 3577:             $failsafe = $settings->{'autofailsafe'};
 3578:         }
 3579:     } else {
 3580:         if ($autorun) {
 3581:             $runon = ' checked="checked" ';
 3582:             $runoff = ' ';
 3583:         } else {
 3584:             $runoff = ' checked="checked" ';
 3585:             $runon = ' ';
 3586:         }
 3587:     }
 3588:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3589:     my $notif_sender;
 3590:     if (ref($settings) eq 'HASH') {
 3591:         $notif_sender = $settings->{'sender_uname'};
 3592:     }
 3593:     my $datatable='<tr class="LC_odd_row">'.
 3594:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3595:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3596:                   '<input type="radio" name="autoenroll_run"'.
 3597:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3598:                   '<label><input type="radio" name="autoenroll_run"'.
 3599:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3600:                   '</tr><tr>'.
 3601:                   '<td>'.&mt('Notification messages - sender').
 3602:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3603:                   &mt('username').':&nbsp;'.
 3604:                   '<input type="text" name="sender_uname" value="'.
 3605:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3606:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3607:                   '<tr class="LC_odd_row">'.
 3608:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3609:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3610:                   '<input type="radio" name="autoassign_coowners"'.
 3611:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3612:                   '<label><input type="radio" name="autoassign_coowners"'.
 3613:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3614:                   '</tr><tr>'.
 3615:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3616:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3617:                   '<input type="text" name="autoenroll_failsafe"'.
 3618:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3619:     $$rowtotal += 4;
 3620:     return $datatable;
 3621: }
 3622: 
 3623: sub print_autoupdate {
 3624:     my ($position,$dom,$settings,$rowtotal) = @_;
 3625:     my ($enable,$datatable);
 3626:     if ($position eq 'top') {
 3627:         my %choices = &Apache::lonlocal::texthash (
 3628:                           run        => 'Auto-update active?',
 3629:                           classlists => 'Update information in classlists?',
 3630:                           unexpired  => 'Skip updates for users without active or future roles?',
 3631:                           lastactive => 'Skip updates for inactive users?',
 3632:         );
 3633:         my $itemcount = 0;
 3634:         my $updateon = ' ';
 3635:         my $updateoff = ' checked="checked" ';
 3636:         if (ref($settings) eq 'HASH') {
 3637:             if ($settings->{'run'} eq '1') {
 3638:                 $updateon = $updateoff;
 3639:                 $updateoff = ' ';
 3640:             }
 3641:         }
 3642:         $enable = '<tr class="LC_odd_row">'.
 3643:                   '<td>'.$choices{'run'}.'</td>'.
 3644:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3645:                   '<input type="radio" name="autoupdate_run"'.
 3646:                   $updateoff.' value="0" />'.&mt('No').'</label>&nbsp;'.
 3647:                   '<label><input type="radio" name="autoupdate_run"'.
 3648:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3649:                   '</tr>';
 3650:         my @toggles = ('classlists','unexpired');
 3651:         my %defaultchecked = ('classlists' => 'off',
 3652:                               'unexpired'  => 'off'
 3653:                               );
 3654:         $$rowtotal ++;
 3655:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3656:                                                      \%choices,$itemcount,'','','left','no');
 3657:         $datatable = $enable.$datatable;
 3658:         $$rowtotal += $itemcount;
 3659:         my $lastactiveon = ' ';
 3660:         my $lastactiveoff = ' checked="checked" ';
 3661:         my $lastactivestyle = 'none';
 3662:         my $lastactivedays;
 3663:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3664:         if (ref($settings) eq 'HASH') {
 3665:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3666:                 $lastactiveon = $lastactiveoff;
 3667:                 $lastactiveoff = ' ';
 3668:                 $lastactivestyle = 'inline-block';
 3669:                 $lastactivedays = $settings->{'lastactive'};
 3670:             }
 3671:         }
 3672:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3673:         $datatable .= '<tr'.$css_class.'>'.
 3674:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3675:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3676:                       '<input type="radio" name="lastactive"'.
 3677:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3678:                       '&nbsp;<label>'.
 3679:                       '<input type="radio" name="lastactive"'.
 3680:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3681:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3682:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3683:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3684:                           $lastactivedays.'" />').
 3685:                       '</span></td>'.
 3686:                       '</tr>';
 3687:         $$rowtotal ++;
 3688:     } elsif ($position eq 'middle') {
 3689:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3690:         my $numinrow = 3;
 3691:         my $locknamesettings;
 3692:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3693:                                      $dom,$numinrow,$othertitle,
 3694:                                     'lockablenames',$rowtotal);
 3695:         $$rowtotal ++;
 3696:     } else {
 3697:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3698:         my @fields = ('lastname','firstname','middlename','generation',
 3699:                       'permanentemail','id');
 3700:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3701:         my $numrows = 0;
 3702:         if (ref($types) eq 'ARRAY') {
 3703:             if (@{$types} > 0) {
 3704:                 $datatable = 
 3705:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3706:                                          \@fields,$types,\$numrows);
 3707:                     $$rowtotal += @{$types}; 
 3708:             }
 3709:         }
 3710:         $datatable .= 
 3711:             &usertype_update_row($settings,{'default' => $othertitle},
 3712:                                  \%fieldtitles,\@fields,['default'],
 3713:                                  \$numrows);
 3714:         $$rowtotal ++;     
 3715:     }
 3716:     return $datatable;
 3717: }
 3718: 
 3719: sub print_autocreate {
 3720:     my ($dom,$settings,$rowtotal) = @_;
 3721:     my (%createon,%createoff,%currhash);
 3722:     my @types = ('xml','req');
 3723:     if (ref($settings) eq 'HASH') {
 3724:         foreach my $item (@types) {
 3725:             $createoff{$item} = ' checked="checked" ';
 3726:             $createon{$item} = ' ';
 3727:             if (exists($settings->{$item})) {
 3728:                 if ($settings->{$item}) {
 3729:                     $createon{$item} = ' checked="checked" ';
 3730:                     $createoff{$item} = ' ';
 3731:                 }
 3732:             }
 3733:         }
 3734:         if ($settings->{'xmldc'} ne '') {
 3735:             $currhash{$settings->{'xmldc'}} = 1;
 3736:         }
 3737:     } else {
 3738:         foreach my $item (@types) {
 3739:             $createoff{$item} = ' checked="checked" ';
 3740:             $createon{$item} = ' ';
 3741:         }
 3742:     }
 3743:     $$rowtotal += 2;
 3744:     my $numinrow = 2;
 3745:     my $datatable='<tr class="LC_odd_row">'.
 3746:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3747:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3748:                   '<input type="radio" name="autocreate_xml"'.
 3749:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3750:                   '<label><input type="radio" name="autocreate_xml"'.
 3751:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3752:                   '</td></tr><tr>'.
 3753:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3754:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3755:                   '<input type="radio" name="autocreate_req"'.
 3756:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3757:                   '<label><input type="radio" name="autocreate_req"'.
 3758:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3759:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3760:                                                    'autocreate_xmldc',%currhash);
 3761:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3762:     if ($numdc > 1) {
 3763:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3764:                       '</td><td class="LC_left_item">';
 3765:     } else {
 3766:         $datatable .= &mt('Course creation processed as:').
 3767:                       '</td><td class="LC_right_item">';
 3768:     }
 3769:     $datatable .= $dctable.'</td></tr>';
 3770:     $$rowtotal += $rows;
 3771:     return $datatable;
 3772: }
 3773: 
 3774: sub print_directorysrch {
 3775:     my ($position,$dom,$settings,$rowtotal) = @_;
 3776:     my $datatable;
 3777:     if ($position eq 'top') {
 3778:         my $instsrchon = ' ';
 3779:         my $instsrchoff = ' checked="checked" ';
 3780:         my ($exacton,$containson,$beginson);
 3781:         my $instlocalon = ' ';
 3782:         my $instlocaloff = ' checked="checked" ';
 3783:         if (ref($settings) eq 'HASH') {
 3784:             if ($settings->{'available'} eq '1') {
 3785:                 $instsrchon = $instsrchoff;
 3786:                 $instsrchoff = ' ';
 3787:             }
 3788:             if ($settings->{'localonly'} eq '1') {
 3789:                 $instlocalon = $instlocaloff;
 3790:                 $instlocaloff = ' ';
 3791:             }
 3792:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3793:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3794:                     if ($type eq 'exact') {
 3795:                         $exacton = ' checked="checked" ';
 3796:                     } elsif ($type eq 'contains') {
 3797:                         $containson = ' checked="checked" ';
 3798:                     } elsif ($type eq 'begins') {
 3799:                         $beginson = ' checked="checked" ';
 3800:                     }
 3801:                 }
 3802:             } else {
 3803:                 if ($settings->{'searchtypes'} eq 'exact') {
 3804:                     $exacton = ' checked="checked" ';
 3805:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3806:                     $containson = ' checked="checked" ';
 3807:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3808:                     $exacton = ' checked="checked" ';
 3809:                     $containson = ' checked="checked" ';
 3810:                 }
 3811:             }
 3812:         }
 3813:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3814:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3815: 
 3816:         my $numinrow = 4;
 3817:         my $cansrchrow = 0;
 3818:         $datatable='<tr class="LC_odd_row">'.
 3819:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3820:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3821:                    '<input type="radio" name="dirsrch_available"'.
 3822:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3823:                    '<label><input type="radio" name="dirsrch_available"'.
 3824:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3825:                    '</tr><tr>'.
 3826:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3827:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3828:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3829:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3830:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3831:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3832:                    '</tr>';
 3833:         $$rowtotal += 2;
 3834:         if (ref($usertypes) eq 'HASH') {
 3835:             if (keys(%{$usertypes}) > 0) {
 3836:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3837:                                              $numinrow,$othertitle,'cansearch',
 3838:                                              $rowtotal);
 3839:                 $cansrchrow = 1;
 3840:             }
 3841:         }
 3842:         if ($cansrchrow) {
 3843:             $$rowtotal ++;
 3844:             $datatable .= '<tr>';
 3845:         } else {
 3846:             $datatable .= '<tr class="LC_odd_row">';
 3847:         }
 3848:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3849:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3850:         foreach my $title (@{$titleorder}) {
 3851:             if (defined($searchtitles->{$title})) {
 3852:                 my $check = ' ';
 3853:                 if (ref($settings) eq 'HASH') {
 3854:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3855:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3856:                             $check = ' checked="checked" ';
 3857:                         }
 3858:                     }
 3859:                 }
 3860:                 $datatable .= '<td class="LC_left_item">'.
 3861:                               '<span class="LC_nobreak"><label>'.
 3862:                               '<input type="checkbox" name="searchby" '.
 3863:                               'value="'.$title.'"'.$check.'/>'.
 3864:                               $searchtitles->{$title}.'</label></span></td>';
 3865:             }
 3866:         }
 3867:         $datatable .= '</tr></table></td></tr>';
 3868:         $$rowtotal ++;
 3869:         if ($cansrchrow) {
 3870:             $datatable .= '<tr class="LC_odd_row">';
 3871:         } else {
 3872:             $datatable .= '<tr>';
 3873:         }
 3874:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3875:                       '<td class="LC_left_item" colspan="2">'.
 3876:                       '<span class="LC_nobreak"><label>'.
 3877:                       '<input type="checkbox" name="searchtypes" '.
 3878:                       $exacton.' value="exact" />'.&mt('Exact match').
 3879:                       '</label>&nbsp;'.
 3880:                       '<label><input type="checkbox" name="searchtypes" '.
 3881:                       $beginson.' value="begins" />'.&mt('Begins with').
 3882:                       '</label>&nbsp;'.
 3883:                       '<label><input type="checkbox" name="searchtypes" '.
 3884:                       $containson.' value="contains" />'.&mt('Contains').
 3885:                       '</label></span></td></tr>';
 3886:         $$rowtotal ++;
 3887:     } else {
 3888:         my $domsrchon = ' checked="checked" ';
 3889:         my $domsrchoff = ' ';
 3890:         my $domlocalon = ' ';
 3891:         my $domlocaloff = ' checked="checked" ';
 3892:         if (ref($settings) eq 'HASH') {
 3893:             if ($settings->{'lclocalonly'} eq '1') {
 3894:                 $domlocalon = $domlocaloff;
 3895:                 $domlocaloff = ' ';
 3896:             }
 3897:             if ($settings->{'lcavailable'} eq '0') {
 3898:                 $domsrchoff = $domsrchon;
 3899:                 $domsrchon = ' ';
 3900:             }
 3901:         }
 3902:         $datatable='<tr class="LC_odd_row">'.
 3903:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3904:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3905:                       '<input type="radio" name="dirsrch_domavailable"'.
 3906:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3907:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3908:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3909:                       '</tr><tr>'.
 3910:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3911:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3912:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3913:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3914:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3915:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3916:                       '</tr>';
 3917:         $$rowtotal += 2;
 3918:     }
 3919:     return $datatable;
 3920: }
 3921: 
 3922: sub print_contacts {
 3923:     my ($position,$dom,$settings,$rowtotal) = @_;
 3924:     my $datatable;
 3925:     my @contacts = ('adminemail','supportemail');
 3926:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3927:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3928:     if ($position eq 'top') {
 3929:         if (ref($settings) eq 'HASH') {
 3930:             foreach my $item (@contacts) {
 3931:                 if (exists($settings->{$item})) {
 3932:                     $to{$item} = $settings->{$item};
 3933:                 }
 3934:             }
 3935:         }
 3936:     } elsif ($position eq 'middle') {
 3937:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3938:                      'updatesmail','idconflictsmail','hostipmail');
 3939:         foreach my $type (@mailings) {
 3940:             $otheremails{$type} = '';
 3941:         }
 3942:     } elsif ($position eq 'lower') {
 3943:         if (ref($settings) eq 'HASH') {
 3944:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3945:                 %lonstatus = %{$settings->{'lonstatus'}};
 3946:             }
 3947:         }
 3948:     } else {
 3949:         @mailings = ('helpdeskmail','otherdomsmail');
 3950:         foreach my $type (@mailings) {
 3951:             $otheremails{$type} = '';
 3952:         }
 3953:         $bccemails{'helpdeskmail'} = '';
 3954:         $bccemails{'otherdomsmail'} = '';
 3955:         $includestr{'helpdeskmail'} = '';
 3956:         $includestr{'otherdomsmail'} = '';
 3957:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3958:     }
 3959:     if (ref($settings) eq 'HASH') {
 3960:         unless (($position eq 'top') || ($position eq 'lower')) {
 3961:             foreach my $type (@mailings) {
 3962:                 if (exists($settings->{$type})) {
 3963:                     if (ref($settings->{$type}) eq 'HASH') {
 3964:                         foreach my $item (@contacts) {
 3965:                             if ($settings->{$type}{$item}) {
 3966:                                 $checked{$type}{$item} = ' checked="checked" ';
 3967:                             }
 3968:                         }
 3969:                         $otheremails{$type} = $settings->{$type}{'others'};
 3970:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3971:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3972:                             if ($settings->{$type}{'include'} ne '') {
 3973:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3974:                                 $includestr{$type} = &unescape($includestr{$type});
 3975:                             }
 3976:                         }
 3977:                     }
 3978:                 } elsif ($type eq 'lonstatusmail') {
 3979:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3980:                 }
 3981:             }
 3982:         }
 3983:         if ($position eq 'bottom') {
 3984:             foreach my $type (@mailings) {
 3985:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3986:                 if ($settings->{$type}{'include'} ne '') {
 3987:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3988:                     $includestr{$type} = &unescape($includestr{$type});
 3989:                 }
 3990:             }
 3991:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3992:                 if (ref($fields) eq 'ARRAY') {
 3993:                     foreach my $field (@{$fields}) {
 3994:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3995:                     }
 3996:                 }
 3997:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3998:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3999:                 } else {
 4000:                     $maxsize = '1.0';
 4001:                 }
 4002:             } else {
 4003:                 if (ref($fields) eq 'ARRAY') {
 4004:                     foreach my $field (@{$fields}) {
 4005:                         $currfield{$field} = 'yes';
 4006:                     }
 4007:                 }
 4008:                 $maxsize = '1.0';
 4009:             }
 4010:         }
 4011:     } else {
 4012:         if ($position eq 'top') {
 4013:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4014:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4015:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4016:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4017:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4018:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4019:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4020:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4021:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4022:         } elsif ($position eq 'bottom') {
 4023:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4024:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4025:             if (ref($fields) eq 'ARRAY') {
 4026:                 foreach my $field (@{$fields}) {
 4027:                     $currfield{$field} = 'yes';
 4028:                 }
 4029:             }
 4030:             $maxsize = '1.0';
 4031:         }
 4032:     }
 4033:     my ($titles,$short_titles) = &contact_titles();
 4034:     my $rownum = 0;
 4035:     my $css_class;
 4036:     if ($position eq 'top') {
 4037:         foreach my $item (@contacts) {
 4038:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4039:             $datatable .= '<tr'.$css_class.'>'. 
 4040:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4041:                           '</span></td><td class="LC_right_item">'.
 4042:                           '<input type="text" name="'.$item.'" value="'.
 4043:                           $to{$item}.'" /></td></tr>';
 4044:             $rownum ++;
 4045:         }
 4046:     } elsif ($position eq 'bottom') {
 4047:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4048:         $datatable .= '<tr'.$css_class.'>'.
 4049:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4050:                       &mt('(e-mail, subject, and description always shown)').
 4051:                       '</td><td class="LC_left_item">';
 4052:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4053:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4054:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4055:             foreach my $field (@{$fields}) {
 4056:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4057:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4058:                     $datatable .= ' '.&mt('(logged-in users)');
 4059:                 }
 4060:                 $datatable .='</td><td>';
 4061:                 my $clickaction;
 4062:                 if ($field eq 'screenshot') {
 4063:                     $clickaction = ' onclick="screenshotSize(this);"';
 4064:                 }
 4065:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4066:                     foreach my $option (@{$possoptions->{$field}}) {
 4067:                         my $checked;
 4068:                         if ($currfield{$field} eq $option) {
 4069:                             $checked = ' checked="checked"';
 4070:                         }
 4071:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4072:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4073:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4074:                                       '</label></span>'.('&nbsp;'x2);
 4075:                     }
 4076:                 }
 4077:                 if ($field eq 'screenshot') {
 4078:                     my $display;
 4079:                     if ($currfield{$field} eq 'no') {
 4080:                         $display = ' style="display:none"';
 4081:                     }
 4082:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4083:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4084:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4085:                 }
 4086:                 $datatable .= '</td></tr>';
 4087:             }
 4088:             $datatable .= '</table>';
 4089:         }
 4090:         $datatable .= '</td></tr>'."\n";
 4091:         $rownum ++;
 4092:     }
 4093:     unless (($position eq 'top') || ($position eq 'lower')) {
 4094:         foreach my $type (@mailings) {
 4095:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4096:             $datatable .= '<tr'.$css_class.'>'.
 4097:                           '<td><span class="LC_nobreak">'.
 4098:                           $titles->{$type}.': </span></td>'.
 4099:                           '<td class="LC_left_item">';
 4100:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4101:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4102:             }
 4103:             $datatable .= '<span class="LC_nobreak">';
 4104:             foreach my $item (@contacts) {
 4105:                 $datatable .= '<label>'.
 4106:                               '<input type="checkbox" name="'.$type.'"'.
 4107:                               $checked{$type}{$item}.
 4108:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4109:                               '</label>&nbsp;';
 4110:             }
 4111:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4112:                           '<input type="text" name="'.$type.'_others" '.
 4113:                           'value="'.$otheremails{$type}.'"  />';
 4114:             my %locchecked;
 4115:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4116:                 foreach my $loc ('s','b') {
 4117:                     if ($includeloc{$type} eq $loc) {
 4118:                         $locchecked{$loc} = ' checked="checked"';
 4119:                         last;
 4120:                     }
 4121:                 }
 4122:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4123:                               '<input type="text" name="'.$type.'_bcc" '.
 4124:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4125:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4126:                               &mt('Text automatically added to e-mail:').' '.
 4127:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4128:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4129:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4130:                               ('&nbsp;'x2).
 4131:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4132:                               '</span></fieldset>';
 4133:             }
 4134:             $datatable .= '</td></tr>'."\n";
 4135:             $rownum ++;
 4136:         }
 4137:     }
 4138:     if ($position eq 'middle') {
 4139:         my %choices;
 4140:         my $corelink = &core_link_msu();
 4141:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4142:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4143:                                         $corelink);
 4144:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4145:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4146:         my %defaultchecked = ('reporterrors'  => 'on',
 4147:                               'reportupdates' => 'on',
 4148:                               'reportstatus'  => 'on');
 4149:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4150:                                                    \%choices,$rownum);
 4151:         $datatable .= $reports;
 4152:     } elsif ($position eq 'lower') {
 4153:         my (%current,%excluded,%weights);
 4154:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4155:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4156:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4157:         } else {
 4158:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4159:         }
 4160:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4161:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4162:         } else {
 4163:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4164:         }
 4165:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4166:             foreach my $type ('E','W','N','U') {
 4167:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4168:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4169:                 } else {
 4170:                     $weights{$type} = $defaults->{$type};
 4171:                 }
 4172:             }
 4173:         } else {
 4174:             foreach my $type ('E','W','N','U') {
 4175:                 $weights{$type} = $defaults->{$type};
 4176:             }
 4177:         }
 4178:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4179:             if (@{$lonstatus{'excluded'}} > 0) {
 4180:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4181:             }
 4182:         }
 4183:         foreach my $item ('errorthreshold','errorsysmail') { 
 4184:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4185:             $datatable .= '<tr'.$css_class.'>'.
 4186:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4187:                           $titles->{$item}.
 4188:                           '</span></td><td class="LC_left_item">'.
 4189:                           '<input type="text" name="'.$item.'" value="'.
 4190:                           $current{$item}.'" size="5" /></td></tr>';
 4191:             $rownum ++;
 4192:         }
 4193:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4194:         $datatable .= '<tr'.$css_class.'>'.
 4195:                       '<td class="LC_left_item">'.
 4196:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4197:                       '</span></td><td class="LC_left_item"><table><tr>';
 4198:         foreach my $type ('E','W','N','U') {
 4199:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4200:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4201:                           $weights{$type}.'" size="5" /></td>';
 4202:         }
 4203:         $datatable .= '</tr></table></tr>';
 4204:         $rownum ++;
 4205:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4206:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4207:                       $titles->{'errorexcluded'}.'</td>'.
 4208:                       '<td class="LC_left_item"><table>';
 4209:         my $numinrow = 4;
 4210:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4211:         for (my $i=0; $i<@ids; $i++) {
 4212:             my $rem = $i%($numinrow);
 4213:             if ($rem == 0) {
 4214:                 if ($i > 0) {
 4215:                     $datatable .= '</tr>';
 4216:                 }
 4217:                 $datatable .= '<tr>';
 4218:             }
 4219:             my $check;
 4220:             if ($excluded{$ids[$i]}) {
 4221:                 $check = ' checked="checked" ';
 4222:             }
 4223:             $datatable .= '<td class="LC_left_item">'.
 4224:                           '<span class="LC_nobreak"><label>'.
 4225:                           '<input type="checkbox" name="errorexcluded" '.
 4226:                           'value="'.$ids[$i].'"'.$check.' />'.
 4227:                           $ids[$i].'</label></span></td>';
 4228:         }
 4229:         my $colsleft = $numinrow - @ids%($numinrow);
 4230:         if ($colsleft > 1 ) {
 4231:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4232:                           '&nbsp;</td>';
 4233:         } elsif ($colsleft == 1) {
 4234:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4235:         }
 4236:         $datatable .= '</tr></table></td></tr>';
 4237:         $rownum ++;
 4238:     } elsif ($position eq 'bottom') {
 4239:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4240:         my (@posstypes,%usertypeshash);
 4241:         if (ref($types) eq 'ARRAY') {
 4242:             @posstypes = @{$types};
 4243:         }
 4244:         if (@posstypes) {
 4245:             if (ref($usertypes) eq 'HASH') {
 4246:                 %usertypeshash = %{$usertypes};
 4247:             }
 4248:             my @overridden;
 4249:             my $numinrow = 4;
 4250:             if (ref($settings) eq 'HASH') {
 4251:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4252:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4253:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4254:                             push(@overridden,$key);
 4255:                             foreach my $item (@contacts) {
 4256:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4257:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4258:                                 }
 4259:                             }
 4260:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4261:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4262:                             $includeloc{'override_'.$key} = '';
 4263:                             $includestr{'override_'.$key} = '';
 4264:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4265:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 4266:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4267:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4268:                             }
 4269:                         }
 4270:                     }
 4271:                 }
 4272:             }
 4273:             my $customclass = 'LC_helpdesk_override';
 4274:             my $optionsprefix = 'LC_options_helpdesk_';
 4275: 
 4276:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4277:  
 4278:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4279:                                          $numinrow,$othertitle,'overrides',
 4280:                                          \$rownum,$onclicktypes,$customclass);
 4281:             $rownum ++;
 4282:             $usertypeshash{'default'} = $othertitle;
 4283:             foreach my $status (@posstypes) {
 4284:                 my $css_class;
 4285:                 if ($rownum%2) {
 4286:                     $css_class = 'LC_odd_row ';
 4287:                 }
 4288:                 $css_class .= $customclass;
 4289:                 my $rowid = $optionsprefix.$status;
 4290:                 my $hidden = 1;
 4291:                 my $currstyle = 'display:none';
 4292:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4293:                     $currstyle = 'display:table-row';
 4294:                     $hidden = 0;
 4295:                 }
 4296:                 my $key = 'override_'.$status;
 4297:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4298:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4299:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4300:                                                   \@contacts,$short_titles);
 4301:                 unless ($hidden) {
 4302:                     $rownum ++;
 4303:                 }
 4304:             }
 4305:         }
 4306:     }
 4307:     $$rowtotal += $rownum;
 4308:     return $datatable;
 4309: }
 4310: 
 4311: sub core_link_msu {
 4312:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4313:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4314: }
 4315: 
 4316: sub overridden_helpdesk {
 4317:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4318:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4319:     my $class = 'LC_left_item';
 4320:     if ($css_class) {
 4321:         $css_class = ' class="'.$css_class.'"';
 4322:     }
 4323:     if ($rowid) {
 4324:         $rowid = ' id="'.$rowid.'"';
 4325:     }
 4326:     if ($rowstyle) {
 4327:         $rowstyle = ' style="'.$rowstyle.'"';
 4328:     }
 4329:     my ($output,$description);
 4330:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4331:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4332:               "<td>$description</td>\n".
 4333:               '<td class="'.$class.'" colspan="2">'.
 4334:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4335:               '<span class="LC_nobreak">';
 4336:     if (ref($contacts) eq 'ARRAY') {
 4337:         foreach my $item (@{$contacts}) {
 4338:             my $check;
 4339:             if (ref($checked) eq 'HASH') {
 4340:                $check = $checked->{$item};
 4341:             }
 4342:             my $title;
 4343:             if (ref($short_titles) eq 'HASH') {
 4344:                 $title = $short_titles->{$item}; 
 4345:             }
 4346:             $output .= '<label>'.
 4347:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4348:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4349:         }
 4350:     }
 4351:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4352:                '<input type="text" name="override_'.$type.'_others" '.
 4353:                'value="'.$otheremails.'"  />';
 4354:     my %locchecked;
 4355:     foreach my $loc ('s','b') {
 4356:         if ($includeloc eq $loc) {
 4357:             $locchecked{$loc} = ' checked="checked"';
 4358:             last;
 4359:         }
 4360:     }
 4361:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4362:                '<input type="text" name="override_'.$type.'_bcc" '.
 4363:                'value="'.$bccemails.'"  /></fieldset>'.
 4364:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4365:                &mt('Text automatically added to e-mail:').' '.
 4366:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4367:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4368:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4369:                ('&nbsp;'x2).
 4370:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4371:                '</span></fieldset>'.
 4372:                '</td></tr>'."\n";
 4373:     return $output;
 4374: }
 4375: 
 4376: sub contacts_javascript {
 4377:     return <<"ENDSCRIPT";
 4378: 
 4379: <script type="text/javascript">
 4380: // <![CDATA[
 4381: 
 4382: function screenshotSize(field) {
 4383:     if (document.getElementById('help_screenshotsize')) {
 4384:         if (field.value == 'no') {
 4385:             document.getElementById('help_screenshotsize').style.display="none";
 4386:         } else {
 4387:             document.getElementById('help_screenshotsize').style.display="";
 4388:         }
 4389:     }
 4390:     return;
 4391: }
 4392: 
 4393: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4394:     if (form.elements[checkbox].length != undefined) {
 4395:         var count = 0;
 4396:         if (docount) {
 4397:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4398:                 if (form.elements[checkbox][i].checked) {
 4399:                     count ++;
 4400:                 }
 4401:             }
 4402:         }
 4403:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4404:             var type = form.elements[checkbox][i].value;
 4405:             if (document.getElementById(prefix+type)) {
 4406:                 if (form.elements[checkbox][i].checked) {
 4407:                     document.getElementById(prefix+type).style.display = 'table-row';
 4408:                     if (count % 2 == 1) {
 4409:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4410:                     } else {
 4411:                         document.getElementById(prefix+type).className = target;
 4412:                     }
 4413:                     count ++;
 4414:                 } else {
 4415:                     document.getElementById(prefix+type).style.display = 'none';
 4416:                 }
 4417:             }
 4418:         }
 4419:     }
 4420:     return;
 4421: }
 4422: 
 4423: 
 4424: // ]]>
 4425: </script>
 4426: 
 4427: ENDSCRIPT
 4428: }
 4429: 
 4430: sub print_helpsettings {
 4431:     my ($position,$dom,$settings,$rowtotal) = @_;
 4432:     my $confname = $dom.'-domainconfig';
 4433:     my $formname = 'display';
 4434:     my ($datatable,$itemcount);
 4435:     if ($position eq 'top') {
 4436:         $itemcount = 1;
 4437:         my (%choices,%defaultchecked,@toggles);
 4438:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4439:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4440:                                      &mt('LON-CAPA bug tracker'),600,500));
 4441:         %defaultchecked = ('submitbugs' => 'on');
 4442:         @toggles = ('submitbugs');
 4443:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4444:                                                      \%choices,$itemcount);
 4445:         $$rowtotal ++;
 4446:     } else {
 4447:         my $css_class;
 4448:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4449:         my (%customroles,%ordered,%current);
 4450:         if (ref($settings) eq 'HASH') {
 4451:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4452:                 %current = %{$settings->{'adhoc'}};
 4453:             }
 4454:         }
 4455:         my $count = 0;
 4456:         foreach my $key (sort(keys(%existing))) {
 4457:             if ($key=~/^rolesdef\_(\w+)$/) {
 4458:                 my $rolename = $1;
 4459:                 my (%privs,$order);
 4460:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4461:                 $customroles{$rolename} = \%privs;
 4462:                 if (ref($current{$rolename}) eq 'HASH') {
 4463:                     $order = $current{$rolename}{'order'};
 4464:                 }
 4465:                 if ($order eq '') {
 4466:                     $order = $count;
 4467:                 }
 4468:                 $ordered{$order} = $rolename;
 4469:                 $count++;
 4470:             }
 4471:         }
 4472:         my $maxnum = scalar(keys(%ordered));
 4473:         my @roles_by_num = ();
 4474:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4475:             push(@roles_by_num,$item);
 4476:         }
 4477:         my $context = 'domprefs';
 4478:         my $crstype = 'Course';
 4479:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4480:         my @accesstypes = ('all','dh','da','none');
 4481:         my ($numstatustypes,@jsarray);
 4482:         if (ref($types) eq 'ARRAY') {
 4483:             if (@{$types} > 0) {
 4484:                 $numstatustypes = scalar(@{$types});
 4485:                 push(@accesstypes,'status');
 4486:                 @jsarray = ('bystatus');
 4487:             }
 4488:         }
 4489:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4490:         if (keys(%domhelpdesk)) {
 4491:             push(@accesstypes,('inc','exc'));
 4492:             push(@jsarray,('notinc','notexc'));
 4493:         }
 4494:         my $hiddenstr = join("','",@jsarray);
 4495:         my $context = 'domprefs';
 4496:         my $crstype = 'Course';
 4497:         my $prefix = 'helproles_';
 4498:         my $add_class = 'LC_hidden';
 4499:         foreach my $num (@roles_by_num) {
 4500:             my $role = $ordered{$num};
 4501:             my ($desc,$access,@statuses);
 4502:             if (ref($current{$role}) eq 'HASH') {
 4503:                 $desc = $current{$role}{'desc'};
 4504:                 $access = $current{$role}{'access'};
 4505:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4506:                     @statuses = @{$current{$role}{'insttypes'}};
 4507:                 }
 4508:             }
 4509:             if ($desc eq '') {
 4510:                 $desc = $role;
 4511:             }
 4512:             my $identifier = 'custhelp'.$num;
 4513:             my %full=();
 4514:             my %levels= (
 4515:                          course => {},
 4516:                          domain => {},
 4517:                          system => {},
 4518:                         );
 4519:             my %levelscurrent=(
 4520:                                course => {},
 4521:                                domain => {},
 4522:                                system => {},
 4523:                               );
 4524:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4525:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4526:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4527:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4528:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4529:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4530:             for (my $k=0; $k<=$maxnum; $k++) {
 4531:                 my $vpos = $k+1;
 4532:                 my $selstr;
 4533:                 if ($k == $num) {
 4534:                     $selstr = ' selected="selected" ';
 4535:                 }
 4536:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4537:             }
 4538:             $datatable .= '</select>'.('&nbsp;'x2).
 4539:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4540:                           '</td>'.
 4541:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4542:                           &mt('Name shown to users:').
 4543:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4544:                           '</fieldset>'.
 4545:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4546:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4547:                           '<fieldset>'.
 4548:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4549:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4550:                                                                    \%levelscurrent,$identifier,
 4551:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4552:                           '</fieldset></td>';
 4553:             $itemcount ++;
 4554:         }
 4555:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4556:         my $newcust = 'custhelp'.$count;
 4557:         my (%privs,%levelscurrent);
 4558:         my %full=();
 4559:         my %levels= (
 4560:                      course => {},
 4561:                      domain => {},
 4562:                      system => {},
 4563:                     );
 4564:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4565:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4566:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4567:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4568:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4569:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4570:         for (my $k=0; $k<$maxnum+1; $k++) {
 4571:             my $vpos = $k+1;
 4572:             my $selstr;
 4573:             if ($k == $maxnum) {
 4574:                 $selstr = ' selected="selected" ';
 4575:             }
 4576:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4577:         }
 4578:         $datatable .= '</select>&nbsp;'."\n".
 4579:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4580:                       '</label></span></td>'.
 4581:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4582:                       '<span class="LC_nobreak">'.
 4583:                       &mt('Internal name:').
 4584:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4585:                       '</span>'.('&nbsp;'x4).
 4586:                       '<span class="LC_nobreak">'.
 4587:                       &mt('Name shown to users:').
 4588:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4589:                       '</span></fieldset>'.
 4590:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4591:                                              $usertypes,$types,\%domhelpdesk).
 4592:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4593:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4594:                                                                 \@templateroles,$newcust).
 4595:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4596:                                                                \%levelscurrent,$newcust).
 4597:                       '</fieldset>'.
 4598:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4599:                       '</td></tr>';
 4600:         $count ++;
 4601:         $$rowtotal += $count;
 4602:     }
 4603:     return $datatable;
 4604: }
 4605: 
 4606: sub adhocbutton {
 4607:     my ($prefix,$num,$field,$visibility) = @_;
 4608:     my %lt = &Apache::lonlocal::texthash(
 4609:                                           show => 'Show details',
 4610:                                           hide => 'Hide details',
 4611:                                         );
 4612:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4613:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4614:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4615:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4616: }
 4617: 
 4618: sub helpsettings_javascript {
 4619:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4620:     return unless(ref($roles_by_num) eq 'ARRAY');
 4621:     my %html_js_lt = &Apache::lonlocal::texthash(
 4622:                                           show => 'Show details',
 4623:                                           hide => 'Hide details',
 4624:                                         );
 4625:     &html_escape(\%html_js_lt);
 4626:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4627:     return <<"ENDSCRIPT";
 4628: <script type="text/javascript">
 4629: // <![CDATA[
 4630: 
 4631: function reorderHelpRoles(form,item) {
 4632:     var changedVal;
 4633: $jstext
 4634:     var newpos = 'helproles_${total}_pos';
 4635:     var maxh = 1 + $total;
 4636:     var current = new Array();
 4637:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4638:     if (item == newpos) {
 4639:         changedVal = newitemVal;
 4640:     } else {
 4641:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4642:         current[newitemVal] = newpos;
 4643:     }
 4644:     for (var i=0; i<helproles.length; i++) {
 4645:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4646:         if (elementName != item) {
 4647:             if (form.elements[elementName]) {
 4648:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4649:                 current[currVal] = elementName;
 4650:             }
 4651:         }
 4652:     }
 4653:     var oldVal;
 4654:     for (var j=0; j<maxh; j++) {
 4655:         if (current[j] == undefined) {
 4656:             oldVal = j;
 4657:         }
 4658:     }
 4659:     if (oldVal < changedVal) {
 4660:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4661:            var elementName = current[k];
 4662:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4663:         }
 4664:     } else {
 4665:         for (var k=changedVal; k<oldVal; k++) {
 4666:             var elementName = current[k];
 4667:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4668:         }
 4669:     }
 4670:     return;
 4671: }
 4672: 
 4673: function helpdeskAccess(num) {
 4674:     var curraccess = null;
 4675:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4676:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4677:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4678:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4679:             }
 4680:         }
 4681:     }
 4682:     var shown = Array();
 4683:     var hidden = Array();
 4684:     if (curraccess == 'none') {
 4685:         hidden = Array('$hiddenstr');
 4686:     } else {
 4687:         if (curraccess == 'status') {
 4688:             shown = Array('bystatus');
 4689:             hidden = Array('notinc','notexc');
 4690:         } else {
 4691:             if (curraccess == 'exc') {
 4692:                 shown = Array('notexc');
 4693:                 hidden = Array('notinc','bystatus');
 4694:             }
 4695:             if (curraccess == 'inc') {
 4696:                 shown = Array('notinc');
 4697:                 hidden = Array('notexc','bystatus');
 4698:             }
 4699:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4700:                 hidden = Array('notinc','notexc','bystatus');
 4701:             }
 4702:         }
 4703:     }
 4704:     if (hidden.length > 0) {
 4705:         for (var i=0; i<hidden.length; i++) {
 4706:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4707:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4708:             }
 4709:         }
 4710:     }
 4711:     if (shown.length > 0) {
 4712:         for (var i=0; i<shown.length; i++) {
 4713:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4714:                 if (shown[i] == 'privs') {
 4715:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4716:                 } else {
 4717:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4718:                 }
 4719:             }
 4720:         }
 4721:     }
 4722:     return;
 4723: }
 4724: 
 4725: function toggleHelpdeskItem(num,field) {
 4726:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4727:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4728:             document.getElementById('helproles_'+num+'_'+field).className =
 4729:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4730:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4731:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4732:             }
 4733:         } else {
 4734:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4735:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4736:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4737:             }
 4738:         }
 4739:     }
 4740:     return;
 4741: }
 4742: 
 4743: // ]]>
 4744: </script>
 4745: 
 4746: ENDSCRIPT
 4747: }
 4748: 
 4749: sub helpdeskroles_access {
 4750:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4751:         $usertypes,$types,$domhelpdesk) = @_;
 4752:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4753:     my %lt = &Apache::lonlocal::texthash(
 4754:                     'rou'    => 'Role usage',
 4755:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4756:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4757:                     'dh'     => 'All with domain helpdesk role',
 4758:                     'da'     => 'All with domain helpdesk assistant role',
 4759:                     'none'   => 'None',
 4760:                     'status' => 'Determined based on institutional status',
 4761:                     'inc'    => 'Include all, but exclude specific personnel',
 4762:                     'exc'    => 'Exclude all, but include specific personnel',
 4763:                   );
 4764:     my %usecheck = (
 4765:                      all => ' checked="checked"',
 4766:                    );
 4767:     my %displaydiv = (
 4768:                       status => 'none',
 4769:                       inc    => 'none',
 4770:                       exc    => 'none',
 4771:                       priv   => 'block',
 4772:                      );
 4773:     my $output;
 4774:     if (ref($current) eq 'HASH') {
 4775:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4776:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4777:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4778:                 delete($usecheck{'all'});
 4779:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4780:                     my $access = $1;
 4781:                     $displaydiv{$access} = 'inline';
 4782:                 } elsif ($current->{access} eq 'none') {
 4783:                     $displaydiv{'priv'} = 'none';
 4784:                 }
 4785:             }
 4786:         }
 4787:     }
 4788:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4789:               '<p>'.$lt{'whi'}.'</p>';
 4790:     foreach my $access (@{$accesstypes}) {
 4791:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4792:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4793:                    $lt{$access}.'</label>';
 4794:         if ($access eq 'status') {
 4795:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4796:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4797:                                                                  $othertitle,$usertypes,$types).
 4798:                        '</div>';
 4799:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4800:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4801:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4802:                        '</div>';
 4803:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4804:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4805:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4806:                        '</div>';
 4807:         }
 4808:         $output .= '</p>';
 4809:     }
 4810:     $output .= '</fieldset>';
 4811:     return $output;
 4812: }
 4813: 
 4814: sub radiobutton_prefs {
 4815:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4816:         $additional,$align,$firstval) = @_;
 4817:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4818:                    (ref($choices) eq 'HASH'));
 4819: 
 4820:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4821: 
 4822:     foreach my $item (@{$toggles}) {
 4823:         if ($defaultchecked->{$item} eq 'on') {
 4824:             $checkedon{$item} = ' checked="checked" ';
 4825:             $checkedoff{$item} = ' ';
 4826:         } elsif ($defaultchecked->{$item} eq 'off') {
 4827:             $checkedoff{$item} = ' checked="checked" ';
 4828:             $checkedon{$item} = ' ';
 4829:         }
 4830:     }
 4831:     if (ref($settings) eq 'HASH') {
 4832:         foreach my $item (@{$toggles}) {
 4833:             if ($settings->{$item} eq '1') {
 4834:                 $checkedon{$item} =  ' checked="checked" ';
 4835:                 $checkedoff{$item} = ' ';
 4836:             } elsif ($settings->{$item} eq '0') {
 4837:                 $checkedoff{$item} =  ' checked="checked" ';
 4838:                 $checkedon{$item} = ' ';
 4839:             }
 4840:         }
 4841:     }
 4842:     if ($onclick) {
 4843:         $onclick = ' onclick="'.$onclick.'"';
 4844:     }
 4845:     foreach my $item (@{$toggles}) {
 4846:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4847:         $datatable .=
 4848:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4849:             '<span class="LC_nobreak">'.$choices->{$item}.
 4850:             '</span></td>';
 4851:         if ($align eq 'left') {
 4852:             $datatable .= '<td class="LC_left_item">';
 4853:         } else {
 4854:             $datatable .= '<td class="LC_right_item">';
 4855:         }
 4856:         $datatable .= '<span class="LC_nobreak">';
 4857:         if ($firstval eq 'no') {
 4858:             $datatable .=
 4859:                 '<label><input type="radio" name="'.
 4860:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 4861:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4862:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 4863:         } else {
 4864:             $datatable .=
 4865:                 '<label><input type="radio" name="'.
 4866:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4867:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4868:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 4869:         }
 4870:         $datatable .= '</span>'.$additional.'</td></tr>';
 4871:         $itemcount ++;
 4872:     }
 4873:     return ($datatable,$itemcount);
 4874: }
 4875: 
 4876: sub print_ltitools {
 4877:     my ($dom,$settings,$rowtotal) = @_;
 4878:     my $rownum = 0;
 4879:     my $css_class;
 4880:     my $itemcount = 1;
 4881:     my $maxnum = 0;
 4882:     my %ordered;
 4883:     if (ref($settings) eq 'HASH') {
 4884:         foreach my $item (keys(%{$settings})) {
 4885:             if (ref($settings->{$item}) eq 'HASH') {
 4886:                 my $num = $settings->{$item}{'order'};
 4887:                 $ordered{$num} = $item;
 4888:             }
 4889:         }
 4890:     }
 4891:     my $confname = $dom.'-domainconfig';
 4892:     my $switchserver = &check_switchserver($dom,$confname);
 4893:     my $maxnum = scalar(keys(%ordered));
 4894:     my $datatable;
 4895:     my %lt = &ltitools_names();
 4896:     my @courseroles = ('cc','in','ta','ep','st');
 4897:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4898:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4899:     if (keys(%ordered)) {
 4900:         my @items = sort { $a <=> $b } keys(%ordered);
 4901:         for (my $i=0; $i<@items; $i++) {
 4902:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4903:             my $item = $ordered{$items[$i]};
 4904:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4905:             if (ref($settings->{$item}) eq 'HASH') {
 4906:                 $title = $settings->{$item}->{'title'};
 4907:                 $url = $settings->{$item}->{'url'};
 4908:                 $key = $settings->{$item}->{'key'};
 4909:                 $secret = $settings->{$item}->{'secret'};
 4910:                 $lifetime = $settings->{$item}->{'lifetime'};
 4911:                 my $image = $settings->{$item}->{'image'};
 4912:                 if ($image ne '') {
 4913:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4914:                 }
 4915:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4916:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4917:                 } else {
 4918:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4919:                 }
 4920:             }
 4921:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4922:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4923:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4924:             for (my $k=0; $k<=$maxnum; $k++) {
 4925:                 my $vpos = $k+1;
 4926:                 my $selstr;
 4927:                 if ($k == $i) {
 4928:                     $selstr = ' selected="selected" ';
 4929:                 }
 4930:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4931:             }
 4932:             $datatable .= '</select>'.('&nbsp;'x2).
 4933:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4934:                 &mt('Delete?').'</label></span></td>'.
 4935:                 '<td colspan="2">'.
 4936:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4937:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4938:                 ('&nbsp;'x2).
 4939:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4940:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4941:                 ('&nbsp;'x2).
 4942:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4943:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4944:                 ('&nbsp;'x2).
 4945:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4946:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4947:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4948:                 '<br /><br />'.
 4949:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4950:                 ' value="'.$url.'" /></span>'.
 4951:                 ('&nbsp;'x2).
 4952:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4953:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4954:                 ('&nbsp;'x2).
 4955:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4956:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4957:                 ('&nbsp;'x2).
 4958:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4959:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4960:                 '<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>'.
 4961:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4962:                 '</fieldset>'.
 4963:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4964:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4965:             my %currdisp;
 4966:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4967:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4968:                     $currdisp{'window'} = ' checked="checked"';
 4969:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4970:                     $currdisp{'tab'} = ' checked="checked"';
 4971:                 } else {
 4972:                     $currdisp{'iframe'} = ' checked="checked"';
 4973:                 }
 4974:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4975:                     $currdisp{'width'} = $1;
 4976:                 }
 4977:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4978:                      $currdisp{'height'} = $1;
 4979:                 }
 4980:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4981:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4982:             } else {
 4983:                 $currdisp{'iframe'} = ' checked="checked"';
 4984:             }
 4985:             foreach my $disp ('iframe','tab','window') {
 4986:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4987:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4988:             }
 4989:             $datatable .= ('&nbsp;'x4);
 4990:             foreach my $dimen ('width','height') {
 4991:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4992:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4993:                               ('&nbsp;'x2);
 4994:             }
 4995:             $datatable .= '</span><br />'.
 4996:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4997:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4998:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4999:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 5000:                           '</textarea></div><div style=""></div><br />';
 5001:             my %units = (
 5002:                           'passback' => 'days',
 5003:                           'roster'   => 'seconds',
 5004:                         );
 5005:             foreach my $extra ('passback','roster') {
 5006:                 my $validsty = 'none';
 5007:                 my $currvalid;
 5008:                 my $checkedon = '';
 5009:                 my $checkedoff = ' checked="checked"';
 5010:                 if ($settings->{$item}->{$extra}) {
 5011:                     $checkedon = $checkedoff;
 5012:                     $checkedoff = '';
 5013:                     $validsty = 'inline-block';
 5014:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 5015:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 5016:                     }
 5017:                 }
 5018:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 5019:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5020:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 5021:                               &mt('No').'</label>'.('&nbsp;'x2).
 5022:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 5023:                               &mt('Yes').'</label></span></div>'.
 5024:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 5025:                               '<span class="LC_nobreak">'.
 5026:                               &mt("at least [_1] $units{$extra} after launch",
 5027:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 5028:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5029:             }
 5030:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5031:             if ($imgsrc) {
 5032:                 $datatable .= $imgsrc.
 5033:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 5034:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 5035:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 5036:             } else {
 5037:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5038:             }
 5039:             if ($switchserver) {
 5040:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5041:             } else {
 5042:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 5043:             }
 5044:             $datatable .= '</span></fieldset>';
 5045:             my (%checkedfields,%rolemaps,$userincdom);
 5046:             if (ref($settings->{$item}) eq 'HASH') {
 5047:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 5048:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 5049:                 }
 5050:                 $userincdom = $settings->{$item}->{'incdom'};
 5051:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 5052:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 5053:                     $checkedfields{'roles'} = 1;
 5054:                 }
 5055:             }
 5056:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5057:                           '<span class="LC_nobreak">';
 5058:             my $userfieldstyle = 'display:none;';
 5059:             my $seluserdom = '';
 5060:             my $unseluserdom = ' selected="selected"';
 5061:             foreach my $field (@fields) {
 5062:                 my ($checked,$onclick,$id,$spacer);
 5063:                 if ($checkedfields{$field}) {
 5064:                     $checked = ' checked="checked"';
 5065:                 }
 5066:                 if ($field eq 'user') {
 5067:                     $id = ' id="ltitools_user_field_'.$i.'"';
 5068:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 5069:                     if ($checked) {
 5070:                         $userfieldstyle = 'display:inline-block';
 5071:                         if ($userincdom) {
 5072:                             $seluserdom = $unseluserdom;
 5073:                             $unseluserdom = '';
 5074:                         }
 5075:                     }
 5076:                 } else {
 5077:                     $spacer = ('&nbsp;' x2);
 5078:                 }
 5079:                 $datatable .= '<label>'.
 5080:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 5081:                               $lt{$field}.'</label>'.$spacer;
 5082:             }
 5083:             $datatable .= '</span>';
 5084:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 5085:                           '<span class="LC_nobreak"> : '.
 5086:                           '<select name="ltitools_userincdom_'.$i.'">'.
 5087:                           '<option value="">'.&mt('Select').'</option>'.
 5088:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 5089:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 5090:                           '</select></span></div>';
 5091:             $datatable .= '</fieldset>'.
 5092:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5093:             foreach my $role (@courseroles) {
 5094:                 my ($selected,$selectnone);
 5095:                 if (!$rolemaps{$role}) {
 5096:                     $selectnone = ' selected="selected"';
 5097:                 }
 5098:                 $datatable .= '<td style="text-align: center">'. 
 5099:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5100:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 5101:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5102:                 foreach my $ltirole (@ltiroles) {
 5103:                     unless ($selectnone) {
 5104:                         if ($rolemaps{$role} eq $ltirole) {
 5105:                             $selected = ' selected="selected"';
 5106:                         } else {
 5107:                             $selected = '';
 5108:                         }
 5109:                     }
 5110:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5111:                 }
 5112:                 $datatable .= '</select></td>';
 5113:             }
 5114:             $datatable .= '</tr></table></fieldset>';
 5115:             my %courseconfig;
 5116:             if (ref($settings->{$item}) eq 'HASH') {
 5117:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 5118:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 5119:                 }
 5120:             }
 5121:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5122:             foreach my $item ('label','title','target','linktext','explanation','append') {
 5123:                 my $checked;
 5124:                 if ($courseconfig{$item}) {
 5125:                     $checked = ' checked="checked"';
 5126:                 }
 5127:                 $datatable .= '<label>'.
 5128:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 5129:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 5130:             }
 5131:             $datatable .= '</span></fieldset>'.
 5132:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5133:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 5134:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 5135:                 my %custom = %{$settings->{$item}->{'custom'}};
 5136:                 if (keys(%custom) > 0) {
 5137:                     foreach my $key (sort(keys(%custom))) {
 5138:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 5139:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 5140:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5141:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 5142:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 5143:                     }
 5144:                 }
 5145:             }
 5146:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 5147:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 5148:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 5149:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 5150:             $datatable .= '</table></fieldset></td></tr>'."\n";
 5151:             $itemcount ++;
 5152:         }
 5153:     }
 5154:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5155:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 5156:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5157:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 5158:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 5159:     for (my $k=0; $k<$maxnum+1; $k++) {
 5160:         my $vpos = $k+1;
 5161:         my $selstr;
 5162:         if ($k == $maxnum) {
 5163:             $selstr = ' selected="selected" ';
 5164:         }
 5165:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5166:     }
 5167:     $datatable .= '</select>&nbsp;'."\n".
 5168:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5169:                   '<td colspan="2">'.
 5170:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5171:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 5172:                   ('&nbsp;'x2).
 5173:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 5174:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5175:                   ('&nbsp;'x2).
 5176:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 5177:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 5178:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 5179:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5180:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5181:                   '<br />'.
 5182:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 5183:                   ('&nbsp;'x2).
 5184:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 5185:                   ('&nbsp;'x2).
 5186:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 5187:                   ('&nbsp;'x2).
 5188:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 5189:                   '<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".
 5190:                   '</fieldset>'.
 5191:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5192:                   '<span class="LC_nobreak">'.&mt('Display target:');
 5193:     my %defaultdisp;
 5194:     $defaultdisp{'iframe'} = ' checked="checked"';
 5195:     foreach my $disp ('iframe','tab','window') {
 5196:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 5197:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 5198:     }
 5199:     $datatable .= ('&nbsp;'x4);
 5200:     foreach my $dimen ('width','height') {
 5201:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5202:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 5203:                       ('&nbsp;'x2);
 5204:     }
 5205:     $datatable .= '</span><br />'.
 5206:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5207:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 5208:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5209:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 5210:                   '</div><div style=""></div><br />';
 5211:     my %units = (
 5212:                   'passback' => 'days',
 5213:                   'roster'   => 'seconds',
 5214:                 );
 5215:     my %defaulttimes = (
 5216:                      'passback' => '7',
 5217:                      'roster'   => '300',
 5218:                    );
 5219:     foreach my $extra ('passback','roster') {
 5220:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 5221:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5222:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 5223:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 5224:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 5225:                       &mt('Yes').'</label></span></div>'.
 5226:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 5227:                       '<span class="LC_nobreak">'.
 5228:                       &mt("at least [_1] $units{$extra} after launch",
 5229:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 5230:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5231:     }
 5232:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 5233:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5234:     if ($switchserver) {
 5235:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5236:     } else {
 5237:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 5238:     }
 5239:     $datatable .= '</span></fieldset>'.
 5240:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5241:                   '<span class="LC_nobreak">';
 5242:     foreach my $field (@fields) {
 5243:         my ($id,$onclick,$spacer);
 5244:         if ($field eq 'user') {
 5245:             $id = ' id="ltitools_user_field_add"';
 5246:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 5247:         } else {
 5248:             $spacer = ('&nbsp;' x2);
 5249:         }
 5250:         $datatable .= '<label>'.
 5251:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 5252:                       $lt{$field}.'</label>'.$spacer;
 5253:     }
 5254:     $datatable .= '</span>'.
 5255:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 5256:                   '<span class="LC_nobreak"> : '.
 5257:                   '<select name="ltitools_userincdom_add">'.
 5258:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 5259:                   '<option value="0">'.&mt('username').'</option>'.
 5260:                   '<option value="1">'.&mt('username:domain').'</option>'.
 5261:                   '</select></span></div></fieldset>';
 5262:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5263:     foreach my $role (@courseroles) {
 5264:         my ($checked,$checkednone);
 5265:         $datatable .= '<td style="text-align: center">'.
 5266:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5267:                       '<select name="ltitools_add_roles_'.$role.'">'.
 5268:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 5269:         foreach my $ltirole (@ltiroles) {
 5270:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 5271:         }
 5272:         $datatable .= '</select></td>';
 5273:     }
 5274:     $datatable .= '</tr></table></fieldset>'.
 5275:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5276:     foreach my $item ('label','title','target','linktext','explanation','append') {
 5277:         $datatable .= '<label>'.
 5278:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 5279:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 5280:     }
 5281:     $datatable .= '</span></fieldset>'.
 5282:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5283:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5284:                   '<tr><td><span class="LC_nobreak">'.
 5285:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 5286:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 5287:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 5288:                   '</table></fieldset>'."\n".
 5289:                   '</td>'."\n".
 5290:                   '</tr>'."\n";
 5291:     $itemcount ++;
 5292:     return $datatable;
 5293: }
 5294: 
 5295: sub ltitools_names {
 5296:     my %lt = &Apache::lonlocal::texthash(
 5297:                                           'title'          => 'Title',
 5298:                                           'version'        => 'Version',
 5299:                                           'msgtype'        => 'Message Type',
 5300:                                           'sigmethod'      => 'Signature Method',
 5301:                                           'url'            => 'URL',
 5302:                                           'key'            => 'Key',
 5303:                                           'lifetime'       => 'Nonce lifetime (s)',
 5304:                                           'secret'         => 'Secret',
 5305:                                           'icon'           => 'Icon',   
 5306:                                           'user'           => 'User',
 5307:                                           'fullname'       => 'Full Name',
 5308:                                           'firstname'      => 'First Name',
 5309:                                           'lastname'       => 'Last Name',
 5310:                                           'email'          => 'E-mail',
 5311:                                           'roles'          => 'Role',
 5312:                                           'window'         => 'Window',
 5313:                                           'tab'            => 'Tab',
 5314:                                           'iframe'         => 'iFrame',
 5315:                                           'height'         => 'Height',
 5316:                                           'width'          => 'Width',
 5317:                                           'linktext'       => 'Default Link Text',
 5318:                                           'explanation'    => 'Default Explanation',
 5319:                                           'passback'       => 'Tool can return grades:',
 5320:                                           'roster'         => 'Tool can retrieve roster:',
 5321:                                           'crstarget'      => 'Display target',
 5322:                                           'crslabel'       => 'Course label',
 5323:                                           'crstitle'       => 'Course title', 
 5324:                                           'crslinktext'    => 'Link Text',
 5325:                                           'crsexplanation' => 'Explanation',
 5326:                                           'crsappend'      => 'Provider URL',
 5327:                                         );
 5328:     return %lt;
 5329: }
 5330: 
 5331: sub print_proctoring {
 5332:     my ($dom,$settings,$rowtotal) = @_;
 5333:     my $itemcount = 1;
 5334:     my (%ordered,%providernames,%current,%currentdef);
 5335:     my $confname = $dom.'-domainconfig';
 5336:     my $switchserver = &check_switchserver($dom,$confname);
 5337:     if (ref($settings) eq 'HASH') {
 5338:         foreach my $item (keys(%{$settings})) {
 5339:             if (ref($settings->{$item}) eq 'HASH') {
 5340:                 my $num = $settings->{$item}{'order'};
 5341:                 $ordered{$num} = $item;
 5342:             }
 5343:         }
 5344:     } else {
 5345:         %ordered = (
 5346:                      1 => 'proctorio',
 5347:                      2 => 'examity',
 5348:                    );
 5349:     }
 5350:     %providernames = &proctoring_providernames();
 5351:     my $maxnum = scalar(keys(%ordered));
 5352:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5353:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5354:     if (ref($requref) eq 'HASH') {
 5355:         %requserfields = %{$requref};
 5356:     }
 5357:     if (ref($opturef) eq 'HASH') {
 5358:         %optuserfields = %{$opturef};
 5359:     }
 5360:     if (ref($defref) eq 'HASH') {
 5361:         %defaults = %{$defref};
 5362:     }
 5363:     if (ref($extref) eq 'HASH') {
 5364:         %extended = %{$extref};
 5365:     }
 5366:     if (ref($crsref) eq 'HASH') {
 5367:         %crsconf = %{$crsref};
 5368:     }
 5369:     if (ref($rolesref) eq 'ARRAY') {
 5370:         @courseroles = @{$rolesref};
 5371:     }
 5372:     if (ref($ltiref) eq 'ARRAY') {
 5373:         @ltiroles = @{$ltiref};
 5374:     }
 5375:     my $datatable;
 5376:     my $css_class;
 5377:     if (keys(%ordered)) {
 5378:         my @items = sort { $a <=> $b } keys(%ordered);
 5379:         for (my $i=0; $i<@items; $i++) {
 5380:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5381:             my $provider = $ordered{$items[$i]};
 5382:             my $optionsty = 'none';
 5383:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5384:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5385:             if (ref($settings) eq 'HASH') {
 5386:                 if (ref($settings->{$provider}) eq 'HASH') {
 5387:                     %current = %{$settings->{$provider}};
 5388:                     if ($current{'available'}) {
 5389:                         $optionsty = 'block';
 5390:                         $available = 1;
 5391:                     }
 5392:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5393:                         $lifetime = $current{'lifetime'};
 5394:                     }
 5395:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5396:                         $version = $current{'version'};
 5397:                     }
 5398:                     if ($current{'image'} ne '') {
 5399:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5400:                     }
 5401:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5402:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5403:                     }
 5404:                     $userincdom = $current{'incdom'};
 5405:                     if (ref($current{'roles'}) eq 'HASH') {
 5406:                         %rolemaps = %{$current{'roles'}};
 5407:                         $checkedfields{'roles'} = 1;
 5408:                     }
 5409:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5410:                         foreach my $val (@{$current{'defaults'}}) {
 5411:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5412:                                 $inuse{$val} = 1;
 5413:                             } else {
 5414:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5415:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5416:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5417:                                             $inuse{$poss} = $val;
 5418:                                             last;
 5419:                                         }
 5420:                                     }
 5421:                                 }
 5422:                             }
 5423:                         }
 5424:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5425:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5426:                             my $currval = $current{'defaults'}{$key}; 
 5427:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5428:                                 $inuse{$key} = 1;
 5429:                             } else {
 5430:                                 my $match;
 5431:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5432:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5433:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5434:                                             $inuse{$poss} = $key;
 5435:                                             last;
 5436:                                         }
 5437:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5438:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5439:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5440:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5441:                                                     $currentdef{$inner} = $currval;
 5442:                                                     $match = 1;
 5443:                                                     last;
 5444:                                                 }
 5445:                                             } elsif ($inner eq $key) {
 5446:                                                 $currentdef{$key} = $currval;
 5447:                                                 $match = 1;
 5448:                                                 last;
 5449:                                             }
 5450:                                         }
 5451:                                     }
 5452:                                     last if ($match);
 5453:                                 }
 5454:                             }
 5455:                         }
 5456:                     }
 5457:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5458:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5459:                     }
 5460:                 }
 5461:             }
 5462:             my %lt = &proctoring_titles($provider);
 5463:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5464:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5465:             my %checkedavailable = (
 5466:                                    yes => '',
 5467:                                    no  => ' checked="checked"',
 5468:                                  );
 5469:             if ($available) {
 5470:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5471:                 $checkedavailable{'no'} = '';
 5472:             }
 5473:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5474:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5475:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5476:             for (my $k=0; $k<$maxnum; $k++) {
 5477:                 my $vpos = $k+1;
 5478:                 my $selstr;
 5479:                 if ($k == $i) {
 5480:                     $selstr = ' selected="selected" ';
 5481:                 }
 5482:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5483:             }
 5484:             if ($version eq '') {
 5485:                 if ($provider eq 'proctorio') {
 5486:                     $version = '1.0';
 5487:                 } elsif ($provider eq 'examity') {
 5488:                     $version = '1.1';
 5489:                 }
 5490:             }
 5491:             if ($lifetime eq '') {
 5492:                 $lifetime = '300';
 5493:             }
 5494:             $datatable .=
 5495:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5496:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5497:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5498:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5499:                 '</td>'.
 5500:                 '<td colspan="2">'.
 5501:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5502:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5503:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5504:                 ('&nbsp;'x2).
 5505:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5506:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5507:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5508:                 ('&nbsp;'x2).
 5509:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5510:                 '<br />'.
 5511:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5512:                 '<br />'.
 5513:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5514:                 ('&nbsp;'x2).
 5515:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5516:                 '<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";
 5517:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5518:             if ($imgsrc) {
 5519:                 $datatable .= $imgsrc.
 5520:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5521:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5522:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5523:             }
 5524:             $datatable .= '&nbsp;';
 5525:             if ($switchserver) {
 5526:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5527:             } else {
 5528:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5529:             }
 5530:             unless ($imgsrc) {
 5531:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5532:             }
 5533:             $datatable .= '</fieldset>'."\n";
 5534:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5535:                 if (@{$requserfields{$provider}} > 0) {
 5536:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5537:                     foreach my $field (@{$requserfields{$provider}}) {
 5538:                         $datatable .= '<span class="LC_nobreak">'.
 5539:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5540:                                       $lt{$field}.'</label>';
 5541:                         if ($field eq 'user') {
 5542:                             my $seluserdom = '';
 5543:                             my $unseluserdom = ' selected="selected"';
 5544:                             if ($userincdom) {
 5545:                                 $seluserdom = $unseluserdom;
 5546:                                 $unseluserdom = '';
 5547:                             }
 5548:                             $datatable .= ':&nbsp;'.
 5549:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5550:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5551:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5552:                                 '</select>&nbsp;';
 5553:                         } else {
 5554:                             $datatable .= '&nbsp;';
 5555:                             if ($field eq 'roles') {
 5556:                                 $showroles = 1; 
 5557:                             } 
 5558:                         }
 5559:                         $datatable .= '</span> ';
 5560:                     }
 5561:                 }
 5562:                 $datatable .= '</fieldset>'."\n";
 5563:             }
 5564:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5565:                 if (@{$optuserfields{$provider}} > 0) {
 5566:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5567:                     foreach my $field (@{$optuserfields{$provider}}) {
 5568:                         my $checked;
 5569:                         if ($checkedfields{$field}) {
 5570:                             $checked = ' checked="checked"';
 5571:                         }
 5572:                         $datatable .= '<span class="LC_nobreak">'.
 5573:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5574:                     }
 5575:                     $datatable .= '</fieldset>'."\n";
 5576:                 }
 5577:             }
 5578:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5579:                 if (@{$defaults{$provider}}) {
 5580:                     my (%options,@selectboxes);
 5581:                     if (ref($extended{$provider}) eq 'HASH') {
 5582:                         %options = %{$extended{$provider}};
 5583:                     }
 5584:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5585:                     my ($rem,$numinrow,$dropdowns);
 5586:                     if ($provider eq 'proctorio') { 
 5587:                         $datatable .= '<table>';
 5588:                         $numinrow = 4;
 5589:                     }
 5590:                     my $i = 0;
 5591:                     foreach my $field (@{$defaults{$provider}}) {
 5592:                         my $checked;
 5593:                         if ($inuse{$field}) {
 5594:                             $checked = ' checked="checked"';
 5595:                         }
 5596:                         if ($provider eq 'examity') {
 5597:                             if ($field eq 'display') {
 5598:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5599:                                 foreach my $option ('iframe','tab','window') {
 5600:                                     my $checkdisp;
 5601:                                     if ($currentdef{'target'} eq $option) {
 5602:                                         $checkdisp = ' checked="checked"';
 5603:                                     }
 5604:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5605:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5606:                                 }
 5607:                                 $datatable .= ('&nbsp;'x4);
 5608:                                 foreach my $dimen ('width','height') {
 5609:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5610:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5611:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5612:                                                   ('&nbsp;'x2);
 5613:                                 }
 5614:                                 $datatable .= '</span><br />'.
 5615:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5616:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5617:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5618:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5619:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5620:                                               $currentdef{'explanation'}.
 5621:                                               '</textarea></div><div style=""></div><br />';
 5622:                             }
 5623:                         } else {
 5624:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5625:                                 my ($output,$selnone);
 5626:                                 unless ($checked) {
 5627:                                     $selnone = ' selected="selected"';
 5628:                                 }
 5629:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5630:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5631:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5632:                                 foreach my $option (@{$options{$field}}) {
 5633:                                     my $sel; 
 5634:                                     if ($inuse{$field} eq $option) {
 5635:                                         $sel = ' selected="selected"';
 5636:                                     }
 5637:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5638:                                 }
 5639:                                 $output .= '</select></span>';
 5640:                                 push(@selectboxes,$output);
 5641:                             } else {
 5642:                                 $rem = $i%($numinrow);
 5643:                                 if ($rem == 0) {
 5644:                                     if ($i > 0) {
 5645:                                         $datatable .= '</tr>';
 5646:                                     }
 5647:                                     $datatable .= '<tr>';
 5648:                                 }
 5649:                                 $datatable .= '<td class="LC_left_item">'.
 5650:                                               '<span class="LC_nobreak">'.
 5651:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5652:                                               $fieldtitles{$field}.'</label></span></td>';
 5653:                                 $i++;
 5654:                             }
 5655:                         }
 5656:                     }
 5657:                     if ($provider eq 'proctorio') {
 5658:                         if ($numinrow) {
 5659:                             $rem = $i%$numinrow;
 5660:                         }
 5661:                         my $colsleft = $numinrow - $rem;
 5662:                         if ($colsleft > 1) {
 5663:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5664:                         } else {
 5665:                             $datatable .= '<td class="LC_left_item">';
 5666:                         }
 5667:                         $datatable .= '&nbsp;'.
 5668:                                       '</td></tr></table>';
 5669:                         if (@selectboxes) {
 5670:                             $datatable .= '<hr /><table>';
 5671:                             $numinrow = 2;
 5672:                             for (my $i=0; $i<@selectboxes; $i++) {
 5673:                                 $rem = $i%($numinrow);
 5674:                                 if ($rem == 0) {
 5675:                                     if ($i > 0) {
 5676:                                         $datatable .= '</tr>';
 5677:                                     }
 5678:                                     $datatable .= '<tr>';
 5679:                                 }
 5680:                                 $datatable .= '<td class="LC_left_item">'.
 5681:                                               $selectboxes[$i].'</td>';
 5682:                             }
 5683:                             if ($numinrow) {
 5684:                                 $rem = $i%$numinrow;
 5685:                             }
 5686:                             $colsleft = $numinrow - $rem;
 5687:                             if ($colsleft > 1) {
 5688:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5689:                             } else {
 5690:                                 $datatable .= '<td class="LC_left_item">';
 5691:                             }
 5692:                             $datatable .= '&nbsp;'.
 5693:                                           '</td></tr></table>';
 5694:                         }
 5695:                     }
 5696:                     $datatable .= '</fieldset>';
 5697:                 }
 5698:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5699:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5700:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5701:                     my ($rem,$numinrow);
 5702:                     if ($provider eq 'proctorio') {
 5703:                         $datatable .= '<table>';
 5704:                         $numinrow = 4;
 5705:                     }
 5706:                     my $i = 0;
 5707:                     foreach my $item (@{$crsconf{$provider}}) {
 5708:                         my $name;
 5709:                         if ($provider eq 'examity') {
 5710:                             $name = $lt{'crs'.$item};
 5711:                         } elsif ($provider eq 'proctorio') {
 5712:                             $name = $fieldtitles{$item};
 5713:                             $rem = $i%($numinrow);
 5714:                             if ($rem == 0) {
 5715:                                 if ($i > 0) {
 5716:                                     $datatable .= '</tr>';
 5717:                                 }
 5718:                                 $datatable .= '<tr>';
 5719:                             }
 5720:                             $datatable .= '<td class="LC_left_item>';
 5721:                         }
 5722:                         my $checked;
 5723:                         if ($crsconfig{$item}) {
 5724:                             $checked = ' checked="checked"';
 5725:                         }
 5726:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5727:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5728:                                       $name.'</label></span>';
 5729:                         if ($provider eq 'examity') {
 5730:                             $datatable .= '&nbsp; ';
 5731:                         }
 5732:                         $datatable .= "\n";
 5733:                         $i++;
 5734:                     }
 5735:                     if ($provider eq 'proctorio') {
 5736:                         if ($numinrow) {
 5737:                             $rem = $i%$numinrow;
 5738:                         }
 5739:                         my $colsleft = $numinrow - $rem;
 5740:                         if ($colsleft > 1) {
 5741:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5742:                         } else {
 5743:                             $datatable .= '<td class="LC_left_item">';
 5744:                         }
 5745:                         $datatable .= '&nbsp;'.
 5746:                                       '</td></tr></table>';
 5747:                     }
 5748:                     $datatable .= '</fieldset>';
 5749:                 }
 5750:                 if ($showroles) {
 5751:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5752:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5753:                     foreach my $role (@courseroles) {
 5754:                         my ($selected,$selectnone);
 5755:                         if (!$rolemaps{$role}) {
 5756:                             $selectnone = ' selected="selected"';
 5757:                         }
 5758:                         $datatable .= '<td style="text-align: center">'.
 5759:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5760:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5761:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5762:                         foreach my $ltirole (@ltiroles) {
 5763:                             unless ($selectnone) {
 5764:                                 if ($rolemaps{$role} eq $ltirole) {
 5765:                                     $selected = ' selected="selected"';
 5766:                                 } else {
 5767:                                     $selected = '';
 5768:                                 }
 5769:                             }
 5770:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5771:                         }
 5772:                         $datatable .= '</select></td>';
 5773:                     }
 5774:                     $datatable .= '</tr></table></fieldset>'.
 5775:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5776:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5777:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5778:                                   '<tr><td></td><td>lms</td>'.
 5779:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5780:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5781:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5782:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5783:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5784:                         if (keys(%custom) > 0) {
 5785:                             foreach my $key (sort(keys(%custom))) {
 5786:                                 next if ($key eq 'lms');
 5787:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5788:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5789:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5790:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5791:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5792:                             }
 5793:                         }
 5794:                     }
 5795:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5796:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5797:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5798:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5799:                                   '</table></fieldset></td></tr>'."\n";
 5800:                 }
 5801:                 $datatable .= '</td></tr>';
 5802:             }
 5803:             $itemcount ++;
 5804:         }
 5805:     }
 5806:     return $datatable;
 5807: }
 5808: 
 5809: sub proctoring_data {
 5810:     my $requserfields = {
 5811:                       proctorio => ['user'],
 5812:                       examity   => ['roles','user'],
 5813:                      };
 5814:     my $optuserfields = {
 5815:                         proctorio => ['fullname'],
 5816:                         examity   => ['fullname','firstname','lastname','email'],
 5817:                      };
 5818:     my $defaults = {
 5819:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5820:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5821:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5822:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 5823:                                 'reentry','calculator','whiteboard'],
 5824:                   examity   => ['display'],
 5825:                 };
 5826:     my $extended = { 
 5827:                   proctorio => {
 5828:                                  verifyid => ['verifyidauto','verifyidlive'],
 5829:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 5830:                                  tabslinks => ['notabs','linksonly'],
 5831:                                  reentry => ['noreentry','agentreentry'],
 5832:                                  calculator => ['calculatorbasic','calculatorsci'],
 5833:                                },
 5834:                   examity => {
 5835:                                display => {
 5836:                                            target      => ['iframe','tab','window'],
 5837:                                            width       => '',
 5838:                                            height      => '',
 5839:                                            linktext    => '',
 5840:                                            explanation => '',
 5841:                                           },
 5842:                              },
 5843:                 };
 5844:     my $crsconf = {
 5845:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5846:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5847:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5848:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 5849:                                'reentry','calculator','whiteboard'],
 5850:                  examity => ['label','title','target','linktext','explanation','append'],
 5851:                };
 5852:     my $courseroles = ['cc','in','ta','ep','st'];
 5853:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 5854:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 5855: }
 5856: 
 5857: sub proctoring_titles {
 5858:     my ($item) = @_;
 5859:     my (%common_lt,%custom_lt);
 5860:     %common_lt = &Apache::lonlocal::texthash (
 5861:                      'avai'      => 'Available?',
 5862:                      'base'      => 'Basic Settings',
 5863:                      'requ'      => 'User data required to be sent on launch',
 5864:                      'optu'      => 'User data optionally sent on launch',
 5865:                      'udsl'      => 'User data sent on launch',
 5866:                      'defa'      => 'Defaults for items configurable in course',
 5867:                      'sigmethod' => 'Signature Method',
 5868:                      'key'       => 'Key',
 5869:                      'lifetime'  => 'Nonce lifetime (s)',
 5870:                      'secret'    => 'Secret',
 5871:                      'icon'      => 'Icon',
 5872:                      'fullname'  => 'Full Name',
 5873:                      'visible'   => 'Visible input',
 5874:                      'username'  => 'username',
 5875:                      'user'      => 'User',
 5876:                  );
 5877:     if ($item eq 'proctorio') {
 5878:         %custom_lt = &Apache::lonlocal::texthash (
 5879:                          'version'        => 'OAuth version',
 5880:                          'url'            => 'API URL',
 5881:                          'uname:dom'      => 'username-domain',
 5882:         );
 5883:     } elsif ($item eq 'examity') {
 5884:         %custom_lt = &Apache::lonlocal::texthash (
 5885:                          'version'        => 'LTI Version',
 5886:                          'url'            => 'URL',
 5887:                          'uname:dom'      => 'username:domain',
 5888:                          'msgtype'        => 'Message Type',
 5889:                          'firstname'      => 'First Name',
 5890:                          'lastname'       => 'Last Name',
 5891:                          'email'          => 'E-mail',
 5892:                          'roles'          => 'Role',
 5893:                          'crstarget'      => 'Display target',
 5894:                          'crslabel'       => 'Course label',
 5895:                          'crstitle'       => 'Course title', 
 5896:                          'crslinktext'    => 'Link Text',
 5897:                          'crsexplanation' => 'Explanation',
 5898:                          'crsappend'      => 'Provider URL',
 5899:         );
 5900:     }
 5901:     my %lt = (%common_lt,%custom_lt);
 5902:     return %lt;
 5903: }
 5904: 
 5905: sub proctoring_fieldtitles {
 5906:     my ($item) = @_;
 5907:     if ($item eq 'proctorio') {
 5908:         return &Apache::lonlocal::texthash (
 5909:                   'recordvideo' => 'Record video',
 5910:                   'recordaudio' => 'Record audio',
 5911:                   'recordscreen' => 'Record screen',
 5912:                   'recordwebtraffic' => 'Record web traffic',
 5913:                   'recordroomstart' => 'Record room scan',
 5914:                   'verifyvideo' => 'Verify webcam',
 5915:                   'verifyaudio' => 'Verify microphone',
 5916:                   'verifydesktop' => 'Verify desktop recording',
 5917:                   'verifyid' => 'Photo ID verification',
 5918:                   'verifysignature' => 'Require signature',
 5919:                   'fullscreen' => 'Fullscreen',
 5920:                   'clipboard' => 'Disable copy/paste',
 5921:                   'tabslinks' => 'New tabs/windows',
 5922:                   'closetabs' => 'Close other tabs',
 5923:                   'onescreen' => 'Limit to single screen',
 5924:                   'print' => 'Disable Printing',
 5925:                   'downloads' => 'Disable Downloads',
 5926:                   'cache' => 'Empty cache after exam',
 5927:                   'rightclick' => 'Disable right click',
 5928:                   'reentry' => 'Re-entry to exam',
 5929:                   'calculator' => 'Onscreen calculator',
 5930:                   'whiteboard' => 'Onscreen whiteboard',
 5931:                   'verifyidauto' => 'Automated verification',
 5932:                   'verifyidlive' => 'Live agent verification',
 5933:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 5934:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 5935:                   'fullscreensever' => 'Forced, navigation away ends exam',
 5936:                   'notabs' => 'Disaallowed',
 5937:                   'linksonly' => 'Allowed from links in exam',
 5938:                   'noreentry' => 'Disallowed',
 5939:                   'agentreentry' => 'Agent required for re-entry',
 5940:                   'calculatorbasic' => 'Basic',
 5941:                   'calculatorsci' => 'Scientific',
 5942:         );
 5943:     } elsif ($item eq 'examity') {
 5944:         return &Apache::lonlocal::texthash (
 5945:                 'target'         => 'Display target',   
 5946:                 'window'         => 'Window',
 5947:                 'tab'            => 'Tab',
 5948:                 'iframe'         => 'iFrame',
 5949:                 'height'         => 'Height (pixels)',
 5950:                 'width'          => 'Width (pixels)',
 5951:                 'linktext'       => 'Default Link Text',
 5952:                 'explanation'    => 'Default Explanation',
 5953:                 'append'         => 'Provider URL',
 5954:         );
 5955:     }
 5956: }
 5957: 
 5958: sub proctoring_providernames {
 5959:     return (
 5960:              proctorio => 'Proctorio',
 5961:              examity   => 'Examity',
 5962:            );
 5963: }
 5964: 
 5965: sub print_lti {
 5966:     my ($dom,$settings,$rowtotal) = @_;
 5967:     my $itemcount = 1;
 5968:     my $maxnum = 0;
 5969:     my $css_class;
 5970:     my %ordered;
 5971:     if (ref($settings) eq 'HASH') {
 5972:         foreach my $item (keys(%{$settings})) {
 5973:             if (ref($settings->{$item}) eq 'HASH') {
 5974:                 my $num = $settings->{$item}{'order'};
 5975:                 if ($num eq '') {
 5976:                     $num = scalar(keys(%{$settings}));
 5977:                 }
 5978:                 $ordered{$num} = $item;
 5979:             }
 5980:         }
 5981:     }
 5982:     my $maxnum = scalar(keys(%ordered));
 5983:     my $datatable;
 5984:     my %lt = &lti_names();
 5985:     if (keys(%ordered)) {
 5986:         my @items = sort { $a <=> $b } keys(%ordered);
 5987:         for (my $i=0; $i<@items; $i++) {
 5988:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5989:             my $item = $ordered{$items[$i]};
 5990:             my ($key,$secret,$lifetime,$consumer,$requser,$crsinc,$current);
 5991:             if (ref($settings->{$item}) eq 'HASH') {
 5992:                 $key = $settings->{$item}->{'key'};
 5993:                 $secret = $settings->{$item}->{'secret'};
 5994:                 $lifetime = $settings->{$item}->{'lifetime'};
 5995:                 $consumer = $settings->{$item}->{'consumer'};
 5996:                 $requser = $settings->{$item}->{'requser'};
 5997:                 $crsinc = $settings->{$item}->{'crsinc'};
 5998:                 $current = $settings->{$item};
 5999:             }
 6000:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6001:             my %checkedrequser = (
 6002:                                    yes => ' checked="checked"',
 6003:                                    no  => '',
 6004:                                  );
 6005:             if (!$requser) {
 6006:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 6007:                 $checkedrequser{'yes'} = '';
 6008:             }
 6009:             my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6010:             my %checkedcrsinc = (
 6011:                                       yes => ' checked="checked"',
 6012:                                       no  => '',
 6013:                                    );
 6014:             if (!$crsinc) {
 6015:                 $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6016:                 $checkedcrsinc{'yes'} = '';
 6017:             }
 6018:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6019:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6020:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6021:             for (my $k=0; $k<=$maxnum; $k++) {
 6022:                 my $vpos = $k+1;
 6023:                 my $selstr;
 6024:                 if ($k == $i) {
 6025:                     $selstr = ' selected="selected" ';
 6026:                 }
 6027:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6028:             }
 6029:             $datatable .= '</select>'.('&nbsp;'x2).
 6030:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6031:                 &mt('Delete?').'</label></span></td>'.
 6032:                 '<td colspan="2">'.
 6033:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6034:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 6035:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6036:                 ('&nbsp;'x2).
 6037:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6038:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6039:                 ('&nbsp;'x2).
 6040:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6041:                 'value="'.$lifetime.'" size="3" /></span>'.
 6042:                 ('&nbsp;'x2).
 6043:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6044:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6045:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6046:                 '<br /><br />'.
 6047:                 '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6048:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6049:                 '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6050:                 ('&nbsp;'x4).
 6051:                 '<span class="LC_nobreak">'.$lt{'key'}.
 6052:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 6053:                 ('&nbsp;'x2).
 6054:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6055:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 6056:                 '<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>'.
 6057:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6058:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6059:             $itemcount ++;
 6060:         }
 6061:     }
 6062:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6063:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6064:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6065:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6066:                   '<select name="lti_pos_add"'.$chgstr.'>';
 6067:     for (my $k=0; $k<$maxnum+1; $k++) {
 6068:         my $vpos = $k+1;
 6069:         my $selstr;
 6070:         if ($k == $maxnum) {
 6071:             $selstr = ' selected="selected" ';
 6072:         }
 6073:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6074:     }
 6075:     $datatable .= '</select>&nbsp;'."\n".
 6076:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6077:                   '<td colspan="2">'.
 6078:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6079:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 6080:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6081:                   ('&nbsp;'x2).
 6082:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6083:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6084:                   ('&nbsp;'x2).
 6085:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 6086:                   ('&nbsp;'x2).
 6087:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6088:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6089:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6090:                   '<br /><br />'.
 6091:                   '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6092:                   '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6093:                   '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6094:                   ('&nbsp;'x4).
 6095:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 6096:                   ('&nbsp;'x2).
 6097:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 6098:                   '<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".
 6099:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6100:                   '</td>'."\n".
 6101:                   '</tr>'."\n";
 6102:     $$rowtotal ++;
 6103:     return $datatable;;
 6104: }
 6105: 
 6106: sub lti_names {
 6107:     my %lt = &Apache::lonlocal::texthash(
 6108:                                           'version'   => 'LTI Version',
 6109:                                           'url'       => 'URL',
 6110:                                           'key'       => 'Key',
 6111:                                           'lifetime'  => 'Nonce lifetime (s)',
 6112:                                           'consumer'  => 'Consumer',
 6113:                                           'secret'    => 'Secret',
 6114:                                           'requser'   => "User's identity sent",
 6115:                                           'crsinc'    => "Course's identity sent",
 6116:                                           'email'     => 'Email address',
 6117:                                           'sourcedid' => 'User ID',
 6118:                                           'other'     => 'Other',
 6119:                                           'passback'  => 'Can return grades to Consumer:',
 6120:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6121:                                           'topmenu'   => 'Display LON-CAPA page header',
 6122:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6123:                                         );
 6124:     return %lt;
 6125: }
 6126: 
 6127: sub lti_options {
 6128:     my ($num,$current,$itemcount,%lt) = @_;
 6129:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6130:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6131:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6132:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6133:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6134:     $checked{'mapcrstype'} = {};
 6135:     $checked{'makeuser'} = {};
 6136:     $checked{'selfenroll'} = {};
 6137:     $checked{'crssec'} = {};
 6138:     $checked{'crssecsrc'} = {};
 6139:     $checked{'lcauth'} = {};
 6140:     $checked{'menuitem'} = {};
 6141:     if ($num eq 'add') {
 6142:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6143:     }
 6144:     my $userfieldsty = 'none';
 6145:     my $crsfieldsty = 'none';
 6146:     my $crssecfieldsty = 'none';
 6147:     my $secsrcfieldsty = 'none';
 6148:     my $callbacksty = 'none';
 6149:     my $passbacksty = 'none';
 6150:     my $optionsty = 'block';
 6151:     my $crssty = 'block';
 6152:     my $lcauthparm;
 6153:     my $lcauthparmstyle = 'display:none';
 6154:     my $lcauthparmtext;
 6155:     my $menusty;
 6156:     my $numinrow = 4;
 6157:     my %menutitles = &ltimenu_titles();
 6158: 
 6159:     if (ref($current) eq 'HASH') {
 6160:         if (!$current->{'requser'}) {
 6161:             $optionsty = 'none';
 6162:             $crssty = 'none';
 6163:         } elsif (!$current->{'crsinc'}) {
 6164:             $crssty = 'none';
 6165:         }
 6166:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6167:             $checked{'mapuser'}{'sourcedid'} = '';
 6168:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6169:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 6170:             } else {
 6171:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6172:                 $userfield = $current->{'mapuser'};
 6173:                 $userfieldsty = 'inline-block';
 6174:             }
 6175:         }
 6176:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6177:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6178:             if ($current->{'mapcrs'} eq 'context_id') {
 6179:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 6180:             } else {
 6181:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6182:                 $cidfield = $current->{'mapcrs'};
 6183:                 $crsfieldsty = 'inline-block';
 6184:             }
 6185:         }
 6186:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6187:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6188:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6189:             }
 6190:         }
 6191:         if (!$current->{'storecrs'}) {
 6192:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6193:             $checked{'storecrs'}{'Y'} = '';
 6194:         }
 6195:         if ($current->{'makecrs'}) {
 6196:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6197:         }
 6198:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6199:             foreach my $role (@{$current->{'makeuser'}}) {
 6200:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6201:             }
 6202:         }
 6203:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6204:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6205:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6206:                 $lcauthparm = $current->{'lcauthparm'};
 6207:                 $lcauthparmstyle = 'display:table-row'; 
 6208:                 if ($current->{'lcauth'} eq 'localauth') {
 6209:                     $lcauthparmtext = &mt('Local auth argument');
 6210:                 } else {
 6211:                     $lcauthparmtext = &mt('Kerberos domain');
 6212:                 }
 6213:             }
 6214:         }
 6215:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6216:             foreach my $role (@{$current->{'selfenroll'}}) {
 6217:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6218:             }
 6219:         }
 6220:         if (ref($current->{'maproles'}) eq 'HASH') {
 6221:             %rolemaps = %{$current->{'maproles'}};
 6222:         }
 6223:         if ($current->{'section'} ne '') {
 6224:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 6225:             $crssecfieldsty = 'inline-block';
 6226:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6227:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6228:             } else {
 6229:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6230:                 $crssecsrc = $current->{'section'};
 6231:                 $secsrcfieldsty = 'inline-block';
 6232:             }
 6233:         } else {
 6234:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6235:         }
 6236:         if ($current->{'callback'} ne '') {
 6237:             $callback = $current->{'callback'};
 6238:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6239:             $callbacksty = 'inline-block';
 6240:         } else {
 6241:             $checked{'callback'}{'N'} = ' checked="checked"';
 6242:         }
 6243:         if ($current->{'topmenu'}) {
 6244:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6245:         } else {
 6246:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6247:         }
 6248:         if ($current->{'inlinemenu'}) {
 6249:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6250:         } else {
 6251:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6252:         }
 6253:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6254:             $menusty = 'inline-block';
 6255:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6256:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6257:                     if (exists($menutitles{$item})) {
 6258:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6259:                     }
 6260:                 }
 6261:             }
 6262:         } else {
 6263:             $menusty = 'none';
 6264:         }
 6265:     } else {
 6266:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6267:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6268:         $checked{'callback'}{'N'} = ' checked="checked"';
 6269:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6270:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 6271:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6272:         $menusty = 'inline-block'; 
 6273:     }
 6274:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6275:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6276:                                official   => 'Official',
 6277:                                unofficial => 'Unofficial',
 6278:                                community  => 'Community',
 6279:                                textbook   => 'Textbook',
 6280:                                placement  => 'Placement Test',
 6281:                                lti        => 'LTI Provider',
 6282:     );
 6283:     my @authtypes = ('internal','krb4','krb5','localauth');
 6284:     my %shortauth = (
 6285:                      internal => 'int',
 6286:                      krb4 => 'krb4',
 6287:                      krb5 => 'krb5',
 6288:                      localauth  => 'loc'
 6289:                     );
 6290:     my %authnames = &authtype_names();
 6291:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6292:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6293:     my @courseroles = ('cc','in','ta','ep','st');
 6294:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6295:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6296:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6297:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6298:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6299:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6300:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6301:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6302:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6303:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6304:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6305:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6306:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6307:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6308:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6309:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6310:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6311:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6312:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6313:     foreach my $option ('sourcedid','email','other') {
 6314:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6315:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6316:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6317:     }
 6318:     $output .= '</span></div>'.
 6319:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6320:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6321:                'value="'.$userfield.'" /></div></fieldset>'.
 6322:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6323:     foreach my $ltirole (@ltiroles) {
 6324:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6325:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 6326:     }
 6327:     $output .= '</fieldset>'.
 6328:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6329:                '<table>'.
 6330:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6331:                '</table>'.
 6332:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6333:                '<td class="LC_left_item">';
 6334:     foreach my $auth ('lti',@authtypes) {
 6335:         my $authtext;
 6336:         if ($auth eq 'lti') {
 6337:             $authtext = &mt('None');
 6338:         } else {
 6339:             $authtext = $authnames{$shortauth{$auth}};
 6340:         }
 6341:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6342:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6343:                    $authtext.'</label></span> &nbsp;';
 6344:     }
 6345:     $output .= '</td></tr>'.
 6346:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6347:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6348:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6349:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6350:                '</table></fieldset>'.
 6351:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6352:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6353:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6354:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6355:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6356:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6357:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6358:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6359:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6360:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6361:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6362:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6363:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6364:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6365:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6366:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6367:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6368:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6369:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6370:                    ('&nbsp;'x2);
 6371:     }
 6372:     $output .= '</span></div></fieldset>'.
 6373:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6374:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6375:                &mt('Unique course identifier').':&nbsp;';
 6376:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6377:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6378:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6379:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6380:     }
 6381:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6382:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6383:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6384:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6385:     foreach my $type (@coursetypes) {
 6386:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6387:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6388:                    ('&nbsp;'x2);
 6389:     }
 6390:     $output .= '</span><br /><br />'.
 6391:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6392:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6393:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6394:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6395:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6396:                '</fieldset>'.
 6397:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6398:     foreach my $ltirole (@lticourseroles) {
 6399:         my ($selected,$selectnone);
 6400:         if ($rolemaps{$ltirole} eq '') {
 6401:             $selectnone = ' selected="selected"';
 6402:         }
 6403:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6404:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6405:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6406:         foreach my $role (@courseroles) {
 6407:             unless ($selectnone) {
 6408:                 if ($rolemaps{$ltirole} eq $role) {
 6409:                     $selected = ' selected="selected"';
 6410:                 } else {
 6411:                     $selected = '';
 6412:                 }
 6413:             }
 6414:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6415:                        &Apache::lonnet::plaintext($role,'Course').
 6416:                        '</option>';
 6417:         }
 6418:         $output .= '</select></td>';
 6419:     }
 6420:     $output .= '</tr></table></fieldset>'.
 6421:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6422:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6423:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6424:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6425:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6426:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6427:                '</fieldset>'.
 6428:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6429:     foreach my $lticrsrole (@lticourseroles) {
 6430:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6431:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6432:     }
 6433:     $output .= '</fieldset>'.
 6434:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6435:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6436:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6437:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6438:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6439:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6440:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6441:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6442:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6443:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6444:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6445:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6446:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6447:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6448:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6449:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6450:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6451:     foreach my $extra ('roster','passback') {
 6452:         my $checkedon = '';
 6453:         my $checkedoff = ' checked="checked"';
 6454:         if ($extra eq 'passback') {
 6455:             $pb1p1chk = ' checked="checked"';
 6456:             $pb1p0chk = '';
 6457:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6458:         } else {
 6459:             $onclickpb = ''; 
 6460:         }
 6461:         if (ref($current) eq 'HASH') {
 6462:             if (($current->{$extra})) {
 6463:                 $checkedon = $checkedoff;
 6464:                 $checkedoff = '';
 6465:                 if ($extra eq 'passback') {
 6466:                     $passbacksty = 'inline-block';
 6467:                 }
 6468:                 if ($current->{'passbackformat'} eq '1.0') {
 6469:                     $pb1p0chk =  ' checked="checked"';
 6470:                     $pb1p1chk = '';
 6471:                 }
 6472:             }
 6473:         }
 6474:         $output .= $lt{$extra}.'&nbsp;'.
 6475:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6476:                    &mt('No').'</label>'.('&nbsp;'x2).
 6477:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6478:                    &mt('Yes').'</label><br />';
 6479:     }
 6480:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6481:                '<span class="LC_nobreak">'.&mt('Grade format').
 6482:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6483:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6484:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6485:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6486:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6487:     $output .= '</span></div></fieldset>';
 6488: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6489: #
 6490: #    $output .= '</fieldset>'.
 6491: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6492:     return $output;
 6493: }
 6494: 
 6495: sub ltimenu_titles {
 6496:     return &Apache::lonlocal::texthash(
 6497:                                         fullname    => 'Full name',
 6498:                                         coursetitle => 'Course title',
 6499:                                         role        => 'Role',
 6500:                                         logout      => 'Logout',
 6501:                                         grades      => 'Grades',
 6502:     );
 6503: }
 6504: 
 6505: sub print_coursedefaults {
 6506:     my ($position,$dom,$settings,$rowtotal) = @_;
 6507:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6508:     my $itemcount = 1;
 6509:     my %choices =  &Apache::lonlocal::texthash (
 6510:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6511:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6512:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6513:         coursecredits        => 'Credits can be specified for courses',
 6514:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6515:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6516:         texengine            => 'Default method to display mathematics',
 6517:         postsubmit           => 'Disable submit button/keypress following student submission',
 6518:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6519:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6520:     );
 6521:     my %staticdefaults = (
 6522:                            anonsurvey_threshold => 10,
 6523:                            uploadquota          => 500,
 6524:                            postsubmit           => 60,
 6525:                            mysqltables          => 172800,
 6526:                          );
 6527:     if ($position eq 'top') {
 6528:         %defaultchecked = (
 6529:                             'canuse_pdfforms' => 'off',
 6530:                             'uselcmath'       => 'on',
 6531:                             'usejsme'         => 'on',
 6532:                             'canclone'        => 'none',
 6533:                           );
 6534:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 6535:         my $deftex = $Apache::lonnet::deftex;
 6536:         if (ref($settings) eq 'HASH') {
 6537:             if ($settings->{'texengine'}) {
 6538:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6539:                     $deftex = $settings->{'texengine'};
 6540:                 }
 6541:             }
 6542:         }
 6543:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6544:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6545:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6546:                        '</span></td><td class="LC_right_item">'.
 6547:                        '<select name="texengine">'."\n";
 6548:         my %texoptions = (
 6549:                             MathJax  => 'MathJax',
 6550:                             mimetex  => &mt('Convert to Images'),
 6551:                             tth      => &mt('TeX to HTML'),
 6552:                          );
 6553:         foreach my $renderer ('MathJax','mimetex','tth') {
 6554:             my $selected = '';
 6555:             if ($renderer eq $deftex) {
 6556:                 $selected = ' selected="selected"';
 6557:             }
 6558:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6559:         }
 6560:         $mathdisp .= '</select></td></tr>'."\n";
 6561:         $itemcount ++;
 6562:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6563:                                                      \%choices,$itemcount);
 6564:         $datatable = $mathdisp.$datatable;
 6565:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6566:         $datatable .=
 6567:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6568:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6569:             '</span></td><td class="LC_left_item">';
 6570:         my $currcanclone = 'none';
 6571:         my $onclick;
 6572:         my @cloneoptions = ('none','domain');
 6573:         my %clonetitles = &Apache::lonlocal::texthash (
 6574:                              none     => 'No additional course requesters',
 6575:                              domain   => "Any course requester in course's domain",
 6576:                              instcode => 'Course requests for official courses ...',
 6577:                           );
 6578:         my (%codedefaults,@code_order,@posscodes);
 6579:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6580:                                                     \@code_order) eq 'ok') {
 6581:             if (@code_order > 0) {
 6582:                 push(@cloneoptions,'instcode');
 6583:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6584:             }
 6585:         }
 6586:         if (ref($settings) eq 'HASH') {
 6587:             if ($settings->{'canclone'}) {
 6588:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6589:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6590:                         if (@code_order > 0) {
 6591:                             $currcanclone = 'instcode';
 6592:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6593:                         }
 6594:                     }
 6595:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6596:                     $currcanclone = $settings->{'canclone'};
 6597:                 }
 6598:             }
 6599:         }
 6600:         foreach my $option (@cloneoptions) {
 6601:             my ($checked,$additional);
 6602:             if ($currcanclone eq $option) {
 6603:                 $checked = ' checked="checked"';
 6604:             }
 6605:             if ($option eq 'instcode') {
 6606:                 if (@code_order) {
 6607:                     my $show = 'none';
 6608:                     if ($checked) {
 6609:                         $show = 'block';
 6610:                     }
 6611:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6612:                                   &mt('Institutional codes for new and cloned course have identical:').
 6613:                                   '<br />';
 6614:                     foreach my $item (@code_order) {
 6615:                         my $codechk;
 6616:                         if ($checked) {
 6617:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6618:                                 $codechk = ' checked="checked"';
 6619:                             }
 6620:                         }
 6621:                         $additional .= '<label>'.
 6622:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6623:                                        $item.'</label>';
 6624:                     }
 6625:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6626:                 }
 6627:             }
 6628:             $datatable .=
 6629:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6630:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6631:                 '</label>&nbsp;'.$additional.'</span><br />';
 6632:         }
 6633:         $datatable .= '</td>'.
 6634:                       '</tr>';
 6635:         $itemcount ++;
 6636:     } else {
 6637:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6638:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6639:         my $currusecredits = 0;
 6640:         my $postsubmitclient = 1;
 6641:         my @types = ('official','unofficial','community','textbook','placement');
 6642:         if (ref($settings) eq 'HASH') {
 6643:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6644:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6645:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6646:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6647:                 }
 6648:             }
 6649:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6650:                 foreach my $type (@types) {
 6651:                     next if ($type eq 'community');
 6652:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6653:                     if ($defcredits{$type} ne '') {
 6654:                         $currusecredits = 1;
 6655:                     }
 6656:                 }
 6657:             }
 6658:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6659:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6660:                     $postsubmitclient = 0;
 6661:                     foreach my $type (@types) {
 6662:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6663:                     }
 6664:                 } else {
 6665:                     foreach my $type (@types) {
 6666:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6667:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6668:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6669:                             } else {
 6670:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6671:                             }
 6672:                         } else {
 6673:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6674:                         }
 6675:                     }
 6676:                 }
 6677:             } else {
 6678:                 foreach my $type (@types) {
 6679:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6680:                 }
 6681:             }
 6682:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6683:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6684:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6685:                 }
 6686:             } else {
 6687:                 foreach my $type (@types) {
 6688:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6689:                 }
 6690:             }
 6691:         } else {
 6692:             foreach my $type (@types) {
 6693:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6694:             }
 6695:         }
 6696:         if (!$currdefresponder) {
 6697:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6698:         } elsif ($currdefresponder < 1) {
 6699:             $currdefresponder = 1;
 6700:         }
 6701:         foreach my $type (@types) {
 6702:             if ($curruploadquota{$type} eq '') {
 6703:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6704:             }
 6705:         }
 6706:         $datatable .=
 6707:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6708:                 $choices{'anonsurvey_threshold'}.
 6709:                 '</span></td>'.
 6710:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6711:                 '<input type="text" name="anonsurvey_threshold"'.
 6712:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6713:                 '</td></tr>'."\n";
 6714:         $itemcount ++;
 6715:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6716:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6717:                       $choices{'uploadquota'}.
 6718:                       '</span></td>'.
 6719:                       '<td style="text-align: right" class="LC_right_item">'.
 6720:                       '<table><tr>';
 6721:         foreach my $type (@types) {
 6722:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6723:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6724:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6725:         }
 6726:         $datatable .= '</tr></table></td></tr>'."\n";
 6727:         $itemcount ++;
 6728:         my $onclick = "toggleDisplay(this.form,'credits');";
 6729:         my $display = 'none';
 6730:         if ($currusecredits) {
 6731:             $display = 'block';
 6732:         }
 6733:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6734:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6735:         foreach my $type (@types) {
 6736:             next if ($type eq 'community');
 6737:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6738:                            '<input type="text" name="'.$type.'_credits"'.
 6739:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6740:         }
 6741:         $additional .= '</tr></table></div>'."\n";
 6742:         %defaultchecked = ('coursecredits' => 'off');
 6743:         @toggles = ('coursecredits');
 6744:         my $current = {
 6745:                         'coursecredits' => $currusecredits,
 6746:                       };
 6747:         (my $table,$itemcount) =
 6748:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6749:                                \%choices,$itemcount,$onclick,$additional,'left');
 6750:         $datatable .= $table;
 6751:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6752:         my $display = 'none';
 6753:         if ($postsubmitclient) {
 6754:             $display = 'block';
 6755:         }
 6756:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6757:                       &mt('Number of seconds submit is disabled').'<br />'.
 6758:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6759:                       '<table><tr>';
 6760:         foreach my $type (@types) {
 6761:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6762:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6763:                            $deftimeout{$type}.'" size="5" /></td>';
 6764:         }
 6765:         $additional .= '</tr></table></div>'."\n";
 6766:         %defaultchecked = ('postsubmit' => 'on');
 6767:         @toggles = ('postsubmit');
 6768:         $current = {
 6769:                        'postsubmit' => $postsubmitclient,
 6770:                    };
 6771:         ($table,$itemcount) =
 6772:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6773:                                \%choices,$itemcount,$onclick,$additional,'left');
 6774:         $datatable .= $table;
 6775:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6776:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6777:                       $choices{'mysqltables'}.
 6778:                       '</span></td>'.
 6779:                       '<td style="text-align: right" class="LC_right_item">'.
 6780:                       '<table><tr>';
 6781:         foreach my $type (@types) {
 6782:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6783:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6784:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6785:         }
 6786:         $datatable .= '</tr></table></td></tr>'."\n";
 6787:         $itemcount ++;
 6788: 
 6789:     }
 6790:     $$rowtotal += $itemcount;
 6791:     return $datatable;
 6792: }
 6793: 
 6794: sub print_selfenrollment {
 6795:     my ($position,$dom,$settings,$rowtotal) = @_;
 6796:     my ($css_class,$datatable);
 6797:     my $itemcount = 1;
 6798:     my @types = ('official','unofficial','community','textbook','placement');
 6799:     if (($position eq 'top') || ($position eq 'middle')) {
 6800:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6801:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6802:         my @rows;
 6803:         my $key;
 6804:         if ($position eq 'top') {
 6805:             $key = 'admin'; 
 6806:             if (ref($rowsref) eq 'ARRAY') {
 6807:                 @rows = @{$rowsref};
 6808:             }
 6809:         } elsif ($position eq 'middle') {
 6810:             $key = 'default';
 6811:             @rows = ('types','registered','approval','limit');
 6812:         }
 6813:         foreach my $row (@rows) {
 6814:             if (defined($titlesref->{$row})) {
 6815:                 $itemcount ++;
 6816:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6817:                 $datatable .= '<tr'.$css_class.'>'.
 6818:                               '<td>'.$titlesref->{$row}.'</td>'.
 6819:                               '<td class="LC_left_item">'.
 6820:                               '<table><tr>';
 6821:                 my (%current,%currentcap);
 6822:                 if (ref($settings) eq 'HASH') {
 6823:                     if (ref($settings->{$key}) eq 'HASH') {
 6824:                         foreach my $type (@types) {
 6825:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6826:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6827:                             }
 6828:                             if (($row eq 'limit') && ($key eq 'default')) {
 6829:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6830:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6831:                                 }
 6832:                             }
 6833:                         }
 6834:                     }
 6835:                 }
 6836:                 my %roles = (
 6837:                              '0' => &Apache::lonnet::plaintext('dc'),
 6838:                             ); 
 6839:             
 6840:                 foreach my $type (@types) {
 6841:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6842:                         $datatable .= '<th>'.&mt($type).'</th>';
 6843:                     }
 6844:                 }
 6845:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6846:                     $datatable .= '</tr><tr>';
 6847:                 }
 6848:                 foreach my $type (@types) {
 6849:                     if ($type eq 'community') {
 6850:                         $roles{'1'} = &mt('Community personnel');
 6851:                     } else {
 6852:                         $roles{'1'} = &mt('Course personnel');
 6853:                     }
 6854:                     $datatable .= '<td style="vertical-align: top">';
 6855:                     if ($position eq 'top') {
 6856:                         my %checked;
 6857:                         if ($current{$type} eq '0') {
 6858:                             $checked{'0'} = ' checked="checked"';
 6859:                         } else {
 6860:                             $checked{'1'} = ' checked="checked"';
 6861:                         }
 6862:                         foreach my $role ('1','0') {
 6863:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6864:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6865:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6866:                                           $roles{$role}.'</label></span> ';
 6867:                         }
 6868:                     } else {
 6869:                         if ($row eq 'types') {
 6870:                             my %checked;
 6871:                             if ($current{$type} =~ /^(all|dom)$/) {
 6872:                                 $checked{$1} = ' checked="checked"';
 6873:                             } else {
 6874:                                 $checked{''} = ' checked="checked"';
 6875:                             }
 6876:                             foreach my $val ('','dom','all') {
 6877:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6878:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6879:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6880:                             }
 6881:                         } elsif ($row eq 'registered') {
 6882:                             my %checked;
 6883:                             if ($current{$type} eq '1') {
 6884:                                 $checked{'1'} = ' checked="checked"';
 6885:                             } else {
 6886:                                 $checked{'0'} = ' checked="checked"';
 6887:                             }
 6888:                             foreach my $val ('0','1') {
 6889:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6890:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6891:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6892:                             }
 6893:                         } elsif ($row eq 'approval') {
 6894:                             my %checked;
 6895:                             if ($current{$type} =~ /^([12])$/) {
 6896:                                 $checked{$1} = ' checked="checked"';
 6897:                             } else {
 6898:                                 $checked{'0'} = ' checked="checked"';
 6899:                             }
 6900:                             for my $val (0..2) {
 6901:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6902:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6903:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6904:                             }
 6905:                         } elsif ($row eq 'limit') {
 6906:                             my %checked;
 6907:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6908:                                 $checked{$1} = ' checked="checked"';
 6909:                             } else {
 6910:                                 $checked{'none'} = ' checked="checked"';
 6911:                             }
 6912:                             my $cap;
 6913:                             if ($currentcap{$type} =~ /^\d+$/) {
 6914:                                 $cap = $currentcap{$type};
 6915:                             }
 6916:                             foreach my $val ('none','allstudents','selfenrolled') {
 6917:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6918:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6919:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6920:                             }
 6921:                             $datatable .= '<br />'.
 6922:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6923:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6924:                                           '</span>'; 
 6925:                         }
 6926:                     }
 6927:                     $datatable .= '</td>';
 6928:                 }
 6929:                 $datatable .= '</tr>';
 6930:             }
 6931:             $datatable .= '</table></td></tr>';
 6932:         }
 6933:     } elsif ($position eq 'bottom') {
 6934:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6935:     }
 6936:     $$rowtotal += $itemcount;
 6937:     return $datatable;
 6938: }
 6939: 
 6940: sub print_validation_rows {
 6941:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6942:     my ($itemsref,$namesref,$fieldsref);
 6943:     if ($caller eq 'selfenroll') { 
 6944:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6945:     } elsif ($caller eq 'requestcourses') {
 6946:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6947:     }
 6948:     my %currvalidation;
 6949:     if (ref($settings) eq 'HASH') {
 6950:         if (ref($settings->{'validation'}) eq 'HASH') {
 6951:             %currvalidation = %{$settings->{'validation'}};
 6952:         }
 6953:     }
 6954:     my $datatable;
 6955:     my $itemcount = 0;
 6956:     foreach my $item (@{$itemsref}) {
 6957:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6958:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6959:                       $namesref->{$item}.
 6960:                       '</span></td>'.
 6961:                       '<td class="LC_left_item">';
 6962:         if (($item eq 'url') || ($item eq 'button')) {
 6963:             $datatable .= '<span class="LC_nobreak">'.
 6964:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6965:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6966:         } elsif ($item eq 'fields') {
 6967:             my @currfields;
 6968:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6969:                 @currfields = @{$currvalidation{$item}};
 6970:             }
 6971:             foreach my $field (@{$fieldsref}) {
 6972:                 my $check = '';
 6973:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6974:                     $check = ' checked="checked"';
 6975:                 }
 6976:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6977:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6978:                               ' value="'.$field.'"'.$check.' />'.$field.
 6979:                               '</label></span> ';
 6980:             }
 6981:         } elsif ($item eq 'markup') {
 6982:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6983:                            $currvalidation{$item}.
 6984:                               '</textarea>';
 6985:         }
 6986:         $datatable .= '</td></tr>'."\n";
 6987:         if (ref($rowtotal)) {
 6988:             $itemcount ++;
 6989:         }
 6990:     }
 6991:     if ($caller eq 'requestcourses') {
 6992:         my %currhash;
 6993:         if (ref($settings) eq 'HASH') {
 6994:             if (ref($settings->{'validation'}) eq 'HASH') {
 6995:                 if ($settings->{'validation'}{'dc'} ne '') {
 6996:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6997:                 }
 6998:             }
 6999:         }
 7000:         my $numinrow = 2;
 7001:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7002:                                                        'validationdc',%currhash);
 7003:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7004:         $datatable .= '<tr'.$css_class.'><td>';
 7005:         if ($numdc > 1) {
 7006:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7007:         } else {
 7008:             $datatable .=  &mt('Course creation processed as: ');
 7009:         }
 7010:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7011:         $itemcount ++;
 7012:     }
 7013:     if (ref($rowtotal)) {
 7014:         $$rowtotal += $itemcount;
 7015:     }
 7016:     return $datatable;
 7017: }
 7018: 
 7019: sub print_privacy {
 7020:     my ($position,$dom,$settings,$rowtotal) = @_;
 7021:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7022:     my $itemcount = 0;
 7023:     unless ($position eq 'top') {
 7024:         @items = ('domain','author','course','community');
 7025:         %names = &Apache::lonlocal::texthash (
 7026:                      domain => 'Assigned domain role(s)',
 7027:                      author => 'Assigned co-author role(s)',
 7028:                      course => 'Assigned course role(s)',
 7029:                      community => 'Assigned community role',
 7030:                  );
 7031:         $numinrow = 4;
 7032:         ($othertitle,$usertypes,$types) =
 7033:             &Apache::loncommon::sorted_inst_types($dom);
 7034:     }
 7035:     if (($position eq 'top') || ($position eq 'middle')) {
 7036:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7037:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7038:         if ($position eq 'top') {
 7039:             my %curr;
 7040:             my @options = ('none','user','domain','auto');
 7041:             my %titles = &Apache::lonlocal::texthash (
 7042:                 none   => 'Not allowed',
 7043:                 user   => 'User authorizes',
 7044:                 domain => 'DC authorizes',
 7045:                 auto   => 'Unrestricted',
 7046:                 instdom => 'Other domain shares institution/provider',
 7047:                 extdom => 'Other domain has different institution/provider',
 7048:             );
 7049:             my %names = &Apache::lonlocal::texthash (
 7050:                 domain => 'Domain role',
 7051:                 author => 'Co-author role',
 7052:                 course => 'Course role',
 7053:                 community => 'Community role',
 7054:             );
 7055:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7056:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7057:             foreach my $domtype ('instdom','extdom') {
 7058:                 my (%checked,$skip);
 7059:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7060:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7061:                               '<td class="LC_left_item">';
 7062:                 if ($domtype eq 'instdom') {
 7063:                     unless (@instdoms > 1) {
 7064:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7065:                         $skip = 1;
 7066:                     }
 7067:                 } elsif ($domtype eq 'extdom') {
 7068:                     if (keys(%by_location) == 0) {
 7069:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7070:                         $skip = 1;
 7071:                     }
 7072:                 }
 7073:                 unless ($skip) {
 7074:                     foreach my $roletype ('domain','author','course','community') {
 7075:                         $checked{'auto'} = ' checked="checked"';
 7076:                         if (ref($settings) eq 'HASH') {
 7077:                             if (ref($settings->{approval}) eq 'HASH') {
 7078:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7079:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7080:                                         $checked{$1} = ' checked="checked"';
 7081:                                         $checked{'auto'} = '';
 7082:                                     }
 7083:                                 }
 7084:                             }
 7085:                         }
 7086:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7087:                         foreach my $option (@options) {
 7088:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7089:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7090:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7091:                                           '</label></span>&nbsp; ';
 7092:                         }
 7093:                         $datatable .= '</fieldset>';
 7094:                     }
 7095:                 }
 7096:                 $datatable .= '</td></tr>';
 7097:                 $itemcount ++;
 7098:             }
 7099:         } elsif ($position eq 'middle') {
 7100:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7101:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7102:                     foreach my $item (@{$types}) {
 7103:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7104:                                                                $numinrow,$itemcount,'','','','','',
 7105:                                                                '',$usertypes->{$item});
 7106:                         $itemcount ++;
 7107:                     }
 7108:                 }
 7109:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7110:                                                        $numinrow,$itemcount,'','','','','',
 7111:                                                        '',$othertitle);
 7112:                 $itemcount ++;
 7113:             } else {
 7114:                 my (@insttypes,%insttitles);
 7115:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7116:                     @insttypes = @{$types};
 7117:                     %insttitles = %{$usertypes};
 7118:                 }
 7119:                 foreach my $item (@insttypes,'default') {
 7120:                     my $title;
 7121:                     if ($item eq 'default') {
 7122:                         $title = $othertitle;
 7123:                     } else {
 7124:                         $title = $insttitles{$item};
 7125:                     }
 7126:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7127:                     $datatable .= '<tr'.$css_class.'>'.
 7128:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7129:                                   '<td class="LC_left_item">'.
 7130:                                   &mt('Nothing to set here, as there are no other domains').
 7131:                                   '</td></tr>';
 7132:                     $itemcount ++;
 7133:                 }
 7134:             }
 7135:         }
 7136:     } else {
 7137:         my $prefix;
 7138:         if ($position eq 'lower') {
 7139:             $prefix = 'priv';
 7140:         } else {
 7141:             $prefix = 'unpriv';
 7142:         }
 7143:         foreach my $item (@items) {
 7144:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7145:                                                    $numinrow,$itemcount,'','','','','',
 7146:                                                    '',$names{$item});
 7147:             $itemcount ++;
 7148:         }
 7149:     }
 7150:     if (ref($rowtotal)) {
 7151:         $$rowtotal += $itemcount;
 7152:     }
 7153:     return $datatable;
 7154: }
 7155: 
 7156: sub print_passwords {
 7157:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7158:     my ($datatable,$css_class);
 7159:     my $itemcount = 0;
 7160:     my %titles = &Apache::lonlocal::texthash (
 7161:         captcha        => '"Forgot Password" CAPTCHA validation',
 7162:         link           => 'Reset link expiration (hours)',
 7163:         case           => 'Case-sensitive usernames/e-mail',
 7164:         prelink        => 'Information required (form 1)',
 7165:         postlink       => 'Information required (form 2)',
 7166:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7167:         customtext     => 'Domain specific text (HTML)',
 7168:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7169:         intauth_check  => 'Check bcrypt cost if authenticated',
 7170:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7171:         permanent      => 'Permanent e-mail address',
 7172:         critical       => 'Critical notification address',
 7173:         notify         => 'Notification address',
 7174:         min            => 'Minimum password length',
 7175:         max            => 'Maximum password length',
 7176:         chars          => 'Required characters',
 7177:         expire         => 'Password expiration (days)',
 7178:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7179:     );
 7180:     if ($position eq 'top') {
 7181:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7182:         my $shownlinklife = 2;
 7183:         my $prelink = 'both';
 7184:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7185:         if (ref($settings) eq 'HASH') {
 7186:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7187:                 $shownlinklife = $settings->{resetlink};
 7188:             }
 7189:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7190:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7191:             }
 7192:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7193:                 $prelink = $settings->{resetprelink};
 7194:             }
 7195:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7196:                 %postlink = %{$settings->{resetpostlink}};
 7197:             }
 7198:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7199:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7200:             }
 7201:             if ($settings->{resetremove}) {
 7202:                 $nostdtext = 1;
 7203:             }
 7204:             if ($settings->{resetcustom}) {
 7205:                 $customurl = $settings->{resetcustom};
 7206:             }
 7207:         } else {
 7208:             if (ref($types) eq 'ARRAY') {
 7209:                 foreach my $item (@{$types}) {
 7210:                     $casesens{$item} = 1;
 7211:                     $postlink{$item} = ['username','email'];
 7212:                 }
 7213:             }
 7214:             $casesens{'default'} = 1;
 7215:             $postlink{'default'} = ['username','email'];
 7216:             $prelink = 'both';
 7217:             %emailsrc = (
 7218:                           permanent => 1,
 7219:                           critical  => 1,
 7220:                           notify    => 1,
 7221:             );
 7222:         }
 7223:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7224:         $itemcount ++;
 7225:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7226:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7227:                       '<td class="LC_left_item">'.
 7228:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7229:                       'name="passwords_link" size="3" /></td></tr>';
 7230:         $itemcount ++;
 7231:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7232:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7233:                       '<td class="LC_left_item">';
 7234:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7235:             foreach my $item (@{$types}) {
 7236:                 my $checkedcase;
 7237:                 if ($casesens{$item}) {
 7238:                     $checkedcase = ' checked="checked"';
 7239:                 }
 7240:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7241:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7242:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7243:                               '</span>&nbsp;&nbsp; ';
 7244:             }
 7245:         }
 7246:         my $checkedcase;
 7247:         if ($casesens{'default'}) {
 7248:             $checkedcase = ' checked="checked"';
 7249:         }
 7250:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7251:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7252:                       $othertitle.'</label></span></td>';
 7253:         $itemcount ++;
 7254:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7255:         my %checkedpre = (
 7256:                              both => ' checked="checked"',
 7257:                              either => '',
 7258:                          );
 7259:         if ($prelink eq 'either') {
 7260:             $checkedpre{either} = ' checked="checked"';
 7261:             $checkedpre{both} = '';
 7262:         }
 7263:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7264:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7265:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7266:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7267:                       '<span class="LC_nobreak"><label>'.
 7268:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7269:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7270:         $itemcount ++;
 7271:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7272:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7273:                       '<td class="LC_left_item">';
 7274:         my %postlinked;
 7275:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7276:             foreach my $item (@{$types}) {
 7277:                 undef(%postlinked);
 7278:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7279:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7280:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7281:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7282:                 }
 7283:                 foreach my $field ('email','username') {
 7284:                     my $checked;
 7285:                     if ($postlinked{$field}) {
 7286:                         $checked = ' checked="checked"';
 7287:                     }
 7288:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7289:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7290:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7291:                                   '<span>&nbsp;&nbsp; ';
 7292:                 }
 7293:                 $datatable .= '</fieldset>';
 7294:             }
 7295:         }
 7296:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7297:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7298:         }
 7299:         $datatable .= '<fieldset style="display: inline-block;">'.
 7300:                       '<legend>'.$othertitle.'</legend>';
 7301:         foreach my $field ('email','username') {
 7302:             my $checked;
 7303:             if ($postlinked{$field}) {
 7304:                 $checked = ' checked="checked"';
 7305:             }
 7306:             $datatable .= '<span class="LC_nobreak"><label>'.
 7307:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7308:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7309:                           '<span>&nbsp;&nbsp; ';
 7310:         }
 7311:         $datatable .= '</fieldset></td></tr>';
 7312:         $itemcount ++;
 7313:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7314:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7315:                       '<td class="LC_left_item">';
 7316:         foreach my $type ('permanent','critical','notify') {
 7317:             my $checkedemail;
 7318:             if ($emailsrc{$type}) {
 7319:                 $checkedemail = ' checked="checked"';
 7320:             }
 7321:             $datatable .= '<span class="LC_nobreak"><label>'.
 7322:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7323:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7324:                           '<span>&nbsp;&nbsp; ';
 7325:         }
 7326:         $datatable .= '</td></tr>';
 7327:         $itemcount ++;
 7328:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7329:         my $switchserver = &check_switchserver($dom,$confname);
 7330:         my ($showstd,$noshowstd);
 7331:         if ($nostdtext) {
 7332:             $noshowstd = ' checked="checked"';
 7333:         } else {
 7334:             $showstd = ' checked="checked"';
 7335:         }
 7336:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7337:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7338:                       &mt('Retain standard text:').
 7339:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7340:                       &mt('Yes').'</label>'.'&nbsp;'.
 7341:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7342:                       &mt('No').'</label></span><br />'.
 7343:                       '<span class="LC_fontsize_small">'.
 7344:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7345:                       &mt('Include custom text:');
 7346:         if ($customurl) {
 7347:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7348:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7349:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7350:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7351:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7352:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7353:         }
 7354:         if ($switchserver) {
 7355:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7356:         } else {
 7357:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7358:                          '<input type="file" name="passwords_customfile" /></span>';
 7359:         }
 7360:         $datatable .= '</td></tr>';
 7361:     } elsif ($position eq 'middle') {
 7362:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7363:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7364:         my %defaults;
 7365:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7366:             %defaults = %{$domconf{'defaults'}};
 7367:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7368:                 $defaults{'intauth_cost'} = 10;
 7369:             }
 7370:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7371:                 $defaults{'intauth_check'} = 0;
 7372:             }
 7373:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7374:                 $defaults{'intauth_switch'} = 0;
 7375:             }
 7376:         } else {
 7377:             %defaults = (
 7378:                           'intauth_cost'   => 10,
 7379:                           'intauth_check'  => 0,
 7380:                           'intauth_switch' => 0,
 7381:                         );
 7382:         }
 7383:         foreach my $item (@items) {
 7384:             if ($itemcount%2) {
 7385:                 $css_class = '';
 7386:             } else {
 7387:                 $css_class = ' class="LC_odd_row" ';
 7388:             }
 7389:             $datatable .= '<tr'.$css_class.'>'.
 7390:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7391:                           '</span></td><td class="LC_left_item" colspan="3">';
 7392:             if ($item eq 'intauth_switch') {
 7393:                 my @options = (0,1,2);
 7394:                 my %optiondesc = &Apache::lonlocal::texthash (
 7395:                                    0 => 'No',
 7396:                                    1 => 'Yes',
 7397:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7398:                                  );
 7399:                 $datatable .= '<table width="100%">';
 7400:                 foreach my $option (@options) {
 7401:                     my $checked = ' ';
 7402:                     if ($defaults{$item} eq $option) {
 7403:                         $checked = ' checked="checked"';
 7404:                     }
 7405:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7406:                                   '<label><input type="radio" name="'.$item.
 7407:                                   '" value="'.$option.'"'.$checked.' />'.
 7408:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7409:                 }
 7410:                 $datatable .= '</table>';
 7411:             } elsif ($item eq 'intauth_check') {
 7412:                 my @options = (0,1,2);
 7413:                 my %optiondesc = &Apache::lonlocal::texthash (
 7414:                                    0 => 'No',
 7415:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7416:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7417:                                  );
 7418:                 $datatable .= '<table width="100%">';
 7419:                 foreach my $option (@options) {
 7420:                     my $checked = ' ';
 7421:                     my $onclick;
 7422:                     if ($defaults{$item} eq $option) {
 7423:                         $checked = ' checked="checked"';
 7424:                     }
 7425:                     if ($option == 2) {
 7426:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7427:                     }
 7428:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7429:                                   '<label><input type="radio" name="'.$item.
 7430:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7431:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7432:                 }
 7433:                 $datatable .= '</table>';
 7434:             } else {
 7435:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7436:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7437:             }
 7438:             $datatable .= '</td></tr>';
 7439:             $itemcount ++;
 7440:         }
 7441:     } elsif ($position eq 'lower') {
 7442:         my ($min,$max,%chars,$expire,$numsaved);
 7443:         $min = $Apache::lonnet::passwdmin;
 7444:         if (ref($settings) eq 'HASH') {
 7445:             if ($settings->{min}) {
 7446:                 $min = $settings->{min};
 7447:             }
 7448:             if ($settings->{max}) {
 7449:                 $max = $settings->{max};
 7450:             }
 7451:             if (ref($settings->{chars}) eq 'ARRAY') {
 7452:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7453:             }
 7454:             if ($settings->{expire}) {
 7455:                 $expire = $settings->{expire};
 7456:             }
 7457:             if ($settings->{numsaved}) {
 7458:                 $numsaved = $settings->{numsaved};
 7459:             }
 7460:         }
 7461:         my %rulenames = &Apache::lonlocal::texthash(
 7462:                                                      uc => 'At least one upper case letter',
 7463:                                                      lc => 'At least one lower case letter',
 7464:                                                      num => 'At least one number',
 7465:                                                      spec => 'At least one non-alphanumeric',
 7466:                                                    );
 7467:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7468:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7469:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7470:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7471:                       'onblur="javascript:warnIntPass(this);" />'.
 7472:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7473:                       '</span></td></tr>';
 7474:         $itemcount ++;
 7475:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7476:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7477:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7478:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7479:                       'onblur="javascript:warnIntPass(this);" />'.
 7480:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7481:                       '</span></td></tr>';
 7482:         $itemcount ++;
 7483:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7484:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7485:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7486:                       '</span></td>';
 7487:         my $numinrow = 2;
 7488:         my @possrules = ('uc','lc','num','spec');
 7489:         $datatable .= '<td class="LC_left_item"><table>';
 7490:         for (my $i=0; $i<@possrules; $i++) {
 7491:             my ($rem,$checked);
 7492:             if ($chars{$possrules[$i]}) {
 7493:                 $checked = ' checked="checked"';
 7494:             }
 7495:             $rem = $i%($numinrow);
 7496:             if ($rem == 0) {
 7497:                 if ($i > 0) {
 7498:                     $datatable .= '</tr>';
 7499:                 }
 7500:                 $datatable .= '<tr>';
 7501:             }
 7502:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7503:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7504:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7505:         }
 7506:         my $rem = @possrules%($numinrow);
 7507:         my $colsleft = $numinrow - $rem;
 7508:         if ($colsleft > 1 ) {
 7509:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7510:                           '&nbsp;</td>';
 7511:         } elsif ($colsleft == 1) {
 7512:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7513:         }
 7514:         $datatable .='</table></td></tr>';
 7515:         $itemcount ++;
 7516:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7517:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7518:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7519:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7520:                       'onblur="javascript:warnIntPass(this);" />'.
 7521:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7522:                       '</span></td></tr>';
 7523:         $itemcount ++;
 7524:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7525:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7526:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7527:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7528:                       'onblur="javascript:warnIntPass(this);" />'. 
 7529:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7530:                       '</span></td></tr>';
 7531:     } else {
 7532:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7533:         my %ownerchg = (
 7534:                           by  => {},
 7535:                           for => {},
 7536:                        );
 7537:         my %ownertitles = &Apache::lonlocal::texthash (
 7538:                             by  => 'Course owner status(es) allowed',
 7539:                             for => 'Student status(es) allowed',
 7540:                           );
 7541:         if (ref($settings) eq 'HASH') {
 7542:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7543:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7544:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7545:                 }
 7546:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7547:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7548:                 }
 7549:             }
 7550:         }
 7551:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7552:         $datatable .= '<tr '.$css_class.'>'.
 7553:                       '<td>'.
 7554:                       &mt('Requirements').'<ul>'.
 7555:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7556:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7557:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7558:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7559:                       '</ul>'.
 7560:                       '</td>'.
 7561:                       '<td class="LC_left_item">';
 7562:         foreach my $item ('by','for') {
 7563:             $datatable .= '<fieldset style="display: inline-block;">'.
 7564:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7565:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7566:                 foreach my $type (@{$types}) {
 7567:                     my $checked;
 7568:                     if ($ownerchg{$item}{$type}) {
 7569:                         $checked = ' checked="checked"';
 7570:                     }
 7571:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7572:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7573:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7574:                                   '</span>&nbsp;&nbsp; ';
 7575:                 }
 7576:             }
 7577:             my $checked;
 7578:             if ($ownerchg{$item}{'default'}) {
 7579:                 $checked = ' checked="checked"';
 7580:             }
 7581:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7582:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7583:                           $othertitle.'</label></span></fieldset>';
 7584:         }
 7585:         $datatable .= '</td></tr>';
 7586:     }
 7587:     return $datatable;
 7588: }
 7589: 
 7590: sub print_wafproxy {
 7591:     my ($position,$dom,$settings,$rowtotal) = @_;
 7592:     my $css_class;
 7593:     my $itemcount = 0;
 7594:     my $datatable;
 7595:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7596:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7597:     my %lt = &wafproxy_titles();
 7598:     foreach my $server (sort(keys(%servers))) {
 7599:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7600:         next if ($serverhome eq '');
 7601:         my $serverdom;
 7602:         if ($serverhome ne $server) {
 7603:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7604:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7605:                 $othercontrol{$server} = $serverdom;
 7606:             }
 7607:         } else {
 7608:             $serverdom = &Apache::lonnet::host_domain($server);
 7609:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7610:             if ($serverdom ne $dom) {
 7611:                 $othercontrol{$server} = $serverdom;
 7612:             } else {
 7613:                 $setdom = 1;
 7614:                 if (ref($settings) eq 'HASH') {
 7615:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7616:                         $aliases{$dom} = $settings->{'alias'};
 7617:                         if ($aliases{$dom} ne '') {
 7618:                             $showdom = 1;
 7619:                         }
 7620:                     }
 7621:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7622:                         $saml{$dom} = $settings->{'saml'};
 7623:                     }
 7624:                 }
 7625:             }
 7626:         }
 7627:     }
 7628:     if ($setdom) {
 7629:         %{$values{$dom}} = ();
 7630:         if (ref($settings) eq 'HASH') {
 7631:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7632:                 $values{$dom}{$item} = $settings->{$item};
 7633:             }
 7634:         }
 7635:     }
 7636:     if (keys(%othercontrol)) {
 7637:         %otherdoms = reverse(%othercontrol);
 7638:         foreach my $domain (keys(%otherdoms)) {
 7639:             %{$values{$domain}} = ();
 7640:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7641:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7642:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7643:                 if (exists($config{'wafproxy'}{'saml'})) { 
 7644:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 7645:                 }
 7646:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7647:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7648:                 }
 7649:             }
 7650:         }
 7651:     }
 7652:     if ($position eq 'top') {
 7653:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7654:         my %aliasinfo;
 7655:         foreach my $server (sort(keys(%servers))) {
 7656:             $itemcount ++;
 7657:             my $dom_in_effect;
 7658:             my $aliasrows = '<tr>'.
 7659:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7660:                             &mt('Hostname').':&nbsp;'.
 7661:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 7662:             if ($othercontrol{$server}) {
 7663:                 $dom_in_effect = $othercontrol{$server};
 7664:                 my ($current,$forsaml);
 7665:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7666:                     $current = $aliases{$dom_in_effect}{$server};
 7667:                 }
 7668:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 7669:                     if ($saml{$dom_in_effect}{$server}) {
 7670:                         $forsaml = 1;
 7671:                     }
 7672:                 }
 7673:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7674:                               &mt('Alias').':&nbsp';
 7675:                 if ($current) {
 7676:                     $aliasrows .= $current;
 7677:                     if ($forsaml) {
 7678:                          $aliasrows .= '&nbsp;('.&mt('also for Shibboleth').')';
 7679:                     }
 7680:                 } else {
 7681:                     $aliasrows .= &mt('None');
 7682:                 }
 7683:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7684:                               &mt('controlled by domain: [_1]',
 7685:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7686:             } else {
 7687:                 $dom_in_effect = $dom;
 7688:                 my ($current,$samlon,$samloff);
 7689:                 $samloff = ' checked="checked"';
 7690:                 if (ref($aliases{$dom}) eq 'HASH') {
 7691:                     if ($aliases{$dom}{$server}) {
 7692:                         $current = $aliases{$dom}{$server};
 7693:                     }
 7694:                 }
 7695:                 if (ref($saml{$dom}) eq 'HASH') {
 7696:                     if ($saml{$dom}{$server}) {
 7697:                         $samlon = $samloff;
 7698:                         undef($samloff);
 7699:                     }
 7700:                 }
 7701:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7702:                               &mt('Alias').':&nbsp;'.
 7703:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7704:                               'value="'.$current.'" size="30" />'.
 7705:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 7706:                               &mt('Alias used for Shibboleth').':&nbsp;<label>'.
 7707:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 7708:                               &mt('No').'</label>&nbsp;<label>'. 
 7709:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 7710:                               &mt('Yes').'</label></span>'.
 7711:                               '</td>'; 
 7712:             }
 7713:             $aliasrows .= '</tr>';
 7714:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7715:         }
 7716:         if ($aliasinfo{$dom}) {
 7717:             my ($onclick,$wafon,$wafoff,$showtable);
 7718:             $onclick = ' onclick="javascript:toggleWAF();"';
 7719:             $wafoff = ' checked="checked"';
 7720:             $showtable = ' style="display:none";';
 7721:             if ($showdom) {
 7722:                 $wafon = $wafoff;
 7723:                 $wafoff = '';
 7724:                 $showtable = ' style="display:inline;"';
 7725:             }
 7726:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7727:             $datatable = '<tr'.$css_class.'>'.
 7728:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7729:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7730:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7731:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7732:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7733:                          &mt('No').'</label></span></td>'.
 7734:                          '<td class="LC_left_item">'.
 7735:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 7736:                          '</table></td></tr>';
 7737:             $itemcount++;
 7738:         }
 7739:         if (keys(%otherdoms)) {
 7740:             foreach my $key (sort(keys(%otherdoms))) {
 7741:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7742:                 $datatable .= '<tr'.$css_class.'>'.
 7743:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 7744:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 7745:                               '</table></td></tr>';
 7746:                 $itemcount++;
 7747:             }
 7748:         }
 7749:     } else {
 7750:         my %ip_methods = &remoteip_methods();
 7751:         if ($setdom) {
 7752:             $itemcount ++;
 7753:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7754:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 7755:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 7756:             $wafstyle = ' style="display:none;"';
 7757:             $nowafstyle = ' style="display:table-row;"';
 7758:             $currwafdisplay = ' style="display: none"';
 7759:             $wafrangestyle = ' style="display: none"';
 7760:             $curr_remotip = 'n';
 7761:             $ssltossl = ' checked="checked"';
 7762:             if ($showdom) {
 7763:                 $wafstyle = ' style="display:table-row;"';
 7764:                 $nowafstyle =  ' style="display:none;"';
 7765:                 if (keys(%{$values{$dom}})) {
 7766:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 7767:                         $curr_remotip = $values{$dom}{remoteip};
 7768:                     }
 7769:                     if ($curr_remotip eq 'h') {
 7770:                         $currwafdisplay = ' style="display:table-row"';
 7771:                         $wafrangestyle = ' style="display:inline-block;"';
 7772:                     }
 7773:                     if ($values{$dom}{'sslopt'}) {
 7774:                         $alltossl = ' checked="checked"';
 7775:                         $ssltossl = '';
 7776:                     }
 7777:                 }
 7778:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 7779:                     $vpndircheck = ' checked="checked"';
 7780:                     $currwafvpn = ' style="display:table-row;"';
 7781:                     $wafrangestyle = ' style="display:inline-block;"';
 7782:                 } else {
 7783:                     $vpnaliascheck = ' checked="checked"';
 7784:                     $currwafvpn = ' style="display:none;"';
 7785:                 }
 7786:             }
 7787:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7788:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 7789:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 7790:                           '</tr>'.
 7791:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7792:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 7793:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 7794:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 7795:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 7796:                           '<td class="LC_left_item"><table>'.
 7797:                           '<tr>'.
 7798:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 7799:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 7800:             foreach my $option ('m','h','n') {
 7801:                 my $sel;
 7802:                 if ($option eq $curr_remotip) {
 7803:                    $sel = ' selected="selected"';
 7804:                 }
 7805:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 7806:                               $ip_methods{$option}.'</option>';
 7807:             }
 7808:             $datatable .= '</select></td></tr>'."\n".
 7809:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 7810:                           $lt{'ipheader'}.':&nbsp;'.
 7811:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 7812:                           'name="wafproxy_ipheader" />'.
 7813:                           '</td></tr>'."\n".
 7814:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 7815:                           $lt{'trusted'}.':<br />'.
 7816:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 7817:                           $values{$dom}{'trusted'}.'</textarea>'.
 7818:                           '</td></tr>'."\n".
 7819:                           '<tr><td><hr /></td></tr>'."\n".
 7820:                           '<tr>'.
 7821:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 7822:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 7823:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 7824:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 7825:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 7826:             foreach my $item ('vpnint','vpnext') {
 7827:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 7828:                               '<td valign="top">'.$lt{$item}.':<br />'.
 7829:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 7830:                               $values{$dom}{$item}.'</textarea>'.
 7831:                               '</td></tr>'."\n";
 7832:             }
 7833:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 7834:                           '<tr>'.
 7835:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 7836:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 7837:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 7838:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 7839:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 7840:                           '</table></td></tr>';
 7841:         }
 7842:         if (keys(%otherdoms)) {
 7843:             foreach my $domain (sort(keys(%otherdoms))) {
 7844:                 $itemcount ++;
 7845:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7846:                 $datatable .= '<tr'.$css_class.'>'.
 7847:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 7848:                               '<td class="LC_left_item"><table>';
 7849:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 7850:                     my $showval = &mt('None');
 7851:                     if ($item eq 'ssl') {
 7852:                         $showval = $lt{'ssltossl'};
 7853:                     }
 7854:                     if ($values{$domain}{$item}) {
 7855:                         $showval = $values{$domain}{$item};
 7856:                         if ($item eq 'ssl') {
 7857:                             $showval = $lt{'alltossl'};
 7858:                         } elsif ($item eq 'remoteip') {
 7859:                             $showval = $ip_methods{$values{$domain}{$item}};
 7860:                         }
 7861:                     }
 7862:                     $datatable .= '<tr>'.
 7863:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 7864:                 }
 7865:                 $datatable .= '</table></td></tr>';
 7866:             }
 7867:         }
 7868:     }
 7869:     $$rowtotal += $itemcount;
 7870:     return $datatable;
 7871: }
 7872: 
 7873: sub wafproxy_titles {
 7874:     return &Apache::lonlocal::texthash(
 7875:                remoteip   => "Method for determining user's IP",
 7876:                ipheader   => 'Request header containing remote IP',
 7877:                trusted    => 'Trusted IP range(s)',
 7878:                vpnaccess  => 'Access from institutional VPN',
 7879:                vpndirect  => 'via regular hostname (no WAF)',
 7880:                vpnaliased => 'via aliased hostname (WAF)',
 7881:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 7882:                vpnext     => 'IP Range(s) for backend WAF connections',
 7883:                sslopt     => 'Forwarding http/https',
 7884:                alltossl   => 'WAF forwards both http and https requests to https',
 7885:                ssltossl   => 'WAF forwards http requests to http and https to https',
 7886:            );
 7887: }
 7888: 
 7889: sub remoteip_methods {
 7890:     return &Apache::lonlocal::texthash(
 7891:               m => 'Use Apache mod_remoteip',
 7892:               h => 'Use headers parsed by LON-CAPA',
 7893:               n => 'Not in use',
 7894:            );
 7895: }
 7896: 
 7897: sub print_usersessions {
 7898:     my ($position,$dom,$settings,$rowtotal) = @_;
 7899:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 7900:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7901:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7902: 
 7903:     my @alldoms = &Apache::lonnet::all_domains();
 7904:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7905:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7906:     my %altids = &id_for_thisdom(%servers);
 7907:     if ($position eq 'top') {
 7908:         if (keys(%serverhomes) > 1) {
 7909:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7910:             my ($curroffloadnow,$curroffloadoth);
 7911:             if (ref($settings) eq 'HASH') {
 7912:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7913:                     $curroffloadnow = $settings->{'offloadnow'};
 7914:                 }
 7915:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7916:                     $curroffloadoth = $settings->{'offloadoth'};
 7917:                 }
 7918:             }
 7919:             my $other_insts = scalar(keys(%by_location));
 7920:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7921:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7922:         } else {
 7923:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7924:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7925:                           '</td></tr>';
 7926:         }
 7927:     } else {
 7928:         my %titles = &usersession_titles();
 7929:         my ($prefix,@types);
 7930:         if ($position eq 'bottom') {
 7931:             $prefix = 'remote';
 7932:             @types = ('version','excludedomain','includedomain');
 7933:         } else {
 7934:             $prefix = 'hosted';
 7935:             @types = ('excludedomain','includedomain');
 7936:         }
 7937:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7938:     }
 7939:     $$rowtotal += $itemcount;
 7940:     return $datatable;
 7941: }
 7942: 
 7943: sub rules_by_location {
 7944:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 7945:     my ($datatable,$itemcount,$css_class);
 7946:     if (keys(%{$by_location}) == 0) {
 7947:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7948:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 7949:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7950:                      '</td></tr>';
 7951:         $itemcount = 1;
 7952:     } else {
 7953:         $itemcount = 0;
 7954:         my $numinrow = 5;
 7955:         my (%current,%checkedon,%checkedoff);
 7956:         my @locations = sort(keys(%{$by_location}));
 7957:         foreach my $type (@{$types}) {
 7958:             $checkedon{$type} = '';
 7959:             $checkedoff{$type} = ' checked="checked"';
 7960:         }
 7961:         if (ref($settings) eq 'HASH') {
 7962:             if (ref($settings->{$prefix}) eq 'HASH') {
 7963:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 7964:                     $current{$key} = $settings->{$prefix}{$key};
 7965:                     if ($key eq 'version') {
 7966:                         if ($current{$key} ne '') {
 7967:                             $checkedon{$key} = ' checked="checked"';
 7968:                             $checkedoff{$key} = '';
 7969:                         }
 7970:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 7971:                         $checkedon{$key} = ' checked="checked"';
 7972:                         $checkedoff{$key} = '';
 7973:                     }
 7974:                 }
 7975:             }
 7976:         }
 7977:         foreach my $type (@{$types}) {
 7978:             next if ($type ne 'version' && !@locations);
 7979:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7980:             $datatable .= '<tr'.$css_class.'>
 7981:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 7982:                            <span class="LC_nobreak">&nbsp;
 7983:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7984:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7985:             if ($type eq 'version') {
 7986:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 7987:                 my $selector = '<select name="'.$prefix.'_version">';
 7988:                 foreach my $version (@lcversions) {
 7989:                     my $selected = '';
 7990:                     if ($current{'version'} eq $version) {
 7991:                         $selected = ' selected="selected"';
 7992:                     }
 7993:                     $selector .= ' <option value="'.$version.'"'.
 7994:                                  $selected.'>'.$version.'</option>';
 7995:                 }
 7996:                 $selector .= '</select> ';
 7997:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7998:             } else {
 7999:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8000:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8001:                              ' />'.('&nbsp;'x2).
 8002:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8003:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8004:                              "\n".
 8005:                              '</div><div><table>';
 8006:                 my $rem;
 8007:                 for (my $i=0; $i<@locations; $i++) {
 8008:                     my ($showloc,$value,$checkedtype);
 8009:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8010:                         my $ip = $by_location->{$locations[$i]}->[0];
 8011:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8012:                             $value = join(':',@{$by_ip->{$ip}});
 8013:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8014:                             if (ref($current{$type}) eq 'ARRAY') {
 8015:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8016:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8017:                                         $checkedtype = ' checked="checked"';
 8018:                                         last;
 8019:                                     }
 8020:                                 }
 8021:                             }
 8022:                         }
 8023:                     }
 8024:                     $rem = $i%($numinrow);
 8025:                     if ($rem == 0) {
 8026:                         if ($i > 0) {
 8027:                             $datatable .= '</tr>';
 8028:                         }
 8029:                         $datatable .= '<tr>';
 8030:                     }
 8031:                     $datatable .= '<td class="LC_left_item">'.
 8032:                                   '<span class="LC_nobreak"><label>'.
 8033:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8034:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8035:                                   '</label></span></td>';
 8036:                 }
 8037:                 $rem = @locations%($numinrow);
 8038:                 my $colsleft = $numinrow - $rem;
 8039:                 if ($colsleft > 1 ) {
 8040:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8041:                                   '&nbsp;</td>';
 8042:                 } elsif ($colsleft == 1) {
 8043:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8044:                 }
 8045:                 $datatable .= '</tr></table>';
 8046:             }
 8047:             $datatable .= '</td></tr>';
 8048:             $itemcount ++;
 8049:         }
 8050:     }
 8051:     return ($datatable,$itemcount);
 8052: }
 8053: 
 8054: sub print_ssl {
 8055:     my ($position,$dom,$settings,$rowtotal) = @_;
 8056:     my ($css_class,$datatable);
 8057:     my $itemcount = 1;
 8058:     if ($position eq 'top') {
 8059:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8060:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8061:         my $same_institution;
 8062:         if ($intdom ne '') {
 8063:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8064:             if (ref($internet_names) eq 'ARRAY') {
 8065:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8066:                     $same_institution = 1;
 8067:                 }
 8068:             }
 8069:         }
 8070:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8071:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8072:         if ($same_institution) {
 8073:             my %domservers = &Apache::lonnet::get_servers($dom);
 8074:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8075:         } else {
 8076:             $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.");
 8077:         }
 8078:         $datatable .= '</td></tr>';
 8079:         $itemcount ++;
 8080:     } else {
 8081:         my %titles = &ssl_titles();
 8082:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8083:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8084:         my @alldoms = &Apache::lonnet::all_domains();
 8085:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8086:         my @domservers = &Apache::lonnet::get_servers($dom);
 8087:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8088:         my %altids = &id_for_thisdom(%servers);
 8089:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8090:             my $legacy;
 8091:             unless (ref($settings) eq 'HASH') {
 8092:                 my $name;
 8093:                 if ($position eq 'connto') {
 8094:                     $name = 'loncAllowInsecure';
 8095:                 } else {
 8096:                     $name = 'londAllowInsecure';
 8097:                 }
 8098:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8099:                 my @ids=&Apache::lonnet::current_machine_ids();
 8100:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8101:                     my %what = (
 8102:                                    $name => 1,
 8103:                                );
 8104:                     my ($result,$returnhash) =
 8105:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8106:                     if ($result eq 'ok') {
 8107:                         if (ref($returnhash) eq 'HASH') {
 8108:                             $legacy = $returnhash->{$name};
 8109:                         }
 8110:                     }
 8111:                 } else {
 8112:                     $legacy = $Apache::lonnet::perlvar{$name};
 8113:                 }
 8114:             }
 8115:             foreach my $type ('dom','intdom','other') {
 8116:                 my %checked;
 8117:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8118:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8119:                               '<td class="LC_right_item">';
 8120:                 my $skip; 
 8121:                 if ($type eq 'dom') {
 8122:                     unless (keys(%servers) > 1) {
 8123:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8124:                         $skip = 1;
 8125:                     }
 8126:                 }
 8127:                 if ($type eq 'intdom') {
 8128:                     unless (@instdoms > 1) {
 8129:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8130:                         $skip = 1;
 8131:                     } 
 8132:                 } elsif ($type eq 'other') {
 8133:                     if (keys(%by_location) == 0) {
 8134:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8135:                         $skip = 1;
 8136:                     }
 8137:                 }
 8138:                 unless ($skip) {
 8139:                     $checked{'yes'} = ' checked="checked"'; 
 8140:                     if (ref($settings) eq 'HASH') {
 8141:                         if (ref($settings->{$position}) eq 'HASH') {
 8142:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8143:                                 $checked{$1} = $checked{'yes'};
 8144:                                 delete($checked{'yes'}); 
 8145:                             }
 8146:                         }
 8147:                     } else {
 8148:                         if ($legacy == 0) {
 8149:                             $checked{'req'} = $checked{'yes'};
 8150:                             delete($checked{'yes'});    
 8151:                         }
 8152:                     }
 8153:                     foreach my $option ('no','yes','req') {
 8154:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8155:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8156:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8157:                                       '</label></span>'.('&nbsp;'x2);
 8158:                     }
 8159:                 }
 8160:                 $datatable .= '</td></tr>';
 8161:                 $itemcount ++; 
 8162:             }
 8163:         } else {
 8164:             my $prefix = 'replication';
 8165:             my @types = ('certreq','nocertreq');
 8166:             if (keys(%by_location) == 0) {
 8167:                 $datatable .= '<tr'.$css_class.'><td>'.
 8168:                               &mt('Nothing to set here, as there are no other institutions').
 8169:                               '</td></tr>';
 8170:                 $itemcount ++;
 8171:             } else {
 8172:                 ($datatable,$itemcount) = 
 8173:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8174:             }
 8175:         }
 8176:     }
 8177:     $$rowtotal += $itemcount;
 8178:     return $datatable;
 8179: }
 8180: 
 8181: sub ssl_titles {
 8182:     return &Apache::lonlocal::texthash (
 8183:                dom           => 'LON-CAPA servers/VMs from same domain',
 8184:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8185:                other         => 'External LON-CAPA servers/VMs',
 8186:                connto        => 'Connections to other servers',
 8187:                connfrom      => 'Connections from other servers',
 8188:                replication   => 'Replicating content to other institutions',
 8189:                certreq       => 'Client certificate required, but specific domains exempt',
 8190:                nocertreq     => 'No client certificate required, except for specific domains',
 8191:                no            => 'SSL not used',
 8192:                yes           => 'SSL Optional (used if available)',
 8193:                req           => 'SSL Required',
 8194:     );
 8195: }
 8196: 
 8197: sub print_trust {
 8198:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8199:     my ($css_class,$datatable,%checked,%choices);
 8200:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8201:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8202:     my $itemcount = 1;
 8203:     my %titles = &trust_titles();
 8204:     my @types = ('exc','inc');
 8205:     if ($prefix eq 'top') {
 8206:         $prefix = 'content';
 8207:     } elsif ($prefix eq 'bottom') {
 8208:         $prefix = 'msg';
 8209:     }
 8210:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8211:     $$rowtotal += $itemcount;
 8212:     return $datatable;
 8213: }
 8214: 
 8215: sub trust_titles {
 8216:     return &Apache::lonlocal::texthash(
 8217:                content  => "Access to this domain's content by others",
 8218:                shared   => "Access to other domain's content by this domain",
 8219:                enroll   => "Enrollment in this domain's courses by others", 
 8220:                othcoau  => "Co-author roles in this domain for others",
 8221:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8222:                domroles => "Domain roles in this domain assignable to others",
 8223:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8224:                reqcrs   => "Requests for creation of courses in this domain by others",
 8225:                msg      => "Users in other domains can send messages to this domain",
 8226:                exc      => "Allow all, but exclude specific domains",
 8227:                inc      => "Deny all, but include specific domains",
 8228:            );
 8229: } 
 8230: 
 8231: sub build_location_hashes {
 8232:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8233:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8234:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8235:     my %iphost = &Apache::lonnet::get_iphost();
 8236:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8237:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8238:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8239:         foreach my $id (@{$iphost{$primary_ip}}) {
 8240:             my $intdom = &Apache::lonnet::internet_dom($id);
 8241:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8242:                 push(@{$intdoms},$intdom);
 8243:             }
 8244:         }
 8245:     }
 8246:     foreach my $ip (keys(%iphost)) {
 8247:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8248:             foreach my $id (@{$iphost{$ip}}) {
 8249:                 my $location = &Apache::lonnet::internet_dom($id);
 8250:                 if ($location) {
 8251:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8252:                         my $dom = &Apache::lonnet::host_domain($id);
 8253:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8254:                             push(@{$instdoms},$dom);
 8255:                         }
 8256:                         next;
 8257:                     }
 8258:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8259:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8260:                             push(@{$by_ip->{$ip}},$location);
 8261:                         }
 8262:                     } else {
 8263:                         $by_ip->{$ip} = [$location];
 8264:                     }
 8265:                 }
 8266:             }
 8267:         }
 8268:     }
 8269:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8270:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8271:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8272:             my $first = $by_ip->{$ip}->[0];
 8273:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8274:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8275:                     push(@{$by_location->{$first}},$ip);
 8276:                 }
 8277:             } else {
 8278:                 $by_location->{$first} = [$ip];
 8279:             }
 8280:         }
 8281:     }
 8282:     return;
 8283: }
 8284: 
 8285: sub current_offloads_to {
 8286:     my ($dom,$settings,$servers) = @_;
 8287:     my (%spareid,%otherdomconfigs);
 8288:     if (ref($servers) eq 'HASH') {
 8289:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8290:             my $gotspares;
 8291:             if (ref($settings) eq 'HASH') {
 8292:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8293:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8294:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8295:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8296:                         $gotspares = 1;
 8297:                     }
 8298:                 }
 8299:             }
 8300:             unless ($gotspares) {
 8301:                 my $gotspares;
 8302:                 my $serverhomeID =
 8303:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8304:                 my $serverhomedom =
 8305:                     &Apache::lonnet::host_domain($serverhomeID);
 8306:                 if ($serverhomedom ne $dom) {
 8307:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8308:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8309:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8310:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8311:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8312:                                 $gotspares = 1;
 8313:                             }
 8314:                         }
 8315:                     } else {
 8316:                         $otherdomconfigs{$serverhomedom} =
 8317:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8318:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8319:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8320:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8321:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8322:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8323:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8324:                                         $gotspares = 1;
 8325:                                     }
 8326:                                 }
 8327:                             }
 8328:                         }
 8329:                     }
 8330:                 }
 8331:             }
 8332:             unless ($gotspares) {
 8333:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8334:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8335:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8336:                } else {
 8337:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8338:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8339:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8340:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8341:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8342:                     } else {
 8343:                         my %what = (
 8344:                              spareid => 1,
 8345:                         );
 8346:                         my ($result,$returnhash) = 
 8347:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8348:                         if ($result eq 'ok') { 
 8349:                             if (ref($returnhash) eq 'HASH') {
 8350:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8351:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8352:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8353:                                 }
 8354:                             }
 8355:                         }
 8356:                     }
 8357:                 }
 8358:             }
 8359:         }
 8360:     }
 8361:     return %spareid;
 8362: }
 8363: 
 8364: sub spares_row {
 8365:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8366:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8367:     my $css_class;
 8368:     my $numinrow = 4;
 8369:     my $itemcount = 1;
 8370:     my $datatable;
 8371:     my %typetitles = &sparestype_titles();
 8372:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8373:         foreach my $server (sort(keys(%{$servers}))) {
 8374:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8375:             my ($othercontrol,$serverdom);
 8376:             if ($serverhome ne $server) {
 8377:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8378:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8379:             } else {
 8380:                 $serverdom = &Apache::lonnet::host_domain($server);
 8381:                 if ($serverdom ne $dom) {
 8382:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8383:                 }
 8384:             }
 8385:             next unless (ref($spareid->{$server}) eq 'HASH');
 8386:             my ($checkednow,$checkedoth);
 8387:             if (ref($curroffloadnow) eq 'HASH') {
 8388:                 if ($curroffloadnow->{$server}) {
 8389:                     $checkednow = ' checked="checked"';
 8390:                 }
 8391:             }
 8392:             if (ref($curroffloadoth) eq 'HASH') {
 8393:                 if ($curroffloadoth->{$server}) {
 8394:                     $checkedoth = ' checked="checked"';
 8395:                 }
 8396:             }
 8397:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8398:             $datatable .= '<tr'.$css_class.'>
 8399:                            <td rowspan="2">
 8400:                             <span class="LC_nobreak">'.
 8401:                           &mt('[_1] when busy, offloads to:'
 8402:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8403:                           '<span class="LC_nobreak">'."\n".
 8404:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8405:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8406:                           "\n";
 8407:             if ($other_insts) {
 8408:                 $datatable .= '<br />'.
 8409:                               '<span class="LC_nobreak">'."\n".
 8410:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8411:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8412:                           "\n";
 8413:             }
 8414:             my (%current,%canselect);
 8415:             my @choices = 
 8416:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8417:             foreach my $type ('primary','default') {
 8418:                 if (ref($spareid->{$server}) eq 'HASH') {
 8419:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8420:                         my @spares = @{$spareid->{$server}{$type}};
 8421:                         if (@spares > 0) {
 8422:                             if ($othercontrol) {
 8423:                                 $current{$type} = join(', ',@spares);
 8424:                             } else {
 8425:                                 $current{$type} .= '<table>';
 8426:                                 my $numspares = scalar(@spares);
 8427:                                 for (my $i=0;  $i<@spares; $i++) {
 8428:                                     my $rem = $i%($numinrow);
 8429:                                     if ($rem == 0) {
 8430:                                         if ($i > 0) {
 8431:                                             $current{$type} .= '</tr>';
 8432:                                         }
 8433:                                         $current{$type} .= '<tr>';
 8434:                                     }
 8435:                                     $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;'.
 8436:                                                        $spareid->{$server}{$type}[$i].
 8437:                                                        '</label></td>'."\n";
 8438:                                 }
 8439:                                 my $rem = @spares%($numinrow);
 8440:                                 my $colsleft = $numinrow - $rem;
 8441:                                 if ($colsleft > 1 ) {
 8442:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8443:                                                        '" class="LC_left_item">'.
 8444:                                                        '&nbsp;</td>';
 8445:                                 } elsif ($colsleft == 1) {
 8446:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8447:                                 }
 8448:                                 $current{$type} .= '</tr></table>';
 8449:                             }
 8450:                         }
 8451:                     }
 8452:                     if ($current{$type} eq '') {
 8453:                         $current{$type} = &mt('None specified');
 8454:                     }
 8455:                     if ($othercontrol) {
 8456:                         if ($type eq 'primary') {
 8457:                             $canselect{$type} = $othercontrol;
 8458:                         }
 8459:                     } else {
 8460:                         $canselect{$type} = 
 8461:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8462:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8463:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8464:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8465:                         if (@choices > 0) {
 8466:                             foreach my $lonhost (@choices) {
 8467:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8468:                             }
 8469:                         }
 8470:                         $canselect{$type} .= '</select>'."\n";
 8471:                     }
 8472:                 } else {
 8473:                     $current{$type} = &mt('Could not be determined');
 8474:                     if ($type eq 'primary') {
 8475:                         $canselect{$type} =  $othercontrol;
 8476:                     }
 8477:                 }
 8478:                 if ($type eq 'default') {
 8479:                     $datatable .= '<tr'.$css_class.'>';
 8480:                 }
 8481:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8482:                               '<td>'.$current{$type}.'</td>'."\n".
 8483:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8484:             }
 8485:             $itemcount ++;
 8486:         }
 8487:     }
 8488:     $$rowtotal += $itemcount;
 8489:     return $datatable;
 8490: }
 8491: 
 8492: sub possible_newspares {
 8493:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8494:     my $serverhostname = &Apache::lonnet::hostname($server);
 8495:     my %excluded;
 8496:     if ($serverhostname ne '') {
 8497:         %excluded = (
 8498:                        $serverhostname => 1,
 8499:                     );
 8500:     }
 8501:     if (ref($currspares) eq 'HASH') {
 8502:         foreach my $type (keys(%{$currspares})) {
 8503:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8504:                 if (@{$currspares->{$type}} > 0) {
 8505:                     foreach my $curr (@{$currspares->{$type}}) {
 8506:                         my $hostname = &Apache::lonnet::hostname($curr);
 8507:                         $excluded{$hostname} = 1;
 8508:                     }
 8509:                 }
 8510:             }
 8511:         }
 8512:     }
 8513:     my @choices;
 8514:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8515:         if (keys(%{$serverhomes}) > 1) {
 8516:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8517:                 unless ($excluded{$name}) {
 8518:                     if (exists($altids->{$serverhomes->{$name}})) {
 8519:                         push(@choices,$altids->{$serverhomes->{$name}});
 8520:                     } else {
 8521:                         push(@choices,$serverhomes->{$name});
 8522:                     }
 8523:                 }
 8524:             }
 8525:         }
 8526:     }
 8527:     return sort(@choices);
 8528: }
 8529: 
 8530: sub print_loadbalancing {
 8531:     my ($dom,$settings,$rowtotal) = @_;
 8532:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8533:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8534:     my $numinrow = 1;
 8535:     my $datatable;
 8536:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8537:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8538:     if (ref($settings) eq 'HASH') {
 8539:         %existing = %{$settings};
 8540:     }
 8541:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8542:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8543:                                   \%currtargets,\%currrules,\%currcookies);
 8544:     } else {
 8545:         return;
 8546:     }
 8547:     my ($othertitle,$usertypes,$types) =
 8548:         &Apache::loncommon::sorted_inst_types($dom);
 8549:     my $rownum = 8;
 8550:     if (ref($types) eq 'ARRAY') {
 8551:         $rownum += scalar(@{$types});
 8552:     }
 8553:     my @css_class = ('LC_odd_row','LC_even_row');
 8554:     my $balnum = 0;
 8555:     my $islast;
 8556:     my (@toshow,$disabledtext);
 8557:     if (keys(%currbalancer) > 0) {
 8558:         @toshow = sort(keys(%currbalancer));
 8559:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8560:             push(@toshow,'');
 8561:         }
 8562:     } else {
 8563:         @toshow = ('');
 8564:         $disabledtext = &mt('No existing load balancer');
 8565:     }
 8566:     foreach my $lonhost (@toshow) {
 8567:         if ($balnum == scalar(@toshow)-1) {
 8568:             $islast = 1;
 8569:         } else {
 8570:             $islast = 0;
 8571:         }
 8572:         my $cssidx = $balnum%2;
 8573:         my $targets_div_style = 'display: none';
 8574:         my $disabled_div_style = 'display: block';
 8575:         my $homedom_div_style = 'display: none';
 8576:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8577:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8578:                       '<p>';
 8579:         if ($lonhost eq '') {
 8580:             $datatable .= '<span class="LC_nobreak">';
 8581:             if (keys(%currbalancer) > 0) {
 8582:                 $datatable .= &mt('Add balancer:');
 8583:             } else {
 8584:                 $datatable .= &mt('Enable balancer:');
 8585:             }
 8586:             $datatable .= '&nbsp;'.
 8587:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8588:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8589:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8590:                           '<option value="" selected="selected">'.&mt('None').
 8591:                           '</option>'."\n";
 8592:             foreach my $server (sort(keys(%servers))) {
 8593:                 next if ($currbalancer{$server});
 8594:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8595:             }
 8596:             $datatable .=
 8597:                 '</select>'."\n".
 8598:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8599:         } else {
 8600:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8601:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8602:                            &mt('Stop balancing').'</label>'.
 8603:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8604:             $targets_div_style = 'display: block';
 8605:             $disabled_div_style = 'display: none';
 8606:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8607:                 $homedom_div_style = 'display: block';
 8608:             }
 8609:         }
 8610:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8611:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8612:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8613:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8614:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8615:         my @sparestypes = ('primary','default');
 8616:         my %typetitles = &sparestype_titles();
 8617:         my %hostherechecked = (
 8618:                                   no => ' checked="checked"',
 8619:                               );
 8620:         my %balcookiechecked = (
 8621:                                   no => ' checked="checked"', 
 8622:                                );
 8623:         foreach my $sparetype (@sparestypes) {
 8624:             my $targettable;
 8625:             for (my $i=0; $i<$numspares; $i++) {
 8626:                 my $checked;
 8627:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8628:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8629:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8630:                             $checked = ' checked="checked"';
 8631:                         }
 8632:                     }
 8633:                 }
 8634:                 my ($chkboxval,$disabled);
 8635:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8636:                     $chkboxval = $spares[$i];
 8637:                 }
 8638:                 if (exists($currbalancer{$spares[$i]})) {
 8639:                     $disabled = ' disabled="disabled"';
 8640:                 }
 8641:                 $targettable .=
 8642:                     '<td><span class="LC_nobreak"><label>'.
 8643:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8644:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8645:                     '</span></label></span></td>';
 8646:                 my $rem = $i%($numinrow);
 8647:                 if ($rem == 0) {
 8648:                     if (($i > 0) && ($i < $numspares-1)) {
 8649:                         $targettable .= '</tr>';
 8650:                     }
 8651:                     if ($i < $numspares-1) {
 8652:                         $targettable .= '<tr>';
 8653:                     }
 8654:                 }
 8655:             }
 8656:             if ($targettable ne '') {
 8657:                 my $rem = $numspares%($numinrow);
 8658:                 my $colsleft = $numinrow - $rem;
 8659:                 if ($colsleft > 1 ) {
 8660:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8661:                                     '&nbsp;</td>';
 8662:                 } elsif ($colsleft == 1) {
 8663:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8664:                 }
 8665:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8666:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8667:             }
 8668:             $hostherechecked{$sparetype} = '';
 8669:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8670:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8671:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8672:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8673:                         $hostherechecked{'no'} = '';
 8674:                     }
 8675:                 }
 8676:             }
 8677:         }
 8678:         if ($currcookies{$lonhost}) {
 8679:             %balcookiechecked = (
 8680:                                     yes => ' checked="checked"',
 8681:                                 );
 8682:         }
 8683:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8684:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8685:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8686:         foreach my $sparetype (@sparestypes) {
 8687:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8688:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8689:                           '</i></label><br />';
 8690:         }
 8691:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8692:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8693:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8694:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8695:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8696:                       '</div></td></tr>'.
 8697:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8698:                                            $othertitle,$usertypes,$types,\%servers,
 8699:                                            \%currbalancer,$lonhost,
 8700:                                            $targets_div_style,$homedom_div_style,
 8701:                                            $css_class[$cssidx],$balnum,$islast);
 8702:         $$rowtotal += $rownum;
 8703:         $balnum ++;
 8704:     }
 8705:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8706:     return $datatable;
 8707: }
 8708: 
 8709: sub get_loadbalancers_config {
 8710:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8711:     return unless ((ref($servers) eq 'HASH') &&
 8712:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8713:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8714:                    (ref($currcookies) eq 'HASH'));
 8715:     if (keys(%{$existing}) > 0) {
 8716:         my $oldlonhost;
 8717:         foreach my $key (sort(keys(%{$existing}))) {
 8718:             if ($key eq 'lonhost') {
 8719:                 $oldlonhost = $existing->{'lonhost'};
 8720:                 $currbalancer->{$oldlonhost} = 1;
 8721:             } elsif ($key eq 'targets') {
 8722:                 if ($oldlonhost) {
 8723:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8724:                 }
 8725:             } elsif ($key eq 'rules') {
 8726:                 if ($oldlonhost) {
 8727:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8728:                 }
 8729:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8730:                 $currbalancer->{$key} = 1;
 8731:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8732:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8733:                 if ($existing->{$key}{'cookie'}) {
 8734:                     $currcookies->{$key} = 1;
 8735:                 }
 8736:             }
 8737:         }
 8738:     } else {
 8739:         my ($balancerref,$targetsref) =
 8740:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8741:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8742:             foreach my $server (sort(keys(%{$balancerref}))) {
 8743:                 $currbalancer->{$server} = 1;
 8744:                 $currtargets->{$server} = $targetsref->{$server};
 8745:             }
 8746:         }
 8747:     }
 8748:     return;
 8749: }
 8750: 
 8751: sub loadbalancing_rules {
 8752:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8753:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8754:         $css_class,$balnum,$islast) = @_;
 8755:     my $output;
 8756:     my $num = 0;
 8757:     my ($alltypes,$othertypes,$titles) =
 8758:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8759:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8760:         foreach my $type (@{$alltypes}) {
 8761:             $num ++;
 8762:             my $current;
 8763:             if (ref($currrules) eq 'HASH') {
 8764:                 $current = $currrules->{$type};
 8765:             }
 8766:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8767:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8768:                     $current = '';
 8769:                 }
 8770:             }
 8771:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8772:                                              $servers,$currbalancer,$lonhost,$dom,
 8773:                                              $targets_div_style,$homedom_div_style,
 8774:                                              $css_class,$balnum,$num,$islast);
 8775:         }
 8776:     }
 8777:     return $output;
 8778: }
 8779: 
 8780: sub loadbalancing_titles {
 8781:     my ($dom,$intdom,$usertypes,$types) = @_;
 8782:     my %othertypes = (
 8783:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8784:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8785:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8786:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8787:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8788:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8789:                      );
 8790:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8791:     my @available;
 8792:     if (ref($types) eq 'ARRAY') {
 8793:         @available = @{$types};
 8794:     }
 8795:     unless (grep(/^default$/,@available)) {
 8796:         push(@available,'default');
 8797:     }
 8798:     unshift(@alltypes,@available);
 8799:     my %titles;
 8800:     foreach my $type (@alltypes) {
 8801:         if ($type =~ /^_LC_/) {
 8802:             $titles{$type} = $othertypes{$type};
 8803:         } elsif ($type eq 'default') {
 8804:             $titles{$type} = &mt('All users from [_1]',$dom);
 8805:             if (ref($types) eq 'ARRAY') {
 8806:                 if (@{$types} > 0) {
 8807:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8808:                 }
 8809:             }
 8810:         } elsif (ref($usertypes) eq 'HASH') {
 8811:             $titles{$type} = $usertypes->{$type};
 8812:         }
 8813:     }
 8814:     return (\@alltypes,\%othertypes,\%titles);
 8815: }
 8816: 
 8817: sub loadbalance_rule_row {
 8818:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8819:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8820:     my @rulenames;
 8821:     my %ruletitles = &offloadtype_text();
 8822:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8823:         @rulenames = ('balancer','offloadedto','specific');
 8824:     } else {
 8825:         @rulenames = ('default','homeserver');
 8826:         if ($type eq '_LC_external') {
 8827:             push(@rulenames,'externalbalancer');
 8828:         } else {
 8829:             push(@rulenames,'specific');
 8830:         }
 8831:         push(@rulenames,'none');
 8832:     }
 8833:     my $style = $targets_div_style;
 8834:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8835:         $style = $homedom_div_style;
 8836:     }
 8837:     my $space;
 8838:     if ($islast && $num == 1) {
 8839:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8840:     }
 8841:     my $output =
 8842:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 8843:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8844:         '<td valaign="top">'.$space.
 8845:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8846:     for (my $i=0; $i<@rulenames; $i++) {
 8847:         my $rule = $rulenames[$i];
 8848:         my ($checked,$extra);
 8849:         if ($rulenames[$i] eq 'default') {
 8850:             $rule = '';
 8851:         }
 8852:         if ($rulenames[$i] eq 'specific') {
 8853:             if (ref($servers) eq 'HASH') {
 8854:                 my $default;
 8855:                 if (($current ne '') && (exists($servers->{$current}))) {
 8856:                     $checked = ' checked="checked"';
 8857:                 }
 8858:                 unless ($checked) {
 8859:                     $default = ' selected="selected"';
 8860:                 }
 8861:                 $extra =
 8862:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8863:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8864:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8865:                     '<option value=""'.$default.'></option>'."\n";
 8866:                 foreach my $server (sort(keys(%{$servers}))) {
 8867:                     if (ref($currbalancer) eq 'HASH') {
 8868:                         next if (exists($currbalancer->{$server}));
 8869:                     }
 8870:                     my $selected;
 8871:                     if ($server eq $current) {
 8872:                         $selected = ' selected="selected"';
 8873:                     }
 8874:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8875:                 }
 8876:                 $extra .= '</select>';
 8877:             }
 8878:         } elsif ($rule eq $current) {
 8879:             $checked = ' checked="checked"';
 8880:         }
 8881:         $output .= '<span class="LC_nobreak"><label>'.
 8882:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8883:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8884:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8885:                    ')"'.$checked.' />&nbsp;';
 8886:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8887:             $output .= $ruletitles{'particular'};
 8888:         } else {
 8889:             $output .= $ruletitles{$rulenames[$i]};
 8890:         }
 8891:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8892:     }
 8893:     $output .= '</div></td></tr>'."\n";
 8894:     return $output;
 8895: }
 8896: 
 8897: sub offloadtype_text {
 8898:     my %ruletitles = &Apache::lonlocal::texthash (
 8899:            'default'          => 'Offloads to default destinations',
 8900:            'homeserver'       => "Offloads to user's home server",
 8901:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8902:            'specific'         => 'Offloads to specific server',
 8903:            'none'             => 'No offload',
 8904:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8905:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8906:            'particular'       => 'Session hosted (after re-auth) on server:',
 8907:     );
 8908:     return %ruletitles;
 8909: }
 8910: 
 8911: sub sparestype_titles {
 8912:     my %typestitles = &Apache::lonlocal::texthash (
 8913:                           'primary' => 'primary',
 8914:                           'default' => 'default',
 8915:                       );
 8916:     return %typestitles;
 8917: }
 8918: 
 8919: sub contact_titles {
 8920:     my %titles = &Apache::lonlocal::texthash (
 8921:                    'supportemail'    => 'Support E-mail address',
 8922:                    'adminemail'      => 'Default Server Admin E-mail address',
 8923:                    'errormail'       => 'Error reports to be e-mailed to',
 8924:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8925:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8926:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8927:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8928:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8929:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8930:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8931:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8932:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 8933:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 8934:                    'errorweights'    => 'Weights used to compute error count',
 8935:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8936:                  );
 8937:     my %short_titles = &Apache::lonlocal::texthash (
 8938:                            adminemail   => 'Admin E-mail address',
 8939:                            supportemail => 'Support E-mail',
 8940:                        );   
 8941:     return (\%titles,\%short_titles);
 8942: }
 8943: 
 8944: sub helpform_fields {
 8945:     my %titles =  &Apache::lonlocal::texthash (
 8946:                        'username'   => 'Name',
 8947:                        'user'       => 'Username/domain',
 8948:                        'phone'      => 'Phone',
 8949:                        'cc'         => 'Cc e-mail',
 8950:                        'course'     => 'Course Details',
 8951:                        'section'    => 'Sections',
 8952:                        'screenshot' => 'File upload',
 8953:     );
 8954:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8955:     my %possoptions = (
 8956:                         username     => ['yes','no','req'],
 8957:                         phone        => ['yes','no','req'],
 8958:                         user         => ['yes','no'],
 8959:                         cc           => ['yes','no'],
 8960:                         course       => ['yes','no'],
 8961:                         section      => ['yes','no'],
 8962:                         screenshot   => ['yes','no'],
 8963:                       );
 8964:     my %fieldoptions = &Apache::lonlocal::texthash (
 8965:                          'yes'  => 'Optional',
 8966:                          'req'  => 'Required',
 8967:                          'no'   => "Not shown",
 8968:     );
 8969:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8970: }
 8971: 
 8972: sub tool_titles {
 8973:     my %titles = &Apache::lonlocal::texthash (
 8974:                      aboutme    => 'Personal web page',
 8975:                      blog       => 'Blog',
 8976:                      webdav     => 'WebDAV',
 8977:                      portfolio  => 'Portfolio',
 8978:                      official   => 'Official courses (with institutional codes)',
 8979:                      unofficial => 'Unofficial courses',
 8980:                      community  => 'Communities',
 8981:                      textbook   => 'Textbook courses',
 8982:                      placement  => 'Placement tests',
 8983:                  );
 8984:     return %titles;
 8985: }
 8986: 
 8987: sub courserequest_titles {
 8988:     my %titles = &Apache::lonlocal::texthash (
 8989:                                    official   => 'Official',
 8990:                                    unofficial => 'Unofficial',
 8991:                                    community  => 'Communities',
 8992:                                    textbook   => 'Textbook',
 8993:                                    placement  => 'Placement tests',
 8994:                                    lti        => 'LTI Provider',
 8995:                                    norequest  => 'Not allowed',
 8996:                                    approval   => 'Approval by DC',
 8997:                                    validate   => 'With validation',
 8998:                                    autolimit  => 'Numerical limit',
 8999:                                    unlimited  => '(blank for unlimited)',
 9000:                  );
 9001:     return %titles;
 9002: }
 9003: 
 9004: sub authorrequest_titles {
 9005:     my %titles = &Apache::lonlocal::texthash (
 9006:                                    norequest  => 'Not allowed',
 9007:                                    approval   => 'Approval by Dom. Coord.',
 9008:                                    automatic  => 'Automatic approval',
 9009:                  );
 9010:     return %titles;
 9011: }
 9012: 
 9013: sub courserequest_conditions {
 9014:     my %conditions = &Apache::lonlocal::texthash (
 9015:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9016:        validate   => '(Processing of request subject to institutional validation).',
 9017:                  );
 9018:     return %conditions;
 9019: }
 9020: 
 9021: 
 9022: sub print_usercreation {
 9023:     my ($position,$dom,$settings,$rowtotal) = @_;
 9024:     my $numinrow = 4;
 9025:     my $datatable;
 9026:     if ($position eq 'top') {
 9027:         $$rowtotal ++;
 9028:         my $rowcount = 0;
 9029:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9030:         if (ref($rules) eq 'HASH') {
 9031:             if (keys(%{$rules}) > 0) {
 9032:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9033:                                                 $ruleorder,$numinrow,$rowcount);
 9034:                 $$rowtotal ++;
 9035:                 $rowcount ++;
 9036:             }
 9037:         }
 9038:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9039:         if (ref($idrules) eq 'HASH') {
 9040:             if (keys(%{$idrules}) > 0) {
 9041:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9042:                                                 $idruleorder,$numinrow,$rowcount);
 9043:                 $$rowtotal ++;
 9044:                 $rowcount ++;
 9045:             }
 9046:         }
 9047:         if ($rowcount == 0) {
 9048:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9049:             $$rowtotal ++;
 9050:             $rowcount ++;
 9051:         }
 9052:     } elsif ($position eq 'middle') {
 9053:         my @creators = ('author','course','requestcrs');
 9054:         my ($rules,$ruleorder) =
 9055:             &Apache::lonnet::inst_userrules($dom,'username');
 9056:         my %lt = &usercreation_types();
 9057:         my %checked;
 9058:         if (ref($settings) eq 'HASH') {
 9059:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9060:                 foreach my $item (@creators) {
 9061:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9062:                 }
 9063:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9064:                 foreach my $item (@creators) {
 9065:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9066:                         $checked{$item} = 'none';
 9067:                     }
 9068:                 }
 9069:             }
 9070:         }
 9071:         my $rownum = 0;
 9072:         foreach my $item (@creators) {
 9073:             $rownum ++;
 9074:             if ($checked{$item} eq '') {
 9075:                 $checked{$item} = 'any';
 9076:             }
 9077:             my $css_class;
 9078:             if ($rownum%2) {
 9079:                 $css_class = '';
 9080:             } else {
 9081:                 $css_class = ' class="LC_odd_row" ';
 9082:             }
 9083:             $datatable .= '<tr'.$css_class.'>'.
 9084:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9085:                          '</span></td><td style="text-align: right">';
 9086:             my @options = ('any');
 9087:             if (ref($rules) eq 'HASH') {
 9088:                 if (keys(%{$rules}) > 0) {
 9089:                     push(@options,('official','unofficial'));
 9090:                 }
 9091:             }
 9092:             push(@options,'none');
 9093:             foreach my $option (@options) {
 9094:                 my $type = 'radio';
 9095:                 my $check = ' ';
 9096:                 if ($checked{$item} eq $option) {
 9097:                     $check = ' checked="checked" ';
 9098:                 } 
 9099:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9100:                               '<input type="'.$type.'" name="can_createuser_'.
 9101:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9102:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9103:             }
 9104:             $datatable .= '</td></tr>';
 9105:         }
 9106:     } else {
 9107:         my @contexts = ('author','course','domain');
 9108:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9109:         my %checked;
 9110:         if (ref($settings) eq 'HASH') {
 9111:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9112:                 foreach my $item (@contexts) {
 9113:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9114:                         foreach my $auth (@authtypes) {
 9115:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9116:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9117:                             }
 9118:                         }
 9119:                     }
 9120:                 }
 9121:             }
 9122:         } else {
 9123:             foreach my $item (@contexts) {
 9124:                 foreach my $auth (@authtypes) {
 9125:                     $checked{$item}{$auth} = ' checked="checked" ';
 9126:                 }
 9127:             }
 9128:         }
 9129:         my %title = &context_names();
 9130:         my %authname = &authtype_names();
 9131:         my $rownum = 0;
 9132:         my $css_class; 
 9133:         foreach my $item (@contexts) {
 9134:             if ($rownum%2) {
 9135:                 $css_class = '';
 9136:             } else {
 9137:                 $css_class = ' class="LC_odd_row" ';
 9138:             }
 9139:             $datatable .=   '<tr'.$css_class.'>'.
 9140:                             '<td>'.$title{$item}.
 9141:                             '</td><td class="LC_left_item">'.
 9142:                             '<span class="LC_nobreak">';
 9143:             foreach my $auth (@authtypes) {
 9144:                 $datatable .= '<label>'. 
 9145:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9146:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9147:                               $authname{$auth}.'</label>&nbsp;';
 9148:             }
 9149:             $datatable .= '</span></td></tr>';
 9150:             $rownum ++;
 9151:         }
 9152:         $$rowtotal += $rownum;
 9153:     }
 9154:     return $datatable;
 9155: }
 9156: 
 9157: sub print_selfcreation {
 9158:     my ($position,$dom,$settings,$rowtotal) = @_;
 9159:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9160:         $emaildomain,$datatable);
 9161:     if (ref($settings) eq 'HASH') {
 9162:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9163:             $createsettings = $settings->{'cancreate'};
 9164:             if (ref($createsettings) eq 'HASH') {
 9165:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9166:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9167:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9168:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9169:                         @selfcreate = ('email','login','sso');
 9170:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9171:                         @selfcreate = ($createsettings->{'selfcreate'});
 9172:                     }
 9173:                 }
 9174:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9175:                     $processing = $createsettings->{'selfcreateprocessing'};
 9176:                 }
 9177:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9178:                     $emailoptions = $createsettings->{'emailoptions'};
 9179:                 }
 9180:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9181:                     $emailverified = $createsettings->{'emailverified'};
 9182:                 }
 9183:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9184:                     $emaildomain = $createsettings->{'emaildomain'};
 9185:                 }
 9186:             }
 9187:         }
 9188:     }
 9189:     my %radiohash;
 9190:     my $numinrow = 4;
 9191:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9192:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9193:     if ($position eq 'top') {
 9194:         my %choices = &Apache::lonlocal::texthash (
 9195:                                                       cancreate_login      => 'Institutional Login',
 9196:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9197:                                                   );
 9198:         my @toggles = sort(keys(%choices));
 9199:         my %defaultchecked = (
 9200:                                'cancreate_login' => 'off',
 9201:                                'cancreate_sso'   => 'off',
 9202:                              );
 9203:         my ($onclick,$itemcount);
 9204:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9205:                                                      \%choices,$itemcount,$onclick);
 9206:         $$rowtotal += $itemcount;
 9207:         
 9208:         if (ref($usertypes) eq 'HASH') {
 9209:             if (keys(%{$usertypes}) > 0) {
 9210:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9211:                                              $dom,$numinrow,$othertitle,
 9212:                                              'statustocreate',$rowtotal);
 9213:                 $$rowtotal ++;
 9214:             }
 9215:         }
 9216:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9217:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9218:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9219:         my $rem;
 9220:         my $numperrow = 2;
 9221:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9222:         $datatable .= '<tr'.$css_class.'>'.
 9223:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9224:                      '<td class="LC_left_item">'."\n".
 9225:                      '<table>'."\n";
 9226:         for (my $i=0; $i<@fields; $i++) {
 9227:             $rem = $i%($numperrow);
 9228:             if ($rem == 0) {
 9229:                 if ($i > 0) {
 9230:                     $datatable .= '</tr>';
 9231:                 }
 9232:                 $datatable .= '<tr>';
 9233:             }
 9234:             my $currval;
 9235:             if (ref($createsettings) eq 'HASH') {
 9236:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9237:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9238:                 }
 9239:             }
 9240:             $datatable .= '<td class="LC_left_item">'.
 9241:                           '<span class="LC_nobreak">'.
 9242:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9243:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9244:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9245:         }
 9246:         my $colsleft = $numperrow - $rem;
 9247:         if ($colsleft > 1 ) {
 9248:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9249:                          '&nbsp;</td>';
 9250:         } elsif ($colsleft == 1) {
 9251:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9252:         }
 9253:         $datatable .= '</tr></table></td></tr>';
 9254:         $$rowtotal ++;
 9255:     } elsif ($position eq 'middle') {
 9256:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9257:         my @posstypes;
 9258:         if (ref($types) eq 'ARRAY') {
 9259:             @posstypes = @{$types};
 9260:         }
 9261:         unless (grep(/^default$/,@posstypes)) {
 9262:             push(@posstypes,'default');
 9263:         }
 9264:         my %usertypeshash;
 9265:         if (ref($usertypes) eq 'HASH') {
 9266:             %usertypeshash = %{$usertypes};
 9267:         }
 9268:         $usertypeshash{'default'} = $othertitle;
 9269:         foreach my $status (@posstypes) {
 9270:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9271:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9272:             $$rowtotal ++;
 9273:         }
 9274:     } else {
 9275:         my %choices = &Apache::lonlocal::texthash (
 9276:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9277:                                                   );
 9278:         my @toggles = sort(keys(%choices));
 9279:         my %defaultchecked = (
 9280:                                'cancreate_email' => 'off',
 9281:                              );
 9282:         my $customclass = 'LC_selfcreate_email';
 9283:         my $classprefix = 'LC_canmodify_emailusername_';
 9284:         my $optionsprefix = 'LC_options_emailusername_';
 9285:         my $display = 'none';
 9286:         my $rowstyle = 'display:none';
 9287:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9288:             $display = 'block';
 9289:             $rowstyle = 'display:table-row';
 9290:         }
 9291:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9292:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9293:                                                      \%choices,$$rowtotal,$onclick);
 9294:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9295:                                          $rowstyle);
 9296:         $$rowtotal ++;
 9297:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9298:                                       $rowstyle);
 9299:         $$rowtotal ++;
 9300:         my (@ordered,@posstypes,%usertypeshash);
 9301:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9302:         my ($emailrules,$emailruleorder) =
 9303:             &Apache::lonnet::inst_userrules($dom,'email');
 9304:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9305:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9306:         if (ref($types) eq 'ARRAY') {
 9307:             @posstypes = @{$types};
 9308:         }
 9309:         if (@posstypes) {
 9310:             unless (grep(/^default$/,@posstypes)) {
 9311:                 push(@posstypes,'default');
 9312:             }
 9313:             if (ref($usertypes) eq 'HASH') {
 9314:                 %usertypeshash = %{$usertypes};
 9315:             }
 9316:             my $currassign;
 9317:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9318:                 $currassign = {
 9319:                                   selfassign => $domdefaults{'inststatusguest'},
 9320:                               };
 9321:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9322:             } else {
 9323:                 $currassign = { selfassign => [] };
 9324:             }
 9325:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9326:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9327:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9328:                                          $numinrow,$othertitle,'selfassign',
 9329:                                          $rowtotal,$onclicktypes,$customclass,
 9330:                                          $rowstyle);
 9331:             $$rowtotal ++;
 9332:             $usertypeshash{'default'} = $othertitle;
 9333:             foreach my $status (@posstypes) {
 9334:                 my $css_class;
 9335:                 if ($$rowtotal%2) {
 9336:                     $css_class = 'LC_odd_row ';
 9337:                 }
 9338:                 $css_class .= $customclass;
 9339:                 my $rowid = $optionsprefix.$status;
 9340:                 my $hidden = 1;
 9341:                 my $currstyle = 'display:none';
 9342:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9343:                     $currstyle = $rowstyle;
 9344:                     $hidden = 0; 
 9345:                 }
 9346:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9347:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9348:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9349:                 unless ($hidden) {
 9350:                     $$rowtotal ++;
 9351:                 }
 9352:             }
 9353:         } else {
 9354:             my $css_class;
 9355:             if ($$rowtotal%2) {
 9356:                 $css_class = 'LC_odd_row ';
 9357:             }
 9358:             $css_class .= $customclass;
 9359:             $usertypeshash{'default'} = $othertitle;
 9360:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9361:                                          $emailrules,$emailruleorder,$settings,'default','',
 9362:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9363:             $$rowtotal ++;
 9364:         }
 9365:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9366:         $numinrow = 1;
 9367:         if (@posstypes) {
 9368:             foreach my $status (@posstypes) {
 9369:                 my $rowid = $classprefix.$status;
 9370:                 my $datarowstyle = 'display:none';
 9371:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 9372:                     $datarowstyle = $rowstyle; 
 9373:                 }
 9374:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9375:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9376:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9377:                 unless ($datarowstyle eq 'display:none') {
 9378:                     $$rowtotal ++;
 9379:                 }
 9380:             }
 9381:         } else {
 9382:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9383:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9384:                                                    $infotitles,'',$customclass,$rowstyle);
 9385:         }
 9386:     }
 9387:     return $datatable;
 9388: }
 9389: 
 9390: sub selfcreate_javascript {
 9391:     return <<"ENDSCRIPT";
 9392: 
 9393: <script type="text/javascript">
 9394: // <![CDATA[
 9395: 
 9396: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9397:     var x = document.getElementsByClassName(target);
 9398:     var insttypes = 0;
 9399:     var insttypeRegExp = new RegExp(prefix);
 9400:     if ((x.length != undefined) && (x.length > 0)) {
 9401:         if (form.elements[radio].length != undefined) {
 9402:             for (var i=0; i<form.elements[radio].length; i++) {
 9403:                 if (form.elements[radio][i].checked) {
 9404:                     if (form.elements[radio][i].value == 1) {
 9405:                         for (var j=0; j<x.length; j++) {
 9406:                             if (x[j].id == 'undefined') {
 9407:                                 x[j].style.display = 'table-row';
 9408:                             } else if (insttypeRegExp.test(x[j].id)) {
 9409:                                 insttypes ++;
 9410:                             } else {
 9411:                                 x[j].style.display = 'table-row';
 9412:                             }
 9413:                         }
 9414:                     } else {
 9415:                         for (var j=0; j<x.length; j++) {
 9416:                             x[j].style.display = 'none';
 9417:                         }
 9418:                     }
 9419:                     break;
 9420:                 }
 9421:             }
 9422:             if (insttypes > 0) {
 9423:                 toggleDataRow(form,checkbox,target,altprefix);
 9424:                 toggleDataRow(form,checkbox,target,prefix,1);
 9425:             }
 9426:         }
 9427:     }
 9428:     return;
 9429: }
 9430: 
 9431: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9432:     if (form.elements[checkbox].length != undefined) {
 9433:         var count = 0;
 9434:         if (docount) {
 9435:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9436:                 if (form.elements[checkbox][i].checked) {
 9437:                     count ++;
 9438:                 }
 9439:             }
 9440:         }
 9441:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9442:             var type = form.elements[checkbox][i].value;
 9443:             if (document.getElementById(prefix+type)) {
 9444:                 if (form.elements[checkbox][i].checked) {
 9445:                     document.getElementById(prefix+type).style.display = 'table-row';
 9446:                     if (count % 2 == 1) {
 9447:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9448:                     } else {
 9449:                         document.getElementById(prefix+type).className = target;
 9450:                     }
 9451:                     count ++;
 9452:                 } else {
 9453:                     document.getElementById(prefix+type).style.display = 'none';
 9454:                 }
 9455:             }
 9456:         }
 9457:     }
 9458:     return;
 9459: }
 9460: 
 9461: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9462:     var caller = radio+'_'+status;
 9463:     if (form.elements[caller].length != undefined) {
 9464:         for (var i=0; i<form.elements[caller].length; i++) {
 9465:             if (form.elements[caller][i].checked) {
 9466:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9467:                     var curr = form.elements[caller][i].value;
 9468:                     if (prefix) {
 9469:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9470:                     }
 9471:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9472:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9473:                     if (curr == 'custom') {
 9474:                         if (prefix) { 
 9475:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9476:                         }
 9477:                     } else if (curr == 'inst') {
 9478:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9479:                     } else if (curr == 'noninst') {
 9480:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9481:                     }
 9482:                     break;
 9483:                 }
 9484:             }
 9485:         }
 9486:     }
 9487: }
 9488: 
 9489: // ]]>
 9490: </script>
 9491: 
 9492: ENDSCRIPT
 9493: }
 9494: 
 9495: sub noninst_users {
 9496:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9497:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 9498:     my $class = 'LC_left_item';
 9499:     if ($css_class) {
 9500:         $css_class = ' class="'.$css_class.'"'; 
 9501:     }
 9502:     if ($rowid) {
 9503:         $rowid = ' id="'.$rowid.'"';
 9504:     }
 9505:     if ($rowstyle) {
 9506:         $rowstyle = ' style="'.$rowstyle.'"';
 9507:     }
 9508:     my ($output,$description);
 9509:     if ($type eq 'default') {
 9510:         $description = &mt('Requests for: [_1]',$typetitle);
 9511:     } else {
 9512:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9513:     }
 9514:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9515:               "<td>$description</td>\n".           
 9516:               '<td class="'.$class.'" colspan="2">'.
 9517:               '<table><tr>';
 9518:     my %headers = &Apache::lonlocal::texthash( 
 9519:               approve  => 'Processing',
 9520:               email    => 'E-mail',
 9521:               username => 'Username',
 9522:     );
 9523:     foreach my $item ('approve','email','username') {
 9524:         $output .= '<th>'.$headers{$item}.'</th>';
 9525:     }
 9526:     $output .= '</tr><tr>';
 9527:     foreach my $item ('approve','email','username') {
 9528:         $output .= '<td style="vertical-align: top">';
 9529:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9530:         if ($item eq 'approve') {
 9531:             %choices = &Apache::lonlocal::texthash (
 9532:                                                      automatic => 'Automatically approved',
 9533:                                                      approval  => 'Queued for approval',
 9534:                                                    );
 9535:             @options = ('automatic','approval');
 9536:             $hashref = $processing;
 9537:             $defoption = 'automatic';
 9538:             $name = 'cancreate_emailprocess_'.$type;
 9539:         } elsif ($item eq 'email') {
 9540:             %choices = &Apache::lonlocal::texthash (
 9541:                                                      any     => 'Any e-mail',
 9542:                                                      inst    => 'Institutional only',
 9543:                                                      noninst => 'Non-institutional only',
 9544:                                                      custom  => 'Custom restrictions',
 9545:                                                    );
 9546:             @options = ('any','inst','noninst');
 9547:             my $showcustom;
 9548:             if (ref($emailrules) eq 'HASH') {
 9549:                 if (keys(%{$emailrules}) > 0) {
 9550:                     push(@options,'custom');
 9551:                     $showcustom = 'cancreate_emailrule';
 9552:                     if (ref($settings) eq 'HASH') {
 9553:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9554:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9555:                                 if (exists($emailrules->{$rule})) {
 9556:                                     $hascustom ++;
 9557:                                 }
 9558:                             }
 9559:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9560:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9561:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9562:                                     if (exists($emailrules->{$rule})) {
 9563:                                         $hascustom ++;
 9564:                                     }
 9565:                                 }
 9566:                             }
 9567:                         }
 9568:                     }
 9569:                 }
 9570:             }
 9571:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9572:                                                      "'cancreate_emaildomain','$type'".');"';
 9573:             $hashref = $emailoptions;
 9574:             $defoption = 'any';
 9575:             $name = 'cancreate_emailoptions_'.$type;
 9576:         } elsif ($item eq 'username') {
 9577:             %choices = &Apache::lonlocal::texthash (
 9578:                                                      all    => 'Same as e-mail',
 9579:                                                      first  => 'Omit @domain',
 9580:                                                      free   => 'Free to choose',
 9581:                                                    );
 9582:             @options = ('all','first','free');
 9583:             $hashref = $emailverified;
 9584:             $defoption = 'all';
 9585:             $name = 'cancreate_usernameoptions_'.$type;
 9586:         }
 9587:         foreach my $option (@options) {
 9588:             my $checked;
 9589:             if (ref($hashref) eq 'HASH') {
 9590:                 if ($type eq '') {
 9591:                     if (!exists($hashref->{'default'})) {
 9592:                         if ($option eq $defoption) {
 9593:                             $checked = ' checked="checked"';
 9594:                         }
 9595:                     } else {
 9596:                         if ($hashref->{'default'} eq $option) {
 9597:                             $checked = ' checked="checked"';
 9598:                         }
 9599:                     }
 9600:                 } else {
 9601:                     if (!exists($hashref->{$type})) {
 9602:                         if ($option eq $defoption) {
 9603:                             $checked = ' checked="checked"';
 9604:                         }
 9605:                     } else {
 9606:                         if ($hashref->{$type} eq $option) {
 9607:                             $checked = ' checked="checked"';
 9608:                         }
 9609:                     }
 9610:                 }
 9611:             } elsif (($item eq 'email') && ($hascustom)) {
 9612:                 if ($option eq 'custom') {
 9613:                     $checked = ' checked="checked"';
 9614:                 }
 9615:             } elsif ($option eq $defoption) {
 9616:                 $checked = ' checked="checked"';
 9617:             }
 9618:             $output .= '<span class="LC_nobreak"><label>'.
 9619:                        '<input type="radio" name="'.$name.'"'.
 9620:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9621:                        $choices{$option}.'</label></span><br />';
 9622:             if ($item eq 'email') {
 9623:                 if ($option eq 'custom') {
 9624:                     my $id = 'cancreate_emailrule_'.$type;
 9625:                     my $display = 'none';
 9626:                     if ($checked) {
 9627:                         $display = 'inline';
 9628:                     }
 9629:                     my $numinrow = 2;
 9630:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9631:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9632:                                &user_formats_row('email',$settings,$emailrules,
 9633:                                                  $emailruleorder,$numinrow,'',$type);
 9634:                               '</table></fieldset>';
 9635:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9636:                     my %text = &Apache::lonlocal::texthash (
 9637:                                                              inst    => 'must end:',
 9638:                                                              noninst => 'cannot end:',
 9639:                                                            );
 9640:                     my $value;
 9641:                     if (ref($emaildomain) eq 'HASH') {
 9642:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9643:                             $value = $emaildomain->{$type}->{$option}; 
 9644:                         }
 9645:                     }
 9646:                     if ($value eq '') {
 9647:                         $value = '@'.$intdom;
 9648:                     }
 9649:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9650:                     my $display = 'none';
 9651:                     if ($checked) {
 9652:                         $display = 'inline';
 9653:                     }
 9654:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9655:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9656:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9657:                                '</div>';
 9658:                 }
 9659:             }
 9660:         }
 9661:         $output .= '</td>'."\n";
 9662:     }
 9663:     $output .= "</tr></table></td></tr>\n";
 9664:     return $output;
 9665: }
 9666: 
 9667: sub captcha_choice {
 9668:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9669:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9670:         $vertext,$currver);
 9671:     my %lt = &captcha_phrases();
 9672:     $keyentry = 'hidden';
 9673:     my $colspan=2;
 9674:     if ($context eq 'cancreate') {
 9675:         $rowname = &mt('CAPTCHA validation');
 9676:     } elsif ($context eq 'login') {
 9677:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9678:     } elsif ($context eq 'passwords') {
 9679:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9680:         $colspan=1;
 9681:     }
 9682:     if (ref($settings) eq 'HASH') {
 9683:         if ($settings->{'captcha'}) {
 9684:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9685:         } else {
 9686:             $checked{'original'} = ' checked="checked"';
 9687:         }
 9688:         if ($settings->{'captcha'} eq 'recaptcha') {
 9689:             $pubtext = $lt{'pub'};
 9690:             $privtext = $lt{'priv'};
 9691:             $keyentry = 'text';
 9692:             $vertext = $lt{'ver'};
 9693:             $currver = $settings->{'recaptchaversion'};
 9694:             if ($currver ne '2') {
 9695:                 $currver = 1;
 9696:             }
 9697:         }
 9698:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9699:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9700:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9701:         }
 9702:     } else {
 9703:         $checked{'original'} = ' checked="checked"';
 9704:     }
 9705:     my $css_class;
 9706:     if ($itemcount%2) {
 9707:         $css_class = 'LC_odd_row';
 9708:     }
 9709:     if ($customcss) {
 9710:         $css_class .= " $customcss";
 9711:     }
 9712:     $css_class =~ s/^\s+//;
 9713:     if ($css_class) {
 9714:         $css_class = ' class="'.$css_class.'"';
 9715:     }
 9716:     if ($rowstyle) {
 9717:         $css_class .= ' style="'.$rowstyle.'"';
 9718:     }
 9719:     my $output = '<tr'.$css_class.'>'.
 9720:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9721:                  '<table><tr><td>'."\n";
 9722:     foreach my $option ('original','recaptcha','notused') {
 9723:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9724:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9725:                    $lt{$option}.'</label></span>';
 9726:         unless ($option eq 'notused') {
 9727:             $output .= ('&nbsp;'x2)."\n";
 9728:         }
 9729:     }
 9730: #
 9731: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9732: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9733: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9734: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9735: #
 9736:     $output .= '</td></tr>'."\n".
 9737:                '<tr><td class="LC_zero_height">'."\n".
 9738:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9739:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9740:                $currpub.'" size="40" /></span><br />'."\n".
 9741:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9742:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9743:                $currpriv.'" size="40" /></span><br />'.
 9744:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9745:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9746:                $currver.'" size="3" /></span><br />'.
 9747:                '</td></tr></table>'."\n".
 9748:                '</td></tr>';
 9749:     return $output;
 9750: }
 9751: 
 9752: sub user_formats_row {
 9753:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9754:     my $output;
 9755:     my %text = (
 9756:                    'username' => 'new usernames',
 9757:                    'id'       => 'IDs',
 9758:                );
 9759:     unless ($type eq 'email') {
 9760:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9761:         $output = '<tr '.$css_class.'>'.
 9762:                   '<td><span class="LC_nobreak">'.
 9763:                   &mt("Format rules to check for $text{$type}: ").
 9764:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9765:     }
 9766:     my $rem;
 9767:     if (ref($ruleorder) eq 'ARRAY') {
 9768:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9769:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9770:                 my $rem = $i%($numinrow);
 9771:                 if ($rem == 0) {
 9772:                     if ($i > 0) {
 9773:                         $output .= '</tr>';
 9774:                     }
 9775:                     $output .= '<tr>';
 9776:                 }
 9777:                 my $check = ' ';
 9778:                 if (ref($settings) eq 'HASH') {
 9779:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9780:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9781:                             $check = ' checked="checked" ';
 9782:                         }
 9783:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9784:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9785:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9786:                                 $check = ' checked="checked" ';
 9787:                             }
 9788:                         }
 9789:                     }
 9790:                 }
 9791:                 my $name = $type.'_rule';
 9792:                 if ($type eq 'email') {
 9793:                     $name .= '_'.$status;
 9794:                 }
 9795:                 $output .= '<td class="LC_left_item">'.
 9796:                            '<span class="LC_nobreak"><label>'.
 9797:                            '<input type="checkbox" name="'.$name.'" '.
 9798:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9799:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9800:             }
 9801:         }
 9802:         $rem = @{$ruleorder}%($numinrow);
 9803:     }
 9804:     my $colsleft;
 9805:     if ($rem) {
 9806:         $colsleft = $numinrow - $rem;
 9807:     }
 9808:     if ($colsleft > 1 ) {
 9809:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9810:                    '&nbsp;</td>';
 9811:     } elsif ($colsleft == 1) {
 9812:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9813:     }
 9814:     $output .= '</tr></table>';
 9815:     unless ($type eq 'email') {
 9816:         $output .= '</td></tr>';
 9817:     }
 9818:     return $output;
 9819: }
 9820: 
 9821: sub usercreation_types {
 9822:     my %lt = &Apache::lonlocal::texthash (
 9823:                     author     => 'When adding a co-author',
 9824:                     course     => 'When adding a user to a course',
 9825:                     requestcrs => 'When requesting a course',
 9826:                     any        => 'Any',
 9827:                     official   => 'Institutional only ',
 9828:                     unofficial => 'Non-institutional only',
 9829:                     none       => 'None',
 9830:     );
 9831:     return %lt;
 9832: }
 9833: 
 9834: sub selfcreation_types {
 9835:     my %lt = &Apache::lonlocal::texthash (
 9836:                     selfcreate => 'User creates own account',
 9837:                     any        => 'Any',
 9838:                     official   => 'Institutional only ',
 9839:                     unofficial => 'Non-institutional only',
 9840:                     email      => 'E-mail address',
 9841:                     login      => 'Institutional Login',
 9842:                     sso        => 'SSO',
 9843:              );
 9844: }
 9845: 
 9846: sub authtype_names {
 9847:     my %lt = &Apache::lonlocal::texthash(
 9848:                       int    => 'Internal',
 9849:                       krb4   => 'Kerberos 4',
 9850:                       krb5   => 'Kerberos 5',
 9851:                       loc    => 'Local',
 9852:                       lti    => 'LTI',
 9853:                   );
 9854:     return %lt;
 9855: }
 9856: 
 9857: sub context_names {
 9858:     my %context_title = &Apache::lonlocal::texthash(
 9859:        author => 'Creating users when an Author',
 9860:        course => 'Creating users when in a course',
 9861:        domain => 'Creating users when a Domain Coordinator',
 9862:     );
 9863:     return %context_title;
 9864: }
 9865: 
 9866: sub print_usermodification {
 9867:     my ($position,$dom,$settings,$rowtotal) = @_;
 9868:     my $numinrow = 4;
 9869:     my ($context,$datatable,$rowcount);
 9870:     if ($position eq 'top') {
 9871:         $rowcount = 0;
 9872:         $context = 'author'; 
 9873:         foreach my $role ('ca','aa') {
 9874:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9875:                                                    $numinrow,$rowcount);
 9876:             $$rowtotal ++;
 9877:             $rowcount ++;
 9878:         }
 9879:     } elsif ($position eq 'bottom') {
 9880:         $context = 'course';
 9881:         $rowcount = 0;
 9882:         foreach my $role ('st','ep','ta','in','cr') {
 9883:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9884:                                                    $numinrow,$rowcount);
 9885:             $$rowtotal ++;
 9886:             $rowcount ++;
 9887:         }
 9888:     }
 9889:     return $datatable;
 9890: }
 9891: 
 9892: sub print_defaults {
 9893:     my ($position,$dom,$settings,$rowtotal) = @_;
 9894:     my $rownum = 0;
 9895:     my ($datatable,$css_class,$titles);
 9896:     unless ($position eq 'bottom') {
 9897:         $titles = &defaults_titles($dom);
 9898:     }
 9899:     if ($position eq 'top') {
 9900:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9901:                      'datelocale_def','portal_def');
 9902:         my %defaults;
 9903:         if (ref($settings) eq 'HASH') {
 9904:             %defaults = %{$settings};
 9905:         } else {
 9906:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9907:             foreach my $item (@items) {
 9908:                 $defaults{$item} = $domdefaults{$item};
 9909:             }
 9910:         }
 9911:         foreach my $item (@items) {
 9912:             if ($rownum%2) {
 9913:                 $css_class = '';
 9914:             } else {
 9915:                 $css_class = ' class="LC_odd_row" ';
 9916:             }
 9917:             $datatable .= '<tr'.$css_class.'>'.
 9918:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9919:                           '</span></td><td class="LC_right_item" colspan="3">';
 9920:             if ($item eq 'auth_def') {
 9921:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9922:                 my %shortauth = (
 9923:                                  internal => 'int',
 9924:                                  krb4 => 'krb4',
 9925:                                  krb5 => 'krb5',
 9926:                                  localauth  => 'loc',
 9927:                                  lti => 'lti',
 9928:                                 );
 9929:                 my %authnames = &authtype_names();
 9930:                 foreach my $auth (@authtypes) {
 9931:                     my $checked = ' ';
 9932:                     if ($defaults{$item} eq $auth) {
 9933:                         $checked = ' checked="checked" ';
 9934:                     }
 9935:                     $datatable .= '<label><input type="radio" name="'.$item.
 9936:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9937:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9938:                 }
 9939:             } elsif ($item eq 'timezone_def') {
 9940:                 my $includeempty = 1;
 9941:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9942:             } elsif ($item eq 'datelocale_def') {
 9943:                 my $includeempty = 1;
 9944:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9945:             } elsif ($item eq 'lang_def') {
 9946:                 my $includeempty = 1;
 9947:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9948:             } else {
 9949:                 my $size;
 9950:                 if ($item eq 'portal_def') {
 9951:                     $size = ' size="25"';
 9952:                 }
 9953:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9954:                               $defaults{$item}.'"'.$size.' />';
 9955:             }
 9956:             $datatable .= '</td></tr>';
 9957:             $rownum ++;
 9958:         }
 9959:     } else {
 9960:         my %defaults;
 9961:         if (ref($settings) eq 'HASH') {
 9962:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9963:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9964:                 for (my $i=0; $i<$maxnum; $i++) {
 9965:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9966:                     my $item = $settings->{'inststatusorder'}->[$i];
 9967:                     my $title = $settings->{'inststatustypes'}->{$item};
 9968:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9969:                     $datatable .= '<tr'.$css_class.'>'.
 9970:                                   '<td><span class="LC_nobreak">'.
 9971:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9972:                     for (my $k=0; $k<=$maxnum; $k++) {
 9973:                         my $vpos = $k+1;
 9974:                         my $selstr;
 9975:                         if ($k == $i) {
 9976:                             $selstr = ' selected="selected" ';
 9977:                         }
 9978:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9979:                     }
 9980:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9981:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9982:                                   &mt('delete').'</span></td>'.
 9983:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 9984:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9985:                                   '</span></td></tr>';
 9986:                 }
 9987:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9988:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9989:                 $datatable .= '<tr '.$css_class.'>'.
 9990:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9991:                 for (my $k=0; $k<=$maxnum; $k++) {
 9992:                     my $vpos = $k+1;
 9993:                     my $selstr;
 9994:                     if ($k == $maxnum) {
 9995:                         $selstr = ' selected="selected" ';
 9996:                     }
 9997:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9998:                 }
 9999:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10000:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10001:                               '&nbsp;'.&mt('(new)').
10002:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10003:                               &mt('Name displayed').':'.
10004:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10005:                               '</tr>'."\n";
10006:                 $rownum ++;
10007:             }
10008:         }
10009:     }
10010:     $$rowtotal += $rownum;
10011:     return $datatable;
10012: }
10013: 
10014: sub get_languages_hash {
10015:     my %langchoices;
10016:     foreach my $id (&Apache::loncommon::languageids()) {
10017:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10018:         if ($code ne '') {
10019:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10020:         }
10021:     }
10022:     return %langchoices;
10023: }
10024: 
10025: sub defaults_titles {
10026:     my ($dom) = @_;
10027:     my %titles = &Apache::lonlocal::texthash (
10028:                    'auth_def'      => 'Default authentication type',
10029:                    'auth_arg_def'  => 'Default authentication argument',
10030:                    'lang_def'      => 'Default language',
10031:                    'timezone_def'  => 'Default timezone',
10032:                    'datelocale_def' => 'Default locale for dates',
10033:                    'portal_def'     => 'Portal/Default URL',
10034:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10035:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10036:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10037:                  );
10038:     if ($dom) {
10039:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10040:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10041:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10042:         $protocol = 'http' if ($protocol ne 'https');
10043:         if ($uint_dom) {
10044:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10045:                                          $uint_dom);
10046:         }
10047:     }
10048:     return (\%titles);
10049: }
10050: 
10051: sub print_scantron {
10052:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10053:     if ($position eq 'top') {
10054:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10055:     } else {
10056:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10057:     }
10058: }
10059: 
10060: sub scantron_javascript {
10061:     return <<"ENDSCRIPT";
10062: 
10063: <script type="text/javascript">
10064: // <![CDATA[
10065: 
10066: function toggleScantron(form) {
10067:     var csvfieldset = new Array();
10068:     if (document.getElementById('scantroncsv_cols')) {
10069:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10070:     }
10071:     if (document.getElementById('scantroncsv_options')) {
10072:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10073:     }
10074:     if (csvfieldset.length) {
10075:         if (document.getElementById('scantronconfcsv')) {
10076:             var scantroncsv = document.getElementById('scantronconfcsv');
10077:             if (scantroncsv.checked) {
10078:                 for (var i=0; i<csvfieldset.length; i++) {
10079:                     csvfieldset[i].style.display = 'block';
10080:                 }
10081:             } else {
10082:                 for (var i=0; i<csvfieldset.length; i++) {
10083:                     csvfieldset[i].style.display = 'none';
10084:                 }
10085:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10086:                 if (csvselects.length) {
10087:                     for (var j=0; j<csvselects.length; j++) {
10088:                         csvselects[j].selectedIndex = 0;
10089:                     }
10090:                 }
10091:             }
10092:         }
10093:     }
10094:     return;
10095: }
10096: // ]]>
10097: </script>
10098: 
10099: ENDSCRIPT
10100: 
10101: }
10102: 
10103: sub print_scantronformat {
10104:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10105:     my $itemcount = 1;
10106:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10107:         %confhash);
10108:     my $switchserver = &check_switchserver($dom,$confname);
10109:     my %lt = &Apache::lonlocal::texthash (
10110:                 default => 'Default bubblesheet format file error',
10111:                 custom  => 'Custom bubblesheet format file error',
10112:              );
10113:     my %scantronfiles = (
10114:         default => 'default.tab',
10115:         custom => 'custom.tab',
10116:     );
10117:     foreach my $key (keys(%scantronfiles)) {
10118:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10119:                               .$scantronfiles{$key};
10120:     }
10121:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10122:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10123:         if (!$switchserver) {
10124:             my $servadm = $r->dir_config('lonAdmEMail');
10125:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10126:             if ($configuserok eq 'ok') {
10127:                 if ($author_ok eq 'ok') {
10128:                     my %legacyfile = (
10129:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10130:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10131:                     );
10132:                     my %md5chk;
10133:                     foreach my $type (keys(%legacyfile)) {
10134:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10135:                         chomp($md5chk{$type});
10136:                     }
10137:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10138:                         foreach my $type (keys(%legacyfile)) {
10139:                             ($scantronurls{$type},my $error) =
10140:                                 &legacy_scantronformat($r,$dom,$confname,
10141:                                                  $type,$legacyfile{$type},
10142:                                                  $scantronurls{$type},
10143:                                                  $scantronfiles{$type});
10144:                             if ($error ne '') {
10145:                                 $error{$type} = $error;
10146:                             }
10147:                         }
10148:                         if (keys(%error) == 0) {
10149:                             $is_custom = 1;
10150:                             $confhash{'scantron'}{'scantronformat'} =
10151:                                 $scantronurls{'custom'};
10152:                             my $putresult =
10153:                                 &Apache::lonnet::put_dom('configuration',
10154:                                                          \%confhash,$dom);
10155:                             if ($putresult ne 'ok') {
10156:                                 $error{'custom'} =
10157:                                     '<span class="LC_error">'.
10158:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10159:                             }
10160:                         }
10161:                     } else {
10162:                         ($scantronurls{'default'},my $error) =
10163:                             &legacy_scantronformat($r,$dom,$confname,
10164:                                           'default',$legacyfile{'default'},
10165:                                           $scantronurls{'default'},
10166:                                           $scantronfiles{'default'});
10167:                         if ($error eq '') {
10168:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10169:                             my $putresult =
10170:                                 &Apache::lonnet::put_dom('configuration',
10171:                                                          \%confhash,$dom);
10172:                             if ($putresult ne 'ok') {
10173:                                 $error{'default'} =
10174:                                     '<span class="LC_error">'.
10175:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10176:                             }
10177:                         } else {
10178:                             $error{'default'} = $error;
10179:                         }
10180:                     }
10181:                 }
10182:             }
10183:         } else {
10184:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10185:         }
10186:     }
10187:     if (ref($settings) eq 'HASH') {
10188:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10189:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10190:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10191:                 $scantronurl = '';
10192:             } else {
10193:                 $scantronurl = $settings->{'scantronformat'};
10194:             }
10195:             $is_custom = 1;
10196:         } else {
10197:             $scantronurl = $scantronurls{'default'};
10198:         }
10199:     } else {
10200:         if ($is_custom) {
10201:             $scantronurl = $scantronurls{'custom'};
10202:         } else {
10203:             $scantronurl = $scantronurls{'default'};
10204:         }
10205:     }
10206:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10207:     $datatable .= '<tr'.$css_class.'>';
10208:     if (!$is_custom) {
10209:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10210:                       '<span class="LC_nobreak">';
10211:         if ($scantronurl) {
10212:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10213:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10214:         } else {
10215:             $datatable = &mt('File unavailable for display');
10216:         }
10217:         $datatable .= '</span></td>';
10218:         if (keys(%error) == 0) { 
10219:             $datatable .= '<td style="vertical-align: bottom">';
10220:             if (!$switchserver) {
10221:                 $datatable .= &mt('Upload:').'<br />';
10222:             }
10223:         } else {
10224:             my $errorstr;
10225:             foreach my $key (sort(keys(%error))) {
10226:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10227:             }
10228:             $datatable .= '<td>'.$errorstr;
10229:         }
10230:     } else {
10231:         if (keys(%error) > 0) {
10232:             my $errorstr;
10233:             foreach my $key (sort(keys(%error))) {
10234:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10235:             } 
10236:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10237:         } elsif ($scantronurl) {
10238:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10239:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10240:             $datatable .= '<td><span class="LC_nobreak">'.
10241:                           $link.
10242:                           '<label><input type="checkbox" name="scantronformat_del"'.
10243:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10244:                           '<td><span class="LC_nobreak">&nbsp;'.
10245:                           &mt('Replace:').'</span><br />';
10246:         }
10247:     }
10248:     if (keys(%error) == 0) {
10249:         if ($switchserver) {
10250:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10251:         } else {
10252:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10253:                          '<input type="file" name="scantronformat" /></span>';
10254:         }
10255:     }
10256:     $datatable .= '</td></tr>';
10257:     $$rowtotal ++;
10258:     return $datatable;
10259: }
10260: 
10261: sub legacy_scantronformat {
10262:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10263:     my ($url,$error);
10264:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10265:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10266:         (my $result,$url) =
10267:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
10268:                          '','',$newfile);
10269:         if ($result ne 'ok') {
10270:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10271:         }
10272:     }
10273:     return ($url,$error);
10274: }
10275: 
10276: sub print_scantronconfig {
10277:     my ($dom,$settings,$rowtotal) = @_;
10278:     my $itemcount = 2;
10279:     my $is_checked = ' checked="checked"';
10280:     my %optionson = (
10281:                      hdr => ' checked="checked"',
10282:                      pad => ' checked="checked"',
10283:                      rem => ' checked="checked"',
10284:                     );
10285:     my %optionsoff = (
10286:                       hdr => '',
10287:                       pad => '',
10288:                       rem => '',
10289:                      );
10290:     my $currcsvsty = 'none';
10291:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10292:     my @fields = &scantroncsv_fields();
10293:     my %titles = &scantronconfig_titles();
10294:     if (ref($settings) eq 'HASH') {
10295:         if (ref($settings->{config}) eq 'HASH') {
10296:             if ($settings->{config}->{dat}) {
10297:                 $checked{'dat'} = $is_checked;
10298:             }
10299:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10300:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10301:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10302:                     if (keys(%csvfields) > 0) {
10303:                         $checked{'csv'} = $is_checked;
10304:                         $currcsvsty = 'block';
10305:                     }
10306:                 }
10307:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10308:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10309:                     foreach my $option (keys(%optionson)) {
10310:                         unless ($csvoptions{$option}) {
10311:                             $optionsoff{$option} = $optionson{$option};
10312:                             $optionson{$option} = '';
10313:                         }
10314:                     }
10315:                 }
10316:             }
10317:         } else {
10318:             $checked{'dat'} = $is_checked;
10319:         }
10320:     } else {
10321:         $checked{'dat'} = $is_checked;
10322:     }
10323:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10324:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10325:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10326:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10327:     foreach my $item ('dat','csv') {
10328:         my $id;
10329:         if ($item eq 'csv') {
10330:             $id = 'id="scantronconfcsv" ';
10331:         }
10332:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10333:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10334:         if ($item eq 'csv') {
10335:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10336:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10337:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10338:             foreach my $col (@fields) {
10339:                 my $selnone;
10340:                 if ($csvfields{$col} eq '') {
10341:                     $selnone = ' selected="selected"';
10342:                 }
10343:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10344:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10345:                               '<option value=""'.$selnone.'></option>';
10346:                 for (my $i=0; $i<20; $i++) {
10347:                     my $shown = $i+1;
10348:                     my $sel;
10349:                     unless ($selnone) {
10350:                         if (exists($csvfields{$col})) {
10351:                             if ($csvfields{$col} == $i) {
10352:                                 $sel = ' selected="selected"';
10353:                             }
10354:                         }
10355:                     }
10356:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10357:                 }
10358:                 $datatable .= '</select></td></tr>';
10359:            }
10360:            $datatable .= '</table></fieldset>'.
10361:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10362:                          '<legend>'.&mt('CSV Options').'</legend>';
10363:            foreach my $option ('hdr','pad','rem') {
10364:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10365:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10366:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10367:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10368:            }
10369:            $datatable .= '</fieldset>';
10370:            $itemcount ++;
10371:         }
10372:     }
10373:     $datatable .= '</td></tr>';
10374:     $$rowtotal ++;
10375:     return $datatable;
10376: }
10377: 
10378: sub scantronconfig_titles {
10379:     return &Apache::lonlocal::texthash(
10380:                                           dat => 'Standard format (.dat)',
10381:                                           csv => 'Comma separated values (.csv)',
10382:                                           hdr => 'Remove first line in file (contains column titles)',
10383:                                           pad => 'Prepend 0s to PaperID',
10384:                                           rem => 'Remove leading spaces (except Question Response columns)',
10385:                                           CODE => 'CODE',
10386:                                           ID   => 'Student ID',
10387:                                           PaperID => 'Paper ID',
10388:                                           FirstName => 'First Name',
10389:                                           LastName => 'Last Name',
10390:                                           FirstQuestion => 'First Question Response',
10391:                                           Section => 'Section',
10392:     );
10393: }
10394: 
10395: sub scantroncsv_fields {
10396:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10397: }
10398: 
10399: sub print_coursecategories {
10400:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10401:     my $datatable;
10402:     if ($position eq 'top') {
10403:         my (%checked);
10404:         my @catitems = ('unauth','auth');
10405:         my @cattypes = ('std','domonly','codesrch','none');
10406:         $checked{'unauth'} = 'std';
10407:         $checked{'auth'} = 'std';
10408:         if (ref($settings) eq 'HASH') {
10409:             foreach my $type (@cattypes) {
10410:                 if ($type eq $settings->{'unauth'}) {
10411:                     $checked{'unauth'} = $type;
10412:                 }
10413:                 if ($type eq $settings->{'auth'}) {
10414:                     $checked{'auth'} = $type;
10415:                 }
10416:             }
10417:         }
10418:         my %lt = &Apache::lonlocal::texthash (
10419:                                                unauth   => 'Catalog type for unauthenticated users',
10420:                                                auth     => 'Catalog type for authenticated users',
10421:                                                none     => 'No catalog',
10422:                                                std      => 'Standard catalog',
10423:                                                domonly  => 'Domain-only catalog',
10424:                                                codesrch => "Code search form",
10425:                                              );
10426:        my $itemcount = 0;
10427:        foreach my $item (@catitems) {
10428:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10429:            $datatable .= '<tr '.$css_class.'>'.
10430:                          '<td>'.$lt{$item}.'</td>'.
10431:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10432:            foreach my $type (@cattypes) {
10433:                my $ischecked;
10434:                if ($checked{$item} eq $type) {
10435:                    $ischecked=' checked="checked"';
10436:                }
10437:                $datatable .= '<label>'.
10438:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10439:                              ' />'.$lt{$type}.'</label>&nbsp;';
10440:            }
10441:            $datatable .= '</span></td></tr>';
10442:            $itemcount ++;
10443:         }
10444:         $$rowtotal += $itemcount;
10445:     } elsif ($position eq 'middle') {
10446:         my $toggle_cats_crs = ' ';
10447:         my $toggle_cats_dom = ' checked="checked" ';
10448:         my $can_cat_crs = ' ';
10449:         my $can_cat_dom = ' checked="checked" ';
10450:         my $toggle_catscomm_comm = ' ';
10451:         my $toggle_catscomm_dom = ' checked="checked" ';
10452:         my $can_catcomm_comm = ' ';
10453:         my $can_catcomm_dom = ' checked="checked" ';
10454:         my $toggle_catsplace_place = ' ';
10455:         my $toggle_catsplace_dom = ' checked="checked" ';
10456:         my $can_catplace_place = ' ';
10457:         my $can_catplace_dom = ' checked="checked" ';
10458: 
10459:         if (ref($settings) eq 'HASH') {
10460:             if ($settings->{'togglecats'} eq 'crs') {
10461:                 $toggle_cats_crs = $toggle_cats_dom;
10462:                 $toggle_cats_dom = ' ';
10463:             }
10464:             if ($settings->{'categorize'} eq 'crs') {
10465:                 $can_cat_crs = $can_cat_dom;
10466:                 $can_cat_dom = ' ';
10467:             }
10468:             if ($settings->{'togglecatscomm'} eq 'comm') {
10469:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10470:                 $toggle_catscomm_dom = ' ';
10471:             }
10472:             if ($settings->{'categorizecomm'} eq 'comm') {
10473:                 $can_catcomm_comm = $can_catcomm_dom;
10474:                 $can_catcomm_dom = ' ';
10475:             }
10476:             if ($settings->{'togglecatsplace'} eq 'place') {
10477:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10478:                 $toggle_catsplace_dom = ' ';
10479:             }
10480:             if ($settings->{'categorizeplace'} eq 'place') {
10481:                 $can_catplace_place = $can_catplace_dom;
10482:                 $can_catplace_dom = ' ';
10483:             }
10484:         }
10485:         my %title = &Apache::lonlocal::texthash (
10486:                      togglecats      => 'Show/Hide a course in catalog',
10487:                      togglecatscomm  => 'Show/Hide a community in catalog',
10488:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10489:                      categorize      => 'Assign a category to a course',
10490:                      categorizecomm  => 'Assign a category to a community',
10491:                      categorizeplace => 'Assign a category to a placement test',
10492:                     );
10493:         my %level = &Apache::lonlocal::texthash (
10494:                      dom   => 'Set in Domain',
10495:                      crs   => 'Set in Course',
10496:                      comm  => 'Set in Community',
10497:                      place => 'Set in Placement Test',
10498:                     );
10499:         $datatable = '<tr class="LC_odd_row">'.
10500:                   '<td>'.$title{'togglecats'}.'</td>'.
10501:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10502:                   '<input type="radio" name="togglecats"'.
10503:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10504:                   '<label><input type="radio" name="togglecats"'.
10505:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10506:                   '</tr><tr>'.
10507:                   '<td>'.$title{'categorize'}.'</td>'.
10508:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10509:                   '<label><input type="radio" name="categorize"'.
10510:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10511:                   '<label><input type="radio" name="categorize"'.
10512:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10513:                   '</tr><tr class="LC_odd_row">'.
10514:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10515:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10516:                   '<input type="radio" name="togglecatscomm"'.
10517:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10518:                   '<label><input type="radio" name="togglecatscomm"'.
10519:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10520:                   '</tr><tr>'.
10521:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10522:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10523:                   '<label><input type="radio" name="categorizecomm"'.
10524:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10525:                   '<label><input type="radio" name="categorizecomm"'.
10526:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10527:                   '</tr><tr class="LC_odd_row">'.
10528:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10529:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10530:                   '<input type="radio" name="togglecatsplace"'.
10531:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10532:                   '<label><input type="radio" name="togglecatscomm"'.
10533:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10534:                   '</tr><tr>'.
10535:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10536:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10537:                   '<label><input type="radio" name="categorizeplace"'.
10538:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10539:                   '<label><input type="radio" name="categorizeplace"'.
10540:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10541:                   '</tr>';
10542:         $$rowtotal += 6;
10543:     } else {
10544:         my $css_class;
10545:         my $itemcount = 1;
10546:         my $cathash; 
10547:         if (ref($settings) eq 'HASH') {
10548:             $cathash = $settings->{'cats'};
10549:         }
10550:         if (ref($cathash) eq 'HASH') {
10551:             my (@cats,@trails,%allitems,%idx,@jsarray);
10552:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10553:                                                    \%allitems,\%idx,\@jsarray);
10554:             my $maxdepth = scalar(@cats);
10555:             my $colattrib = '';
10556:             if ($maxdepth > 2) {
10557:                 $colattrib = ' colspan="2" ';
10558:             }
10559:             my @path;
10560:             if (@cats > 0) {
10561:                 if (ref($cats[0]) eq 'ARRAY') {
10562:                     my $numtop = @{$cats[0]};
10563:                     my $maxnum = $numtop;
10564:                     my %default_names = (
10565:                           instcode    => &mt('Official courses'),
10566:                           communities => &mt('Communities'),
10567:                           placement   => &mt('Placement Tests'),
10568:                     );
10569: 
10570:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10571:                         ($cathash->{'instcode::0'} eq '') ||
10572:                         (!grep(/^communities$/,@{$cats[0]})) || 
10573:                         ($cathash->{'communities::0'} eq '') ||
10574:                         (!grep(/^placement$/,@{$cats[0]})) ||
10575:                         ($cathash->{'placement::0'} eq '')) {
10576:                         $maxnum ++;
10577:                     }
10578:                     my $lastidx;
10579:                     for (my $i=0; $i<$numtop; $i++) {
10580:                         my $parent = $cats[0][$i];
10581:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10582:                         my $item = &escape($parent).'::0';
10583:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10584:                         $lastidx = $idx{$item};
10585:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10586:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10587:                         for (my $k=0; $k<=$maxnum; $k++) {
10588:                             my $vpos = $k+1;
10589:                             my $selstr;
10590:                             if ($k == $i) {
10591:                                 $selstr = ' selected="selected" ';
10592:                             }
10593:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10594:                         }
10595:                         $datatable .= '</select></span></td><td>';
10596:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10597:                             $datatable .=  '<span class="LC_nobreak">'
10598:                                            .$default_names{$parent}.'</span>';
10599:                             if ($parent eq 'instcode') {
10600:                                 $datatable .= '<br /><span class="LC_nobreak">('
10601:                                               .&mt('with institutional codes')
10602:                                               .')</span></td><td'.$colattrib.'>';
10603:                             } else {
10604:                                 $datatable .= '<table><tr><td>';
10605:                             }
10606:                             $datatable .= '<span class="LC_nobreak">'
10607:                                           .'<label><input type="radio" name="'
10608:                                           .$parent.'" value="1" checked="checked" />'
10609:                                           .&mt('Display').'</label>';
10610:                             if ($parent eq 'instcode') {
10611:                                 $datatable .= '&nbsp;';
10612:                             } else {
10613:                                 $datatable .= '</span></td></tr><tr><td>'
10614:                                               .'<span class="LC_nobreak">';
10615:                             }
10616:                             $datatable .= '<label><input type="radio" name="'
10617:                                           .$parent.'" value="0" />'
10618:                                           .&mt('Do not display').'</label></span>';
10619:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
10620:                                 $datatable .= '</td></tr></table>';
10621:                             }
10622:                             $datatable .= '</td>';
10623:                         } else {
10624:                             $datatable .= $parent
10625:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10626:                                           .'<input type="checkbox" name="deletecategory" '
10627:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10628:                         }
10629:                         my $depth = 1;
10630:                         push(@path,$parent);
10631:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10632:                         pop(@path);
10633:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10634:                         $itemcount ++;
10635:                     }
10636:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10637:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10638:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10639:                     for (my $k=0; $k<=$maxnum; $k++) {
10640:                         my $vpos = $k+1;
10641:                         my $selstr;
10642:                         if ($k == $numtop) {
10643:                             $selstr = ' selected="selected" ';
10644:                         }
10645:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10646:                     }
10647:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10648:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10649:                                   .'</tr>'."\n";
10650:                     $itemcount ++;
10651:                     foreach my $default ('instcode','communities','placement') {
10652:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10653:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10654:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10655:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10656:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10657:                             for (my $k=0; $k<=$maxnum; $k++) {
10658:                                 my $vpos = $k+1;
10659:                                 my $selstr;
10660:                                 if ($k == $maxnum) {
10661:                                     $selstr = ' selected="selected" ';
10662:                                 }
10663:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10664:                             }
10665:                             $datatable .= '</select></span></td>'.
10666:                                           '<td><span class="LC_nobreak">'.
10667:                                           $default_names{$default}.'</span>';
10668:                             if ($default eq 'instcode') {
10669:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10670:                                               .&mt('with institutional codes').')</span>';
10671:                             }
10672:                             $datatable .= '</td>'
10673:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10674:                                           .&mt('Display').'</label>&nbsp;'
10675:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10676:                                           .&mt('Do not display').'</label></span></td></tr>';
10677:                         }
10678:                     }
10679:                 }
10680:             } else {
10681:                 $datatable .= &initialize_categories($itemcount);
10682:             }
10683:         } else {
10684:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10685:                           .&initialize_categories($itemcount);
10686:         }
10687:         $$rowtotal += $itemcount;
10688:     }
10689:     return $datatable;
10690: }
10691: 
10692: sub print_serverstatuses {
10693:     my ($dom,$settings,$rowtotal) = @_;
10694:     my $datatable;
10695:     my @pages = &serverstatus_pages();
10696:     my (%namedaccess,%machineaccess);
10697:     foreach my $type (@pages) {
10698:         $namedaccess{$type} = '';
10699:         $machineaccess{$type}= '';
10700:     }
10701:     if (ref($settings) eq 'HASH') {
10702:         foreach my $type (@pages) {
10703:             if (exists($settings->{$type})) {
10704:                 if (ref($settings->{$type}) eq 'HASH') {
10705:                     foreach my $key (keys(%{$settings->{$type}})) {
10706:                         if ($key eq 'namedusers') {
10707:                             $namedaccess{$type} = $settings->{$type}->{$key};
10708:                         } elsif ($key eq 'machines') {
10709:                             $machineaccess{$type} = $settings->{$type}->{$key};
10710:                         }
10711:                     }
10712:                 }
10713:             }
10714:         }
10715:     }
10716:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10717:     my $rownum = 0;
10718:     my $css_class;
10719:     foreach my $type (@pages) {
10720:         $rownum ++;
10721:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10722:         $datatable .= '<tr'.$css_class.'>'.
10723:                       '<td><span class="LC_nobreak">'.
10724:                       $titles->{$type}.'</span></td>'.
10725:                       '<td class="LC_left_item">'.
10726:                       '<input type="text" name="'.$type.'_namedusers" '.
10727:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10728:                       '<td class="LC_right_item">'.
10729:                       '<span class="LC_nobreak">'.
10730:                       '<input type="text" name="'.$type.'_machines" '.
10731:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10732:                       '</span></td></tr>'."\n";
10733:     }
10734:     $$rowtotal += $rownum;
10735:     return $datatable;
10736: }
10737: 
10738: sub serverstatus_pages {
10739:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10740:             'checksums','clusterstatus','certstatus','metadata_keywords',
10741:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
10742:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
10743: }
10744: 
10745: sub defaults_javascript {
10746:     my ($settings) = @_;
10747:     return unless (ref($settings) eq 'HASH');
10748:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10749:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10750:         if ($maxnum eq '') {
10751:             $maxnum = 0;
10752:         }
10753:         $maxnum ++;
10754:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10755:         return <<"ENDSCRIPT";
10756: <script type="text/javascript">
10757: // <![CDATA[
10758: function reorderTypes(form,caller) {
10759:     var changedVal;
10760: $jstext 
10761:     var newpos = 'addinststatus_pos';
10762:     var current = new Array;
10763:     var maxh = $maxnum;
10764:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10765:     var oldVal;
10766:     if (caller == newpos) {
10767:         changedVal = newitemVal;
10768:     } else {
10769:         var curritem = 'inststatus_pos_'+caller;
10770:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10771:         current[newitemVal] = newpos;
10772:     }
10773:     for (var i=0; i<inststatuses.length; i++) {
10774:         if (inststatuses[i] != caller) {
10775:             var elementName = 'inststatus_pos_'+inststatuses[i];
10776:             if (form.elements[elementName]) {
10777:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10778:                 current[currVal] = elementName;
10779:             }
10780:         }
10781:     }
10782:     for (var j=0; j<maxh; j++) {
10783:         if (current[j] == undefined) {
10784:             oldVal = j;
10785:         }
10786:     }
10787:     if (oldVal < changedVal) {
10788:         for (var k=oldVal+1; k<=changedVal ; k++) {
10789:            var elementName = current[k];
10790:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10791:         }
10792:     } else {
10793:         for (var k=changedVal; k<oldVal; k++) {
10794:             var elementName = current[k];
10795:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10796:         }
10797:     }
10798:     return;
10799: }
10800: 
10801: // ]]>
10802: </script>
10803: 
10804: ENDSCRIPT
10805:     }
10806:     return;
10807: }
10808: 
10809: sub passwords_javascript {
10810:     my %intalert = &Apache::lonlocal::texthash (
10811:         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.',
10812:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10813:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10814:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10815:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
10816:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10817:     );
10818:     &js_escape(\%intalert);
10819:     my $defmin = $Apache::lonnet::passwdmin;
10820:     my $intauthjs = <<"ENDSCRIPT";
10821: 
10822: function warnIntAuth(field) {
10823:     if (field.name == 'intauth_check') {
10824:         if (field.value == '2') {
10825:             alert('$intalert{authcheck}');
10826:         }
10827:     }
10828:     if (field.name == 'intauth_cost') {
10829:         field.value.replace(/\s/g,'');
10830:         if (field.value != '') {
10831:             var regexdigit=/^\\d+\$/;
10832:             if (!regexdigit.test(field.value)) {
10833:                 alert('$intalert{authcost}');
10834:             }
10835:         }
10836:     }
10837:     return;
10838: }
10839: 
10840: function warnIntPass(field) {
10841:     field.value.replace(/^\s+/,'');
10842:     field.value.replace(/\s+\$/,'');
10843:     var regexdigit=/^\\d+\$/;
10844:     if (field.name == 'passwords_min') {
10845:         if (field.value == '') {
10846:             alert('$intalert{passmin}');
10847:             field.value = '$defmin';
10848:         } else {
10849:             if (!regexdigit.test(field.value)) {
10850:                 alert('$intalert{passmin}');
10851:                 field.value = '$defmin';
10852:             }
10853:             var minval = parseInt(field.value,10);
10854:             if (minval < $defmin) {
10855:                 alert('$intalert{passmin}');
10856:                 field.value = '$defmin';
10857:             }
10858:         }
10859:     } else {
10860:         if (field.value == '0') {
10861:             field.value = '';
10862:         }
10863:         if (field.value != '') {
10864:             if (field.name == 'passwords_expire') {
10865:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
10866:                 if (!regexpposnum.test(field.value)) {
10867:                     alert('$intalert{passexp}');
10868:                     field.value = '';
10869:                 } else {
10870:                     var expval = parseFloat(field.value);
10871:                     if (expval == 0) {
10872:                         alert('$intalert{passexp}');
10873:                         field.value = '';
10874:                     }
10875:                 }
10876:             } else {
10877:                 if (!regexdigit.test(field.value)) {
10878:                     if (field.name == 'passwords_max') {
10879:                         alert('$intalert{passmax}');
10880:                     } else {
10881:                         if (field.name == 'passwords_numsaved') {
10882:                             alert('$intalert{passnum}');
10883:                         }
10884:                     }
10885:                     field.value = '';
10886:                 }
10887:             }
10888:         }
10889:     }
10890:     return;
10891: }
10892: 
10893: ENDSCRIPT
10894:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10895: }
10896: 
10897: sub coursecategories_javascript {
10898:     my ($settings) = @_;
10899:     my ($output,$jstext,$cathash);
10900:     if (ref($settings) eq 'HASH') {
10901:         $cathash = $settings->{'cats'};
10902:     }
10903:     if (ref($cathash) eq 'HASH') {
10904:         my (@cats,@jsarray,%idx);
10905:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10906:         if (@jsarray > 0) {
10907:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10908:             for (my $i=0; $i<@jsarray; $i++) {
10909:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10910:                     my $catstr = join('","',@{$jsarray[$i]});
10911:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10912:                 }
10913:             }
10914:         }
10915:     } else {
10916:         $jstext  = '    var categories = Array(1);'."\n".
10917:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10918:     }
10919:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10920:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10921:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
10922:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10923:     &js_escape(\$instcode_reserved);
10924:     &js_escape(\$communities_reserved);
10925:     &js_escape(\$placement_reserved);
10926:     &js_escape(\$choose_again);
10927:     $output = <<"ENDSCRIPT";
10928: <script type="text/javascript">
10929: // <![CDATA[
10930: function reorderCats(form,parent,item,idx) {
10931:     var changedVal;
10932: $jstext
10933:     var newpos = 'addcategory_pos';
10934:     if (parent == '') {
10935:         var has_instcode = 0;
10936:         var maxtop = categories[idx].length;
10937:         for (var j=0; j<maxtop; j++) {
10938:             if (categories[idx][j] == 'instcode::0') {
10939:                 has_instcode == 1;
10940:             }
10941:         }
10942:         if (has_instcode == 0) {
10943:             categories[idx][maxtop] = 'instcode_pos';
10944:         }
10945:     } else {
10946:         newpos += '_'+parent;
10947:     }
10948:     var maxh = 1 + categories[idx].length;
10949:     var current = new Array;
10950:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10951:     if (item == newpos) {
10952:         changedVal = newitemVal;
10953:     } else {
10954:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10955:         current[newitemVal] = newpos;
10956:     }
10957:     for (var i=0; i<categories[idx].length; i++) {
10958:         var elementName = categories[idx][i];
10959:         if (elementName != item) {
10960:             if (form.elements[elementName]) {
10961:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10962:                 current[currVal] = elementName;
10963:             }
10964:         }
10965:     }
10966:     var oldVal;
10967:     for (var j=0; j<maxh; j++) {
10968:         if (current[j] == undefined) {
10969:             oldVal = j;
10970:         }
10971:     }
10972:     if (oldVal < changedVal) {
10973:         for (var k=oldVal+1; k<=changedVal ; k++) {
10974:            var elementName = current[k];
10975:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10976:         }
10977:     } else {
10978:         for (var k=changedVal; k<oldVal; k++) {
10979:             var elementName = current[k];
10980:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10981:         }
10982:     }
10983:     return;
10984: }
10985: 
10986: function categoryCheck(form) {
10987:     if (form.elements['addcategory_name'].value == 'instcode') {
10988:         alert('$instcode_reserved\\n$choose_again');
10989:         return false;
10990:     }
10991:     if (form.elements['addcategory_name'].value == 'communities') {
10992:         alert('$communities_reserved\\n$choose_again');
10993:         return false;
10994:     }
10995:     if (form.elements['addcategory_name'].value == 'placement') {
10996:         alert('$placement_reserved\\n$choose_again');
10997:         return false;
10998:     }
10999:     return true;
11000: }
11001: 
11002: // ]]>
11003: </script>
11004: 
11005: ENDSCRIPT
11006:     return $output;
11007: }
11008: 
11009: sub initialize_categories {
11010:     my ($itemcount) = @_;
11011:     my ($datatable,$css_class,$chgstr);
11012:     my %default_names = &Apache::lonlocal::texthash (
11013:                       instcode    => 'Official courses (with institutional codes)',
11014:                       communities => 'Communities',
11015:                       placement   => 'Placement Tests',
11016:                         );
11017:     my %selnum = (
11018:                    instcode    => '0',
11019:                    communities => '1',
11020:                    placement   => '2',
11021:                  );
11022:     my %selected;
11023:     foreach my $default ('instcode','communities','placement') {
11024:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11025:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11026:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11027:         $selected{$selnum{$default}} = ' selected="selected"';
11028:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11029:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11030:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11031:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11032:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11033:                      .'<option value="3">4</option></select>&nbsp;'
11034:                      .$default_names{$default}
11035:                      .'</span></td><td><span class="LC_nobreak">'
11036:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11037:                      .&mt('Display').'</label>&nbsp;<label>'
11038:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11039:                  .'</label></span></td></tr>';
11040:         $itemcount ++;
11041:     }
11042:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11043:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11044:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11045:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11046:                   .'<option value="0">1</option>'
11047:                   .'<option value="1">2</option>'
11048:                   .'<option value="2">3</option>'
11049:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11050:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11051:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11052:                   .'</td></tr>';
11053:     return $datatable;
11054: }
11055: 
11056: sub build_category_rows {
11057:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11058:     my ($text,$name,$item,$chgstr);
11059:     if (ref($cats) eq 'ARRAY') {
11060:         my $maxdepth = scalar(@{$cats});
11061:         if (ref($cats->[$depth]) eq 'HASH') {
11062:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11063:                 my $numchildren = @{$cats->[$depth]{$parent}};
11064:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11065:                 $text .= '<td><table class="LC_data_table">';
11066:                 my ($idxnum,$parent_name,$parent_item);
11067:                 my $higher = $depth - 1;
11068:                 if ($higher == 0) {
11069:                     $parent_name = &escape($parent).'::'.$higher;
11070:                 } else {
11071:                     if (ref($path) eq 'ARRAY') {
11072:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11073:                     }
11074:                 }
11075:                 $parent_item = 'addcategory_pos_'.$parent_name;
11076:                 for (my $j=0; $j<=$numchildren; $j++) {
11077:                     if ($j < $numchildren) {
11078:                         $name = $cats->[$depth]{$parent}[$j];
11079:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11080:                         $idxnum = $idx->{$item};
11081:                     } else {
11082:                         $name = $parent_name;
11083:                         $item = $parent_item;
11084:                     }
11085:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11086:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11087:                     for (my $i=0; $i<=$numchildren; $i++) {
11088:                         my $vpos = $i+1;
11089:                         my $selstr;
11090:                         if ($j == $i) {
11091:                             $selstr = ' selected="selected" ';
11092:                         }
11093:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11094:                     }
11095:                     $text .= '</select>&nbsp;';
11096:                     if ($j < $numchildren) {
11097:                         my $deeper = $depth+1;
11098:                         $text .= $name.'&nbsp;'
11099:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11100:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11101:                         if(ref($path) eq 'ARRAY') {
11102:                             push(@{$path},$name);
11103:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11104:                             pop(@{$path});
11105:                         }
11106:                     } else {
11107:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11108:                         if ($j == $numchildren) {
11109:                             $text .= $name;
11110:                         } else {
11111:                             $text .= $item;
11112:                         }
11113:                         $text .= '" value="" />';
11114:                     }
11115:                     $text .= '</td></tr>';
11116:                 }
11117:                 $text .= '</table></td>';
11118:             } else {
11119:                 my $higher = $depth-1;
11120:                 if ($higher == 0) {
11121:                     $name = &escape($parent).'::'.$higher;
11122:                 } else {
11123:                     if (ref($path) eq 'ARRAY') {
11124:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11125:                     }
11126:                 }
11127:                 my $colspan;
11128:                 if ($parent ne 'instcode') {
11129:                     $colspan = $maxdepth - $depth - 1;
11130:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11131:                 }
11132:             }
11133:         }
11134:     }
11135:     return $text;
11136: }
11137: 
11138: sub modifiable_userdata_row {
11139:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11140:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11141:     my ($role,$rolename,$statustype);
11142:     $role = $item;
11143:     if ($context eq 'cancreate') {
11144:         if ($item =~ /^(emailusername)_(.+)$/) {
11145:             $role = $1;
11146:             $statustype = $2;
11147:             if (ref($usertypes) eq 'HASH') {
11148:                 if ($usertypes->{$statustype}) {
11149:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11150:                 } else {
11151:                     $rolename = &mt('Data provided by user');
11152:                 }
11153:             }
11154:         }
11155:     } elsif ($context eq 'selfcreate') {
11156:         if (ref($usertypes) eq 'HASH') {
11157:             $rolename = $usertypes->{$role};
11158:         } else {
11159:             $rolename = $role;
11160:         }
11161:     } elsif ($context eq 'lti') {
11162:         $rolename = &mt('Institutional data used (if available)');
11163:     } elsif ($context eq 'privacy') {
11164:         $rolename = $itemdesc;
11165:     } else {
11166:         if ($role eq 'cr') {
11167:             $rolename = &mt('Custom role');
11168:         } else {
11169:             $rolename = &Apache::lonnet::plaintext($role);
11170:         }
11171:     }
11172:     my (@fields,%fieldtitles);
11173:     if (ref($fieldsref) eq 'ARRAY') {
11174:         @fields = @{$fieldsref};
11175:     } else {
11176:         @fields = ('lastname','firstname','middlename','generation',
11177:                    'permanentemail','id');
11178:     }
11179:     if ((ref($titlesref) eq 'HASH')) {
11180:         %fieldtitles = %{$titlesref};
11181:     } else {
11182:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11183:     }
11184:     my $output;
11185:     my $css_class;
11186:     if ($rowcount%2) {
11187:         $css_class = 'LC_odd_row';
11188:     }
11189:     if ($customcss) {
11190:         $css_class .= " $customcss";
11191:     }
11192:     $css_class =~ s/^\s+//;
11193:     if ($css_class) {
11194:         $css_class = ' class="'.$css_class.'"';
11195:     }
11196:     if ($rowstyle) {
11197:         $css_class .= ' style="'.$rowstyle.'"';
11198:     }
11199:     if ($rowid) {
11200:         $rowid = ' id="'.$rowid.'"';
11201:     }
11202:     $output = '<tr '.$css_class.$rowid.'>'.
11203:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11204:               '<td class="LC_left_item" colspan="2"><table>';
11205:     my $rem;
11206:     my %checks;
11207:     my %current;
11208:     if (ref($settings) eq 'HASH') {
11209:         my $hashref;
11210:         if ($context eq 'lti') {
11211:             if (ref($settings) eq 'HASH') {
11212:                 $hashref = $settings->{'instdata'};
11213:             }
11214:         } elsif ($context eq 'privacy') {
11215:             my ($key,$inner) = split(/_/,$role);
11216:             if (ref($settings) eq 'HASH') {
11217:                 if (ref($settings->{$key}) eq 'HASH') {
11218:                     $hashref = $settings->{$key}->{$inner};
11219:                 }
11220:             }
11221:         } elsif (ref($settings->{$context}) eq 'HASH') {
11222:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11223:                 $hashref = $settings->{'lti_instdata'};
11224:             }
11225:             if ($role eq 'emailusername') {
11226:                 if ($statustype) {
11227:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11228:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11229:                     }
11230:                 }
11231:             }
11232:         }
11233:         if (ref($hashref) eq 'HASH') { 
11234:             foreach my $field (@fields) {
11235:                 if ($hashref->{$field}) {
11236:                     if ($role eq 'emailusername') {
11237:                         $checks{$field} = $hashref->{$field};
11238:                     } else {
11239:                         $checks{$field} = ' checked="checked" ';
11240:                     }
11241:                 }
11242:             }
11243:         }
11244:     }
11245:  
11246:     my $total = scalar(@fields);
11247:     for (my $i=0; $i<$total; $i++) {
11248:         $rem = $i%($numinrow);
11249:         if ($rem == 0) {
11250:             if ($i > 0) {
11251:                 $output .= '</tr>';
11252:             }
11253:             $output .= '<tr>';
11254:         }
11255:         my $check = ' ';
11256:         unless ($role eq 'emailusername') {
11257:             if (exists($checks{$fields[$i]})) {
11258:                 $check = $checks{$fields[$i]};
11259:             } elsif ($context eq 'privacy') {
11260:                 if ($role =~ /^priv_(domain|course)$/) {
11261:                     if (ref($settings) ne 'HASH') {
11262:                         $check = ' checked="checked" ';
11263:                     }
11264:                 } elsif ($role =~ /^priv_(author|community)$/) {
11265:                     if (ref($settings) ne 'HASH') {
11266:                         unless ($fields[$i] eq 'id') {
11267:                             $check = ' checked="checked" ';
11268:                         }
11269:                     }
11270:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11271:                     if (ref($settings) ne 'HASH') {
11272:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11273:                             $check = ' checked="checked" ';
11274:                         }
11275:                     }
11276:                 }
11277:             } elsif ($context ne 'lti') {
11278:                 if ($role eq 'st') {
11279:                     if (ref($settings) ne 'HASH') {
11280:                         $check = ' checked="checked" '; 
11281:                     }
11282:                 }
11283:             }
11284:         }
11285:         $output .= '<td class="LC_left_item">'.
11286:                    '<span class="LC_nobreak">';
11287:         my $prefix = 'canmodify';
11288:         if ($role eq 'emailusername') {
11289:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11290:                 $checks{$fields[$i]} = 'omit';
11291:             }
11292:             foreach my $option ('required','optional','omit') {
11293:                 my $checked='';
11294:                 if ($checks{$fields[$i]} eq $option) {
11295:                     $checked='checked="checked" ';
11296:                 }
11297:                 $output .= '<label>'.
11298:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11299:                            &mt($option).'</label>'.('&nbsp;' x2);
11300:             }
11301:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11302:         } else {
11303:             if ($context eq 'lti') {
11304:                 $prefix = 'lti';
11305:             } elsif ($context eq 'privacy') {
11306:                 $prefix = 'privacy';
11307:             }
11308:             $output .= '<label>'.
11309:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11310:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11311:                        '</label>';
11312:         }
11313:         $output .= '</span></td>';
11314:     }
11315:     $rem = $total%$numinrow;
11316:     my $colsleft;
11317:     if ($rem) {
11318:         $colsleft = $numinrow - $rem;
11319:     }
11320:     if ($colsleft > 1) {
11321:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11322:                    '&nbsp;</td>';
11323:     } elsif ($colsleft == 1) {
11324:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11325:     }
11326:     $output .= '</tr></table></td></tr>';
11327:     return $output;
11328: }
11329: 
11330: sub insttypes_row {
11331:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11332:         $customcss,$rowstyle) = @_;
11333:     my %lt = &Apache::lonlocal::texthash (
11334:                       cansearch => 'Users allowed to search',
11335:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11336:                       lockablenames => 'User preference to lock name',
11337:                       selfassign    => 'Self-reportable affiliations',
11338:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11339:              );
11340:     my $showdom;
11341:     if ($context eq 'cansearch') {
11342:         $showdom = ' ('.$dom.')';
11343:     }
11344:     my $class = 'LC_left_item';
11345:     if ($context eq 'statustocreate') {
11346:         $class = 'LC_right_item';
11347:     }
11348:     my $css_class;
11349:     if ($$rowtotal%2) {
11350:         $css_class = 'LC_odd_row';
11351:     }
11352:     if ($customcss) {
11353:         $css_class .= ' '.$customcss;
11354:     }
11355:     $css_class =~ s/^\s+//;
11356:     if ($css_class) {
11357:         $css_class = ' class="'.$css_class.'"';
11358:     }
11359:     if ($rowstyle) {
11360:         $css_class .= ' style="'.$rowstyle.'"';
11361:     }
11362:     if ($onclick) {
11363:         $onclick = 'onclick="'.$onclick.'" ';
11364:     }
11365:     my $output = '<tr'.$css_class.'>'.
11366:                  '<td>'.$lt{$context}.$showdom.
11367:                  '</td><td class="'.$class.'" colspan="2"><table>';
11368:     my $rem;
11369:     if (ref($types) eq 'ARRAY') {
11370:         for (my $i=0; $i<@{$types}; $i++) {
11371:             if (defined($usertypes->{$types->[$i]})) {
11372:                 my $rem = $i%($numinrow);
11373:                 if ($rem == 0) {
11374:                     if ($i > 0) {
11375:                         $output .= '</tr>';
11376:                     }
11377:                     $output .= '<tr>';
11378:                 }
11379:                 my $check = ' ';
11380:                 if (ref($settings) eq 'HASH') {
11381:                     if (ref($settings->{$context}) eq 'ARRAY') {
11382:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11383:                             $check = ' checked="checked" ';
11384:                         }
11385:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11386:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11387:                             $check = ' checked="checked" ';
11388:                         }
11389:                     } elsif ($context eq 'statustocreate') {
11390:                         $check = ' checked="checked" ';
11391:                     }
11392:                 }
11393:                 $output .= '<td class="LC_left_item">'.
11394:                            '<span class="LC_nobreak"><label>'.
11395:                            '<input type="checkbox" name="'.$context.'" '.
11396:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11397:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11398:             }
11399:         }
11400:         $rem = @{$types}%($numinrow);
11401:     }
11402:     my $colsleft = $numinrow - $rem;
11403:     if ($context eq 'overrides') {
11404:         if ($colsleft > 1) {
11405:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11406:         } else {
11407:             $output .= '<td class="LC_left_item">';
11408:         }
11409:         $output .= '&nbsp;';  
11410:     } else {
11411:         if ($rem == 0) {
11412:             $output .= '<tr>';
11413:         }
11414:         if ($colsleft > 1) {
11415:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11416:         } else {
11417:             $output .= '<td class="LC_left_item">';
11418:         }
11419:         my $defcheck = ' ';
11420:         if (ref($settings) eq 'HASH') {  
11421:             if (ref($settings->{$context}) eq 'ARRAY') {
11422:                 if (grep(/^default$/,@{$settings->{$context}})) {
11423:                     $defcheck = ' checked="checked" ';
11424:                 }
11425:             } elsif ($context eq 'statustocreate') {
11426:                 $defcheck = ' checked="checked" ';
11427:             }
11428:         }
11429:         $output .= '<span class="LC_nobreak"><label>'.
11430:                    '<input type="checkbox" name="'.$context.'" '.
11431:                    'value="default"'.$defcheck.$onclick.' />'.
11432:                    $othertitle.'</label></span>';
11433:     }
11434:     $output .= '</td></tr></table></td></tr>';
11435:     return $output;
11436: }
11437: 
11438: sub sorted_searchtitles {
11439:     my %searchtitles = &Apache::lonlocal::texthash(
11440:                          'uname' => 'username',
11441:                          'lastname' => 'last name',
11442:                          'lastfirst' => 'last name, first name',
11443:                      );
11444:     my @titleorder = ('uname','lastname','lastfirst');
11445:     return (\%searchtitles,\@titleorder);
11446: }
11447: 
11448: sub sorted_searchtypes {
11449:     my %srchtypes_desc = (
11450:                            exact    => 'is exact match',
11451:                            contains => 'contains ..',
11452:                            begins   => 'begins with ..',
11453:                          );
11454:     my @srchtypeorder = ('exact','begins','contains');
11455:     return (\%srchtypes_desc,\@srchtypeorder);
11456: }
11457: 
11458: sub usertype_update_row {
11459:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11460:     my $datatable;
11461:     my $numinrow = 4;
11462:     foreach my $type (@{$types}) {
11463:         if (defined($usertypes->{$type})) {
11464:             $$rownums ++;
11465:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11466:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11467:                           '</td><td class="LC_left_item"><table>';
11468:             for (my $i=0; $i<@{$fields}; $i++) {
11469:                 my $rem = $i%($numinrow);
11470:                 if ($rem == 0) {
11471:                     if ($i > 0) {
11472:                         $datatable .= '</tr>';
11473:                     }
11474:                     $datatable .= '<tr>';
11475:                 }
11476:                 my $check = ' ';
11477:                 if (ref($settings) eq 'HASH') {
11478:                     if (ref($settings->{'fields'}) eq 'HASH') {
11479:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11480:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11481:                                 $check = ' checked="checked" ';
11482:                             }
11483:                         }
11484:                     }
11485:                 }
11486: 
11487:                 if ($i == @{$fields}-1) {
11488:                     my $colsleft = $numinrow - $rem;
11489:                     if ($colsleft > 1) {
11490:                         $datatable .= '<td colspan="'.$colsleft.'">';
11491:                     } else {
11492:                         $datatable .= '<td>';
11493:                     }
11494:                 } else {
11495:                     $datatable .= '<td>';
11496:                 }
11497:                 $datatable .= '<span class="LC_nobreak"><label>'.
11498:                               '<input type="checkbox" name="updateable_'.$type.
11499:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11500:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11501:             }
11502:             $datatable .= '</tr></table></td></tr>';
11503:         }
11504:     }
11505:     return $datatable;
11506: }
11507: 
11508: sub modify_login {
11509:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11510:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11511:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
11512:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlnotsso);
11513:     %title = ( coursecatalog => 'Display course catalog',
11514:                adminmail => 'Display administrator E-mail address',
11515:                helpdesk  => 'Display "Contact Helpdesk" link',
11516:                newuser => 'Link for visitors to create a user account',
11517:                loginheader => 'Log-in box header',
11518:                saml => 'Dual SSO and non-SSO login');
11519:     @offon = ('off','on');
11520:     if (ref($domconfig{login}) eq 'HASH') {
11521:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11522:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11523:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11524:             }
11525:         }
11526:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
11527:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
11528:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
11529:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
11530:                     $saml{$lonhost} = 1;
11531:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
11532:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
11533:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
11534:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
11535:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
11536:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
11537:                 }
11538:             }
11539:         }
11540:     }
11541:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11542:                                            \%domconfig,\%loginhash);
11543:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11544:     foreach my $item (@toggles) {
11545:         $loginhash{login}{$item} = $env{'form.'.$item};
11546:     }
11547:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11548:     if (ref($colchanges{'login'}) eq 'HASH') {  
11549:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11550:                                          \%loginhash);
11551:     }
11552: 
11553:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11554:     my %domservers = &Apache::lonnet::get_servers($dom);
11555:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11556:     if (keys(%servers) > 1) {
11557:         foreach my $lonhost (keys(%servers)) {
11558:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
11559:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
11560:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
11561:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
11562:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
11563:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11564:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11565:                         $changes{'loginvia'}{$lonhost} = 1;
11566:                     } else {
11567:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
11568:                         $changes{'loginvia'}{$lonhost} = 1;
11569:                     }
11570:                 } else {
11571:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11572:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11573:                         $changes{'loginvia'}{$lonhost} = 1;
11574:                     }
11575:                 }
11576:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
11577:                     foreach my $item (@loginvia_attribs) {
11578:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
11579:                     }
11580:                 } else {
11581:                     foreach my $item (@loginvia_attribs) {
11582:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11583:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11584:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
11585:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11586:                                 $new = '/';
11587:                             }
11588:                         }
11589:                         if (($item eq 'custompath') && 
11590:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11591:                             $new = '';
11592:                         }
11593:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
11594:                             $changes{'loginvia'}{$lonhost} = 1;
11595:                         }
11596:                         if ($item eq 'exempt') {
11597:                             $new = &check_exempt_addresses($new);
11598:                         }
11599:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11600:                     }
11601:                 }
11602:             } else {
11603:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11604:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11605:                     $changes{'loginvia'}{$lonhost} = 1;
11606:                     foreach my $item (@loginvia_attribs) {
11607:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11608:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11609:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11610:                                 $new = '/';
11611:                             }
11612:                         }
11613:                         if (($item eq 'custompath') && 
11614:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11615:                             $new = '';
11616:                         }
11617:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11618:                     }
11619:                 }
11620:             }
11621:         }
11622:     }
11623: 
11624:     my $servadm = $r->dir_config('lonAdmEMail');
11625:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11626:     if (ref($domconfig{'login'}) eq 'HASH') {
11627:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11628:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11629:                 if ($lang eq 'nolang') {
11630:                     push(@currlangs,$lang);
11631:                 } elsif (defined($langchoices{$lang})) {
11632:                     push(@currlangs,$lang);
11633:                 } else {
11634:                     next;
11635:                 }
11636:             }
11637:         }
11638:     }
11639:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11640:     if (@currlangs > 0) {
11641:         foreach my $lang (@currlangs) {
11642:             if (grep(/^\Q$lang\E$/,@delurls)) {
11643:                 $changes{'helpurl'}{$lang} = 1;
11644:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11645:                 $changes{'helpurl'}{$lang} = 1;
11646:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11647:                 push(@newlangs,$lang);
11648:             } else {
11649:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11650:             }
11651:         }
11652:     }
11653:     unless (grep(/^nolang$/,@currlangs)) {
11654:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11655:             $changes{'helpurl'}{'nolang'} = 1;
11656:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11657:             push(@newlangs,'nolang');
11658:         }
11659:     }
11660:     if ($env{'form.loginhelpurl_add_lang'}) {
11661:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11662:             ($env{'form.loginhelpurl_add_file.filename'})) {
11663:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11664:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11665:         }
11666:     }
11667:     if ((@newlangs > 0) || ($addedfile)) {
11668:         my $error;
11669:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11670:         if ($configuserok eq 'ok') {
11671:             if ($switchserver) {
11672:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11673:             } elsif ($author_ok eq 'ok') {
11674:                 my @allnew = @newlangs;
11675:                 if ($addedfile ne '') {
11676:                     push(@allnew,$addedfile);
11677:                 }
11678:                 foreach my $lang (@allnew) {
11679:                     my $formelem = 'loginhelpurl_'.$lang;
11680:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11681:                         $formelem = 'loginhelpurl_add_file';
11682:                     }
11683:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11684:                                                                "help/$lang",'','',$newfile{$lang});
11685:                     if ($result eq 'ok') {
11686:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11687:                         $changes{'helpurl'}{$lang} = 1;
11688:                     } else {
11689:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11690:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11691:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11692:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11693:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11694:                         }
11695:                     }
11696:                 }
11697:             } else {
11698:                 $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);
11699:             }
11700:         } else {
11701:             $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);
11702:         }
11703:         if ($error) {
11704:             &Apache::lonnet::logthis($error);
11705:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11706:         }
11707:     }
11708: 
11709:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11710:     if (ref($domconfig{'login'}) eq 'HASH') {
11711:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11712:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11713:                 if ($domservers{$lonhost}) {
11714:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11715:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11716:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11717:                     }
11718:                 }
11719:             }
11720:         }
11721:     }
11722:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11723:     foreach my $lonhost (sort(keys(%domservers))) {
11724:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11725:             $changes{'headtag'}{$lonhost} = 1;
11726:         } else {
11727:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11728:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11729:             }
11730:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11731:                 push(@newhosts,$lonhost);
11732:             } elsif ($currheadtagurls{$lonhost}) {
11733:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11734:                 if ($currexempt{$lonhost}) {
11735:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11736:                         $changes{'headtag'}{$lonhost} = 1;
11737:                     }
11738:                 } elsif ($possexempt{$lonhost}) {
11739:                     $changes{'headtag'}{$lonhost} = 1;
11740:                 }
11741:                 if ($possexempt{$lonhost}) {
11742:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11743:                 }
11744:             }
11745:         }
11746:     }
11747:     if (@newhosts) {
11748:         my $error;
11749:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11750:         if ($configuserok eq 'ok') {
11751:             if ($switchserver) {
11752:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11753:             } elsif ($author_ok eq 'ok') {
11754:                 foreach my $lonhost (@newhosts) {
11755:                     my $formelem = 'loginheadtag_'.$lonhost;
11756:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11757:                                                                           "login/headtag/$lonhost",'','',
11758:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
11759:                     if ($result eq 'ok') {
11760:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11761:                         $changes{'headtag'}{$lonhost} = 1;
11762:                         if ($possexempt{$lonhost}) {
11763:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11764:                         }
11765:                     } else {
11766:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11767:                                            $newheadtagurls{$lonhost},$result);
11768:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11769:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11770:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11771:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11772:                         }
11773:                     }
11774:                 }
11775:             } else {
11776:                 $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);
11777:             }
11778:         } else {
11779:             $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);
11780:         }
11781:         if ($error) {
11782:             &Apache::lonnet::logthis($error);
11783:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11784:         }
11785:     }
11786:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
11787:     my @newsamlimgs;
11788:     foreach my $lonhost (keys(%domservers)) {
11789:         if ($env{'form.saml_'.$lonhost}) {
11790:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
11791:                 push(@newsamlimgs,$lonhost);
11792:             }
11793:             foreach my $item ('text','alt','url','title','notsso') {
11794:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
11795:             }
11796:             if ($saml{$lonhost}) {
11797:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
11798: #FIXME Need to obsolete published image
11799:                     delete($currsaml{$lonhost}{'img'});
11800:                     $changes{'saml'}{$lonhost} = 1;
11801:                 }
11802:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
11803:                     $changes{'saml'}{$lonhost} = 1;
11804:                 }
11805:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
11806:                     $changes{'saml'}{$lonhost} = 1;
11807:                 }
11808:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
11809:                     $changes{'saml'}{$lonhost} = 1;
11810:                 }
11811:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
11812:                     $changes{'saml'}{$lonhost} = 1;
11813:                 }
11814:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
11815:                     $changes{'saml'}{$lonhost} = 1;
11816:                 }
11817:             } else {
11818:                 $changes{'saml'}{$lonhost} = 1;
11819:             }
11820:             foreach my $item ('text','alt','url','title','notsso') {
11821:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
11822:             }
11823:         } else {
11824:             if ($saml{$lonhost}) {  
11825:                 $changes{'saml'}{$lonhost} = 1;
11826:                 delete($currsaml{$lonhost});
11827:             }
11828:         }
11829:     }
11830:     foreach my $posshost (keys(%currsaml)) {
11831:         unless (exists($domservers{$posshost})) { 
11832:             delete($currsaml{$posshost}); 
11833:         }
11834:     }
11835:     %{$loginhash{'login'}{'saml'}} = %currsaml;
11836:     if (@newsamlimgs) {
11837:         my $error;
11838:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11839:         if ($configuserok eq 'ok') {
11840:             if ($switchserver) {
11841:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
11842:             } elsif ($author_ok eq 'ok') {
11843:                 foreach my $lonhost (@newsamlimgs) {
11844:                     my $formelem = 'saml_img_'.$lonhost;
11845:                     my ($result,$imgurl) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11846:                                                         "login/saml/$lonhost",'','',
11847:                                                         $env{'form.saml_img_'.$lonhost.'.filename'});
11848:                     if ($result eq 'ok') {
11849:                         $currsaml{$lonhost}{'img'} = $imgurl;
11850:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
11851:                         $changes{'saml'}{$lonhost} = 1;
11852:                     } else {
11853:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
11854:                                            $lonhost,$result);
11855:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11856:                     }
11857:                 }
11858:             } else {
11859:                 $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);
11860:             }
11861:         } else {
11862:             $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);
11863:         }
11864:         if ($error) {
11865:             &Apache::lonnet::logthis($error);
11866:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11867:         }
11868:     }
11869:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11870: 
11871:     my $defaulthelpfile = '/adm/loginproblems.html';
11872:     my $defaulttext = &mt('Default in use');
11873: 
11874:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11875:                                              $dom);
11876:     if ($putresult eq 'ok') {
11877:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11878:         my %defaultchecked = (
11879:                     'coursecatalog' => 'on',
11880:                     'helpdesk'      => 'on',
11881:                     'adminmail'     => 'off',
11882:                     'newuser'       => 'off',
11883:         );
11884:         if (ref($domconfig{'login'}) eq 'HASH') {
11885:             foreach my $item (@toggles) {
11886:                 if ($defaultchecked{$item} eq 'on') { 
11887:                     if (($domconfig{'login'}{$item} eq '0') &&
11888:                         ($env{'form.'.$item} eq '1')) {
11889:                         $changes{$item} = 1;
11890:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11891:                               $domconfig{'login'}{$item} eq '1') &&
11892:                              ($env{'form.'.$item} eq '0')) {
11893:                         $changes{$item} = 1;
11894:                     }
11895:                 } elsif ($defaultchecked{$item} eq 'off') {
11896:                     if (($domconfig{'login'}{$item} eq '1') &&
11897:                         ($env{'form.'.$item} eq '0')) {
11898:                         $changes{$item} = 1;
11899:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11900:                               $domconfig{'login'}{$item} eq '0') &&
11901:                              ($env{'form.'.$item} eq '1')) {
11902:                         $changes{$item} = 1;
11903:                     }
11904:                 }
11905:             }
11906:         }
11907:         if (keys(%changes) > 0 || $colchgtext) {
11908:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11909:             if (exists($changes{'saml'})) {
11910:                 my $hostid_in_use;
11911:                 my @hosts = &Apache::lonnet::current_machine_ids();
11912:                 if (@hosts > 1) {
11913:                     foreach my $hostid (@hosts) {
11914:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
11915:                             $hostid_in_use = $hostid;
11916:                             last;
11917:                         }
11918:                     }
11919:                 } else {
11920:                     $hostid_in_use = $r->dir_config('lonHostID');
11921:                 }
11922:                 if (($hostid_in_use) &&
11923:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
11924:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
11925:                 }
11926:                 if (ref($lastactref) eq 'HASH') {
11927:                     if (ref($changes{'saml'}) eq 'HASH') {
11928:                         my %updates;
11929:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
11930:                         $lastactref->{'samllanding'} = \%updates;
11931:                     }
11932:                 }
11933:             }
11934:             if (ref($lastactref) eq 'HASH') {
11935:                 $lastactref->{'domainconfig'} = 1;
11936:             }
11937:             $resulttext = &mt('Changes made:').'<ul>';
11938:             foreach my $item (sort(keys(%changes))) {
11939:                 if ($item eq 'loginvia') {
11940:                     if (ref($changes{$item}) eq 'HASH') {
11941:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11942:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11943:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11944:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11945:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11946:                                     $protocol = 'http' if ($protocol ne 'https');
11947:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11948: 
11949:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11950:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11951:                                     } else {
11952:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11953:                                     }
11954:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11955:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11956:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11957:                                     }
11958:                                     $resulttext .= '</li>';
11959:                                 } else {
11960:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11961:                                 }
11962:                             } else {
11963:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11964:                             }
11965:                         }
11966:                         $resulttext .= '</ul></li>';
11967:                     }
11968:                 } elsif ($item eq 'helpurl') {
11969:                     if (ref($changes{$item}) eq 'HASH') {
11970:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11971:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11972:                                 my ($chg,$link);
11973:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11974:                                 if ($lang eq 'nolang') {
11975:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11976:                                 } else {
11977:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11978:                                 }
11979:                                 $resulttext .= '<li>'.$chg.'</li>';
11980:                             } else {
11981:                                 my $chg;
11982:                                 if ($lang eq 'nolang') {
11983:                                     $chg = &mt('custom log-in help file for no preferred language');
11984:                                 } else {
11985:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11986:                                 }
11987:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11988:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11989:                                                       '?inhibitmenu=yes',$chg,600,500).
11990:                                                '</li>';
11991:                             }
11992:                         }
11993:                     }
11994:                 } elsif ($item eq 'headtag') {
11995:                     if (ref($changes{$item}) eq 'HASH') {
11996:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11997:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11998:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11999:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12000:                                 $resulttext .= '<li><a href="'.
12001:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12002:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12003:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12004:                                 if ($possexempt{$lonhost}) {
12005:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12006:                                 } else {
12007:                                     $resulttext .= &mt('included for any client IP');
12008:                                 }
12009:                                 $resulttext .= '</li>';
12010:                             }
12011:                         }
12012:                     }
12013:                 } elsif ($item eq 'saml') {
12014:                     if (ref($changes{$item}) eq 'HASH') {
12015:                         my %notlt = (
12016:                                        text   => 'Text for log-in by SSO',
12017:                                        img    => 'SSO button image',
12018:                                        alt    => 'Alt text for button image',
12019:                                        url    => 'SSO URL',
12020:                                        title  => 'Tooltip for SSO link',
12021:                                        notsso => 'Text for non-SSO log-in',
12022:                                     );
12023:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12024:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12025:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12026:                                                '<ul>';
12027:                                 foreach my $key ('text','img','alt','url','title','notsso') {
12028:                                     if ($currsaml{$lonhost}{$key} eq '') {
12029:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12030:                                     } else {
12031:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12032:                                         if ($key eq 'img') {
12033:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12034:                                         }
12035:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12036:                                                                   $value).'</li>';
12037:                                     }
12038:                                 }
12039:                                 $resulttext .= '</ul></li>';
12040:                             } else {
12041:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12042:                             }
12043:                         }
12044:                     }
12045:                 } elsif ($item eq 'captcha') {
12046:                     if (ref($loginhash{'login'}) eq 'HASH') {
12047:                         my $chgtxt;
12048:                         if ($loginhash{'login'}{$item} eq 'notused') {
12049:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12050:                         } else {
12051:                             my %captchas = &captcha_phrases();
12052:                             if ($captchas{$loginhash{'login'}{$item}}) {
12053:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12054:                             } else {
12055:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12056:                             }
12057:                         }
12058:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12059:                     }
12060:                 } elsif ($item eq 'recaptchakeys') {
12061:                     if (ref($loginhash{'login'}) eq 'HASH') {
12062:                         my ($privkey,$pubkey);
12063:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12064:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12065:                             $privkey = $loginhash{'login'}{$item}{'private'};
12066:                         }
12067:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12068:                         if (!$pubkey) {
12069:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12070:                         } else {
12071:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12072:                         }
12073:                         if (!$privkey) {
12074:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12075:                         } else {
12076:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12077:                         }
12078:                         $chgtxt .= '</ul>';
12079:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12080:                     }
12081:                 } elsif ($item eq 'recaptchaversion') {
12082:                     if (ref($loginhash{'login'}) eq 'HASH') {
12083:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12084:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12085:                                            '</li>';
12086:                         }
12087:                     }
12088:                 } else {
12089:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12090:                 }
12091:             }
12092:             $resulttext .= $colchgtext.'</ul>';
12093:         } else {
12094:             $resulttext = &mt('No changes made to log-in page settings');
12095:         }
12096:     } else {
12097:         $resulttext = '<span class="LC_error">'.
12098: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12099:     }
12100:     if ($errors) {
12101:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12102:                        $errors.'</ul>';
12103:     }
12104:     return $resulttext;
12105: }
12106: 
12107: sub check_exempt_addresses {
12108:     my ($iplist) = @_;
12109:     $iplist =~ s/^\s+//;
12110:     $iplist =~ s/\s+$//;
12111:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12112:     my (@okips,$new);
12113:     foreach my $ip (@poss_ips) {
12114:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12115:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12116:                 push(@okips,$ip);
12117:             }
12118:         }
12119:     }
12120:     if (@okips > 0) {
12121:         $new = join(',',@okips);
12122:     } else {
12123:         $new = '';
12124:     }
12125:     return $new;
12126: }
12127: 
12128: sub color_font_choices {
12129:     my %choices =
12130:         &Apache::lonlocal::texthash (
12131:             img => "Header",
12132:             bgs => "Background colors",
12133:             links => "Link colors",
12134:             images => "Images",
12135:             font => "Font color",
12136:             fontmenu => "Font menu",
12137:             pgbg => "Page",
12138:             tabbg => "Header",
12139:             sidebg => "Border",
12140:             link => "Link",
12141:             alink => "Active link",
12142:             vlink => "Visited link",
12143:         );
12144:     return %choices;
12145: }
12146: 
12147: sub modify_rolecolors {
12148:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12149:     my ($resulttext,%rolehash);
12150:     $rolehash{'rolecolors'} = {};
12151:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12152:         if ($domconfig{'rolecolors'} eq '') {
12153:             $domconfig{'rolecolors'} = {};
12154:         }
12155:     }
12156:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12157:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12158:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12159:                                              $dom);
12160:     if ($putresult eq 'ok') {
12161:         if (keys(%changes) > 0) {
12162:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12163:             if (ref($lastactref) eq 'HASH') {
12164:                 $lastactref->{'domainconfig'} = 1;
12165:             }
12166:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12167:                                              $rolehash{'rolecolors'});
12168:         } else {
12169:             $resulttext = &mt('No changes made to default color schemes');
12170:         }
12171:     } else {
12172:         $resulttext = '<span class="LC_error">'.
12173: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12174:     }
12175:     if ($errors) {
12176:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12177:                        $errors.'</ul>';
12178:     }
12179:     return $resulttext;
12180: }
12181: 
12182: sub modify_colors {
12183:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12184:     my (%changes,%choices);
12185:     my @bgs;
12186:     my @links = ('link','alink','vlink');
12187:     my @logintext;
12188:     my @images;
12189:     my $servadm = $r->dir_config('lonAdmEMail');
12190:     my $errors;
12191:     my %defaults;
12192:     foreach my $role (@{$roles}) {
12193:         if ($role eq 'login') {
12194:             %choices = &login_choices();
12195:             @logintext = ('textcol','bgcol');
12196:         } else {
12197:             %choices = &color_font_choices();
12198:         }
12199:         if ($role eq 'login') {
12200:             @images = ('img','logo','domlogo','login');
12201:             @bgs = ('pgbg','mainbg','sidebg');
12202:         } else {
12203:             @images = ('img');
12204:             @bgs = ('pgbg','tabbg','sidebg');
12205:         }
12206:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12207:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
12208:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
12209:         }
12210:         if ($role eq 'login') {
12211:             foreach my $item (@logintext) {
12212:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12213:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12214:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12215:                 }
12216:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
12217:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12218:                 }
12219:             }
12220:         } else {
12221:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
12222:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
12223:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
12224:             }
12225:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
12226:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
12227:             }
12228:         }
12229:         foreach my $item (@bgs) {
12230:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12231:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12232:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12233:             }
12234:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
12235:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12236:             }
12237:         }
12238:         foreach my $item (@links) {
12239:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12240:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12241:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12242:             }
12243:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
12244:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12245:             }
12246:         }
12247:         my ($configuserok,$author_ok,$switchserver) = 
12248:             &config_check($dom,$confname,$servadm);
12249:         my ($width,$height) = &thumb_dimensions();
12250:         if (ref($domconfig->{$role}) ne 'HASH') {
12251:             $domconfig->{$role} = {};
12252:         }
12253:         foreach my $img (@images) {
12254:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
12255:                 if (defined($env{'form.login_showlogo_'.$img})) {
12256:                     $confhash->{$role}{'showlogo'}{$img} = 1;
12257:                 } else { 
12258:                     $confhash->{$role}{'showlogo'}{$img} = 0;
12259:                 }
12260:             } 
12261: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
12262: 		 && !defined($domconfig->{$role}{$img})
12263: 		 && !$env{'form.'.$role.'_del_'.$img}
12264: 		 && $env{'form.'.$role.'_import_'.$img}) {
12265: 		# import the old configured image from the .tab setting
12266: 		# if they haven't provided a new one 
12267: 		$domconfig->{$role}{$img} = 
12268: 		    $env{'form.'.$role.'_import_'.$img};
12269: 	    }
12270:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
12271:                 my $error;
12272:                 if ($configuserok eq 'ok') {
12273:                     if ($switchserver) {
12274:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
12275:                     } else {
12276:                         if ($author_ok eq 'ok') {
12277:                             my ($result,$logourl) = 
12278:                                 &publishlogo($r,'upload',$role.'_'.$img,
12279:                                            $dom,$confname,$img,$width,$height);
12280:                             if ($result eq 'ok') {
12281:                                 $confhash->{$role}{$img} = $logourl;
12282:                                 $changes{$role}{'images'}{$img} = 1;
12283:                             } else {
12284:                                 $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);
12285:                             }
12286:                         } else {
12287:                             $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);
12288:                         }
12289:                     }
12290:                 } else {
12291:                     $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);
12292:                 }
12293:                 if ($error) {
12294:                     &Apache::lonnet::logthis($error);
12295:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12296:                 }
12297:             } elsif ($domconfig->{$role}{$img} ne '') {
12298:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
12299:                     my $error;
12300:                     if ($configuserok eq 'ok') {
12301: # is confname an author?
12302:                         if ($switchserver eq '') {
12303:                             if ($author_ok eq 'ok') {
12304:                                 my ($result,$logourl) = 
12305:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
12306:                                             $dom,$confname,$img,$width,$height);
12307:                                 if ($result eq 'ok') {
12308:                                     $confhash->{$role}{$img} = $logourl;
12309: 				    $changes{$role}{'images'}{$img} = 1;
12310:                                 }
12311:                             }
12312:                         }
12313:                     }
12314:                 }
12315:             }
12316:         }
12317:         if (ref($domconfig) eq 'HASH') {
12318:             if (ref($domconfig->{$role}) eq 'HASH') {
12319:                 foreach my $img (@images) {
12320:                     if ($domconfig->{$role}{$img} ne '') {
12321:                         if ($env{'form.'.$role.'_del_'.$img}) {
12322:                             $confhash->{$role}{$img} = '';
12323:                             $changes{$role}{'images'}{$img} = 1;
12324:                         } else {
12325:                             if ($confhash->{$role}{$img} eq '') {
12326:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
12327:                             }
12328:                         }
12329:                     } else {
12330:                         if ($env{'form.'.$role.'_del_'.$img}) {
12331:                             $confhash->{$role}{$img} = '';
12332:                             $changes{$role}{'images'}{$img} = 1;
12333:                         } 
12334:                     }
12335:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
12336:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
12337:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
12338:                                 $domconfig->{$role}{'showlogo'}{$img}) {
12339:                                 $changes{$role}{'showlogo'}{$img} = 1; 
12340:                             }
12341:                         } else {
12342:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12343:                                 $changes{$role}{'showlogo'}{$img} = 1;
12344:                             }
12345:                         }
12346:                     }
12347:                 }
12348:                 if ($domconfig->{$role}{'font'} ne '') {
12349:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
12350:                         $changes{$role}{'font'} = 1;
12351:                     }
12352:                 } else {
12353:                     if ($confhash->{$role}{'font'}) {
12354:                         $changes{$role}{'font'} = 1;
12355:                     }
12356:                 }
12357:                 if ($role ne 'login') {
12358:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
12359:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
12360:                             $changes{$role}{'fontmenu'} = 1;
12361:                         }
12362:                     } else {
12363:                         if ($confhash->{$role}{'fontmenu'}) {
12364:                             $changes{$role}{'fontmenu'} = 1;
12365:                         }
12366:                     }
12367:                 }
12368:                 foreach my $item (@bgs) {
12369:                     if ($domconfig->{$role}{$item} ne '') {
12370:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12371:                             $changes{$role}{'bgs'}{$item} = 1;
12372:                         } 
12373:                     } else {
12374:                         if ($confhash->{$role}{$item}) {
12375:                             $changes{$role}{'bgs'}{$item} = 1;
12376:                         }
12377:                     }
12378:                 }
12379:                 foreach my $item (@links) {
12380:                     if ($domconfig->{$role}{$item} ne '') {
12381:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12382:                             $changes{$role}{'links'}{$item} = 1;
12383:                         }
12384:                     } else {
12385:                         if ($confhash->{$role}{$item}) {
12386:                             $changes{$role}{'links'}{$item} = 1;
12387:                         }
12388:                     }
12389:                 }
12390:                 foreach my $item (@logintext) {
12391:                     if ($domconfig->{$role}{$item} ne '') {
12392:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
12393:                             $changes{$role}{'logintext'}{$item} = 1;
12394:                         }
12395:                     } else {
12396:                         if ($confhash->{$role}{$item}) {
12397:                             $changes{$role}{'logintext'}{$item} = 1;
12398:                         }
12399:                     }
12400:                 }
12401:             } else {
12402:                 &default_change_checker($role,\@images,\@links,\@bgs,
12403:                                         \@logintext,$confhash,\%changes); 
12404:             }
12405:         } else {
12406:             &default_change_checker($role,\@images,\@links,\@bgs,
12407:                                     \@logintext,$confhash,\%changes); 
12408:         }
12409:     }
12410:     return ($errors,%changes);
12411: }
12412: 
12413: sub config_check {
12414:     my ($dom,$confname,$servadm) = @_;
12415:     my ($configuserok,$author_ok,$switchserver,%currroles);
12416:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12417:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12418:                                                    $confname,$servadm);
12419:     if ($configuserok eq 'ok') {
12420:         $switchserver = &check_switchserver($dom,$confname);
12421:         if ($switchserver eq '') {
12422:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12423:         }
12424:     }
12425:     return ($configuserok,$author_ok,$switchserver);
12426: }
12427: 
12428: sub default_change_checker {
12429:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12430:     foreach my $item (@{$links}) {
12431:         if ($confhash->{$role}{$item}) {
12432:             $changes->{$role}{'links'}{$item} = 1;
12433:         }
12434:     }
12435:     foreach my $item (@{$bgs}) {
12436:         if ($confhash->{$role}{$item}) {
12437:             $changes->{$role}{'bgs'}{$item} = 1;
12438:         }
12439:     }
12440:     foreach my $item (@{$logintext}) {
12441:         if ($confhash->{$role}{$item}) {
12442:             $changes->{$role}{'logintext'}{$item} = 1;
12443:         }
12444:     }
12445:     foreach my $img (@{$images}) {
12446:         if ($env{'form.'.$role.'_del_'.$img}) {
12447:             $confhash->{$role}{$img} = '';
12448:             $changes->{$role}{'images'}{$img} = 1;
12449:         }
12450:         if ($role eq 'login') {
12451:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12452:                 $changes->{$role}{'showlogo'}{$img} = 1;
12453:             }
12454:         }
12455:     }
12456:     if ($confhash->{$role}{'font'}) {
12457:         $changes->{$role}{'font'} = 1;
12458:     }
12459: }
12460: 
12461: sub display_colorchgs {
12462:     my ($dom,$changes,$roles,$confhash) = @_;
12463:     my (%choices,$resulttext);
12464:     if (!grep(/^login$/,@{$roles})) {
12465:         $resulttext = &mt('Changes made:').'<br />';
12466:     }
12467:     foreach my $role (@{$roles}) {
12468:         if ($role eq 'login') {
12469:             %choices = &login_choices();
12470:         } else {
12471:             %choices = &color_font_choices();
12472:         }
12473:         if (ref($changes->{$role}) eq 'HASH') {
12474:             if ($role ne 'login') {
12475:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
12476:             }
12477:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
12478:                 if ($role ne 'login') {
12479:                     $resulttext .= '<ul>';
12480:                 }
12481:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
12482:                     if ($role ne 'login') {
12483:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
12484:                     }
12485:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
12486:                         if (($role eq 'login') && ($key eq 'showlogo')) {
12487:                             if ($confhash->{$role}{$key}{$item}) {
12488:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
12489:                             } else {
12490:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
12491:                             }
12492:                         } elsif ($confhash->{$role}{$item} eq '') {
12493:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
12494:                         } else {
12495:                             my $newitem = $confhash->{$role}{$item};
12496:                             if ($key eq 'images') {
12497:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
12498:                             }
12499:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
12500:                         }
12501:                     }
12502:                     if ($role ne 'login') {
12503:                         $resulttext .= '</ul></li>';
12504:                     }
12505:                 } else {
12506:                     if ($confhash->{$role}{$key} eq '') {
12507:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
12508:                     } else {
12509:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
12510:                     }
12511:                 }
12512:                 if ($role ne 'login') {
12513:                     $resulttext .= '</ul>';
12514:                 }
12515:             }
12516:         }
12517:     }
12518:     return $resulttext;
12519: }
12520: 
12521: sub thumb_dimensions {
12522:     return ('200','50');
12523: }
12524: 
12525: sub check_dimensions {
12526:     my ($inputfile) = @_;
12527:     my ($fullwidth,$fullheight);
12528:     if ($inputfile =~ m|^[/\w.\-]+$|) {
12529:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
12530:             my $imageinfo = <PIPE>;
12531:             if (!close(PIPE)) {
12532:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
12533:             }
12534:             chomp($imageinfo);
12535:             my ($fullsize) = 
12536:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
12537:             if ($fullsize) {
12538:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
12539:             }
12540:         }
12541:     }
12542:     return ($fullwidth,$fullheight);
12543: }
12544: 
12545: sub check_configuser {
12546:     my ($uhome,$dom,$confname,$servadm) = @_;
12547:     my ($configuserok,%currroles);
12548:     if ($uhome eq 'no_host') {
12549:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
12550:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
12551:         $configuserok = 
12552:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
12553:                              $configpass,'','','','','',undef,$servadm);
12554:     } else {
12555:         $configuserok = 'ok';
12556:         %currroles = 
12557:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
12558:     }
12559:     return ($configuserok,%currroles);
12560: }
12561: 
12562: sub check_authorstatus {
12563:     my ($dom,$confname,%currroles) = @_;
12564:     my $author_ok;
12565:     if (!$currroles{':'.$dom.':au'}) {
12566:         my $start = time;
12567:         my $end = 0;
12568:         $author_ok = 
12569:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
12570:                                         'au',$end,$start,'','','domconfig');
12571:     } else {
12572:         $author_ok = 'ok';
12573:     }
12574:     return $author_ok;
12575: }
12576: 
12577: sub publishlogo {
12578:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
12579:     my ($output,$fname,$logourl,$madethumb);
12580:     if ($action eq 'upload') {
12581:         $fname=$env{'form.'.$formname.'.filename'};
12582:         chop($env{'form.'.$formname});
12583:     } else {
12584:         ($fname) = ($formname =~ /([^\/]+)$/);
12585:     }
12586:     if ($savefileas ne '') {
12587:         $fname = $savefileas;
12588:     }
12589:     $fname=&Apache::lonnet::clean_filename($fname);
12590: # See if there is anything left
12591:     unless ($fname) { return ('error: no uploaded file'); }
12592:     $fname="$subdir/$fname";
12593:     my $docroot=$r->dir_config('lonDocRoot');
12594:     my $filepath="$docroot/priv";
12595:     my $relpath = "$dom/$confname";
12596:     my ($fnamepath,$file,$fetchthumb);
12597:     $file=$fname;
12598:     if ($fname=~m|/|) {
12599:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
12600:     }
12601:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
12602:     my $count;
12603:     for ($count=5;$count<=$#parts;$count++) {
12604:         $filepath.="/$parts[$count]";
12605:         if ((-e $filepath)!=1) {
12606:             mkdir($filepath,02770);
12607:         }
12608:     }
12609:     # Check for bad extension and disallow upload
12610:     if ($file=~/\.(\w+)$/ &&
12611:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
12612:         $output = 
12613:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
12614:     } elsif ($file=~/\.(\w+)$/ &&
12615:         !defined(&Apache::loncommon::fileembstyle($1))) {
12616:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
12617:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
12618:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
12619:     } elsif (-d "$filepath/$file") {
12620:         $output = &mt('Filename is a directory name - rename the file and re-upload');
12621:     } else {
12622:         my $source = $filepath.'/'.$file;
12623:         my $logfile;
12624:         if (!open($logfile,">>",$source.'.log')) {
12625:             return (&mt('No write permission to Authoring Space'));
12626:         }
12627:         print $logfile
12628: "\n================= Publish ".localtime()." ================\n".
12629: $env{'user.name'}.':'.$env{'user.domain'}."\n";
12630: # Save the file
12631:         if (!open(FH,">",$source)) {
12632:             &Apache::lonnet::logthis('Failed to create '.$source);
12633:             return (&mt('Failed to create file'));
12634:         }
12635:         if ($action eq 'upload') {
12636:             if (!print FH ($env{'form.'.$formname})) {
12637:                 &Apache::lonnet::logthis('Failed to write to '.$source);
12638:                 return (&mt('Failed to write file'));
12639:             }
12640:         } else {
12641:             my $original = &Apache::lonnet::filelocation('',$formname);
12642:             if(!copy($original,$source)) {
12643:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
12644:                 return (&mt('Failed to write file'));
12645:             }
12646:         }
12647:         close(FH);
12648:         chmod(0660, $source); # Permissions to rw-rw---.
12649: 
12650:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
12651:         my $copyfile=$targetdir.'/'.$file;
12652: 
12653:         my @parts=split(/\//,$targetdir);
12654:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
12655:         for (my $count=5;$count<=$#parts;$count++) {
12656:             $path.="/$parts[$count]";
12657:             if (!-e $path) {
12658:                 print $logfile "\nCreating directory ".$path;
12659:                 mkdir($path,02770);
12660:             }
12661:         }
12662:         my $versionresult;
12663:         if (-e $copyfile) {
12664:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
12665:         } else {
12666:             $versionresult = 'ok';
12667:         }
12668:         if ($versionresult eq 'ok') {
12669:             if (copy($source,$copyfile)) {
12670:                 print $logfile "\nCopied original source to ".$copyfile."\n";
12671:                 $output = 'ok';
12672:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
12673:                 push(@{$modified_urls},[$copyfile,$source]);
12674:                 my $metaoutput = 
12675:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
12676:                 unless ($registered_cleanup) {
12677:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
12678:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12679:                     $registered_cleanup=1;
12680:                 }
12681:             } else {
12682:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
12683:                 $output = &mt('Failed to copy file to RES space').", $!";
12684:             }
12685:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
12686:                 my $inputfile = $filepath.'/'.$file;
12687:                 my $outfile = $filepath.'/'.'tn-'.$file;
12688:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
12689:                 if ($fullwidth ne '' && $fullheight ne '') { 
12690:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
12691:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
12692:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
12693:                         system({$args[0]} @args);
12694:                         chmod(0660, $filepath.'/tn-'.$file);
12695:                         if (-e $outfile) {
12696:                             my $copyfile=$targetdir.'/tn-'.$file;
12697:                             if (copy($outfile,$copyfile)) {
12698:                                 print $logfile "\nCopied source to ".$copyfile."\n";
12699:                                 my $thumb_metaoutput = 
12700:                                     &write_metadata($dom,$confname,$formname,
12701:                                                     $targetdir,'tn-'.$file,$logfile);
12702:                                 push(@{$modified_urls},[$copyfile,$outfile]);
12703:                                 unless ($registered_cleanup) {
12704:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
12705:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12706:                                     $registered_cleanup=1;
12707:                                 }
12708:                                 $madethumb = 1;
12709:                             } else {
12710:                                 print $logfile "\nUnable to write ".$copyfile.
12711:                                                ':'.$!."\n";
12712:                             }
12713:                         }
12714:                     }
12715:                 }
12716:             }
12717:         } else {
12718:             $output = $versionresult;
12719:         }
12720:     }
12721:     return ($output,$logourl,$madethumb);
12722: }
12723: 
12724: sub logo_versioning {
12725:     my ($targetdir,$file,$logfile) = @_;
12726:     my $target = $targetdir.'/'.$file;
12727:     my ($maxversion,$fn,$extn,$output);
12728:     $maxversion = 0;
12729:     if ($file =~ /^(.+)\.(\w+)$/) {
12730:         $fn=$1;
12731:         $extn=$2;
12732:     }
12733:     opendir(DIR,$targetdir);
12734:     while (my $filename=readdir(DIR)) {
12735:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
12736:             $maxversion=($1>$maxversion)?$1:$maxversion;
12737:         }
12738:     }
12739:     $maxversion++;
12740:     print $logfile "\nCreating old version ".$maxversion."\n";
12741:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
12742:     if (copy($target,$copyfile)) {
12743:         print $logfile "Copied old target to ".$copyfile."\n";
12744:         $copyfile=$copyfile.'.meta';
12745:         if (copy($target.'.meta',$copyfile)) {
12746:             print $logfile "Copied old target metadata to ".$copyfile."\n";
12747:             $output = 'ok';
12748:         } else {
12749:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
12750:             $output = &mt('Failed to copy old meta').", $!, ";
12751:         }
12752:     } else {
12753:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
12754:         $output = &mt('Failed to copy old target').", $!, ";
12755:     }
12756:     return $output;
12757: }
12758: 
12759: sub write_metadata {
12760:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
12761:     my (%metadatafields,%metadatakeys,$output);
12762:     $metadatafields{'title'}=$formname;
12763:     $metadatafields{'creationdate'}=time;
12764:     $metadatafields{'lastrevisiondate'}=time;
12765:     $metadatafields{'copyright'}='public';
12766:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
12767:                                          $env{'user.domain'};
12768:     $metadatafields{'authorspace'}=$confname.':'.$dom;
12769:     $metadatafields{'domain'}=$dom;
12770:     {
12771:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
12772:         my $mfh;
12773:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
12774:             foreach (sort(keys(%metadatafields))) {
12775:                 unless ($_=~/\./) {
12776:                     my $unikey=$_;
12777:                     $unikey=~/^([A-Za-z]+)/;
12778:                     my $tag=$1;
12779:                     $tag=~tr/A-Z/a-z/;
12780:                     print $mfh "\n\<$tag";
12781:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
12782:                         my $value=$metadatafields{$unikey.'.'.$_};
12783:                         $value=~s/\"/\'\'/g;
12784:                         print $mfh ' '.$_.'="'.$value.'"';
12785:                     }
12786:                     print $mfh '>'.
12787:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
12788:                             .'</'.$tag.'>';
12789:                 }
12790:             }
12791:             $output = 'ok';
12792:             print $logfile "\nWrote metadata";
12793:             close($mfh);
12794:         } else {
12795:             print $logfile "\nFailed to open metadata file";
12796:             $output = &mt('Could not write metadata');
12797:         }
12798:     }
12799:     return $output;
12800: }
12801: 
12802: sub notifysubscribed {
12803:     foreach my $targetsource (@{$modified_urls}){
12804:         next unless (ref($targetsource) eq 'ARRAY');
12805:         my ($target,$source)=@{$targetsource};
12806:         if ($source ne '') {
12807:             if (open(my $logfh,">>",$source.'.log')) {
12808:                 print $logfh "\nCleanup phase: Notifications\n";
12809:                 my @subscribed=&subscribed_hosts($target);
12810:                 foreach my $subhost (@subscribed) {
12811:                     print $logfh "\nNotifying host ".$subhost.':';
12812:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
12813:                     print $logfh $reply;
12814:                 }
12815:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
12816:                 foreach my $subhost (@subscribedmeta) {
12817:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
12818:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
12819:                                                         $subhost);
12820:                     print $logfh $reply;
12821:                 }
12822:                 print $logfh "\n============ Done ============\n";
12823:                 close($logfh);
12824:             }
12825:         }
12826:     }
12827:     return OK;
12828: }
12829: 
12830: sub subscribed_hosts {
12831:     my ($target) = @_;
12832:     my @subscribed;
12833:     if (open(my $fh,"<","$target.subscription")) {
12834:         while (my $subline=<$fh>) {
12835:             if ($subline =~ /^($match_lonid):/) {
12836:                 my $host = $1;
12837:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
12838:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
12839:                         push(@subscribed,$host);
12840:                     }
12841:                 }
12842:             }
12843:         }
12844:     }
12845:     return @subscribed;
12846: }
12847: 
12848: sub check_switchserver {
12849:     my ($dom,$confname) = @_;
12850:     my ($allowed,$switchserver);
12851:     my $home = &Apache::lonnet::homeserver($confname,$dom);
12852:     if ($home eq 'no_host') {
12853:         $home = &Apache::lonnet::domain($dom,'primary');
12854:     }
12855:     my @ids=&Apache::lonnet::current_machine_ids();
12856:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
12857:     if (!$allowed) {
12858: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
12859:     }
12860:     return $switchserver;
12861: }
12862: 
12863: sub modify_quotas {
12864:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12865:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
12866:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
12867:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
12868:         $validationfieldsref);
12869:     if ($action eq 'quotas') {
12870:         $context = 'tools'; 
12871:     } else {
12872:         $context = $action;
12873:     }
12874:     if ($context eq 'requestcourses') {
12875:         @usertools = ('official','unofficial','community','textbook','placement','lti');
12876:         @options =('norequest','approval','validate','autolimit');
12877:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
12878:         %titles = &courserequest_titles();
12879:         $toolregexp = join('|',@usertools);
12880:         %conditions = &courserequest_conditions();
12881:         $confname = $dom.'-domainconfig';
12882:         my $servadm = $r->dir_config('lonAdmEMail');
12883:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12884:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
12885:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12886:     } elsif ($context eq 'requestauthor') {
12887:         @usertools = ('author');
12888:         %titles = &authorrequest_titles();
12889:     } else {
12890:         @usertools = ('aboutme','blog','webdav','portfolio');
12891:         %titles = &tool_titles();
12892:     }
12893:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12894:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12895:     foreach my $key (keys(%env)) {
12896:         if ($context eq 'requestcourses') {
12897:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12898:                 my $item = $1;
12899:                 my $type = $2;
12900:                 if ($type =~ /^limit_(.+)/) {
12901:                     $limithash{$item}{$1} = $env{$key};
12902:                 } else {
12903:                     $confhash{$item}{$type} = $env{$key};
12904:                 }
12905:             }
12906:         } elsif ($context eq 'requestauthor') {
12907:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12908:                 $confhash{$1} = $env{$key};
12909:             }
12910:         } else {
12911:             if ($key =~ /^form\.quota_(.+)$/) {
12912:                 $confhash{'defaultquota'}{$1} = $env{$key};
12913:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
12914:                 $confhash{'authorquota'}{$1} = $env{$key};
12915:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12916:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12917:             }
12918:         }
12919:     }
12920:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12921:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12922:         @approvalnotify = sort(@approvalnotify);
12923:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12924:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
12925:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12926:         foreach my $type (@hasuniquecode) {
12927:             if (grep(/^\Q$type\E$/,@crstypes)) {
12928:                 $confhash{'uniquecode'}{$type} = 1;
12929:             }
12930:         }
12931:         my (%newbook,%allpos);
12932:         if ($context eq 'requestcourses') {
12933:             foreach my $type ('textbooks','templates') {
12934:                 @{$allpos{$type}} = (); 
12935:                 my $invalid;
12936:                 if ($type eq 'textbooks') {
12937:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12938:                 } else {
12939:                     $invalid = &mt('Invalid LON-CAPA course for template');
12940:                 }
12941:                 if ($env{'form.'.$type.'_addbook'}) {
12942:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12943:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12944:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12945:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12946:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12947:                         } else {
12948:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12949:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12950:                             $position =~ s/\D+//g;
12951:                             if ($position ne '') {
12952:                                 $allpos{$type}[$position] = $newbook{$type};
12953:                             }
12954:                         }
12955:                     } else {
12956:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12957:                     }
12958:                 }
12959:             } 
12960:         }
12961:         if (ref($domconfig{$action}) eq 'HASH') {
12962:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12963:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12964:                     $changes{'notify'}{'approval'} = 1;
12965:                 }
12966:             } else {
12967:                 if ($confhash{'notify'}{'approval'}) {
12968:                     $changes{'notify'}{'approval'} = 1;
12969:                 }
12970:             }
12971:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12972:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12973:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12974:                         unless ($confhash{'uniquecode'}{$crstype}) {
12975:                             $changes{'uniquecode'} = 1;
12976:                         }
12977:                     }
12978:                     unless ($changes{'uniquecode'}) {
12979:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12980:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12981:                                 $changes{'uniquecode'} = 1;
12982:                             }
12983:                         }
12984:                     }
12985:                } else {
12986:                    $changes{'uniquecode'} = 1;
12987:                }
12988:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12989:                 $changes{'uniquecode'} = 1;
12990:             }
12991:             if ($context eq 'requestcourses') {
12992:                 foreach my $type ('textbooks','templates') {
12993:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12994:                         my %deletions;
12995:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12996:                         if (@todelete) {
12997:                             map { $deletions{$_} = 1; } @todelete;
12998:                         }
12999:                         my %imgdeletions;
13000:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
13001:                         if (@todeleteimages) {
13002:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
13003:                         }
13004:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
13005:                         for (my $i=0; $i<=$maxnum; $i++) {
13006:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
13007:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
13008:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
13009:                                 if ($deletions{$key}) {
13010:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
13011:                                         #FIXME need to obsolete item in RES space
13012:                                     }
13013:                                     next;
13014:                                 } else {
13015:                                     my $newpos = $env{'form.'.$itemid};
13016:                                     $newpos =~ s/\D+//g;
13017:                                     foreach my $item ('subject','title','publisher','author') {
13018:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13019:                                                  ($type eq 'templates'));
13020:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
13021:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
13022:                                             $changes{$type}{$key} = 1;
13023:                                         }
13024:                                     }
13025:                                     $allpos{$type}[$newpos] = $key;
13026:                                 }
13027:                                 if ($imgdeletions{$key}) {
13028:                                     $changes{$type}{$key} = 1;
13029:                                     #FIXME need to obsolete item in RES space
13030:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
13031:                                     my ($cdom,$cnum) = split(/_/,$key);
13032:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13033:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13034:                                     } else {
13035:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
13036:                                                                                       $cdom,$cnum,$type,$configuserok,
13037:                                                                                       $switchserver,$author_ok);
13038:                                         if ($imgurl) {
13039:                                             $confhash{$type}{$key}{'image'} = $imgurl;
13040:                                             $changes{$type}{$key} = 1; 
13041:                                         }
13042:                                         if ($error) {
13043:                                             &Apache::lonnet::logthis($error);
13044:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13045:                                         }
13046:                                     }
13047:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
13048:                                     $confhash{$type}{$key}{'image'} = 
13049:                                         $domconfig{$action}{$type}{$key}{'image'};
13050:                                 }
13051:                             }
13052:                         }
13053:                     }
13054:                 }
13055:             }
13056:         } else {
13057:             if ($confhash{'notify'}{'approval'}) {
13058:                 $changes{'notify'}{'approval'} = 1;
13059:             }
13060:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
13061:                 $changes{'uniquecode'} = 1;
13062:             }
13063:         }
13064:         if ($context eq 'requestcourses') {
13065:             foreach my $type ('textbooks','templates') {
13066:                 if ($newbook{$type}) {
13067:                     $changes{$type}{$newbook{$type}} = 1;
13068:                     foreach my $item ('subject','title','publisher','author') {
13069:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13070:                                  ($type eq 'template'));
13071:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
13072:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
13073:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
13074:                         }
13075:                     }
13076:                     if ($type eq 'textbooks') {
13077:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
13078:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
13079:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13080:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13081:                             } else {
13082:                                 my ($imageurl,$error) =
13083:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
13084:                                                             $configuserok,$switchserver,$author_ok);
13085:                                 if ($imageurl) {
13086:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13087:                                 }
13088:                                 if ($error) {
13089:                                     &Apache::lonnet::logthis($error);
13090:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13091:                                 }
13092:                             }
13093:                         }
13094:                     }
13095:                 }
13096:                 if (@{$allpos{$type}} > 0) {
13097:                     my $idx = 0;
13098:                     foreach my $item (@{$allpos{$type}}) {
13099:                         if ($item ne '') {
13100:                             $confhash{$type}{$item}{'order'} = $idx;
13101:                             if (ref($domconfig{$action}) eq 'HASH') {
13102:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13103:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13104:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13105:                                             $changes{$type}{$item} = 1;
13106:                                         }
13107:                                     }
13108:                                 }
13109:                             }
13110:                             $idx ++;
13111:                         }
13112:                     }
13113:                 }
13114:             }
13115:             if (ref($validationitemsref) eq 'ARRAY') {
13116:                 foreach my $item (@{$validationitemsref}) {
13117:                     if ($item eq 'fields') {
13118:                         my @changed;
13119:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13120:                         if (@{$confhash{'validation'}{$item}} > 0) {
13121:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13122:                         }
13123:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13124:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13125:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13126:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13127:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13128:                                 } else {
13129:                                     @changed = @{$confhash{'validation'}{$item}};
13130:                                 }
13131:                             } else {
13132:                                 @changed = @{$confhash{'validation'}{$item}};
13133:                             }
13134:                         } else {
13135:                             @changed = @{$confhash{'validation'}{$item}};
13136:                         }
13137:                         if (@changed) {
13138:                             if ($confhash{'validation'}{$item}) {
13139:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13140:                             } else {
13141:                                 $changes{'validation'}{$item} = &mt('None');
13142:                             }
13143:                         }
13144:                     } else {
13145:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13146:                         if ($item eq 'markup') {
13147:                             if ($env{'form.requestcourses_validation_'.$item}) {
13148:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13149:                             }
13150:                         }
13151:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13152:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13153:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13154:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13155:                                 }
13156:                             } else {
13157:                                 if ($confhash{'validation'}{$item} ne '') {
13158:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13159:                                 }
13160:                             }
13161:                         } else {
13162:                             if ($confhash{'validation'}{$item} ne '') {
13163:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13164:                             }
13165:                         }
13166:                     }
13167:                 }
13168:             }
13169:             if ($env{'form.validationdc'}) {
13170:                 my $newval = $env{'form.validationdc'};
13171:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13172:                 if (exists($domcoords{$newval})) {
13173:                     $confhash{'validation'}{'dc'} = $newval;
13174:                 }
13175:             }
13176:             if (ref($confhash{'validation'}) eq 'HASH') {
13177:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13178:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13179:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13180:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13181:                                 if ($confhash{'validation'}{'dc'} eq '') {
13182:                                     $changes{'validation'}{'dc'} = &mt('None');
13183:                                 } else {
13184:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13185:                                 }
13186:                             }
13187:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13188:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13189:                         }
13190:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13191:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13192:                     }
13193:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13194:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13195:                 }
13196:             } else {
13197:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13198:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13199:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13200:                             $changes{'validation'}{'dc'} = &mt('None');
13201:                         }
13202:                     }
13203:                 }
13204:             }
13205:         }
13206:     } else {
13207:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13208:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
13209:     }
13210:     foreach my $item (@usertools) {
13211:         foreach my $type (@{$types},'default','_LC_adv') {
13212:             my $unset; 
13213:             if ($context eq 'requestcourses') {
13214:                 $unset = '0';
13215:                 if ($type eq '_LC_adv') {
13216:                     $unset = '';
13217:                 }
13218:                 if ($confhash{$item}{$type} eq 'autolimit') {
13219:                     $confhash{$item}{$type} .= '=';
13220:                     unless ($limithash{$item}{$type} =~ /\D/) {
13221:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13222:                     }
13223:                 }
13224:             } elsif ($context eq 'requestauthor') {
13225:                 $unset = '0';
13226:                 if ($type eq '_LC_adv') {
13227:                     $unset = '';
13228:                 }
13229:             } else {
13230:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13231:                     $confhash{$item}{$type} = 1;
13232:                 } else {
13233:                     $confhash{$item}{$type} = 0;
13234:                 }
13235:             }
13236:             if (ref($domconfig{$action}) eq 'HASH') {
13237:                 if ($action eq 'requestauthor') {
13238:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13239:                         $changes{$type} = 1;
13240:                     }
13241:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13242:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13243:                         $changes{$item}{$type} = 1;
13244:                     }
13245:                 } else {
13246:                     if ($context eq 'requestcourses') {
13247:                         if ($confhash{$item}{$type} ne $unset) {
13248:                             $changes{$item}{$type} = 1;
13249:                         }
13250:                     } else {
13251:                         if (!$confhash{$item}{$type}) {
13252:                             $changes{$item}{$type} = 1;
13253:                         }
13254:                     }
13255:                 }
13256:             } else {
13257:                 if ($context eq 'requestcourses') {
13258:                     if ($confhash{$item}{$type} ne $unset) {
13259:                         $changes{$item}{$type} = 1;
13260:                     }
13261:                 } elsif ($context eq 'requestauthor') {
13262:                     if ($confhash{$type} ne $unset) {
13263:                         $changes{$type} = 1;
13264:                     }
13265:                 } else {
13266:                     if (!$confhash{$item}{$type}) {
13267:                         $changes{$item}{$type} = 1;
13268:                     }
13269:                 }
13270:             }
13271:         }
13272:     }
13273:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13274:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13275:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13276:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13277:                     if (exists($confhash{'defaultquota'}{$key})) {
13278:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13279:                             $changes{'defaultquota'}{$key} = 1;
13280:                         }
13281:                     } else {
13282:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13283:                     }
13284:                 }
13285:             } else {
13286:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13287:                     if (exists($confhash{'defaultquota'}{$key})) {
13288:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13289:                             $changes{'defaultquota'}{$key} = 1;
13290:                         }
13291:                     } else {
13292:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13293:                     }
13294:                 }
13295:             }
13296:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13297:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
13298:                     if (exists($confhash{'authorquota'}{$key})) {
13299:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
13300:                             $changes{'authorquota'}{$key} = 1;
13301:                         }
13302:                     } else {
13303:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
13304:                     }
13305:                 }
13306:             }
13307:         }
13308:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13309:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13310:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13311:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13312:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13313:                             $changes{'defaultquota'}{$key} = 1;
13314:                         }
13315:                     } else {
13316:                         if (!exists($domconfig{'quotas'}{$key})) {
13317:                             $changes{'defaultquota'}{$key} = 1;
13318:                         }
13319:                     }
13320:                 } else {
13321:                     $changes{'defaultquota'}{$key} = 1;
13322:                 }
13323:             }
13324:         }
13325:         if (ref($confhash{'authorquota'}) eq 'HASH') {
13326:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
13327:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13328:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13329:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
13330:                             $changes{'authorquota'}{$key} = 1;
13331:                         }
13332:                     } else {
13333:                         $changes{'authorquota'}{$key} = 1;
13334:                     }
13335:                 } else {
13336:                     $changes{'authorquota'}{$key} = 1;
13337:                 }
13338:             }
13339:         }
13340:     }
13341: 
13342:     if ($context eq 'requestauthor') {
13343:         $domdefaults{'requestauthor'} = \%confhash;
13344:     } else {
13345:         foreach my $key (keys(%confhash)) {
13346:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13347:                 $domdefaults{$key} = $confhash{$key};
13348:             }
13349:         }
13350:     }
13351: 
13352:     my %quotahash = (
13353:                       $action => { %confhash }
13354:                     );
13355:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13356:                                              $dom);
13357:     if ($putresult eq 'ok') {
13358:         if (keys(%changes) > 0) {
13359:             my $cachetime = 24*60*60;
13360:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13361:             if (ref($lastactref) eq 'HASH') {
13362:                 $lastactref->{'domdefaults'} = 1;
13363:             }
13364:             $resulttext = &mt('Changes made:').'<ul>';
13365:             unless (($context eq 'requestcourses') ||
13366:                     ($context eq 'requestauthor')) {
13367:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13368:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13369:                     foreach my $type (@{$types},'default') {
13370:                         if (defined($changes{'defaultquota'}{$type})) {
13371:                             my $typetitle = $usertypes->{$type};
13372:                             if ($type eq 'default') {
13373:                                 $typetitle = $othertitle;
13374:                             }
13375:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13376:                         }
13377:                     }
13378:                     $resulttext .= '</ul></li>';
13379:                 }
13380:                 if (ref($changes{'authorquota'}) eq 'HASH') {
13381:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
13382:                     foreach my $type (@{$types},'default') {
13383:                         if (defined($changes{'authorquota'}{$type})) {
13384:                             my $typetitle = $usertypes->{$type};
13385:                             if ($type eq 'default') {
13386:                                 $typetitle = $othertitle;
13387:                             }
13388:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
13389:                         }
13390:                     }
13391:                     $resulttext .= '</ul></li>';
13392:                 }
13393:             }
13394:             my %newenv;
13395:             foreach my $item (@usertools) {
13396:                 my (%haschgs,%inconf);
13397:                 if ($context eq 'requestauthor') {
13398:                     %haschgs = %changes;
13399:                     %inconf = %confhash;
13400:                 } else {
13401:                     if (ref($changes{$item}) eq 'HASH') {
13402:                         %haschgs = %{$changes{$item}};
13403:                     }
13404:                     if (ref($confhash{$item}) eq 'HASH') {
13405:                         %inconf = %{$confhash{$item}};
13406:                     }
13407:                 }
13408:                 if (keys(%haschgs) > 0) {
13409:                     my $newacc = 
13410:                         &Apache::lonnet::usertools_access($env{'user.name'},
13411:                                                           $env{'user.domain'},
13412:                                                           $item,'reload',$context);
13413:                     if (($context eq 'requestcourses') ||
13414:                         ($context eq 'requestauthor')) {
13415:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13416:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13417:                         }
13418:                     } else {
13419:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13420:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13421:                         }
13422:                     }
13423:                     unless ($context eq 'requestauthor') {
13424:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13425:                     }
13426:                     foreach my $type (@{$types},'default','_LC_adv') {
13427:                         if ($haschgs{$type}) {
13428:                             my $typetitle = $usertypes->{$type};
13429:                             if ($type eq 'default') {
13430:                                 $typetitle = $othertitle;
13431:                             } elsif ($type eq '_LC_adv') {
13432:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13433:                             }
13434:                             if ($inconf{$type}) {
13435:                                 if ($context eq 'requestcourses') {
13436:                                     my $cond;
13437:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
13438:                                         if ($1 eq '') {
13439:                                             $cond = &mt('(Automatic processing of any request).');
13440:                                         } else {
13441:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
13442:                                         }
13443:                                     } else { 
13444:                                         $cond = $conditions{$inconf{$type}};
13445:                                     }
13446:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
13447:                                 } elsif ($context eq 'requestauthor') {
13448:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
13449:                                                              $titles{$inconf{$type}},$typetitle);
13450: 
13451:                                 } else {
13452:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
13453:                                 }
13454:                             } else {
13455:                                 if ($type eq '_LC_adv') {
13456:                                     if ($inconf{$type} eq '0') {
13457:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13458:                                     } else { 
13459:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
13460:                                     }
13461:                                 } else {
13462:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13463:                                 }
13464:                             }
13465:                         }
13466:                     }
13467:                     unless ($context eq 'requestauthor') {
13468:                         $resulttext .= '</ul></li>';
13469:                     }
13470:                 }
13471:             }
13472:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
13473:                 if (ref($changes{'notify'}) eq 'HASH') {
13474:                     if ($changes{'notify'}{'approval'}) {
13475:                         if (ref($confhash{'notify'}) eq 'HASH') {
13476:                             if ($confhash{'notify'}{'approval'}) {
13477:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
13478:                             } else {
13479:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
13480:                             }
13481:                         }
13482:                     }
13483:                 }
13484:             }
13485:             if ($action eq 'requestcourses') {
13486:                 my @offon = ('off','on');
13487:                 if ($changes{'uniquecode'}) {
13488:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
13489:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
13490:                         $resulttext .= '<li>'.
13491:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
13492:                                        '</li>';
13493:                     } else {
13494:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
13495:                                        '</li>';
13496:                     }
13497:                 }
13498:                 foreach my $type ('textbooks','templates') {
13499:                     if (ref($changes{$type}) eq 'HASH') {
13500:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
13501:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
13502:                             my %coursehash = &Apache::lonnet::coursedescription($key);
13503:                             my $coursetitle = $coursehash{'description'};
13504:                             my $position = $confhash{$type}{$key}{'order'} + 1;
13505:                             $resulttext .= '<li>';
13506:                             foreach my $item ('subject','title','publisher','author') {
13507:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
13508:                                          ($type eq 'templates'));
13509:                                 my $name = $item.':';
13510:                                 $name =~ s/^(\w)/\U$1/;
13511:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
13512:                             }
13513:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
13514:                             if ($type eq 'textbooks') {
13515:                                 if ($confhash{$type}{$key}{'image'}) {
13516:                                     $resulttext .= ' '.&mt('Image: [_1]',
13517:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
13518:                                                    ' alt="Textbook cover" />').'<br />';
13519:                                 }
13520:                             }
13521:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
13522:                         }
13523:                         $resulttext .= '</ul></li>';
13524:                     }
13525:                 }
13526:                 if (ref($changes{'validation'}) eq 'HASH') {
13527:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
13528:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
13529:                         foreach my $item (@{$validationitemsref}) {
13530:                             if (exists($changes{'validation'}{$item})) {
13531:                                 if ($item eq 'markup') {
13532:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13533:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
13534:                                 } else {
13535:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13536:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
13537:                                 }
13538:                             }
13539:                         }
13540:                         if (exists($changes{'validation'}{'dc'})) {
13541:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
13542:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
13543:                         }
13544:                     }
13545:                 }
13546:             }
13547:             $resulttext .= '</ul>';
13548:             if (keys(%newenv)) {
13549:                 &Apache::lonnet::appenv(\%newenv);
13550:             }
13551:         } else {
13552:             if ($context eq 'requestcourses') {
13553:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
13554:             } elsif ($context eq 'requestauthor') {
13555:                 $resulttext = &mt('No changes made to rights to request author space.');
13556:             } else {
13557:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
13558:             }
13559:         }
13560:     } else {
13561:         $resulttext = '<span class="LC_error">'.
13562: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13563:     }
13564:     if ($errors) {
13565:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
13566:                        '<ul>'.$errors.'</ul></p>';
13567:     }
13568:     return $resulttext;
13569: }
13570: 
13571: sub process_textbook_image {
13572:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
13573:     my $filename = $env{'form.'.$caller.'.filename'};
13574:     my ($error,$url);
13575:     my ($width,$height) = (50,50);
13576:     if ($configuserok eq 'ok') {
13577:         if ($switchserver) {
13578:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
13579:                          $switchserver);
13580:         } elsif ($author_ok eq 'ok') {
13581:             my ($result,$imageurl) =
13582:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13583:                              "$type/$cdom/$cnum/cover",$width,$height);
13584:             if ($result eq 'ok') {
13585:                 $url = $imageurl;
13586:             } else {
13587:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13588:             }
13589:         } else {
13590:             $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);
13591:         }
13592:     } else {
13593:         $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);
13594:     }
13595:     return ($url,$error);
13596: }
13597: 
13598: sub modify_ltitools {
13599:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13600:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13601:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
13602:     my $confname = $dom.'-domainconfig';
13603:     my $servadm = $r->dir_config('lonAdmEMail');
13604:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13605:     my (%posslti,%possfield);
13606:     my @courseroles = ('cc','in','ta','ep','st');
13607:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
13608:     map { $posslti{$_} = 1; } @ltiroles;
13609:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
13610:     map { $possfield{$_} = 1; } @allfields;
13611:     my %lt = &ltitools_names(); 
13612:     if ($env{'form.ltitools_add'}) {
13613:         my $title = $env{'form.ltitools_add_title'};
13614:         $title =~ s/(`)/'/g;
13615:         ($newid,my $error) = &get_ltitools_id($dom,$title);
13616:         if ($newid) {
13617:             my $position = $env{'form.ltitools_add_pos'};
13618:             $position =~ s/\D+//g;
13619:             if ($position ne '') {
13620:                 $allpos[$position] = $newid;
13621:             }
13622:             $changes{$newid} = 1;
13623:             foreach my $item ('title','url','key','secret','lifetime') {
13624:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
13625:                 if ($item eq 'lifetime') {
13626:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
13627:                 }
13628:                 if ($env{'form.ltitools_add_'.$item}) {
13629:                     if (($item eq 'key') || ($item eq 'secret')) {
13630:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
13631:                     } else {
13632:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
13633:                     }
13634:                 }
13635:             }
13636:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
13637:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
13638:             }
13639:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
13640:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
13641:             }
13642:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
13643:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
13644:             } else {
13645:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
13646:             }
13647:             foreach my $item ('width','height','linktext','explanation') {
13648:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
13649:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
13650:                 if (($item eq 'width') || ($item eq 'height')) {
13651:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
13652:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
13653:                     }
13654:                 } else {
13655:                     if ($env{'form.ltitools_add_'.$item} ne '') {
13656:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
13657:                     }
13658:                 }
13659:             }
13660:             if ($env{'form.ltitools_add_target'} eq 'window') {
13661:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
13662:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
13663:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
13664:             } else {
13665:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
13666:             }
13667:             foreach my $item ('passback','roster') {
13668:                 if ($env{'form.ltitools_'.$item.'_add'}) {
13669:                     $confhash{$newid}{$item} = 1;
13670:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
13671:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
13672:                         $lifetime =~ s/^\s+|\s+$//g;
13673:                         if ($lifetime =~ /^\d+\.?\d*$/) {
13674:                             $confhash{$newid}{$item.'valid'} = $lifetime;
13675:                         }
13676:                     }
13677:                 }
13678:             }
13679:             if ($env{'form.ltitools_add_image.filename'} ne '') {
13680:                 my ($imageurl,$error) =
13681:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
13682:                                             $configuserok,$switchserver,$author_ok);
13683:                 if ($imageurl) {
13684:                     $confhash{$newid}{'image'} = $imageurl;
13685:                 }
13686:                 if ($error) {
13687:                     &Apache::lonnet::logthis($error);
13688:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13689:                 }
13690:             }
13691:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
13692:             foreach my $field (@fields) {
13693:                 if ($possfield{$field}) {
13694:                     if ($field eq 'roles') {
13695:                         foreach my $role (@courseroles) {
13696:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
13697:                             if (($choice ne '') && ($posslti{$choice})) {
13698:                                 $confhash{$newid}{'roles'}{$role} = $choice;
13699:                                 if ($role eq 'cc') {
13700:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
13701:                                 }
13702:                             }
13703:                         }
13704:                     } else {
13705:                         $confhash{$newid}{'fields'}{$field} = 1;
13706:                     }
13707:                 }
13708:             }
13709:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
13710:                 if ($confhash{$newid}{'fields'}{'user'}) {
13711:                     if ($env{'form.ltitools_userincdom_add'}) {
13712:                         $confhash{$newid}{'incdom'} = 1;
13713:                     }
13714:                 }
13715:             }
13716:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
13717:             foreach my $item (@courseconfig) {
13718:                 $confhash{$newid}{'crsconf'}{$item} = 1;
13719:             }
13720:             if ($env{'form.ltitools_add_custom'}) {
13721:                 my $name = $env{'form.ltitools_add_custom_name'};
13722:                 my $value = $env{'form.ltitools_add_custom_value'};
13723:                 $value =~ s/(`)/'/g;
13724:                 $name =~ s/(`)/'/g;
13725:                 $confhash{$newid}{'custom'}{$name} = $value;
13726:             }
13727:         } else {
13728:             my $error = &mt('Failed to acquire unique ID for new external tool');   
13729:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13730:         }
13731:     }
13732:     if (ref($domconfig{$action}) eq 'HASH') {
13733:         my %deletions;
13734:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
13735:         if (@todelete) {
13736:             map { $deletions{$_} = 1; } @todelete;
13737:         }
13738:         my %customadds;
13739:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
13740:         if (@newcustom) {
13741:             map { $customadds{$_} = 1; } @newcustom;
13742:         } 
13743:         my %imgdeletions;
13744:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
13745:         if (@todeleteimages) {
13746:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13747:         }
13748:         my $maxnum = $env{'form.ltitools_maxnum'};
13749:         for (my $i=0; $i<=$maxnum; $i++) {
13750:             my $itemid = $env{'form.ltitools_id_'.$i};
13751:             $itemid =~ s/\D+//g;
13752:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13753:                 if ($deletions{$itemid}) {
13754:                     if ($domconfig{$action}{$itemid}{'image'}) {
13755:                         #FIXME need to obsolete item in RES space
13756:                     }
13757:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
13758:                     next;
13759:                 } else {
13760:                     my $newpos = $env{'form.ltitools_'.$itemid};
13761:                     $newpos =~ s/\D+//g;
13762:                     foreach my $item ('title','url','lifetime') {
13763:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13764:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
13765:                             $changes{$itemid} = 1;
13766:                         }
13767:                     }
13768:                     foreach my $item ('key','secret') {
13769:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13770:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
13771:                             $changes{$itemid} = 1;
13772:                         }
13773:                     }
13774:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
13775:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
13776:                     }
13777:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
13778:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
13779:                     }
13780:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
13781:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
13782:                     } else {
13783:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
13784:                     }
13785:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
13786:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
13787:                             $changes{$itemid} = 1;
13788:                         }
13789:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
13790:                         $changes{$itemid} = 1;
13791:                     }
13792:                     foreach my $size ('width','height') {
13793:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
13794:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
13795:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
13796:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
13797:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13798:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
13799:                                     $changes{$itemid} = 1;
13800:                                 }
13801:                             } else {
13802:                                 $changes{$itemid} = 1;
13803:                             }
13804:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13805:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
13806:                                 $changes{$itemid} = 1;
13807:                             }
13808:                         }
13809:                     }
13810:                     foreach my $item ('linktext','explanation') {
13811:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
13812:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
13813:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
13814:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13815:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13816:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
13817:                                     $changes{$itemid} = 1;
13818:                                 }
13819:                             } else {
13820:                                 $changes{$itemid} = 1;
13821:                             }
13822:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13823:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
13824:                                 $changes{$itemid} = 1;
13825:                             }
13826:                         }
13827:                     }
13828:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
13829:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13830:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
13831:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13832:                     } else {
13833:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
13834:                     }
13835:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13836:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
13837:                             $changes{$itemid} = 1;
13838:                         }
13839:                     } else {
13840:                         $changes{$itemid} = 1;
13841:                     }
13842:                     foreach my $extra ('passback','roster') {
13843:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
13844:                             $confhash{$itemid}{$extra} = 1;
13845:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
13846:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
13847:                                 $lifetime =~ s/^\s+|\s+$//g;
13848:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
13849:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
13850:                                 }
13851:                             }
13852:                         }
13853:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
13854:                             $changes{$itemid} = 1;
13855:                         }
13856:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
13857:                             $changes{$itemid} = 1;
13858:                         }
13859:                     }
13860:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
13861:                     foreach my $item ('label','title','target','linktext','explanation','append') {
13862:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
13863:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
13864:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
13865:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
13866:                                     $changes{$itemid} = 1;
13867:                                 }
13868:                             } else {
13869:                                 $changes{$itemid} = 1;
13870:                             }
13871:                         }
13872:                     }
13873:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
13874:                     foreach my $field (@fields) {
13875:                         if ($possfield{$field}) {
13876:                             if ($field eq 'roles') {
13877:                                 foreach my $role (@courseroles) {
13878:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
13879:                                     if (($choice ne '') && ($posslti{$choice})) {
13880:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
13881:                                         if ($role eq 'cc') {
13882:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
13883:                                         }
13884:                                     }
13885:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
13886:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
13887:                                             $changes{$itemid} = 1;
13888:                                         }
13889:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
13890:                                         $changes{$itemid} = 1;
13891:                                     }
13892:                                 }
13893:                             } else {
13894:                                 $confhash{$itemid}{'fields'}{$field} = 1;
13895:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
13896:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
13897:                                         $changes{$itemid} = 1;
13898:                                     }
13899:                                 } else {
13900:                                     $changes{$itemid} = 1;
13901:                                 }
13902:                             }
13903:                         }
13904:                     }
13905:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13906:                         if ($confhash{$itemid}{'fields'}{'user'}) {
13907:                             if ($env{'form.ltitools_userincdom_'.$i}) {
13908:                                 $confhash{$itemid}{'incdom'} = 1;
13909:                             }
13910:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
13911:                                 $changes{$itemid} = 1;
13912:                             }
13913:                         }
13914:                     }
13915:                     $allpos[$newpos] = $itemid;
13916:                 }
13917:                 if ($imgdeletions{$itemid}) {
13918:                     $changes{$itemid} = 1;
13919:                     #FIXME need to obsolete item in RES space
13920:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
13921:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
13922:                                                                  $itemid,$configuserok,$switchserver,
13923:                                                                  $author_ok);
13924:                     if ($imgurl) {
13925:                         $confhash{$itemid}{'image'} = $imgurl;
13926:                         $changes{$itemid} = 1;
13927:                     }
13928:                     if ($error) {
13929:                         &Apache::lonnet::logthis($error);
13930:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13931:                     }
13932:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
13933:                     $confhash{$itemid}{'image'} =
13934:                        $domconfig{$action}{$itemid}{'image'};
13935:                 }
13936:                 if ($customadds{$i}) {
13937:                     my $name = $env{'form.ltitools_custom_name_'.$i};
13938:                     $name =~ s/(`)/'/g;
13939:                     $name =~ s/^\s+//;
13940:                     $name =~ s/\s+$//;
13941:                     my $value = $env{'form.ltitools_custom_value_'.$i};
13942:                     $value =~ s/(`)/'/g;
13943:                     $value =~ s/^\s+//;
13944:                     $value =~ s/\s+$//;
13945:                     if ($name ne '') {
13946:                         $confhash{$itemid}{'custom'}{$name} = $value;
13947:                         $changes{$itemid} = 1;
13948:                     }
13949:                 }
13950:                 my %customdels;
13951:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
13952:                 if (@customdeletions) {
13953:                     $changes{$itemid} = 1;
13954:                 }
13955:                 map { $customdels{$_} = 1; } @customdeletions;
13956:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
13957:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
13958:                         unless ($customdels{$key}) {
13959:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
13960:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
13961:                             }
13962:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
13963:                                 $changes{$itemid} = 1;
13964:                             }
13965:                         }
13966:                     }
13967:                 }
13968:                 unless ($changes{$itemid}) {
13969:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
13970:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
13971:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
13972:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
13973:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
13974:                                         $changes{$itemid} = 1;
13975:                                         last;
13976:                                     }
13977:                                 }
13978:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
13979:                                 $changes{$itemid} = 1;
13980:                             }
13981:                         }
13982:                         last if ($changes{$itemid});
13983:                     }
13984:                 }
13985:             }
13986:         }
13987:     }
13988:     if (@allpos > 0) {
13989:         my $idx = 0;
13990:         foreach my $itemid (@allpos) {
13991:             if ($itemid ne '') {
13992:                 $confhash{$itemid}{'order'} = $idx;
13993:                 if (ref($domconfig{$action}) eq 'HASH') {
13994:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13995:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
13996:                             $changes{$itemid} = 1;
13997:                         }
13998:                     }
13999:                 }
14000:                 $idx ++;
14001:             }
14002:         }
14003:     }
14004:     my %ltitoolshash = (
14005:                           $action => { %confhash }
14006:                        );
14007:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
14008:                                              $dom);
14009:     if ($putresult eq 'ok') {
14010:         my %ltienchash = (
14011:                              $action => { %encconfig }
14012:                          );
14013:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
14014:         if (keys(%changes) > 0) {
14015:             my $cachetime = 24*60*60;
14016:             my %ltiall = %confhash;
14017:             foreach my $id (keys(%ltiall)) {
14018:                 if (ref($encconfig{$id}) eq 'HASH') {
14019:                     foreach my $item ('key','secret') {
14020:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14021:                     }
14022:                 }
14023:             }
14024:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
14025:             if (ref($lastactref) eq 'HASH') {
14026:                 $lastactref->{'ltitools'} = 1;
14027:             }
14028:             $resulttext = &mt('Changes made:').'<ul>';
14029:             my %bynum;
14030:             foreach my $itemid (sort(keys(%changes))) {
14031:                 my $position = $confhash{$itemid}{'order'};
14032:                 $bynum{$position} = $itemid;
14033:             }
14034:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14035:                 my $itemid = $bynum{$pos}; 
14036:                 if (ref($confhash{$itemid}) ne 'HASH') {
14037:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14038:                 } else {
14039:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
14040:                     if ($confhash{$itemid}{'image'}) {
14041:                         $resulttext .= '&nbsp;'.
14042:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
14043:                                        ' alt="'.&mt('Tool Provider icon').'" />';
14044:                     }
14045:                     $resulttext .= '</li><ul>';
14046:                     my $position = $pos + 1;
14047:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14048:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
14049:                         if ($confhash{$itemid}{$item} ne '') {
14050:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14051:                         }
14052:                     }
14053:                     if ($encconfig{$itemid}{'key'} ne '') {
14054:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14055:                     }
14056:                     if ($encconfig{$itemid}{'secret'} ne '') {
14057:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14058:                         my $num = length($encconfig{$itemid}{'secret'});
14059:                         $resulttext .= ('*'x$num).'</li>';
14060:                     }
14061:                     $resulttext .= '<li>'.&mt('Configurable in course:');
14062:                     my @possconfig = ('label','title','target','linktext','explanation','append');
14063:                     my $numconfig = 0; 
14064:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
14065:                         foreach my $item (@possconfig) {
14066:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
14067:                                 $numconfig ++;
14068:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
14069:                             }
14070:                         }
14071:                     }
14072:                     if (!$numconfig) {
14073:                         $resulttext .= '&nbsp;'.&mt('None');
14074:                     }
14075:                     $resulttext .= '</li>';
14076:                     foreach my $item ('passback','roster') {
14077:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
14078:                         if ($confhash{$itemid}{$item}) {
14079:                             $resulttext .= &mt('Yes');
14080:                             if ($confhash{$itemid}{$item.'valid'}) {
14081:                                 if ($item eq 'passback') {
14082:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
14083:                                                            $confhash{$itemid}{$item.'valid'});
14084:                                 } else {
14085:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
14086:                                                            $confhash{$itemid}{$item.'valid'});
14087:                                 }
14088:                             }
14089:                         } else {
14090:                             $resulttext .= &mt('No');
14091:                         }
14092:                         $resulttext .= '</li>';
14093:                     }
14094:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
14095:                         my $displaylist;
14096:                         if ($confhash{$itemid}{'display'}{'target'}) {
14097:                             $displaylist = &mt('Display target').':&nbsp;'.
14098:                                            $confhash{$itemid}{'display'}{'target'}.',';
14099:                         }
14100:                         foreach my $size ('width','height') { 
14101:                             if ($confhash{$itemid}{'display'}{$size}) {
14102:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
14103:                                                 $confhash{$itemid}{'display'}{$size}.',';
14104:                             }
14105:                         }
14106:                         if ($displaylist) {
14107:                             $displaylist =~ s/,$//;
14108:                             $resulttext .= '<li>'.$displaylist.'</li>';
14109:                         }
14110:                         foreach my $item ('linktext','explanation') {
14111:                             if ($confhash{$itemid}{'display'}{$item}) {
14112:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
14113:                             }
14114:                         }
14115:                     }
14116:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
14117:                         my $fieldlist;
14118:                         foreach my $field (@allfields) {
14119:                             if ($confhash{$itemid}{'fields'}{$field}) {
14120:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
14121:                             }
14122:                         }
14123:                         if ($fieldlist) {
14124:                             $fieldlist =~ s/,$//;
14125:                             if ($confhash{$itemid}{'fields'}{'user'}) {
14126:                                 if ($confhash{$itemid}{'incdom'}) {
14127:                                     $fieldlist .= ' ('.&mt('username:domain').')';
14128:                                 } else {
14129:                                     $fieldlist .= ' ('.&mt('username').')';
14130:                                 }
14131:                             }
14132:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
14133:                         }
14134:                     }
14135:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
14136:                         my $rolemaps;
14137:                         foreach my $role (@courseroles) {
14138:                             if ($confhash{$itemid}{'roles'}{$role}) {
14139:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14140:                                              $confhash{$itemid}{'roles'}{$role}.',';
14141:                             }
14142:                         }
14143:                         if ($rolemaps) {
14144:                             $rolemaps =~ s/,$//; 
14145:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14146:                         }
14147:                     }
14148:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
14149:                         my $customlist;
14150:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
14151:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
14152:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
14153:                             } 
14154:                         }
14155:                         if ($customlist) {
14156:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14157:                         }
14158:                     } 
14159:                     $resulttext .= '</ul></li>';
14160:                 }
14161:             }
14162:             $resulttext .= '</ul>';
14163:         } else {
14164:             $resulttext = &mt('No changes made.');
14165:         }
14166:     } else {
14167:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14168:     }
14169:     if ($errors) {
14170:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14171:                        $errors.'</ul>';
14172:     }
14173:     return $resulttext;
14174: }
14175: 
14176: sub process_ltitools_image {
14177:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
14178:     my $filename = $env{'form.'.$caller.'.filename'};
14179:     my ($error,$url);
14180:     my ($width,$height) = (21,21);
14181:     if ($configuserok eq 'ok') {
14182:         if ($switchserver) {
14183:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
14184:                          $switchserver);
14185:         } elsif ($author_ok eq 'ok') {
14186:             my ($result,$imageurl,$madethumb) =
14187:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14188:                              "ltitools/$itemid/icon",$width,$height);
14189:             if ($result eq 'ok') {
14190:                 if ($madethumb) {
14191:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14192:                     my $imagethumb = "$path/tn-".$imagefile;
14193:                     $url = $imagethumb;
14194:                 } else {
14195:                     $url = $imageurl;
14196:                 }
14197:             } else {
14198:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14199:             }
14200:         } else {
14201:             $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);
14202:         }
14203:     } else {
14204:         $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);
14205:     }
14206:     return ($url,$error);
14207: }
14208: 
14209: sub get_ltitools_id {
14210:     my ($cdom,$title) = @_;
14211:     # get lock on ltitools db
14212:     my $lockhash = {
14213:                       lock => $env{'user.name'}.
14214:                               ':'.$env{'user.domain'},
14215:                    };
14216:     my $tries = 0;
14217:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14218:     my ($id,$error);
14219:  
14220:     while (($gotlock ne 'ok') && ($tries<10)) {
14221:         $tries ++;
14222:         sleep (0.1);
14223:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
14224:     }
14225:     if ($gotlock eq 'ok') {
14226:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
14227:         if ($currids{'lock'}) {
14228:             delete($currids{'lock'});
14229:             if (keys(%currids)) {
14230:                 my @curr = sort { $a <=> $b } keys(%currids);
14231:                 if ($curr[-1] =~ /^\d+$/) {
14232:                     $id = 1 + $curr[-1];
14233:                 }
14234:             } else {
14235:                 $id = 1;
14236:             }
14237:             if ($id) {
14238:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
14239:                     $error = 'nostore';
14240:                 }
14241:             } else {
14242:                 $error = 'nonumber';
14243:             }
14244:         }
14245:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
14246:     } else {
14247:         $error = 'nolock';
14248:     }
14249:     return ($id,$error);
14250: }
14251: 
14252: sub modify_proctoring {
14253:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14254:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14255:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
14256:     my $confname = $dom.'-domainconfig';
14257:     my $servadm = $r->dir_config('lonAdmEMail');
14258:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14259:     my %providernames = &proctoring_providernames();
14260:     my $maxnum = scalar(keys(%providernames));
14261: 
14262:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
14263:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
14264:     if (ref($requref) eq 'HASH') {
14265:         %requserfields = %{$requref};
14266:     }
14267:     if (ref($opturef) eq 'HASH') {
14268:         %optuserfields = %{$opturef};
14269:     }
14270:     if (ref($defref) eq 'HASH') {
14271:         %defaults = %{$defref};
14272:     }
14273:     if (ref($extref) eq 'HASH') {
14274:         %extended = %{$extref};
14275:     }
14276:     if (ref($crsref) eq 'HASH') {
14277:         %crsconf = %{$crsref};
14278:     }
14279:     if (ref($rolesref) eq 'ARRAY') {
14280:         @courseroles = @{$rolesref};
14281:     }
14282:     if (ref($ltiref) eq 'ARRAY') {
14283:         @ltiroles = @{$ltiref};
14284:     }
14285: 
14286:     if (ref($domconfig{$action}) eq 'HASH') {
14287:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
14288:         if (@todeleteimages) {
14289:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14290:         }
14291:     }
14292:     my %customadds;
14293:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
14294:     if (@newcustom) {
14295:         map { $customadds{$_} = 1; } @newcustom;
14296:     }
14297:     foreach my $provider (sort(keys(%providernames))) {
14298:         $confhash{$provider} = {};
14299:         my $pos = $env{'form.proctoring_pos_'.$provider};
14300:         $pos =~ s/\D+//g;
14301:         $allpos[$pos] = $provider;
14302:         my (%current,%currentenc);
14303:         my $showroles = 0;
14304:         if (ref($domconfig{$action}) eq 'HASH') {
14305:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14306:                 %current = %{$domconfig{$action}{$provider}};
14307:                 foreach my $item ('key','secret') { 
14308:                     $currentenc{$item} = $current{$item};
14309:                     delete($current{$item});
14310:                 }
14311:             }
14312:         }
14313:         if ($env{'form.proctoring_available_'.$provider}) {
14314:             $confhash{$provider}{'available'} = 1;
14315:             unless ($current{'available'}) {
14316:                 $changes{$provider} = 1;
14317:             }
14318:         } else {
14319:             %{$confhash{$provider}} = %current;
14320:             %{$encconfhash{$provider}} = %currentenc;
14321:             $confhash{$provider}{'available'} = 0;
14322:             if ($current{'available'}) {
14323:                 $changes{$provider} = 1;
14324:             }
14325:         }
14326:         if ($confhash{$provider}{'available'}) {
14327:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
14328:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
14329:                 if ($field eq 'lifetime') {
14330:                     if ($possval =~ /^\d+$/) {
14331:                         $confhash{$provider}{$field} = $possval;
14332:                     }
14333:                 } elsif ($field eq 'version') {
14334:                     if ($possval =~ /^\d+\.\d+$/) {
14335:                         $confhash{$provider}{$field} = $possval;
14336:                     }
14337:                 } elsif ($field eq 'sigmethod') {
14338:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
14339:                         $confhash{$provider}{$field} = $possval;
14340:                     }
14341:                 } elsif ($field eq 'url') {
14342:                     $confhash{$provider}{$field} = $possval;
14343:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
14344:                     $encconfhash{$provider}{$field} = $possval;
14345:                     unless ($currentenc{$field} eq $possval) {
14346:                         $changes{$provider} = 1;
14347:                     }
14348:                 }
14349:                 unless (($field eq 'key') || ($field eq 'secret')) {
14350:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
14351:                         $changes{$provider} = 1;
14352:                     }
14353:                 }
14354:             }
14355:             if ($imgdeletions{$provider}) {
14356:                 $changes{$provider} = 1;
14357:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
14358:                 my ($imageurl,$error) =
14359:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
14360:                                               $configuserok,$switchserver,$author_ok);
14361:                 if ($imageurl) {
14362:                     $confhash{$provider}{'image'} = $imageurl;
14363:                     $changes{$provider} = 1; 
14364:                 }
14365:                 if ($error) {
14366:                     &Apache::lonnet::logthis($error);
14367:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14368:                 }
14369:             } elsif (exists($current{'image'})) {
14370:                 $confhash{$provider}{'image'} = $current{'image'};
14371:             }
14372:             if (ref($requserfields{$provider}) eq 'ARRAY') {
14373:                 if (@{$requserfields{$provider}} > 0) {
14374:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
14375:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
14376:                             $confhash{$provider}{'incdom'} = 1;
14377:                         }
14378:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
14379:                             $changes{$provider} = 1;
14380:                         }
14381:                     }
14382:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
14383:                         $showroles = 1;
14384:                     }
14385:                 }
14386:             }
14387:             $confhash{$provider}{'fields'} = [];
14388:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
14389:                 if (@{$optuserfields{$provider}} > 0) {
14390:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
14391:                     foreach my $field (@{$optuserfields{$provider}}) {
14392:                         if (grep(/^\Q$field\E$/,@optfields)) {
14393:                             push(@{$confhash{$provider}{'fields'}},$field);
14394:                         }
14395:                     }
14396:                 }
14397:                 if (ref($current{'fields'}) eq 'ARRAY') {
14398:                     unless ($changes{$provider}) {
14399:                         my @new = sort(@{$confhash{$provider}{'fields'}});
14400:                         my @old = sort(@{$current{'fields'}}); 
14401:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14402:                         if (@diffs) {
14403:                             $changes{$provider} = 1;
14404:                         }
14405:                     }
14406:                 } elsif (@{$confhash{$provider}{'fields'}}) {
14407:                     $changes{$provider} = 1;
14408:                 }
14409:             }
14410:             if (ref($defaults{$provider}) eq 'ARRAY') {  
14411:                 if (@{$defaults{$provider}} > 0) {
14412:                     my %options;
14413:                     if (ref($extended{$provider}) eq 'HASH') {
14414:                         %options = %{$extended{$provider}};
14415:                     }
14416:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14417:                     foreach my $field (@{$defaults{$provider}}) {
14418:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14419:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14420:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14421:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14422:                             }
14423:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14424:                             foreach my $inner (keys(%{$options{$field}})) {
14425:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14426:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14427:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14428:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14429:                                     }
14430:                                 } else {
14431:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14432:                                 }
14433:                             }
14434:                         } else {
14435:                             if (grep(/^\Q$field\E$/,@checked)) {
14436:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14437:                             }
14438:                         }
14439:                     }
14440:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14441:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14442:                             unless ($changes{$provider}) {
14443:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14444:                                 my @old = sort(@{$current{'defaults'}});
14445:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14446:                                 if (@diffs) {
14447:                                     $changes{$provider} = 1; 
14448:                                 }
14449:                             }
14450:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14451:                             if (@{$current{'defaults'}}) {
14452:                                 $changes{$provider} = 1;
14453:                             }
14454:                         }
14455:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14456:                         if (ref($current{'defaults'}) eq 'HASH') {
14457:                             unless ($changes{$provider}) {
14458:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
14459:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
14460:                                         $changes{$provider} = 1;
14461:                                         last;
14462:                                     }
14463:                                 }
14464:                             }
14465:                             unless ($changes{$provider}) {
14466:                                 foreach my $key (keys(%{$current{'defaults'}})) {
14467:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
14468:                                         $changes{$provider} = 1;
14469:                                         last;
14470:                                     }
14471:                                 }
14472:                             }
14473:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
14474:                             $changes{$provider} = 1;
14475:                         }
14476:                     }
14477:                 }
14478:             }
14479:             if (ref($crsconf{$provider}) eq 'ARRAY') {
14480:                 if (@{$crsconf{$provider}} > 0) {
14481:                     $confhash{$provider}{'crsconf'} = [];
14482:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
14483:                     foreach my $crsfield (@{$crsconf{$provider}}) {
14484:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
14485:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
14486:                         }
14487:                     }
14488:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
14489:                         unless ($changes{$provider}) {  
14490:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
14491:                             my @old = sort(@{$current{'crsconf'}});
14492:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14493:                             if (@diffs) {
14494:                                 $changes{$provider} = 1;
14495:                             }
14496:                         }
14497:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
14498:                         $changes{$provider} = 1;
14499:                     }
14500:                 }
14501:             }
14502:             if ($showroles) {
14503:                 $confhash{$provider}{'roles'} = {};
14504:                 foreach my $role (@courseroles) {
14505:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
14506:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
14507:                         $confhash{$provider}{'roles'}{$role} = $poss;
14508:                     }
14509:                 }
14510:                 unless ($changes{$provider}) {
14511:                     if (ref($current{'roles'}) eq 'HASH') {
14512:                         foreach my $role (keys(%{$current{'roles'}})) {
14513:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
14514:                                 $changes{$provider} = 1;
14515:                                 last
14516:                             }
14517:                         }
14518:                         unless ($changes{$provider}) {
14519:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
14520:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
14521:                                     $changes{$provider} = 1;
14522:                                     last;
14523:                                 }
14524:                             }
14525:                         }
14526:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
14527:                         $changes{$provider} = 1;
14528:                     }
14529:                 }
14530:             }
14531:             if (ref($current{'custom'}) eq 'HASH') {
14532:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
14533:                 foreach my $key (keys(%{$current{'custom'}})) {
14534:                     if (grep(/^\Q$key\E$/,@customdels)) {
14535:                         $changes{$provider} = 1;
14536:                     } else {
14537:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
14538:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
14539:                             $changes{$provider} = 1;
14540:                         }
14541:                     }
14542:                 }
14543:             }
14544:             if ($customadds{$provider}) {
14545:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
14546:                 $name =~ s/(`)/'/g;
14547:                 $name =~ s/^\s+//;
14548:                 $name =~ s/\s+$//;
14549:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
14550:                 $value =~ s/(`)/'/g;
14551:                 $value =~ s/^\s+//;
14552:                 $value =~ s/\s+$//;
14553:                 if ($name ne '') {
14554:                     $confhash{$provider}{'custom'}{$name} = $value;
14555:                     $changes{$provider} = 1;
14556:                 }
14557:             }
14558:         }
14559:     }
14560:     if (@allpos > 0) {
14561:         my $idx = 0;
14562:         foreach my $provider (@allpos) {
14563:             if ($provider ne '') {
14564:                 $confhash{$provider}{'order'} = $idx;
14565:                 unless ($changes{$provider}) {
14566:                     if (ref($domconfig{$action}) eq 'HASH') {
14567:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14568:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
14569:                                 $changes{$provider} = 1;
14570:                             }
14571:                         }
14572:                     }
14573:                 }
14574:                 $idx ++;
14575:             }
14576:         }
14577:     }
14578:     my %proc_hash = (
14579:                           $action => { %confhash }
14580:                        );
14581:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
14582:                                              $dom);
14583:     if ($putresult eq 'ok') {
14584:         my %proc_enchash = (
14585:                              $action => { %encconfhash }
14586:                          );
14587:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
14588:         if (keys(%changes) > 0) {
14589:             my $cachetime = 24*60*60;
14590:             my %procall = %confhash;
14591:             foreach my $provider (keys(%procall)) {
14592:                 if (ref($encconfhash{$provider}) eq 'HASH') {
14593:                     foreach my $key ('key','secret') {
14594:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
14595:                     }
14596:                 }
14597:             }
14598:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
14599:             if (ref($lastactref) eq 'HASH') {
14600:                 $lastactref->{'proctoring'} = 1;
14601:             }
14602:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
14603:             my %bynum;
14604:             foreach my $provider (sort(keys(%changes))) {
14605:                 my $position = $confhash{$provider}{'order'};
14606:                 $bynum{$position} = $provider;
14607:             }
14608:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14609:                 my $provider = $bynum{$pos};
14610:                 my %lt = &proctoring_titles($provider);
14611:                 my %fieldtitles = &proctoring_fieldtitles($provider);
14612:                 if (!$confhash{$provider}{'available'}) {
14613:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
14614:                 } else {
14615:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
14616:                     if ($confhash{$provider}{'image'}) {
14617:                         $resulttext .= '&nbsp;'.
14618:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
14619:                                        ' alt="'.&mt('Proctoring icon').'" />';
14620:                     }
14621:                     $resulttext .= '<ul>';
14622:                     my $position = $pos + 1;
14623:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14624:                     foreach my $key ('version','sigmethod','url','lifetime') {
14625:                         if ($confhash{$provider}{$key} ne '') {
14626:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
14627:                         }
14628:                     }
14629:                     if ($encconfhash{$provider}{'key'} ne '') {
14630:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
14631:                     }
14632:                     if ($encconfhash{$provider}{'secret'} ne '') {
14633:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14634:                         my $num = length($encconfhash{$provider}{'secret'});
14635:                         $resulttext .= ('*'x$num).'</li>';
14636:                     }
14637:                     my (@fields,$showroles);
14638:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14639:                         push(@fields,@{$requserfields{$provider}});
14640:                     }
14641:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
14642:                         push(@fields,@{$confhash{$provider}{'fields'}});
14643:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
14644:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
14645:                     }
14646:                     if (@fields) {
14647:                         if (grep(/^roles$/,@fields)) {
14648:                             $showroles = 1;
14649:                         }
14650:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
14651:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
14652:                     }
14653:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14654:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
14655:                             if ($confhash{$provider}{'incdom'}) {
14656:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
14657:                             } else { 
14658:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
14659:                             }
14660:                         }
14661:                     }
14662:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14663:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
14664:                             $resulttext .= '<li>'.$lt{'defa'};
14665:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
14666:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
14667:                             }
14668:                             $resulttext =~ s/,$//;
14669:                             $resulttext .= '</li>';
14670:                         }
14671:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14672:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
14673:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
14674:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
14675:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
14676:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
14677:                                 }
14678:                             }
14679:                             $resulttext .= '</ul></li>';
14680:                         }
14681:                     }
14682:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
14683:                         if (@{$crsconf{$provider}} > 0) {
14684:                             $resulttext .= '<li>'.&mt('Configurable in course:');
14685:                             my $numconfig = 0;
14686:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
14687:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
14688:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
14689:                                         $numconfig ++;
14690:                                         if ($provider eq 'examity') {
14691:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
14692:                                         } else {
14693:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
14694:                                         }
14695:                                     }
14696:                                     $resulttext =~ s/,$//;
14697:                                 }
14698:                             }
14699:                             if (!$numconfig) {
14700:                                 $resulttext .= '&nbsp;'.&mt('None');
14701:                             }
14702:                             $resulttext .= '</li>';
14703:                         }
14704:                     }
14705:                     if ($showroles) {
14706:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
14707:                             my $rolemaps;
14708:                             foreach my $role (@courseroles) {
14709:                                 if ($confhash{$provider}{'roles'}{$role}) {
14710:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14711:                                                  $confhash{$provider}{'roles'}{$role}.',';
14712:                                 }
14713:                             }
14714:                             if ($rolemaps) {
14715:                                 $rolemaps =~ s/,$//;
14716:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14717:                             }
14718:                         }
14719:                     }
14720:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
14721:                         my $customlist;
14722:                         if (keys(%{$confhash{$provider}{'custom'}})) {
14723:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
14724:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
14725:                             }
14726:                             $customlist =~ s/,$//;
14727:                         }
14728:                         if ($customlist) {
14729:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14730:                         }
14731:                     } 
14732:                     $resulttext .= '</ul></li>';
14733:                 }
14734:             }
14735:             $resulttext .= '</ul>';
14736:         } else {
14737:             $resulttext = &mt('No changes made.');
14738:         }
14739:     } else {
14740:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14741:     }
14742:     if ($errors) {
14743:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14744:                        $errors.'</ul>';
14745:     }
14746:     return $resulttext;
14747: }
14748: 
14749: sub process_proctoring_image {
14750:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
14751:     my $filename = $env{'form.'.$caller.'.filename'};
14752:     my ($error,$url);
14753:     my ($width,$height) = (21,21);
14754:     if ($configuserok eq 'ok') {
14755:         if ($switchserver) {
14756:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
14757:                          $switchserver);
14758:         } elsif ($author_ok eq 'ok') {
14759:             my ($result,$imageurl,$madethumb) =
14760:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14761:                              "proctoring/$provider/icon",$width,$height);
14762:             if ($result eq 'ok') {
14763:                 if ($madethumb) {
14764:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14765:                     my $imagethumb = "$path/tn-".$imagefile;
14766:                     $url = $imagethumb;
14767:                 } else {
14768:                     $url = $imageurl;
14769:                 }
14770:             } else {
14771:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14772:             }
14773:         } else {
14774:             $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);
14775:         }
14776:     } else {
14777:         $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);
14778:     }
14779:     return ($url,$error);
14780: }
14781: 
14782: sub modify_lti {
14783:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14784:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14785:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
14786:     my (%posslti,%posslticrs,%posscrstype);
14787:     my @courseroles = ('cc','in','ta','ep','st');
14788:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
14789:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
14790:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
14791:     my %coursetypetitles = &Apache::lonlocal::texthash (
14792:                                official   => 'Official',
14793:                                unofficial => 'Unofficial',
14794:                                community  => 'Community',
14795:                                textbook   => 'Textbook',
14796:                                placement  => 'Placement Test',
14797:                                lti        => 'LTI Provider',
14798:     );
14799:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14800:     my %lt = &lti_names();
14801:     map { $posslti{$_} = 1; } @ltiroles;
14802:     map { $posslticrs{$_} = 1; } @lticourseroles;
14803:     map { $posscrstype{$_} = 1; } @coursetypes;
14804: 
14805:     my %menutitles = &ltimenu_titles();
14806: 
14807:     my (@items,%deletions,%itemids);
14808:     if ($env{'form.lti_add'}) {
14809:         my $consumer = $env{'form.lti_consumer_add'};
14810:         $consumer =~ s/(`)/'/g;
14811:         ($newid,my $error) = &get_lti_id($dom,$consumer);
14812:         if ($newid) {
14813:             $itemids{'add'} = $newid;
14814:             push(@items,'add');
14815:             $changes{$newid} = 1;
14816:         } else {
14817:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
14818:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14819:         }
14820:     }
14821:     if (ref($domconfig{$action}) eq 'HASH') {
14822:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
14823:         if (@todelete) {
14824:             map { $deletions{$_} = 1; } @todelete;
14825:         }
14826:         my $maxnum = $env{'form.lti_maxnum'};
14827:         for (my $i=0; $i<$maxnum; $i++) {
14828:             my $itemid = $env{'form.lti_id_'.$i};
14829:             $itemid =~ s/\D+//g;
14830:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14831:                 if ($deletions{$itemid}) {
14832:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
14833:                 } else {
14834:                     push(@items,$i);
14835:                     $itemids{$i} = $itemid;
14836:                 }
14837:             }
14838:         }
14839:     }
14840:     foreach my $idx (@items) {
14841:         my $itemid = $itemids{$idx};
14842:         next unless ($itemid);
14843:         my $position = $env{'form.lti_pos_'.$itemid};
14844:         $position =~ s/\D+//g;
14845:         if ($position ne '') {
14846:             $allpos[$position] = $itemid;
14847:         }
14848:         foreach my $item ('consumer','key','secret','lifetime','requser','crsinc') {
14849:             my $formitem = 'form.lti_'.$item.'_'.$idx;
14850:             $env{$formitem} =~ s/(`)/'/g;
14851:             if ($item eq 'lifetime') {
14852:                 $env{$formitem} =~ s/[^\d.]//g;
14853:             }
14854:             if ($env{$formitem} ne '') {
14855:                 if (($item eq 'key') || ($item eq 'secret')) {
14856:                     $encconfig{$itemid}{$item} = $env{$formitem};
14857:                 } else {
14858:                     $confhash{$itemid}{$item} = $env{$formitem};
14859:                     unless (($idx eq 'add') || ($changes{$itemid})) {
14860:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
14861:                             $changes{$itemid} = 1;
14862:                         }
14863:                     }
14864:                 }
14865:             }
14866:         }
14867:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
14868:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
14869:         }
14870:         if ($confhash{$itemid}{'requser'}) {
14871:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
14872:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
14873:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
14874:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
14875:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
14876:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
14877:                 $mapuser =~ s/(`)/'/g;
14878:                 $mapuser =~ s/^\s+|\s+$//g; 
14879:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
14880:             }
14881:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
14882:             my @makeuser;
14883:             foreach my $ltirole (sort(@possmakeuser)) {
14884:                 if ($posslti{$ltirole}) {
14885:                     push(@makeuser,$ltirole);
14886:                 }
14887:             }
14888:             $confhash{$itemid}{'makeuser'} = \@makeuser;
14889:             if (@makeuser) {
14890:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
14891:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
14892:                     $confhash{$itemid}{'lcauth'} = $lcauth;
14893:                     if ($lcauth ne 'internal') {
14894:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
14895:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
14896:                         $lcauthparm =~ s/`//g;
14897:                         if ($lcauthparm ne '') {
14898:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
14899:                         }
14900:                     }
14901:                 } else {
14902:                     $confhash{$itemid}{'lcauth'} = 'lti';
14903:                 }
14904:             }
14905:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
14906:             if (@possinstdata) {
14907:                 foreach my $field (@possinstdata) {
14908:                     if (exists($fieldtitles{$field})) {
14909:                         push(@{$confhash{$itemid}{'instdata'}});
14910:                     }
14911:                 }
14912:             }
14913:             if ($env{'form.lti_callback_'.$idx}) {
14914:                 if ($env{'form.lti_callbackparam_'.$idx}) {
14915:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
14916:                     $callback =~ s/^\s+|\s+$//g;
14917:                     $confhash{$itemid}{'callback'} = $callback;
14918:                 }
14919:             }
14920:             foreach my $field ('topmenu','inlinemenu') {
14921:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
14922:                     $confhash{$itemid}{$field} = 1;
14923:                 }
14924:             }
14925:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
14926:                 $confhash{$itemid}{lcmenu} = [];
14927:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
14928:                 foreach my $field (@possmenu) {
14929:                     if (exists($menutitles{$field})) {
14930:                         if ($field eq 'grades') {
14931:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
14932:                         }
14933:                         push(@{$confhash{$itemid}{lcmenu}},$field);
14934:                     }
14935:                 }
14936:             }
14937:             if ($confhash{$itemid}{'crsinc'}) {
14938:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
14939:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
14940:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
14941:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
14942:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
14943:                     $mapcrs =~ s/(`)/'/g;
14944:                     $mapcrs =~ s/^\s+|\s+$//g;
14945:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
14946:                 }
14947:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
14948:                 my @crstypes;
14949:                 foreach my $type (sort(@posstypes)) {
14950:                     if ($posscrstype{$type}) {
14951:                         push(@crstypes,$type);
14952:                     }
14953:                 }
14954:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
14955:                 if ($env{'form.lti_storecrs_'.$idx}) {
14956:                     $confhash{$itemid}{'storecrs'} = 1;
14957:                 }       
14958:                 if ($env{'form.lti_makecrs_'.$idx}) {
14959:                     $confhash{$itemid}{'makecrs'} = 1;
14960:                 }
14961:                 foreach my $ltirole (@lticourseroles) {
14962:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
14963:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
14964:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
14965:                     }
14966:                 }
14967:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
14968:                 my @selfenroll;
14969:                 foreach my $type (sort(@possenroll)) {
14970:                     if ($posslticrs{$type}) {
14971:                         push(@selfenroll,$type);
14972:                     }
14973:                 }
14974:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
14975:                 if ($env{'form.lti_crssec_'.$idx}) {
14976:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
14977:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
14978:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
14979:                         my $section = $env{'form.lti_customsection_'.$idx};
14980:                         $section =~ s/(`)/'/g;
14981:                         $section =~ s/^\s+|\s+$//g;
14982:                         if ($section ne '') {
14983:                             $confhash{$itemid}{'section'} = $section;
14984:                         }
14985:                     }
14986:                 }
14987:                 foreach my $field ('passback','roster') {
14988:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
14989:                         $confhash{$itemid}{$field} = 1;
14990:                     }
14991:                 }
14992:                 if ($env{'form.lti_passback_'.$idx}) {
14993:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
14994:                         $confhash{$itemid}{'passbackformat'} = '1.0';
14995:                     } else {
14996:                         $confhash{$itemid}{'passbackformat'} = '1.1';
14997:                     }
14998:                 }
14999:             }
15000:             unless (($idx eq 'add') || ($changes{$itemid})) {
15001:                 if ($confhash{$itemid}{'crsinc'}) {
15002:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15003:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15004:                             $changes{$itemid} = 1;
15005:                         }
15006:                     }
15007:                     unless ($changes{$itemid}) {
15008:                         if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
15009:                             if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15010:                                 $changes{$itemid} = 1;
15011:                             }
15012:                         }
15013:                     }
15014:                     foreach my $field ('mapcrstype','selfenroll') {
15015:                         unless ($changes{$itemid}) {
15016:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15017:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15018:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15019:                                                                                    $confhash{$itemid}{$field});
15020:                                     if (@diffs) {
15021:                                         $changes{$itemid} = 1;
15022:                                     }
15023:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15024:                                     $changes{$itemid} = 1;
15025:                                 }
15026:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15027:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15028:                                     $changes{$itemid} = 1;
15029:                                 }
15030:                             }
15031:                         }
15032:                     }
15033:                     unless ($changes{$itemid}) {
15034:                         if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
15035:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15036:                                 foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
15037:                                     if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
15038:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15039:                                         $changes{$itemid} = 1;
15040:                                         last;
15041:                                     }
15042:                                 }
15043:                                 unless ($changes{$itemid}) {
15044:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15045:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15046:                                             $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
15047:                                             $changes{$itemid} = 1;
15048:                                             last;
15049:                                         }
15050:                                     }
15051:                                 }
15052:                             } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
15053:                                 $changes{$itemid} = 1;
15054:                             }
15055:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15056:                             unless ($changes{$itemid}) {
15057:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15058:                                     $changes{$itemid} = 1;
15059:                                 }
15060:                             }
15061:                         }
15062:                     }
15063:                 }
15064:                 unless ($changes{$itemid}) {
15065:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15066:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15067:                             $changes{$itemid} = 1;
15068:                         }
15069:                     }
15070:                     unless ($changes{$itemid}) {
15071:                         foreach my $field ('makeuser','lcmenu') {
15072:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15073:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15074:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15075:                                                                                    $confhash{$itemid}{$field});
15076:                                     if (@diffs) {
15077:                                         $changes{$itemid} = 1;
15078:                                     }
15079:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15080:                                     $changes{$itemid} = 1;
15081:                                 }
15082:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15083:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15084:                                     $changes{$itemid} = 1;
15085:                                 }
15086:                             }
15087:                         }
15088:                     }
15089:                 }
15090:             }
15091:         }
15092:     }
15093:     if (@allpos > 0) {
15094:         my $idx = 0;
15095:         foreach my $itemid (@allpos) {
15096:             if ($itemid ne '') {
15097:                 $confhash{$itemid}{'order'} = $idx;
15098:                 if (ref($domconfig{$action}) eq 'HASH') {
15099:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15100:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
15101:                             $changes{$itemid} = 1;
15102:                         }
15103:                     }
15104:                 }
15105:                 $idx ++;
15106:             }
15107:         }
15108:     }
15109:     my %ltihash = (
15110:                           $action => { %confhash }
15111:                        );
15112:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
15113:                                              $dom);
15114:     if ($putresult eq 'ok') {
15115:         my %ltienchash = (
15116:                              $action => { %encconfig }
15117:                          );
15118:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
15119:         if (keys(%changes) > 0) {
15120:             my $cachetime = 24*60*60;
15121:             my %ltiall = %confhash;
15122:             foreach my $id (keys(%ltiall)) {
15123:                 if (ref($encconfig{$id}) eq 'HASH') {
15124:                     foreach my $item ('key','secret') {
15125:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
15126:                     }
15127:                 }
15128:             }
15129:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
15130:             if (ref($lastactref) eq 'HASH') {
15131:                 $lastactref->{'lti'} = 1;
15132:             }
15133:             $resulttext = &mt('Changes made:').'<ul>';
15134:             my %bynum;
15135:             foreach my $itemid (sort(keys(%changes))) {
15136:                 my $position = $confhash{$itemid}{'order'};
15137:                 $bynum{$position} = $itemid;
15138:             }
15139:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15140:                 my $itemid = $bynum{$pos};
15141:                 if (ref($confhash{$itemid}) ne 'HASH') {
15142:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
15143:                 } else {
15144:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
15145:                     my $position = $pos + 1;
15146:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15147:                     foreach my $item ('version','lifetime') {
15148:                         if ($confhash{$itemid}{$item} ne '') {
15149:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
15150:                         }
15151:                     }
15152:                     if ($encconfig{$itemid}{'key'} ne '') {
15153:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
15154:                     }
15155:                     if ($encconfig{$itemid}{'secret'} ne '') {
15156:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15157:                         my $num = length($encconfig{$itemid}{'secret'});
15158:                         $resulttext .= ('*'x$num).'</li>';
15159:                     }
15160:                     if ($confhash{$itemid}{'requser'}) {
15161:                         if ($confhash{$itemid}{'callback'}) {
15162:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
15163:                         } else {
15164:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
15165:                         }
15166:                         if ($confhash{$itemid}{'mapuser'}) {
15167:                             my $shownmapuser;
15168:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
15169:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
15170:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
15171:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
15172:                             } else {
15173:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
15174:                             }
15175:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
15176:                         }
15177:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
15178:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
15179:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
15180:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
15181:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
15182:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
15183:                                 } else {
15184:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
15185:                                                        $confhash{$itemid}{'lcauth'});
15186:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
15187:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
15188:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
15189:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
15190:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
15191:                                         }
15192:                                     } else {
15193:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
15194:                                     }
15195:                                 }
15196:                                 $resulttext .= '</li>';
15197:                             } else {
15198:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
15199:                             }
15200:                         }
15201:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
15202:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
15203:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
15204:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
15205:                             } else {
15206:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
15207:                             }
15208:                         }
15209:                         foreach my $item ('topmenu','inlinemenu') {
15210:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15211:                             if ($confhash{$itemid}{$item}) {
15212:                                 $resulttext .= &mt('Yes');
15213:                             } else {
15214:                                 $resulttext .= &mt('No');
15215:                             }
15216:                             $resulttext .= '</li>';
15217:                         }
15218:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15219:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15220:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
15221:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
15222:                             } else {
15223:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
15224:                             }
15225:                         }
15226:                         if ($confhash{$itemid}{'crsinc'}) {
15227:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15228:                                 my $rolemaps;
15229:                                 foreach my $role (@ltiroles) {
15230:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
15231:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
15232:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
15233:                                                                                 'Course').',';
15234:                                     }
15235:                                 }
15236:                                 if ($rolemaps) {
15237:                                     $rolemaps =~ s/,$//;
15238:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15239:                                 }
15240:                             }
15241:                             if ($confhash{$itemid}{'mapcrs'}) {
15242:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
15243:                             }
15244:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
15245:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
15246:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
15247:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
15248:                                                    '</li>';
15249:                                 } else {
15250:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
15251:                                 }
15252:                             }
15253:                             if ($confhash{$itemid}{'storecrs'}) {
15254:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
15255:                             }
15256:                             if ($confhash{$itemid}{'makecrs'}) {
15257:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
15258:                             } else {
15259:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
15260:                             }
15261:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
15262:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
15263:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
15264:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
15265:                                                    '</li>';
15266:                                 } else {
15267:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
15268:                                 }
15269:                             }
15270:                             if ($confhash{$itemid}{'section'}) {
15271:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
15272:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
15273:                                                          ' (course_section_sourcedid)'.'</li>';  
15274:                                 } else {
15275:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
15276:                                                           $confhash{$itemid}{'section'}.'</li>';
15277:                                 }
15278:                             } else {
15279:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
15280:                             }
15281:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
15282:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15283:                                 if ($confhash{$itemid}{$item}) {
15284:                                     $resulttext .= &mt('Yes');
15285:                                     if ($item eq 'passback') {
15286:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
15287:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
15288:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
15289:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
15290:                                         }
15291:                                     }
15292:                                 } else {
15293:                                     $resulttext .= &mt('No');
15294:                                 }
15295:                                 $resulttext .= '</li>';
15296:                             }
15297:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15298:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15299:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
15300:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
15301:                                 } else {
15302:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
15303:                                 }
15304:                             }
15305:                         }
15306:                     }
15307:                     $resulttext .= '</ul></li>';
15308:                 }
15309:             }
15310:             $resulttext .= '</ul>';
15311:         } else {
15312:             $resulttext = &mt('No changes made.');
15313:         }
15314:     } else {
15315:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15316:     }
15317:     if ($errors) {
15318:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15319:                        $errors.'</ul>';
15320:     }
15321:     return $resulttext;
15322: }
15323: 
15324: sub get_lti_id {
15325:     my ($domain,$consumer) = @_;
15326:     # get lock on lti db
15327:     my $lockhash = {
15328:                       lock => $env{'user.name'}.
15329:                               ':'.$env{'user.domain'},
15330:                    };
15331:     my $tries = 0;
15332:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15333:     my ($id,$error);
15334: 
15335:     while (($gotlock ne 'ok') && ($tries<10)) {
15336:         $tries ++;
15337:         sleep (0.1);
15338:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15339:     }
15340:     if ($gotlock eq 'ok') {
15341:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
15342:         if ($currids{'lock'}) {
15343:             delete($currids{'lock'});
15344:             if (keys(%currids)) {
15345:                 my @curr = sort { $a <=> $b } keys(%currids);
15346:                 if ($curr[-1] =~ /^\d+$/) {
15347:                     $id = 1 + $curr[-1];
15348:                 }
15349:             } else {
15350:                 $id = 1;
15351:             }
15352:             if ($id) {
15353:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
15354:                     $error = 'nostore';
15355:                 }
15356:             } else {
15357:                 $error = 'nonumber';
15358:             }
15359:         }
15360:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
15361:     } else {
15362:         $error = 'nolock';
15363:     }
15364:     return ($id,$error);
15365: }
15366: 
15367: sub modify_autoenroll {
15368:     my ($dom,$lastactref,%domconfig) = @_;
15369:     my ($resulttext,%changes);
15370:     my %currautoenroll;
15371:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
15372:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
15373:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
15374:         }
15375:     }
15376:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
15377:     my %title = ( run => 'Auto-enrollment active',
15378:                   sender => 'Sender for notification messages',
15379:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
15380:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
15381:     my @offon = ('off','on');
15382:     my $sender_uname = $env{'form.sender_uname'};
15383:     my $sender_domain = $env{'form.sender_domain'};
15384:     if ($sender_domain eq '') {
15385:         $sender_uname = '';
15386:     } elsif ($sender_uname eq '') {
15387:         $sender_domain = '';
15388:     }
15389:     my $coowners = $env{'form.autoassign_coowners'};
15390:     my $failsafe = $env{'form.autoenroll_failsafe'};
15391:     $failsafe =~ s{^\s+|\s+$}{}g;
15392:     if ($failsafe =~ /\D/) {
15393:         undef($failsafe);
15394:     }
15395:     my %autoenrollhash =  (
15396:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
15397:                                        'sender_uname' => $sender_uname,
15398:                                        'sender_domain' => $sender_domain,
15399:                                        'co-owners' => $coowners,
15400:                                        'autofailsafe' => $failsafe,
15401:                                 }
15402:                      );
15403:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
15404:                                              $dom);
15405:     if ($putresult eq 'ok') {
15406:         if (exists($currautoenroll{'run'})) {
15407:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
15408:                  $changes{'run'} = 1;
15409:              }
15410:         } elsif ($autorun) {
15411:             if ($env{'form.autoenroll_run'} ne '1') {
15412:                  $changes{'run'} = 1;
15413:             }
15414:         }
15415:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
15416:             $changes{'sender'} = 1;
15417:         }
15418:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
15419:             $changes{'sender'} = 1;
15420:         }
15421:         if ($currautoenroll{'co-owners'} ne '') {
15422:             if ($currautoenroll{'co-owners'} ne $coowners) {
15423:                 $changes{'coowners'} = 1;
15424:             }
15425:         } elsif ($coowners) {
15426:             $changes{'coowners'} = 1;
15427:         }
15428:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
15429:             $changes{'autofailsafe'} = 1;
15430:         }
15431:         if (keys(%changes) > 0) {
15432:             $resulttext = &mt('Changes made:').'<ul>';
15433:             if ($changes{'run'}) {
15434:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
15435:             }
15436:             if ($changes{'sender'}) {
15437:                 if ($sender_uname eq '' || $sender_domain eq '') {
15438:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
15439:                 } else {
15440:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
15441:                 }
15442:             }
15443:             if ($changes{'coowners'}) {
15444:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
15445:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15446:                 if (ref($lastactref) eq 'HASH') {
15447:                     $lastactref->{'domainconfig'} = 1;
15448:                 }
15449:             }
15450:             if ($changes{'autofailsafe'}) {
15451:                 if ($failsafe ne '') {
15452:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
15453:                 } else {
15454:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
15455:                 }
15456:                 &Apache::lonnet::get_domain_defaults($dom,1);
15457:                 if (ref($lastactref) eq 'HASH') {
15458:                     $lastactref->{'domdefaults'} = 1;
15459:                 }
15460:             }
15461:             $resulttext .= '</ul>';
15462:         } else {
15463:             $resulttext = &mt('No changes made to auto-enrollment settings');
15464:         }
15465:     } else {
15466:         $resulttext = '<span class="LC_error">'.
15467: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
15468:     }
15469:     return $resulttext;
15470: }
15471: 
15472: sub modify_autoupdate {
15473:     my ($dom,%domconfig) = @_;
15474:     my ($resulttext,%currautoupdate,%fields,%changes);
15475:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
15476:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
15477:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
15478:         }
15479:     }
15480:     my @offon = ('off','on');
15481:     my %title = &Apache::lonlocal::texthash (
15482:                     run        => 'Auto-update:',
15483:                     classlists => 'Updates to user information in classlists?',
15484:                     unexpired  => 'Skip updates for users without active or future roles?',
15485:                     lastactive => 'Skip updates for inactive users?',
15486:                 );
15487:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15488:     my %fieldtitles = &Apache::lonlocal::texthash (
15489:                         id => 'Student/Employee ID',
15490:                         permanentemail => 'E-mail address',
15491:                         lastname => 'Last Name',
15492:                         firstname => 'First Name',
15493:                         middlename => 'Middle Name',
15494:                         generation => 'Generation',
15495:                       );
15496:     $othertitle = &mt('All users');
15497:     if (keys(%{$usertypes}) >  0) {
15498:         $othertitle = &mt('Other users');
15499:     }
15500:     foreach my $key (keys(%env)) {
15501:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
15502:             my ($usertype,$item) = ($1,$2);
15503:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
15504:                 if ($usertype eq 'default') {   
15505:                     push(@{$fields{$1}},$2);
15506:                 } elsif (ref($types) eq 'ARRAY') {
15507:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
15508:                         push(@{$fields{$1}},$2);
15509:                     }
15510:                 }
15511:             }
15512:         }
15513:     }
15514:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
15515:     @lockablenames = sort(@lockablenames);
15516:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
15517:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15518:         if (@changed) {
15519:             $changes{'lockablenames'} = 1;
15520:         }
15521:     } else {
15522:         if (@lockablenames) {
15523:             $changes{'lockablenames'} = 1;
15524:         }
15525:     }
15526:     my %updatehash = (
15527:                       autoupdate => { run => $env{'form.autoupdate_run'},
15528:                                       classlists => $env{'form.classlists'},
15529:                                       unexpired  => $env{'form.unexpired'},
15530:                                       fields => {%fields},
15531:                                       lockablenames => \@lockablenames,
15532:                                     }
15533:                      );
15534:     my $lastactivedays;
15535:     if ($env{'form.lastactive'}) {
15536:         $lastactivedays = $env{'form.lastactivedays'};
15537:         $lastactivedays =~ s/^\s+|\s+$//g;
15538:         unless ($lastactivedays =~ /^\d+$/) {
15539:             undef($lastactivedays);
15540:             $env{'form.lastactive'} = 0;
15541:         }
15542:     }
15543:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
15544:     foreach my $key (keys(%currautoupdate)) {
15545:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
15546:             if (exists($updatehash{autoupdate}{$key})) {
15547:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
15548:                     $changes{$key} = 1;
15549:                 }
15550:             }
15551:         } elsif ($key eq 'fields') {
15552:             if (ref($currautoupdate{$key}) eq 'HASH') {
15553:                 foreach my $item (@{$types},'default') {
15554:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
15555:                         my $change = 0;
15556:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
15557:                             if (!exists($fields{$item})) {
15558:                                 $change = 1;
15559:                                 last;
15560:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
15561:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
15562:                                     $change = 1;
15563:                                     last;
15564:                                 }
15565:                             }
15566:                         }
15567:                         if ($change) {
15568:                             push(@{$changes{$key}},$item);
15569:                         }
15570:                     } 
15571:                 }
15572:             }
15573:         } elsif ($key eq 'lockablenames') {
15574:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
15575:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15576:                 if (@changed) {
15577:                     $changes{'lockablenames'} = 1;
15578:                 }
15579:             } else {
15580:                 if (@lockablenames) {
15581:                     $changes{'lockablenames'} = 1;
15582:                 }
15583:             }
15584:         }
15585:     }
15586:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
15587:         if (@lockablenames) {
15588:             $changes{'lockablenames'} = 1;
15589:         }
15590:     }
15591:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
15592:         if ($updatehash{'autoupdate'}{'unexpired'}) {
15593:             $changes{'unexpired'} = 1;
15594:         }
15595:     }
15596:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
15597:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
15598:             $changes{'lastactive'} = 1;
15599:         }
15600:     }
15601:     foreach my $item (@{$types},'default') {
15602:         if (defined($fields{$item})) {
15603:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
15604:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
15605:                     my $change = 0;
15606:                     if (ref($fields{$item}) eq 'ARRAY') {
15607:                         foreach my $type (@{$fields{$item}}) {
15608:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
15609:                                 $change = 1;
15610:                                 last;
15611:                             }
15612:                         }
15613:                     }
15614:                     if ($change) {
15615:                         push(@{$changes{'fields'}},$item);
15616:                     }
15617:                 } else {
15618:                     push(@{$changes{'fields'}},$item);
15619:                 }
15620:             } else {
15621:                 push(@{$changes{'fields'}},$item);
15622:             }
15623:         }
15624:     }
15625:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
15626:                                              $dom);
15627:     if ($putresult eq 'ok') {
15628:         if (keys(%changes) > 0) {
15629:             $resulttext = &mt('Changes made:').'<ul>';
15630:             foreach my $key (sort(keys(%changes))) {
15631:                 if ($key eq 'lockablenames') {
15632:                     $resulttext .= '<li>';
15633:                     if (@lockablenames) {
15634:                         $usertypes->{'default'} = $othertitle;
15635:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
15636:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
15637:                     } else {
15638:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
15639:                     }
15640:                     $resulttext .= '</li>';
15641:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
15642:                     foreach my $item (@{$changes{$key}}) {
15643:                         my @newvalues;
15644:                         foreach my $type (@{$fields{$item}}) {
15645:                             push(@newvalues,$fieldtitles{$type});
15646:                         }
15647:                         my $newvaluestr;
15648:                         if (@newvalues > 0) {
15649:                             $newvaluestr = join(', ',@newvalues);
15650:                         } else {
15651:                             $newvaluestr = &mt('none');
15652:                         }
15653:                         if ($item eq 'default') {
15654:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
15655:                         } else {
15656:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
15657:                         }
15658:                     }
15659:                 } else {
15660:                     my $newvalue;
15661:                     if ($key eq 'run') {
15662:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
15663:                     } elsif ($key eq 'lastactive') {
15664:                         $newvalue = $offon[$env{'form.lastactive'}];
15665:                         unless ($lastactivedays eq '') {
15666:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
15667:                         }
15668:                     } else {
15669:                         $newvalue = $offon[$env{'form.'.$key}];
15670:                     }
15671:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
15672:                 }
15673:             }
15674:             $resulttext .= '</ul>';
15675:         } else {
15676:             $resulttext = &mt('No changes made to autoupdates');
15677:         }
15678:     } else {
15679:         $resulttext = '<span class="LC_error">'.
15680: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
15681:     }
15682:     return $resulttext;
15683: }
15684: 
15685: sub modify_autocreate {
15686:     my ($dom,%domconfig) = @_;
15687:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
15688:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
15689:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
15690:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
15691:         }
15692:     }
15693:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
15694:                  req => 'Auto-creation of validated requests for official courses',
15695:                  xmldc => 'Identity of course creator of courses from XML files',
15696:                );
15697:     my @types = ('xml','req');
15698:     foreach my $item (@types) {
15699:         $newvals{$item} = $env{'form.autocreate_'.$item};
15700:         $newvals{$item} =~ s/\D//g;
15701:         $newvals{$item} = 0 if ($newvals{$item} eq '');
15702:     }
15703:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
15704:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15705:     unless (exists($domcoords{$newvals{'xmldc'}})) {
15706:         $newvals{'xmldc'} = '';
15707:     } 
15708:     %autocreatehash =  (
15709:                         autocreate => { xml => $newvals{'xml'},
15710:                                         req => $newvals{'req'},
15711:                                       }
15712:                        );
15713:     if ($newvals{'xmldc'} ne '') {
15714:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
15715:     }
15716:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
15717:                                              $dom);
15718:     if ($putresult eq 'ok') {
15719:         my @items = @types;
15720:         if ($newvals{'xml'}) {
15721:             push(@items,'xmldc');
15722:         }
15723:         foreach my $item (@items) {
15724:             if (exists($currautocreate{$item})) {
15725:                 if ($currautocreate{$item} ne $newvals{$item}) {
15726:                     $changes{$item} = 1;
15727:                 }
15728:             } elsif ($newvals{$item}) {
15729:                 $changes{$item} = 1;
15730:             }
15731:         }
15732:         if (keys(%changes) > 0) {
15733:             my @offon = ('off','on'); 
15734:             $resulttext = &mt('Changes made:').'<ul>';
15735:             foreach my $item (@types) {
15736:                 if ($changes{$item}) {
15737:                     my $newtxt = $offon[$newvals{$item}];
15738:                     $resulttext .= '<li>'.
15739:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
15740:                                        '<b>','</b>').
15741:                                    '</li>';
15742:                 }
15743:             }
15744:             if ($changes{'xmldc'}) {
15745:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
15746:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
15747:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
15748:             }
15749:             $resulttext .= '</ul>';
15750:         } else {
15751:             $resulttext = &mt('No changes made to auto-creation settings');
15752:         }
15753:     } else {
15754:         $resulttext = '<span class="LC_error">'.
15755:             &mt('An error occurred: [_1]',$putresult).'</span>';
15756:     }
15757:     return $resulttext;
15758: }
15759: 
15760: sub modify_directorysrch {
15761:     my ($dom,$lastactref,%domconfig) = @_;
15762:     my ($resulttext,%changes);
15763:     my %currdirsrch;
15764:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
15765:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
15766:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
15767:         }
15768:     }
15769:     my %title = ( available => 'Institutional directory search available',
15770:                   localonly => 'Other domains can search institution',
15771:                   lcavailable => 'LON-CAPA directory search available',
15772:                   lclocalonly => 'Other domains can search LON-CAPA domain',
15773:                   searchby => 'Search types',
15774:                   searchtypes => 'Search latitude');
15775:     my @offon = ('off','on');
15776:     my @otherdoms = ('Yes','No');
15777: 
15778:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
15779:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
15780:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
15781: 
15782:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15783:     if (keys(%{$usertypes}) == 0) {
15784:         @cansearch = ('default');
15785:     } else {
15786:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
15787:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
15788:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
15789:                     push(@{$changes{'cansearch'}},$type);
15790:                 }
15791:             }
15792:             foreach my $type (@cansearch) {
15793:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
15794:                     push(@{$changes{'cansearch'}},$type);
15795:                 }
15796:             }
15797:         } else {
15798:             push(@{$changes{'cansearch'}},@cansearch);
15799:         }
15800:     }
15801: 
15802:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
15803:         foreach my $by (@{$currdirsrch{'searchby'}}) {
15804:             if (!grep(/^\Q$by\E$/,@searchby)) {
15805:                 push(@{$changes{'searchby'}},$by);
15806:             }
15807:         }
15808:         foreach my $by (@searchby) {
15809:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
15810:                 push(@{$changes{'searchby'}},$by);
15811:             }
15812:         }
15813:     } else {
15814:         push(@{$changes{'searchby'}},@searchby);
15815:     }
15816: 
15817:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
15818:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
15819:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
15820:                 push(@{$changes{'searchtypes'}},$type);
15821:             }
15822:         }
15823:         foreach my $type (@searchtypes) {
15824:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
15825:                 push(@{$changes{'searchtypes'}},$type);
15826:             }
15827:         }
15828:     } else {
15829:         if (exists($currdirsrch{'searchtypes'})) {
15830:             foreach my $type (@searchtypes) {  
15831:                 if ($type ne $currdirsrch{'searchtypes'}) { 
15832:                     push(@{$changes{'searchtypes'}},$type);
15833:                 }
15834:             }
15835:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
15836:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
15837:             }   
15838:         } else {
15839:             push(@{$changes{'searchtypes'}},@searchtypes); 
15840:         }
15841:     }
15842: 
15843:     my %dirsrch_hash =  (
15844:             directorysrch => { available => $env{'form.dirsrch_available'},
15845:                                cansearch => \@cansearch,
15846:                                localonly => $env{'form.dirsrch_instlocalonly'},
15847:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
15848:                                lcavailable => $env{'form.dirsrch_domavailable'},
15849:                                searchby => \@searchby,
15850:                                searchtypes => \@searchtypes,
15851:                              }
15852:             );
15853:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
15854:                                              $dom);
15855:     if ($putresult eq 'ok') {
15856:         if (exists($currdirsrch{'available'})) {
15857:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
15858:                  $changes{'available'} = 1;
15859:              }
15860:         } else {
15861:             if ($env{'form.dirsrch_available'} eq '1') {
15862:                 $changes{'available'} = 1;
15863:             }
15864:         }
15865:         if (exists($currdirsrch{'lcavailable'})) {
15866:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
15867:                 $changes{'lcavailable'} = 1;
15868:             }
15869:         } else {
15870:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
15871:                 $changes{'lcavailable'} = 1;
15872:             }
15873:         }
15874:         if (exists($currdirsrch{'localonly'})) {
15875:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
15876:                 $changes{'localonly'} = 1;
15877:             }
15878:         } else {
15879:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
15880:                 $changes{'localonly'} = 1;
15881:             }
15882:         }
15883:         if (exists($currdirsrch{'lclocalonly'})) {
15884:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
15885:                 $changes{'lclocalonly'} = 1;
15886:             }
15887:         } else {
15888:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
15889:                 $changes{'lclocalonly'} = 1;
15890:             }
15891:         }
15892:         if (keys(%changes) > 0) {
15893:             $resulttext = &mt('Changes made:').'<ul>';
15894:             if ($changes{'available'}) {
15895:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
15896:             }
15897:             if ($changes{'lcavailable'}) {
15898:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
15899:             }
15900:             if ($changes{'localonly'}) {
15901:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
15902:             }
15903:             if ($changes{'lclocalonly'}) {
15904:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
15905:             }
15906:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
15907:                 my $chgtext;
15908:                 if (ref($usertypes) eq 'HASH') {
15909:                     if (keys(%{$usertypes}) > 0) {
15910:                         foreach my $type (@{$types}) {
15911:                             if (grep(/^\Q$type\E$/,@cansearch)) {
15912:                                 $chgtext .= $usertypes->{$type}.'; ';
15913:                             }
15914:                         }
15915:                         if (grep(/^default$/,@cansearch)) {
15916:                             $chgtext .= $othertitle;
15917:                         } else {
15918:                             $chgtext =~ s/\; $//;
15919:                         }
15920:                         $resulttext .=
15921:                             '<li>'.
15922:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
15923:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
15924:                             '</li>';
15925:                     }
15926:                 }
15927:             }
15928:             if (ref($changes{'searchby'}) eq 'ARRAY') {
15929:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
15930:                 my $chgtext;
15931:                 foreach my $type (@{$titleorder}) {
15932:                     if (grep(/^\Q$type\E$/,@searchby)) {
15933:                         if (defined($searchtitles->{$type})) {
15934:                             $chgtext .= $searchtitles->{$type}.'; ';
15935:                         }
15936:                     }
15937:                 }
15938:                 $chgtext =~ s/\; $//;
15939:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
15940:             }
15941:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
15942:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
15943:                 my $chgtext;
15944:                 foreach my $type (@{$srchtypeorder}) {
15945:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
15946:                         if (defined($srchtypes_desc->{$type})) {
15947:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
15948:                         }
15949:                     }
15950:                 }
15951:                 $chgtext =~ s/\; $//;
15952:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
15953:             }
15954:             $resulttext .= '</ul>';
15955:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
15956:             if (ref($lastactref) eq 'HASH') {
15957:                 $lastactref->{'directorysrch'} = 1;
15958:             }
15959:         } else {
15960:             $resulttext = &mt('No changes made to directory search settings');
15961:         }
15962:     } else {
15963:         $resulttext = '<span class="LC_error">'.
15964:                       &mt('An error occurred: [_1]',$putresult).'</span>';
15965:     }
15966:     return $resulttext;
15967: }
15968: 
15969: sub modify_contacts {
15970:     my ($dom,$lastactref,%domconfig) = @_;
15971:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
15972:     if (ref($domconfig{'contacts'}) eq 'HASH') {
15973:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
15974:             $currsetting{$key} = $domconfig{'contacts'}{$key};
15975:         }
15976:     }
15977:     my (%others,%to,%bcc,%includestr,%includeloc);
15978:     my @contacts = ('supportemail','adminemail');
15979:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
15980:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
15981:     my @toggles = ('reporterrors','reportupdates','reportstatus');
15982:     my @lonstatus = ('threshold','sysmail','weights','excluded');
15983:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
15984:     foreach my $type (@mailings) {
15985:         @{$newsetting{$type}} = 
15986:             &Apache::loncommon::get_env_multiple('form.'.$type);
15987:         foreach my $item (@contacts) {
15988:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
15989:                 $contacts_hash{contacts}{$type}{$item} = 1;
15990:             } else {
15991:                 $contacts_hash{contacts}{$type}{$item} = 0;
15992:             }
15993:         }
15994:         $others{$type} = $env{'form.'.$type.'_others'};
15995:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
15996:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15997:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
15998:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
15999:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
16000:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
16001:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
16002:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16003:             }
16004:         }
16005:     }
16006:     foreach my $item (@contacts) {
16007:         $to{$item} = $env{'form.'.$item};
16008:         $contacts_hash{'contacts'}{$item} = $to{$item};
16009:     }
16010:     foreach my $item (@toggles) {
16011:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
16012:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
16013:         }
16014:     }
16015:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
16016:     foreach my $item (@lonstatus) {
16017:         if ($item eq 'excluded') {
16018:             my (%serverhomes,@excluded);
16019:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
16020:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
16021:             if (@possexcluded) {
16022:                 foreach my $id (sort(@possexcluded)) {
16023:                     if ($serverhomes{$id}) {
16024:                         push(@excluded,$id);
16025:                     }
16026:                 }
16027:             }
16028:             if (@excluded) {
16029:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
16030:             }
16031:         } elsif ($item eq 'weights') {
16032:             foreach my $type ('E','W','N','U') {
16033:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
16034:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
16035:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
16036:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
16037:                             $env{'form.error'.$item.'_'.$type};
16038:                     }
16039:                 }
16040:             }
16041:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
16042:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
16043:             if ($env{'form.error'.$item} =~ /^\d+$/) {
16044:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
16045:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
16046:                 }
16047:             }
16048:         }
16049:     }
16050:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
16051:         foreach my $field (@{$fields}) {
16052:             if (ref($possoptions->{$field}) eq 'ARRAY') {
16053:                 my $value = $env{'form.helpform_'.$field};
16054:                 $value =~ s/^\s+|\s+$//g;
16055:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
16056:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
16057:                     if ($field eq 'screenshot') {
16058:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
16059:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
16060:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
16061:                         }
16062:                     }
16063:                 }
16064:             }
16065:         }
16066:     }
16067:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16068:     my (@statuses,%usertypeshash,@overrides);
16069:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
16070:         @statuses = @{$types};
16071:         if (ref($usertypes) eq 'HASH') {
16072:             %usertypeshash = %{$usertypes};
16073:         }
16074:     }
16075:     if (@statuses) {
16076:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
16077:         foreach my $type (@possoverrides) {
16078:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
16079:                 push(@overrides,$type);
16080:             }
16081:         }
16082:         if (@overrides) {
16083:             foreach my $type (@overrides) {
16084:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
16085:                 foreach my $item (@contacts) {
16086:                     if (grep(/^\Q$item\E$/,@standard)) {
16087:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
16088:                         $newsetting{'override_'.$type}{$item} = 1;
16089:                     } else {
16090:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
16091:                         $newsetting{'override_'.$type}{$item} = 0;
16092:                     }
16093:                 }
16094:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
16095:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16096:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
16097:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16098:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
16099:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
16100:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
16101:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16102:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
16103:                 }
16104:             }     
16105:         }
16106:     }
16107:     if (keys(%currsetting) > 0) {
16108:         foreach my $item (@contacts) {
16109:             if ($to{$item} ne $currsetting{$item}) {
16110:                 $changes{$item} = 1;
16111:             }
16112:         }
16113:         foreach my $type (@mailings) {
16114:             foreach my $item (@contacts) {
16115:                 if (ref($currsetting{$type}) eq 'HASH') {
16116:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
16117:                         push(@{$changes{$type}},$item);
16118:                     }
16119:                 } else {
16120:                     push(@{$changes{$type}},@{$newsetting{$type}});
16121:                 }
16122:             }
16123:             if ($others{$type} ne $currsetting{$type}{'others'}) {
16124:                 push(@{$changes{$type}},'others');
16125:             }
16126:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16127:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
16128:                     push(@{$changes{$type}},'bcc'); 
16129:                 }
16130:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
16131:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
16132:                     push(@{$changes{$type}},'include');
16133:                 }
16134:             }
16135:         }
16136:         if (ref($fields) eq 'ARRAY') {
16137:             if (ref($currsetting{'helpform'}) eq 'HASH') {
16138:                 foreach my $field (@{$fields}) {
16139:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
16140:                         push(@{$changes{'helpform'}},$field);
16141:                     }
16142:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16143:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
16144:                             push(@{$changes{'helpform'}},'maxsize');
16145:                         }
16146:                     }
16147:                 }
16148:             } else {
16149:                 foreach my $field (@{$fields}) {
16150:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16151:                         push(@{$changes{'helpform'}},$field);
16152:                     }
16153:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16154:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16155:                             push(@{$changes{'helpform'}},'maxsize');
16156:                         }
16157:                     }
16158:                 }
16159:             }
16160:         }
16161:         if (@statuses) {
16162:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
16163:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
16164:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
16165:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
16166:                             foreach my $item (@contacts,'bcc','others','include') {
16167:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
16168:                                     push(@{$changes{'overrides'}},$key);
16169:                                     last;
16170:                                 }
16171:                             }
16172:                         } else {
16173:                             push(@{$changes{'overrides'}},$key);
16174:                         }
16175:                     }
16176:                 }
16177:                 foreach my $key (@overrides) {
16178:                     unless (exists($currsetting{'overrides'}{$key})) {
16179:                         push(@{$changes{'overrides'}},$key);
16180:                     }
16181:                 }
16182:             } else {
16183:                 foreach my $key (@overrides) {
16184:                     push(@{$changes{'overrides'}},$key); 
16185:                 }
16186:             }
16187:         }
16188:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
16189:             foreach my $key ('excluded','weights','threshold','sysmail') {
16190:                 if ($key eq 'excluded') {
16191:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16192:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
16193:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16194:                             (@{$currsetting{'lonstatus'}{$key}})) {
16195:                             my @diffs =
16196:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
16197:                                                                    $currsetting{'lonstatus'}{$key});
16198:                             if (@diffs) {
16199:                                 push(@{$changes{'lonstatus'}},$key);
16200:                             }
16201:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
16202:                             push(@{$changes{'lonstatus'}},$key);
16203:                         }
16204:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16205:                              (@{$currsetting{'lonstatus'}{$key}})) {
16206:                         push(@{$changes{'lonstatus'}},$key);
16207:                     }
16208:                 } elsif ($key eq 'weights') {
16209:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16210:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
16211:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16212:                             foreach my $type ('E','W','N','U') {
16213:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
16214:                                         $currsetting{'lonstatus'}{$key}{$type}) {
16215:                                     push(@{$changes{'lonstatus'}},$key);
16216:                                     last;
16217:                                 }
16218:                             }
16219:                         } else {
16220:                             foreach my $type ('E','W','N','U') {
16221:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
16222:                                     push(@{$changes{'lonstatus'}},$key);
16223:                                     last;
16224:                                 }
16225:                             }
16226:                         }
16227:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16228:                         foreach my $type ('E','W','N','U') {
16229:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
16230:                                 push(@{$changes{'lonstatus'}},$key);
16231:                                 last;
16232:                             }
16233:                         }
16234:                     }
16235:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
16236:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16237:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16238:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
16239:                                 push(@{$changes{'lonstatus'}},$key);
16240:                             }
16241:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
16242:                             push(@{$changes{'lonstatus'}},$key);
16243:                         }
16244:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16245:                         push(@{$changes{'lonstatus'}},$key);
16246:                     }
16247:                 }
16248:             }
16249:         } else {
16250:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16251:                 foreach my $key ('excluded','weights','threshold','sysmail') {
16252:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16253:                         push(@{$changes{'lonstatus'}},$key);
16254:                     }
16255:                 }
16256:             }
16257:         }
16258:     } else {
16259:         my %default;
16260:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
16261:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
16262:         $default{'errormail'} = 'adminemail';
16263:         $default{'packagesmail'} = 'adminemail';
16264:         $default{'helpdeskmail'} = 'supportemail';
16265:         $default{'otherdomsmail'} = 'supportemail';
16266:         $default{'lonstatusmail'} = 'adminemail';
16267:         $default{'requestsmail'} = 'adminemail';
16268:         $default{'updatesmail'} = 'adminemail';
16269:         $default{'hostipmail'} = 'adminemail';
16270:         foreach my $item (@contacts) {
16271:            if ($to{$item} ne $default{$item}) {
16272:                $changes{$item} = 1;
16273:            }
16274:         }
16275:         foreach my $type (@mailings) {
16276:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
16277:                 push(@{$changes{$type}},@{$newsetting{$type}});
16278:             }
16279:             if ($others{$type} ne '') {
16280:                 push(@{$changes{$type}},'others');
16281:             }
16282:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16283:                 if ($bcc{$type} ne '') {
16284:                     push(@{$changes{$type}},'bcc');
16285:                 }
16286:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
16287:                     push(@{$changes{$type}},'include');
16288:                 }
16289:             }
16290:         }
16291:         if (ref($fields) eq 'ARRAY') {
16292:             foreach my $field (@{$fields}) {
16293:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16294:                     push(@{$changes{'helpform'}},$field);
16295:                 }
16296:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16297:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16298:                         push(@{$changes{'helpform'}},'maxsize');
16299:                     }
16300:                 }
16301:             }
16302:         }
16303:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16304:             foreach my $key ('excluded','weights','threshold','sysmail') {
16305:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16306:                     push(@{$changes{'lonstatus'}},$key);
16307:                 }
16308:             }
16309:         }
16310:     }
16311:     foreach my $item (@toggles) {
16312:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
16313:             $changes{$item} = 1;
16314:         } elsif ((!$env{'form.'.$item}) &&
16315:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
16316:             $changes{$item} = 1;
16317:         }
16318:     }
16319:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
16320:                                              $dom);
16321:     if ($putresult eq 'ok') {
16322:         if (keys(%changes) > 0) {
16323:             &Apache::loncommon::devalidate_domconfig_cache($dom);
16324:             if (ref($lastactref) eq 'HASH') {
16325:                 $lastactref->{'domainconfig'} = 1;
16326:             }
16327:             my ($titles,$short_titles)  = &contact_titles();
16328:             $resulttext = &mt('Changes made:').'<ul>';
16329:             foreach my $item (@contacts) {
16330:                 if ($changes{$item}) {
16331:                     $resulttext .= '<li>'.$titles->{$item}.
16332:                                     &mt(' set to: ').
16333:                                     '<span class="LC_cusr_emph">'.
16334:                                     $to{$item}.'</span></li>';
16335:                 }
16336:             }
16337:             foreach my $type (@mailings) {
16338:                 if (ref($changes{$type}) eq 'ARRAY') {
16339:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16340:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
16341:                     } else {
16342:                         $resulttext .= '<li>'.$titles->{$type}.': ';
16343:                     }
16344:                     my @text;
16345:                     foreach my $item (@{$newsetting{$type}}) {
16346:                         push(@text,$short_titles->{$item});
16347:                     }
16348:                     if ($others{$type} ne '') {
16349:                         push(@text,$others{$type});
16350:                     }
16351:                     if (@text) {
16352:                         $resulttext .= '<span class="LC_cusr_emph">'.
16353:                                        join(', ',@text).'</span>';
16354:                     }
16355:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16356:                         if ($bcc{$type} ne '') {
16357:                             my $bcctext;
16358:                             if (@text) {
16359:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
16360:                             } else {
16361:                                 $bcctext = '(Bcc)';
16362:                             }
16363:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
16364:                         } elsif (!@text) {
16365:                             $resulttext .= &mt('No one');
16366:                         }   
16367:                         if ($includestr{$type} ne '') {
16368:                             if ($includeloc{$type} eq 'b') {
16369:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
16370:                             } elsif ($includeloc{$type} eq 's') {
16371:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
16372:                             }
16373:                         }
16374:                     } elsif (!@text) {
16375:                         $resulttext .= &mt('No recipients');
16376:                     }
16377:                     $resulttext .= '</li>';
16378:                 }
16379:             }
16380:             if (ref($changes{'overrides'}) eq 'ARRAY') {
16381:                 my @deletions;
16382:                 foreach my $type (@{$changes{'overrides'}}) {
16383:                     if ($usertypeshash{$type}) {
16384:                         if (grep(/^\Q$type\E/,@overrides)) {
16385:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
16386:                                                       $usertypeshash{$type}).'<ul><li>';
16387:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
16388:                                 my @text;
16389:                                 foreach my $item (@contacts) {
16390:                                     if ($newsetting{'override_'.$type}{$item}) { 
16391:                                         push(@text,$short_titles->{$item});
16392:                                     }
16393:                                 }
16394:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
16395:                                     push(@text,$newsetting{'override_'.$type}{'others'});
16396:                                 }
16397:   
16398:                                 if (@text) {
16399:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
16400:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
16401:                                 }
16402:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
16403:                                     my $bcctext;
16404:                                     if (@text) {
16405:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
16406:                                     } else {
16407:                                         $bcctext = '(Bcc)';
16408:                                     }
16409:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
16410:                                 } elsif (!@text) {
16411:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
16412:                                 }
16413:                                 $resulttext .= '</li>';
16414:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
16415:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
16416:                                     if ($loc eq 'b') {
16417:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
16418:                                     } elsif ($loc eq 's') {
16419:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
16420:                                     }
16421:                                 }
16422:                             }
16423:                             $resulttext .= '</li></ul></li>';
16424:                         } else {
16425:                             push(@deletions,$usertypeshash{$type});
16426:                         }
16427:                     }
16428:                 }
16429:                 if (@deletions) {
16430:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
16431:                                               join(', ',@deletions)).'</li>';
16432:                 }
16433:             }
16434:             my @offon = ('off','on');
16435:             my $corelink = &core_link_msu();
16436:             if ($changes{'reporterrors'}) {
16437:                 $resulttext .= '<li>'.
16438:                                &mt('E-mail error reports to [_1] set to "'.
16439:                                    $offon[$env{'form.reporterrors'}].'".',
16440:                                    $corelink).
16441:                                '</li>';
16442:             }
16443:             if ($changes{'reportupdates'}) {
16444:                 $resulttext .= '<li>'.
16445:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
16446:                                     $offon[$env{'form.reportupdates'}].'".',
16447:                                     $corelink).
16448:                                 '</li>';
16449:             }
16450:             if ($changes{'reportstatus'}) {
16451:                 $resulttext .= '<li>'.
16452:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
16453:                                     $offon[$env{'form.reportstatus'}].'".',
16454:                                     $corelink).
16455:                                 '</li>';
16456:             }
16457:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
16458:                 $resulttext .= '<li>'.
16459:                                &mt('Nightly status check e-mail settings').':<ul>';
16460:                 my (%defval,%use_def,%shown);
16461:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
16462:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
16463:                 $defval{'weights'} =
16464:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
16465:                 $defval{'excluded'} = &mt('None');
16466:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
16467:                     foreach my $item ('threshold','sysmail','weights','excluded') {
16468:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
16469:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
16470:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
16471:                             } elsif ($item eq 'weights') {
16472:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
16473:                                     foreach my $type ('E','W','N','U') {
16474:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
16475:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
16476:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
16477:                                         } else {
16478:                                             $shown{$item} .= $lonstatus_defs->{$type};
16479:                                         }
16480:                                         $shown{$item} .= ', ';
16481:                                     }
16482:                                     $shown{$item} =~ s/, $//;
16483:                                 } else {
16484:                                     $shown{$item} = $defval{$item};
16485:                                 }
16486:                             } elsif ($item eq 'excluded') {
16487:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
16488:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
16489:                                 } else {
16490:                                     $shown{$item} = $defval{$item};
16491:                                 }
16492:                             }
16493:                         } else {
16494:                             $shown{$item} = $defval{$item};
16495:                         }
16496:                     }
16497:                 } else {
16498:                     foreach my $item ('threshold','weights','excluded','sysmail') {
16499:                         $shown{$item} = $defval{$item};
16500:                     }
16501:                 }
16502:                 foreach my $item ('threshold','weights','excluded','sysmail') {
16503:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
16504:                                           $shown{$item}).'</li>';
16505:                 }
16506:                 $resulttext .= '</ul></li>';
16507:             }
16508:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
16509:                 my (@optional,@required,@unused,$maxsizechg);
16510:                 foreach my $field (@{$changes{'helpform'}}) {
16511:                     if ($field eq 'maxsize') {
16512:                         $maxsizechg = 1;
16513:                         next;
16514:                     }
16515:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
16516:                         push(@optional,$field);
16517:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
16518:                         push(@unused,$field);
16519:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
16520:                         push(@required,$field);
16521:                     }
16522:                 }
16523:                 if (@optional) {
16524:                     $resulttext .= '<li>'.
16525:                                    &mt('Help form fields changed to "Optional": [_1].',
16526:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
16527:                                    '</li>';
16528:                 }
16529:                 if (@required) {
16530:                     $resulttext .= '<li>'.
16531:                                    &mt('Help form fields changed to "Required": [_1].',
16532:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
16533:                                    '</li>';
16534:                 }
16535:                 if (@unused) {
16536:                     $resulttext .= '<li>'.
16537:                                    &mt('Help form fields changed to "Not shown": [_1].',
16538:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
16539:                                    '</li>';
16540:                 }
16541:                 if ($maxsizechg) {
16542:                     $resulttext .= '<li>'.
16543:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
16544:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
16545:                                    '</li>';
16546:                 }
16547:             }
16548:             $resulttext .= '</ul>';
16549:         } else {
16550:             $resulttext = &mt('No changes made to contacts and form settings');
16551:         }
16552:     } else {
16553:         $resulttext = '<span class="LC_error">'.
16554:             &mt('An error occurred: [_1].',$putresult).'</span>';
16555:     }
16556:     return $resulttext;
16557: }
16558: 
16559: sub modify_privacy {
16560:     my ($dom,%domconfig) = @_;
16561:     my ($resulttext,%current,%changes);
16562:     if (ref($domconfig{'privacy'}) eq 'HASH') {
16563:         %current = %{$domconfig{'privacy'}};
16564:     }
16565:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
16566:     my @items = ('domain','author','course','community');
16567:     my %names = &Apache::lonlocal::texthash (
16568:                    domain => 'Assigned domain role(s)',
16569:                    author => 'Assigned co-author role(s)',
16570:                    course => 'Assigned course role(s)',
16571:                    community => 'Assigned community role',
16572:                 );
16573:     my %roles = &Apache::lonlocal::texthash (
16574:                    domain => 'Domain role',
16575:                    author => 'Co-author role',
16576:                    course => 'Course role',
16577:                    community => 'Community role',
16578:                 );
16579:     my %titles = &Apache::lonlocal::texthash (
16580:                   approval => 'Approval for role in different domain',
16581:                   othdom   => 'User information available in other domain',
16582:                   priv     => 'Information viewable by privileged user in same domain',
16583:                   unpriv   => 'Information viewable by unprivileged user in same domain',
16584:                   instdom  => 'Other domain shares institution/provider',
16585:                   extdom   => 'Other domain has different institution/provider',
16586:                   none     => 'Not allowed',
16587:                   user     => 'User authorizes',
16588:                   domain   => 'Domain Coordinator authorizes',
16589:                   auto     => 'Unrestricted',
16590:     );
16591:     my %fieldnames = &Apache::lonlocal::texthash (
16592:                         id => 'Student/Employee ID',
16593:                         permanentemail => 'E-mail address',
16594:                         lastname => 'Last Name',
16595:                         firstname => 'First Name',
16596:                         middlename => 'Middle Name',
16597:                         generation => 'Generation',
16598:     );
16599:     my ($othertitle,$usertypes,$types) =
16600:         &Apache::loncommon::sorted_inst_types($dom);
16601:     my (%by_ip,%by_location,@intdoms,@instdoms);
16602:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16603: 
16604:     my %privacyhash = (
16605:                        'approval' => {
16606:                                        instdom => {},
16607:                                        extdom  => {},
16608:                                      },
16609:                        'othdom'   => {},
16610:                        'priv'     => {},
16611:                        'unpriv'   => {},
16612:                       );
16613:     foreach my $item (@items) {
16614:         if (@instdoms > 1) {
16615:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
16616:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
16617:             }
16618:             if (ref($current{'approval'}) eq 'HASH') {
16619:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
16620:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
16621:                         $changes{'approval'} = 1;
16622:                     }
16623:                 }
16624:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
16625:                 $changes{'approval'} = 1;
16626:             }
16627:         }
16628:         if (keys(%by_location) > 0) {
16629:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
16630:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
16631:             }
16632:             if (ref($current{'approval'}) eq 'HASH') {
16633:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
16634:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
16635:                         $changes{'approval'} = 1;
16636:                     }
16637:                 }
16638:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
16639:                 $changes{'approval'} = 1;
16640:             }
16641:         }
16642:         foreach my $status ('priv','unpriv') {
16643:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
16644:             my @newvalues;
16645:             foreach my $field (@possibles) {
16646:                 if (grep(/^\Q$field\E$/,@fields)) {
16647:                     $privacyhash{$status}{$item}{$field} = 1;
16648:                     push(@newvalues,$field);
16649:                 }
16650:             }
16651:             @newvalues = sort(@newvalues);
16652:             if (ref($current{$status}) eq 'HASH') {
16653:                 if (ref($current{$status}{$item}) eq 'HASH') {
16654:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
16655:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
16656:                     if (@diffs > 0) {
16657:                         $changes{$status} = 1;
16658:                     }
16659:                 }
16660:             } else {
16661:                 my @stdfields;
16662:                 foreach my $field (@fields) {
16663:                     if ($field eq 'id') {
16664:                         next if ($status eq 'unpriv');
16665:                         next if (($status eq 'priv') && ($item eq 'community'));
16666:                     }
16667:                     push(@stdfields,$field);
16668:                 }
16669:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
16670:                 if (@diffs > 0) {
16671:                     $changes{$status} = 1;
16672:                 }
16673:             }
16674:         }
16675:     }
16676:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
16677:         my @statuses;
16678:         if (ref($types) eq 'ARRAY') {
16679:             @statuses = @{$types};
16680:         }
16681:         foreach my $type (@statuses,'default') {
16682:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
16683:             my @newvalues;
16684:             foreach my $field (sort(@possfields)) {
16685:                 if (grep(/^\Q$field\E$/,@fields)) {
16686:                     $privacyhash{'othdom'}{$type}{$field} = 1;
16687:                     push(@newvalues,$field);
16688:                 }
16689:             }
16690:             @newvalues = sort(@newvalues);
16691:             if (ref($current{'othdom'}) eq 'HASH') {
16692:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
16693:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
16694:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
16695:                     if (@diffs > 0) {
16696:                         $changes{'othdom'} = 1;
16697:                     }
16698:                 }
16699:             } else {
16700:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
16701:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
16702:                 if (@diffs > 0) {
16703:                     $changes{'othdom'} = 1;
16704:                 }
16705:             }
16706:         }
16707:     }
16708:     my %confighash = (
16709:                         privacy => \%privacyhash,
16710:                      );
16711:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16712:     if ($putresult eq 'ok') {
16713:         if (keys(%changes) > 0) {
16714:             $resulttext = &mt('Changes made: ').'<ul>';
16715:             foreach my $key ('approval','othdom','priv','unpriv') {
16716:                 if ($changes{$key}) {
16717:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16718:                     if ($key eq 'approval') {
16719:                         if (keys(%{$privacyhash{$key}{instdom}})) {
16720:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
16721:                             foreach my $item (@items) {
16722:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
16723:                             }
16724:                             $resulttext .= '</ul></li>';
16725:                         }
16726:                         if (keys(%{$privacyhash{$key}{extdom}})) {
16727:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
16728:                             foreach my $item (@items) {
16729:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
16730:                             }
16731:                             $resulttext .= '</ul></li>';
16732:                         }
16733:                     } elsif ($key eq 'othdom') {
16734:                         my @statuses;
16735:                         if (ref($types) eq 'ARRAY') {
16736:                             @statuses = @{$types};
16737:                         }
16738:                         if (ref($privacyhash{$key}) eq 'HASH') {
16739:                             foreach my $status (@statuses,'default') {
16740:                                 if ($status eq 'default') {
16741:                                     $resulttext .= '<li>'.$othertitle.': ';
16742:                                 } elsif (ref($usertypes) eq 'HASH') {
16743:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
16744:                                 } else {
16745:                                     next;
16746:                                 }
16747:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
16748:                                     if (keys(%{$privacyhash{$key}{$status}})) {
16749:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
16750:                                     } else {
16751:                                         $resulttext .= &mt('none');
16752:                                     }
16753:                                 }
16754:                                 $resulttext .= '</li>';
16755:                             }
16756:                         }
16757:                     } else {
16758:                         foreach my $item (@items) {
16759:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
16760:                                 $resulttext .= '<li>'.$names{$item}.': ';
16761:                                 if (keys(%{$privacyhash{$key}{$item}})) {
16762:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
16763:                                 } else {
16764:                                     $resulttext .= &mt('none');
16765:                                 }
16766:                                 $resulttext .= '</li>';
16767:                             }
16768:                         }
16769:                     }
16770:                     $resulttext .= '</ul></li>';
16771:                 }
16772:             }
16773:         } else {
16774:             $resulttext = &mt('No changes made to user information settings');
16775:         }
16776:     } else {
16777:         $resulttext = '<span class="LC_error">'.
16778:             &mt('An error occurred: [_1]',$putresult).'</span>';
16779:     }
16780:     return $resulttext;
16781: }
16782: 
16783: sub modify_passwords {
16784:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
16785:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
16786:         $updatedefaults,$updateconf);
16787:     my $customfn = 'resetpw.html';
16788:     if (ref($domconfig{'passwords'}) eq 'HASH') {
16789:         %current = %{$domconfig{'passwords'}};
16790:     }
16791:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16792:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16793:     if (ref($types) eq 'ARRAY') {
16794:         @oktypes = @{$types};
16795:     }
16796:     push(@oktypes,'default');
16797: 
16798:     my %titles = &Apache::lonlocal::texthash (
16799:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
16800:         intauth_check  => 'Check bcrypt cost if authenticated',
16801:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
16802:         permanent      => 'Permanent e-mail address',
16803:         critical       => 'Critical notification address',
16804:         notify         => 'Notification address',
16805:         min            => 'Minimum password length',
16806:         max            => 'Maximum password length',
16807:         chars          => 'Required characters',
16808:         expire         => 'Password expiration (days)',
16809:         numsaved       => 'Number of previous passwords to save',
16810:         reset          => 'Resetting Forgotten Password',
16811:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
16812:         rules          => 'Rules for LON-CAPA Passwords',
16813:         crsownerchg    => 'Course Owner Changing Student Passwords',
16814:         username       => 'Username',
16815:         email          => 'E-mail address',
16816:     );
16817: 
16818: #
16819: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
16820: #
16821:     my (%curr_defaults,%save_defaults);
16822:     if (ref($domconfig{'defaults'}) eq 'HASH') {
16823:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
16824:             if ($key =~ /^intauth_(cost|check|switch)$/) {
16825:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
16826:             } else {
16827:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
16828:             }
16829:         }
16830:     }
16831:     my %staticdefaults = (
16832:         'resetlink'      => 2,
16833:         'resetcase'      => \@oktypes,
16834:         'resetprelink'   => 'both',
16835:         'resetemail'     => ['critical','notify','permanent'],
16836:         'intauth_cost'   => 10,
16837:         'intauth_check'  => 0,
16838:         'intauth_switch' => 0,
16839:     );
16840:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
16841:     foreach my $type (@oktypes) {
16842:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
16843:     }
16844:     my $linklife = $env{'form.passwords_link'};
16845:     $linklife =~ s/^\s+|\s+$//g;
16846:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
16847:         $newvalues{'resetlink'} = $linklife;
16848:         if ($current{'resetlink'}) {
16849:             if ($current{'resetlink'} ne $linklife) {
16850:                 $changes{'reset'} = 1;
16851:             }
16852:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16853:             if ($staticdefaults{'resetlink'} ne $linklife) {
16854:                 $changes{'reset'} = 1;
16855:             }
16856:         }
16857:     } elsif ($current{'resetlink'}) {
16858:         $changes{'reset'} = 1;
16859:     }
16860:     my @casesens;
16861:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
16862:     foreach my $case (sort(@posscase)) {
16863:         if (grep(/^\Q$case\E$/,@oktypes)) {
16864:             push(@casesens,$case);
16865:         }
16866:     }
16867:     $newvalues{'resetcase'} = \@casesens;
16868:     if (ref($current{'resetcase'}) eq 'ARRAY') {
16869:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
16870:         if (@diffs > 0) {
16871:             $changes{'reset'} = 1;
16872:         }
16873:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16874:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
16875:         if (@diffs > 0) {
16876:             $changes{'reset'} = 1;
16877:         }
16878:     }
16879:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
16880:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
16881:         if (exists($current{'resetprelink'})) {
16882:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
16883:                 $changes{'reset'} = 1;
16884:             }
16885:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16886:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
16887:                 $changes{'reset'} = 1;
16888:             }
16889:         }
16890:     } elsif ($current{'resetprelink'}) {
16891:         $changes{'reset'} = 1;
16892:     }
16893:     foreach my $type (@oktypes) {
16894:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
16895:         my @postlink;
16896:         foreach my $item (sort(@possplink)) {
16897:             if ($item =~ /^(email|username)$/) {
16898:                 push(@postlink,$item);
16899:             }
16900:         }
16901:         $newvalues{'resetpostlink'}{$type} = \@postlink;
16902:         unless ($changes{'reset'}) {
16903:             if (ref($current{'resetpostlink'}) eq 'HASH') {
16904:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
16905:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
16906:                     if (@diffs > 0) {
16907:                         $changes{'reset'} = 1;
16908:                     }
16909:                 } else {
16910:                     $changes{'reset'} = 1;
16911:                 }
16912:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16913:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
16914:                 if (@diffs > 0) {
16915:                     $changes{'reset'} = 1;
16916:                 }
16917:             }
16918:         }
16919:     }
16920:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
16921:     my @resetemail;
16922:     foreach my $item (sort(@possemailsrc)) {
16923:         if ($item =~ /^(permanent|critical|notify)$/) {
16924:             push(@resetemail,$item);
16925:         }
16926:     }
16927:     $newvalues{'resetemail'} = \@resetemail;
16928:     unless ($changes{'reset'}) {
16929:         if (ref($current{'resetemail'}) eq 'ARRAY') {
16930:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
16931:             if (@diffs > 0) {
16932:                 $changes{'reset'} = 1;
16933:             }
16934:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16935:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
16936:             if (@diffs > 0) {
16937:                 $changes{'reset'} = 1;
16938:             }
16939:         }
16940:     }
16941:     if ($env{'form.passwords_stdtext'} == 0) {
16942:         $newvalues{'resetremove'} = 1;
16943:         unless ($current{'resetremove'}) {
16944:             $changes{'reset'} = 1;
16945:         }
16946:     } elsif ($current{'resetremove'}) {
16947:         $changes{'reset'} = 1;
16948:     }
16949:     if ($env{'form.passwords_customfile.filename'} ne '') {
16950:         my $servadm = $r->dir_config('lonAdmEMail');
16951:         my ($configuserok,$author_ok,$switchserver) =
16952:             &config_check($dom,$confname,$servadm);
16953:         my $error;
16954:         if ($configuserok eq 'ok') {
16955:             if ($switchserver) {
16956:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
16957:             } else {
16958:                 if ($author_ok eq 'ok') {
16959:                     my ($result,$customurl) =
16960:                         &publishlogo($r,'upload','passwords_customfile',$dom,
16961:                                      $confname,'customtext/resetpw','','',$customfn);
16962:                     if ($result eq 'ok') {
16963:                         $newvalues{'resetcustom'} = $customurl;
16964:                         $changes{'reset'} = 1;
16965:                     } else {
16966:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
16967:                     }
16968:                 } else {
16969:                     $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);
16970:                 }
16971:             }
16972:         } else {
16973:             $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);
16974:         }
16975:         if ($error) {
16976:             &Apache::lonnet::logthis($error);
16977:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16978:         }
16979:     } elsif ($current{'resetcustom'}) {
16980:         if ($env{'form.passwords_custom_del'}) {
16981:             $changes{'reset'} = 1;
16982:         } else {
16983:             $newvalues{'resetcustom'} = $current{'resetcustom'};
16984:         }
16985:     }
16986:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
16987:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
16988:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
16989:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
16990:             $changes{'intauth'} = 1;
16991:         }
16992:     } else {
16993:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
16994:     }
16995:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
16996:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
16997:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
16998:             $changes{'intauth'} = 1;
16999:         }
17000:     } else {
17001:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17002:     }
17003:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
17004:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
17005:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
17006:             $changes{'intauth'} = 1;
17007:         }
17008:     } else {
17009:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17010:     }
17011:     foreach my $item ('cost','check','switch') {
17012:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
17013:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
17014:             $updatedefaults = 1;
17015:         }
17016:     }
17017:     foreach my $rule ('min','max','expire','numsaved') {
17018:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
17019:         my $ruleok;
17020:         if ($rule eq 'expire') {
17021:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
17022:                 ($env{'form.passwords_'.$rule} ne '0')) {
17023:                 $ruleok = 1;
17024:             }
17025:         } elsif ($rule eq 'min') {
17026:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
17027:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
17028:                     $ruleok = 1;
17029:                 }
17030:             }
17031:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
17032:                  ($env{'form.passwords_'.$rule} ne '0')) {
17033:             $ruleok = 1;
17034:         }
17035:         if ($ruleok) {
17036:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
17037:             if (exists($current{$rule})) {
17038:                 if ($newvalues{$rule} ne $current{$rule}) {
17039:                     $changes{'rules'} = 1;
17040:                 }
17041:             } elsif ($rule eq 'min') {
17042:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
17043:                     $changes{'rules'} = 1;
17044:                 }
17045:             } else {
17046:                 $changes{'rules'} = 1;
17047:             }
17048:         } elsif (exists($current{$rule})) {
17049:             $changes{'rules'} = 1;
17050:         }
17051:     }
17052:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
17053:     my @chars;
17054:     foreach my $item (sort(@posschars)) {
17055:         if ($item =~ /^(uc|lc|num|spec)$/) {
17056:             push(@chars,$item);
17057:         }
17058:     }
17059:     $newvalues{'chars'} = \@chars;
17060:     unless ($changes{'rules'}) {
17061:         if (ref($current{'chars'}) eq 'ARRAY') {
17062:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
17063:             if (@diffs > 0) {
17064:                 $changes{'rules'} = 1;
17065:             }
17066:         } else {
17067:             if (@chars > 0) {
17068:                 $changes{'rules'} = 1;
17069:             }
17070:         }
17071:     }
17072:     my %crsownerchg = (
17073:                         by => [],
17074:                         for => [],
17075:                       );
17076:     foreach my $item ('by','for') {
17077:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
17078:         foreach my $type (sort(@posstypes)) {
17079:             if (grep(/^\Q$type\E$/,@oktypes)) {
17080:                 push(@{$crsownerchg{$item}},$type);
17081:             }
17082:         }
17083:     }
17084:     $newvalues{'crsownerchg'} = \%crsownerchg;
17085:     if (ref($current{'crsownerchg'}) eq 'HASH') {
17086:         foreach my $item ('by','for') {
17087:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
17088:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
17089:                 if (@diffs > 0) {
17090:                     $changes{'crsownerchg'} = 1;
17091:                     last;
17092:                 }
17093:             }
17094:         }
17095:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
17096:         foreach my $item ('by','for') {
17097:             if (@{$crsownerchg{$item}} > 0) {
17098:                 $changes{'crsownerchg'} = 1;
17099:                 last;
17100:             }
17101:         }
17102:     }
17103: 
17104:     my %confighash = (
17105:                         defaults  => \%save_defaults,
17106:                         passwords => \%newvalues,
17107:                      );
17108:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
17109: 
17110:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17111:     if ($putresult eq 'ok') {
17112:         if (keys(%changes) > 0) {
17113:             $resulttext = &mt('Changes made: ').'<ul>';
17114:             foreach my $key ('reset','intauth','rules','crsownerchg') {
17115:                 if ($changes{$key}) {
17116:                     unless ($key eq 'intauth') {
17117:                         $updateconf = 1;
17118:                     }
17119:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17120:                     if ($key eq 'reset') {
17121:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
17122:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
17123:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
17124:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
17125:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
17126:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
17127:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
17128:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
17129:                             }
17130:                         } else {
17131:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
17132:                         }
17133:                         if ($confighash{'passwords'}{'resetlink'}) {
17134:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
17135:                         } else {
17136:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
17137:                                                   &mt('Will default to 2 hours').'</li>';
17138:                         }
17139:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
17140:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
17141:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
17142:                             } else {
17143:                                 my $casesens;
17144:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
17145:                                     if ($type eq 'default') {
17146:                                         $casesens .= $othertitle.', ';
17147:                                     } elsif ($usertypes->{$type} ne '') {
17148:                                         $casesens .= $usertypes->{$type}.', ';
17149:                                     }
17150:                                 }
17151:                                 $casesens =~ s/\Q, \E$//;
17152:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
17153:                             }
17154:                         } else {
17155:                             $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>';
17156:                         }
17157:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
17158:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
17159:                         } else {
17160:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
17161:                         }
17162:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
17163:                             my $output;
17164:                             if (ref($types) eq 'ARRAY') {
17165:                                 foreach my $type (@{$types}) {
17166:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
17167:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
17168:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
17169:                                         } else {
17170:                                             $output .= $usertypes->{$type}.' -- '.
17171:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
17172:                                         }
17173:                                     }
17174:                                 }
17175:                             }
17176:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
17177:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
17178:                                     $output .= $othertitle.' -- '.&mt('none');
17179:                                 } else {
17180:                                     $output .= $othertitle.' -- '.
17181:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
17182:                                 }
17183:                             }
17184:                             if ($output) {
17185:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
17186:                             } else {
17187:                                 $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>';
17188:                             }
17189:                         } else {
17190:                             $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>';
17191:                         }
17192:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
17193:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
17194:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
17195:                             } else {
17196:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17197:                             }
17198:                         } else {
17199:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17200:                         }
17201:                         if ($confighash{'passwords'}{'resetremove'}) {
17202:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
17203:                         } else {
17204:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
17205:                         }
17206:                         if ($confighash{'passwords'}{'resetcustom'}) {
17207:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
17208:                                                                             &mt('custom text'),600,500,undef,undef,
17209:                                                                             undef,undef,'background-color:#ffffff');
17210:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
17211:                         } else {
17212:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
17213:                         }
17214:                     } elsif ($key eq 'intauth') {
17215:                         foreach my $item ('cost','switch','check') {
17216:                             my $value = $save_defaults{$key.'_'.$item};
17217:                             if ($item eq 'switch') {
17218:                                 my %optiondesc = &Apache::lonlocal::texthash (
17219:                                                      0 => 'No',
17220:                                                      1 => 'Yes',
17221:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
17222:                                                  );
17223:                                 if ($value =~ /^(0|1|2)$/) {
17224:                                     $value = $optiondesc{$value};
17225:                                 } else {
17226:                                     $value = &mt('none -- defaults to No');
17227:                                 }
17228:                             } elsif ($item eq 'check') {
17229:                                 my %optiondesc = &Apache::lonlocal::texthash (
17230:                                                      0 => 'No',
17231:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
17232:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
17233:                                                  );
17234:                                 if ($value =~ /^(0|1|2)$/) {
17235:                                     $value = $optiondesc{$value};
17236:                                 } else {
17237:                                     $value = &mt('none -- defaults to No');
17238:                                 }
17239:                             }
17240:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
17241:                         }
17242:                     } elsif ($key eq 'rules') {
17243:                         foreach my $rule ('min','max','expire','numsaved') {
17244:                             if ($confighash{'passwords'}{$rule} eq '') {
17245:                                 if ($rule eq 'min') {
17246:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
17247:                                                    ' '.&mt('Default of [_1] will be used',
17248:                                                            $Apache::lonnet::passwdmin).'</li>';
17249:                                 } else {
17250:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
17251:                                 }
17252:                             } else {
17253:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
17254:                             }
17255:                         }
17256:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
17257:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
17258:                                 my %rulenames = &Apache::lonlocal::texthash(
17259:                                                      uc => 'At least one upper case letter',
17260:                                                      lc => 'At least one lower case letter',
17261:                                                      num => 'At least one number',
17262:                                                      spec => 'At least one non-alphanumeric',
17263:                                                    );
17264:                                 my $needed = '<ul><li>'.
17265:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
17266:                                              '</li></ul>'; 
17267:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
17268:                             } else {
17269:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17270:                             }
17271:                         } else {
17272:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17273:                         }
17274:                     } elsif ($key eq 'crsownerchg') {
17275:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
17276:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
17277:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
17278:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17279:                             } else {
17280:                                 my %crsownerstr;
17281:                                 foreach my $item ('by','for') {
17282:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
17283:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
17284:                                             if ($type eq 'default') {
17285:                                                 $crsownerstr{$item} .= $othertitle.', ';
17286:                                             } elsif ($usertypes->{$type} ne '') {
17287:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
17288:                                             }
17289:                                         }
17290:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
17291:                                     }
17292:                                 }
17293:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
17294:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
17295:                             }
17296:                         } else {
17297:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17298:                         }
17299:                     }
17300:                     $resulttext .= '</ul></li>';
17301:                 }
17302:             }
17303:             $resulttext .= '</ul>';
17304:         } else {
17305:             $resulttext = &mt('No changes made to password settings');
17306:         }
17307:         my $cachetime = 24*60*60;
17308:         if ($updatedefaults) {
17309:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17310:             if (ref($lastactref) eq 'HASH') {
17311:                 $lastactref->{'domdefaults'} = 1;
17312:             }
17313:         }
17314:         if ($updateconf) {
17315:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
17316:             if (ref($lastactref) eq 'HASH') {
17317:                 $lastactref->{'passwdconf'} = 1;
17318:             }
17319:         }
17320:     } else {
17321:         $resulttext = '<span class="LC_error">'.
17322:             &mt('An error occurred: [_1]',$putresult).'</span>';
17323:     }
17324:     if ($errors) {
17325:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17326:                        $errors.'</ul></p>';
17327:     }
17328:     return $resulttext;
17329: }
17330: 
17331: sub modify_usercreation {
17332:     my ($dom,%domconfig) = @_;
17333:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
17334:     my $warningmsg;
17335:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17336:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17337:             if ($key eq 'cancreate') {
17338:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17339:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17340:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
17341:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17342:                         } else {
17343:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17344:                         }
17345:                     }
17346:                 }
17347:             } elsif ($key eq 'email_rule') {
17348:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17349:             } else {
17350:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17351:             }
17352:         }
17353:     }
17354:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
17355:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
17356:     my @contexts = ('author','course','requestcrs');
17357:     foreach my $item(@contexts) {
17358:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
17359:     }
17360:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17361:         foreach my $item (@contexts) {
17362:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
17363:                 push(@{$changes{'cancreate'}},$item);
17364:             }
17365:         }
17366:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
17367:         foreach my $item (@contexts) {
17368:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
17369:                 if ($cancreate{$item} ne 'any') {
17370:                     push(@{$changes{'cancreate'}},$item);
17371:                 }
17372:             } else {
17373:                 if ($cancreate{$item} ne 'none') {
17374:                     push(@{$changes{'cancreate'}},$item);
17375:                 }
17376:             }
17377:         }
17378:     } else {
17379:         foreach my $item (@contexts)  {
17380:             push(@{$changes{'cancreate'}},$item);
17381:         }
17382:     }
17383: 
17384:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
17385:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
17386:             if (!grep(/^\Q$type\E$/,@username_rule)) {
17387:                 push(@{$changes{'username_rule'}},$type);
17388:             }
17389:         }
17390:         foreach my $type (@username_rule) {
17391:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
17392:                 push(@{$changes{'username_rule'}},$type);
17393:             }
17394:         }
17395:     } else {
17396:         push(@{$changes{'username_rule'}},@username_rule);
17397:     }
17398: 
17399:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
17400:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
17401:             if (!grep(/^\Q$type\E$/,@id_rule)) {
17402:                 push(@{$changes{'id_rule'}},$type);
17403:             }
17404:         }
17405:         foreach my $type (@id_rule) {
17406:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
17407:                 push(@{$changes{'id_rule'}},$type);
17408:             }
17409:         }
17410:     } else {
17411:         push(@{$changes{'id_rule'}},@id_rule);
17412:     }
17413: 
17414:     my @authen_contexts = ('author','course','domain');
17415:     my @authtypes = ('int','krb4','krb5','loc','lti');
17416:     my %authhash;
17417:     foreach my $item (@authen_contexts) {
17418:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
17419:         foreach my $auth (@authtypes) {
17420:             if (grep(/^\Q$auth\E$/,@authallowed)) {
17421:                 $authhash{$item}{$auth} = 1;
17422:             } else {
17423:                 $authhash{$item}{$auth} = 0;
17424:             }
17425:         }
17426:     }
17427:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
17428:         foreach my $item (@authen_contexts) {
17429:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
17430:                 foreach my $auth (@authtypes) {
17431:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
17432:                         push(@{$changes{'authtypes'}},$item);
17433:                         last;
17434:                     }
17435:                 }
17436:             }
17437:         }
17438:     } else {
17439:         foreach my $item (@authen_contexts) {
17440:             push(@{$changes{'authtypes'}},$item);
17441:         }
17442:     }
17443: 
17444:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
17445:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
17446:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
17447:     $save_usercreate{'id_rule'} = \@id_rule;
17448:     $save_usercreate{'username_rule'} = \@username_rule,
17449:     $save_usercreate{'authtypes'} = \%authhash;
17450: 
17451:     my %usercreation_hash =  (
17452:         usercreation     => \%save_usercreate,
17453:     );
17454: 
17455:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
17456:                                              $dom);
17457: 
17458:     if ($putresult eq 'ok') {
17459:         if (keys(%changes) > 0) {
17460:             $resulttext = &mt('Changes made:').'<ul>';
17461:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17462:                 my %lt = &usercreation_types();
17463:                 foreach my $type (@{$changes{'cancreate'}}) {
17464:                     my $chgtext = $lt{$type}.', ';
17465:                     if ($cancreate{$type} eq 'none') {
17466:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
17467:                     } elsif ($cancreate{$type} eq 'any') {
17468:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
17469:                     } elsif ($cancreate{$type} eq 'official') {
17470:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
17471:                     } elsif ($cancreate{$type} eq 'unofficial') {
17472:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
17473:                     }
17474:                     $resulttext .= '<li>'.$chgtext.'</li>';
17475:                 }
17476:             }
17477:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
17478:                 my ($rules,$ruleorder) = 
17479:                     &Apache::lonnet::inst_userrules($dom,'username');
17480:                 my $chgtext = '<ul>';
17481:                 foreach my $type (@username_rule) {
17482:                     if (ref($rules->{$type}) eq 'HASH') {
17483:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
17484:                     }
17485:                 }
17486:                 $chgtext .= '</ul>';
17487:                 if (@username_rule > 0) {
17488:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
17489:                 } else {
17490:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
17491:                 }
17492:             }
17493:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
17494:                 my ($idrules,$idruleorder) = 
17495:                     &Apache::lonnet::inst_userrules($dom,'id');
17496:                 my $chgtext = '<ul>';
17497:                 foreach my $type (@id_rule) {
17498:                     if (ref($idrules->{$type}) eq 'HASH') {
17499:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
17500:                     }
17501:                 }
17502:                 $chgtext .= '</ul>';
17503:                 if (@id_rule > 0) {
17504:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
17505:                 } else {
17506:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
17507:                 }
17508:             }
17509:             my %authname = &authtype_names();
17510:             my %context_title = &context_names();
17511:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
17512:                 my $chgtext = '<ul>';
17513:                 foreach my $type (@{$changes{'authtypes'}}) {
17514:                     my @allowed;
17515:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
17516:                     foreach my $auth (@authtypes) {
17517:                         if ($authhash{$type}{$auth}) {
17518:                             push(@allowed,$authname{$auth});
17519:                         }
17520:                     }
17521:                     if (@allowed > 0) {
17522:                         $chgtext .= join(', ',@allowed).'</li>';
17523:                     } else {
17524:                         $chgtext .= &mt('none').'</li>';
17525:                     }
17526:                 }
17527:                 $chgtext .= '</ul>';
17528:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
17529:                 $resulttext .= '</li>';
17530:             }
17531:             $resulttext .= '</ul>';
17532:         } else {
17533:             $resulttext = &mt('No changes made to user creation settings');
17534:         }
17535:     } else {
17536:         $resulttext = '<span class="LC_error">'.
17537:             &mt('An error occurred: [_1]',$putresult).'</span>';
17538:     }
17539:     if ($warningmsg ne '') {
17540:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17541:     }
17542:     return $resulttext;
17543: }
17544: 
17545: sub modify_selfcreation {
17546:     my ($dom,$lastactref,%domconfig) = @_;
17547:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
17548:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
17549:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17550:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
17551:     if (ref($typesref) eq 'ARRAY') {
17552:         @types = @{$typesref};
17553:     }
17554:     if (ref($usertypesref) eq 'HASH') {
17555:         %usertypes = %{$usertypesref};
17556:     }
17557:     $usertypes{'default'} = $othertitle;
17558: #
17559: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
17560: #
17561:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17562:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17563:             if ($key eq 'cancreate') {
17564:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17565:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17566:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
17567:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
17568:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
17569:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
17570:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
17571:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
17572:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17573:                         } else {
17574:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17575:                         }
17576:                     }
17577:                 }
17578:             } elsif ($key eq 'email_rule') {
17579:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17580:             } else {
17581:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17582:             }
17583:         }
17584:     }
17585: #
17586: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
17587: #
17588:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17589:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17590:             if ($key eq 'selfcreate') {
17591:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
17592:             } else {
17593:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
17594:             }
17595:         }
17596:     }
17597: #
17598: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
17599: #
17600:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17601:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
17602:             if ($key eq 'inststatusguest') {
17603:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
17604:             } else {
17605:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
17606:             }
17607:         }
17608:     }
17609: 
17610:     my @contexts = ('selfcreate');
17611:     @{$cancreate{'selfcreate'}} = ();
17612:     %{$cancreate{'emailusername'}} = ();
17613:     if (@types) {
17614:         @{$cancreate{'statustocreate'}} = ();
17615:     }
17616:     %{$cancreate{'selfcreateprocessing'}} = ();
17617:     %{$cancreate{'shibenv'}} = ();
17618:     %{$cancreate{'emailverified'}} = ();
17619:     %{$cancreate{'emailoptions'}} = ();
17620:     %{$cancreate{'emaildomain'}} = ();
17621:     my %selfcreatetypes = (
17622:                              sso   => 'users authenticated by institutional single sign on',
17623:                              login => 'users authenticated by institutional log-in',
17624:                              email => 'users verified by e-mail',
17625:                           );
17626: #
17627: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
17628: # is permitted.
17629: #
17630:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
17631: 
17632:     my (@statuses,%email_rule);
17633:     foreach my $item ('login','sso','email') {
17634:         if ($item eq 'email') {
17635:             if ($env{'form.cancreate_email'}) {
17636:                 if (@types) {
17637:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
17638:                     foreach my $status (@poss_statuses) {
17639:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
17640:                             push(@statuses,$status);
17641:                         }
17642:                     }
17643:                     $save_inststatus{'inststatusguest'} = \@statuses;
17644:                 } else {
17645:                     push(@statuses,'default');
17646:                 }
17647:                 if (@statuses) {
17648:                     my %curr_rule;
17649:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
17650:                         foreach my $type (@statuses) {
17651:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
17652:                         }
17653:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
17654:                         foreach my $type (@statuses) {
17655:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
17656:                         }
17657:                     }
17658:                     push(@{$cancreate{'selfcreate'}},'email');
17659:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
17660:                     my %curremaildom;
17661:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
17662:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
17663:                     }
17664:                     foreach my $type (@statuses) {
17665:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
17666:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
17667:                         }
17668:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
17669:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
17670:                         }
17671:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
17672: #
17673: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
17674: #
17675:                             my $chosen = $1;
17676:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
17677:                                 my $emaildom;
17678:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
17679:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
17680:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
17681:                                     if (ref($curremaildom{$type}) eq 'HASH') {
17682:                                         if (exists($curremaildom{$type}{$chosen})) {
17683:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
17684:                                                 push(@{$changes{'cancreate'}},'emaildomain');
17685:                                             }
17686:                                         } elsif ($emaildom ne '') {
17687:                                             push(@{$changes{'cancreate'}},'emaildomain');
17688:                                         }
17689:                                     } elsif ($emaildom ne '') {
17690:                                         push(@{$changes{'cancreate'}},'emaildomain');
17691:                                     } 
17692:                                 }
17693:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
17694:                             } elsif ($chosen eq 'custom') {
17695:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
17696:                                 $email_rule{$type} = [];
17697:                                 if (ref($emailrules) eq 'HASH') {
17698:                                     foreach my $rule (@possemail_rules) {
17699:                                         if (exists($emailrules->{$rule})) {
17700:                                             push(@{$email_rule{$type}},$rule);
17701:                                         }
17702:                                     }
17703:                                 }
17704:                                 if (@{$email_rule{$type}}) {
17705:                                     $cancreate{'emailoptions'}{$type} = 'custom';
17706:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
17707:                                         if (@{$curr_rule{$type}} > 0) {
17708:                                             foreach my $rule (@{$curr_rule{$type}}) {
17709:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
17710:                                                     push(@{$changes{'email_rule'}},$type);
17711:                                                 }
17712:                                             }
17713:                                         }
17714:                                         foreach my $type (@{$email_rule{$type}}) {
17715:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
17716:                                                 push(@{$changes{'email_rule'}},$type);
17717:                                             }
17718:                                         }
17719:                                     } else {
17720:                                         push(@{$changes{'email_rule'}},$type);
17721:                                     }
17722:                                 }
17723:                             } else {
17724:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
17725:                             }
17726:                         }
17727:                     }
17728:                     if (@types) {
17729:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17730:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
17731:                             if (@changed) {
17732:                                 push(@{$changes{'inststatus'}},'inststatusguest');
17733:                             }
17734:                         } else {
17735:                             push(@{$changes{'inststatus'}},'inststatusguest');
17736:                         }
17737:                     }
17738:                 } else {
17739:                     delete($env{'form.cancreate_email'});
17740:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17741:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
17742:                             push(@{$changes{'inststatus'}},'inststatusguest');
17743:                         }
17744:                     }
17745:                 }
17746:             } else {
17747:                 $save_inststatus{'inststatusguest'} = [];
17748:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17749:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
17750:                         push(@{$changes{'inststatus'}},'inststatusguest');
17751:                     }
17752:                 }
17753:             }
17754:         } else {
17755:             if ($env{'form.cancreate_'.$item}) {
17756:                 push(@{$cancreate{'selfcreate'}},$item);
17757:             }
17758:         }
17759:     }
17760:     my (%userinfo,%savecaptcha);
17761:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
17762: #
17763: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
17764: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
17765: #
17766: 
17767:     if ($env{'form.cancreate_email'}) {
17768:         push(@contexts,'emailusername');
17769:         if (@statuses) {
17770:             foreach my $type (@statuses) {
17771:                 if (ref($infofields) eq 'ARRAY') {
17772:                     foreach my $field (@{$infofields}) {
17773:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
17774:                             $cancreate{'emailusername'}{$type}{$field} = $1;
17775:                         }
17776:                     }
17777:                 }
17778:             }
17779:         }
17780: #
17781: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
17782: # queued requests for self-creation of account verified by e-mail.
17783: #
17784: 
17785:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
17786:         @approvalnotify = sort(@approvalnotify);
17787:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
17788:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17789:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
17790:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
17791:                     push(@{$changes{'cancreate'}},'notify');
17792:                 }
17793:             } else {
17794:                 if ($cancreate{'notify'}{'approval'}) {
17795:                     push(@{$changes{'cancreate'}},'notify');
17796:                 }
17797:             }
17798:         } elsif ($cancreate{'notify'}{'approval'}) {
17799:             push(@{$changes{'cancreate'}},'notify');
17800:         }
17801: 
17802:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
17803:     }
17804: #  
17805: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
17806: # institutional log-in.
17807: #
17808:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
17809:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
17810:                ($domdefaults{'auth_def'} eq 'localauth'))) {
17811:             $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.').' '.
17812:                           &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.');
17813:         }
17814:     }
17815:     my @fields = ('lastname','firstname','middlename','generation',
17816:                   'permanentemail','id');
17817:     my @shibfields = (@fields,'inststatus');
17818:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17819: #
17820: # Where usernames may created for institutional log-in and/or institutional single sign on:
17821: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
17822: # may self-create accounts 
17823: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
17824: # which the user may supply, if institutional data is unavailable.
17825: #
17826:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
17827:         if (@types) {
17828:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
17829:             push(@contexts,'statustocreate');
17830:             foreach my $type (@types) {
17831:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
17832:                 foreach my $field (@fields) {
17833:                     if (grep(/^\Q$field\E$/,@modifiable)) {
17834:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
17835:                     } else {
17836:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
17837:                     }
17838:                 }
17839:             }
17840:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
17841:                 foreach my $type (@types) {
17842:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
17843:                         foreach my $field (@fields) {
17844:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
17845:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
17846:                                 push(@{$changes{'selfcreate'}},$type);
17847:                                 last;
17848:                             }
17849:                         }
17850:                     }
17851:                 }
17852:             } else {
17853:                 foreach my $type (@types) {
17854:                     push(@{$changes{'selfcreate'}},$type);
17855:                 }
17856:             }
17857:         }
17858:         foreach my $field (@shibfields) {
17859:             if ($env{'form.shibenv_'.$field} ne '') {
17860:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
17861:             }
17862:         }
17863:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17864:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
17865:                 foreach my $field (@shibfields) {
17866:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
17867:                         push(@{$changes{'cancreate'}},'shibenv');
17868:                     }
17869:                 }
17870:             } else {
17871:                 foreach my $field (@shibfields) {
17872:                     if ($env{'form.shibenv_'.$field}) {
17873:                         push(@{$changes{'cancreate'}},'shibenv');
17874:                         last;
17875:                     }
17876:                 }
17877:             }
17878:         }
17879:     }
17880:     foreach my $item (@contexts) {
17881:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
17882:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
17883:                 if (ref($cancreate{$item}) eq 'ARRAY') {
17884:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
17885:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17886:                             push(@{$changes{'cancreate'}},$item);
17887:                         }
17888:                     }
17889:                 }
17890:             }
17891:             if (ref($cancreate{$item}) eq 'ARRAY') {
17892:                 foreach my $type (@{$cancreate{$item}}) {
17893:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
17894:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17895:                             push(@{$changes{'cancreate'}},$item);
17896:                         }
17897:                     }
17898:                 }
17899:             }
17900:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
17901:             if (ref($cancreate{$item}) eq 'HASH') {
17902:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
17903:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17904:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
17905:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17906:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17907:                                     push(@{$changes{'cancreate'}},$item);
17908:                                 }
17909:                             }
17910:                         }
17911:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17912:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17913:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17914:                                 push(@{$changes{'cancreate'}},$item);
17915:                             }
17916:                         }
17917:                     }
17918:                 }
17919:                 foreach my $type (keys(%{$cancreate{$item}})) {
17920:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17921:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17922:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17923:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17924:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17925:                                         push(@{$changes{'cancreate'}},$item);
17926:                                     }
17927:                                 }
17928:                             } else {
17929:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17930:                                     push(@{$changes{'cancreate'}},$item);
17931:                                 }
17932:                             }
17933:                         }
17934:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17935:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17936:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17937:                                 push(@{$changes{'cancreate'}},$item);
17938:                             }
17939:                         }
17940:                     }
17941:                 }
17942:             }
17943:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
17944:             if (ref($cancreate{$item}) eq 'ARRAY') {
17945:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
17946:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17947:                         push(@{$changes{'cancreate'}},$item);
17948:                     }
17949:                 }
17950:             }
17951:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17952:             if (ref($cancreate{$item}) eq 'HASH') {
17953:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17954:                     push(@{$changes{'cancreate'}},$item);
17955:                 }
17956:             }
17957:         } elsif ($item eq 'emailusername') {
17958:             if (ref($cancreate{$item}) eq 'HASH') {
17959:                 foreach my $type (keys(%{$cancreate{$item}})) {
17960:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17961:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17962:                             if ($cancreate{$item}{$type}{$field}) {
17963:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17964:                                     push(@{$changes{'cancreate'}},$item);
17965:                                 }
17966:                                 last;
17967:                             }
17968:                         }
17969:                     }
17970:                 }
17971:             }
17972:         }
17973:     }
17974: #
17975: # Populate %save_usercreate hash with updates to self-creation configuration.
17976: #
17977:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
17978:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
17979:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
17980:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
17981:     if (ref($cancreate{'notify'}) eq 'HASH') {
17982:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
17983:     }
17984:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
17985:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
17986:     }
17987:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
17988:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
17989:     }
17990:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
17991:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
17992:     }
17993:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
17994:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
17995:     }
17996:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17997:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
17998:     }
17999:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
18000:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
18001:     }
18002:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
18003:     $save_usercreate{'email_rule'} = \%email_rule;
18004: 
18005:     my %userconfig_hash = (
18006:             usercreation     => \%save_usercreate,
18007:             usermodification => \%save_usermodify,
18008:             inststatus       => \%save_inststatus,
18009:     );
18010: 
18011:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
18012:                                              $dom);
18013: #
18014: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
18015: #
18016:     if ($putresult eq 'ok') {
18017:         if (keys(%changes) > 0) {
18018:             $resulttext = &mt('Changes made:').'<ul>';
18019:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18020:                 my %lt = &selfcreation_types();
18021:                 foreach my $type (@{$changes{'cancreate'}}) {
18022:                     my $chgtext = '';
18023:                     if ($type eq 'selfcreate') {
18024:                         if (@{$cancreate{$type}} == 0) {
18025:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
18026:                         } else {
18027:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
18028:                                         '<ul>';
18029:                             foreach my $case (@{$cancreate{$type}}) {
18030:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
18031:                             }
18032:                             $chgtext .= '</ul>';
18033:                             if (ref($cancreate{$type}) eq 'ARRAY') {
18034:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
18035:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18036:                                         if (@{$cancreate{'statustocreate'}} == 0) {
18037:                                             $chgtext .= '<span class="LC_warning">'.
18038:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
18039:                                                         '</span><br />';
18040:                                         }
18041:                                     }
18042:                                 }
18043:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
18044:                                     if (!@statuses) {
18045:                                         $chgtext .= '<span class="LC_warning">'.
18046:                                                     &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.").
18047:                                                     '</span><br />';
18048: 
18049:                                     }
18050:                                 }
18051:                             }
18052:                         }
18053:                     } elsif ($type eq 'shibenv') {
18054:                         if (keys(%{$cancreate{$type}}) == 0) {
18055:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
18056:                         } else {
18057:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
18058:                                         '<ul>';
18059:                             foreach my $field (@shibfields) {
18060:                                 next if ($cancreate{$type}{$field} eq '');
18061:                                 if ($field eq 'inststatus') {
18062:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
18063:                                 } else {
18064:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
18065:                                 }
18066:                             }
18067:                             $chgtext .= '</ul>';
18068:                         }
18069:                     } elsif ($type eq 'statustocreate') {
18070:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
18071:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
18072:                             if (@{$cancreate{'selfcreate'}} > 0) {
18073:                                 if (@{$cancreate{'statustocreate'}} == 0) {
18074:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
18075:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
18076:                                         $chgtext .= '<br />'.
18077:                                                     '<span class="LC_warning">'.
18078:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
18079:                                                     '</span>';
18080:                                     }
18081:                                 } elsif (keys(%usertypes) > 0) {
18082:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18083:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
18084:                                     } else {
18085:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
18086:                                     }
18087:                                     $chgtext .= '<ul>';
18088:                                     foreach my $case (@{$cancreate{$type}}) {
18089:                                         if ($case eq 'default') {
18090:                                             $chgtext .= '<li>'.$othertitle.'</li>';
18091:                                         } else {
18092:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
18093:                                         }
18094:                                     }
18095:                                     $chgtext .= '</ul>';
18096:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18097:                                         $chgtext .= '<span class="LC_warning">'.
18098:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
18099:                                                     '</span>';
18100:                                     }
18101:                                 }
18102:                             } else {
18103:                                 if (@{$cancreate{$type}} == 0) {
18104:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
18105:                                 } else {
18106:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
18107:                                 }
18108:                             }
18109:                             $chgtext .= '<br />';
18110:                         }
18111:                     } elsif ($type eq 'selfcreateprocessing') {
18112:                         my %choices = &Apache::lonlocal::texthash (
18113:                                                                     automatic => 'Automatic approval',
18114:                                                                     approval  => 'Queued for approval',
18115:                                                                   );
18116:                         if (@types) {
18117:                             if (@statuses) {
18118:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
18119:                                             '<ul>';
18120:                                 foreach my $status (@statuses) {
18121:                                     if ($status eq 'default') {
18122:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18123:                                     } else {
18124:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18125:                                     }
18126:                                 }
18127:                                 $chgtext .= '</ul>';
18128:                             }
18129:                         } else {
18130:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
18131:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
18132:                         }
18133:                     } elsif ($type eq 'emailverified') {
18134:                         my %options = &Apache::lonlocal::texthash (
18135:                                                                     all   => 'Same as e-mail',
18136:                                                                     first => 'Omit @domain',
18137:                                                                     free  => 'Free to choose',
18138:                                                                   );
18139:                         if (@types) {
18140:                             if (@statuses) {
18141:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
18142:                                             '<ul>';
18143:                                 foreach my $status (@statuses) {
18144:                                     if ($status eq 'default') {
18145:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18146:                                     } else {
18147:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18148:                                     }
18149:                                 }
18150:                                 $chgtext .= '</ul>';
18151:                             }
18152:                         } else {
18153:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
18154:                                             $options{$cancreate{'emailverified'}{'default'}});
18155:                         }
18156:                     } elsif ($type eq 'emailoptions') {
18157:                         my %options = &Apache::lonlocal::texthash (
18158:                                                                     any     => 'Any e-mail',
18159:                                                                     inst    => 'Institutional only',
18160:                                                                     noninst => 'Non-institutional only',
18161:                                                                     custom  => 'Custom restrictions',
18162:                                                                   );
18163:                         if (@types) {
18164:                             if (@statuses) {
18165:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
18166:                                             '<ul>';
18167:                                 foreach my $status (@statuses) {
18168:                                     if ($type eq 'default') {
18169:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18170:                                     } else {
18171:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18172:                                     }
18173:                                 }
18174:                                 $chgtext .= '</ul>';
18175:                             }
18176:                         } else {
18177:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
18178:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
18179:                             } else {
18180:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
18181:                                                 $options{$cancreate{'emailoptions'}{'default'}});
18182:                             }
18183:                         }
18184:                     } elsif ($type eq 'emaildomain') {
18185:                         my $output;
18186:                         if (@statuses) {
18187:                             foreach my $type (@statuses) {
18188:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
18189:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
18190:                                         if ($type eq 'default') {
18191:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18192:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18193:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18194:                                             } else {
18195:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
18196:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18197:                                             }
18198:                                         } else {
18199:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18200:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18201:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18202:                                             } else {
18203:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
18204:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
18205:                                             }
18206:                                         }
18207:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
18208:                                         if ($type eq 'default') {
18209:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18210:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18211:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18212:                                             } else {
18213:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
18214:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18215:                                             }
18216:                                         } else {
18217:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18218:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18219:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18220:                                             } else {
18221:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
18222:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
18223:                                             }
18224:                                         }
18225:                                     }
18226:                                 }
18227:                             }
18228:                         }
18229:                         if ($output ne '') {
18230:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
18231:                                         '<ul>'.$output.'</ul>';
18232:                         }
18233:                     } elsif ($type eq 'captcha') {
18234:                         if ($savecaptcha{$type} eq 'notused') {
18235:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
18236:                         } else {
18237:                             my %captchas = &captcha_phrases();
18238:                             if ($captchas{$savecaptcha{$type}}) {
18239:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
18240:                             } else {
18241:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
18242:                             }
18243:                         }
18244:                     } elsif ($type eq 'recaptchakeys') {
18245:                         my ($privkey,$pubkey);
18246:                         if (ref($savecaptcha{$type}) eq 'HASH') {
18247:                             $pubkey = $savecaptcha{$type}{'public'};
18248:                             $privkey = $savecaptcha{$type}{'private'};
18249:                         }
18250:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
18251:                         if (!$pubkey) {
18252:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
18253:                         } else {
18254:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
18255:                         }
18256:                         if (!$privkey) {
18257:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
18258:                         } else {
18259:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
18260:                         }
18261:                         $chgtext .= '</ul>';
18262:                     } elsif ($type eq 'recaptchaversion') {
18263:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
18264:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
18265:                         }
18266:                     } elsif ($type eq 'emailusername') {
18267:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
18268:                             if (@statuses) {
18269:                                 foreach my $type (@statuses) {
18270:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
18271:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
18272:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
18273:                                                     '<ul>';
18274:                                             foreach my $field (@{$infofields}) {
18275:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
18276:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
18277:                                                 }
18278:                                             }
18279:                                             $chgtext .= '</ul>';
18280:                                         } else {
18281:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18282:                                         }
18283:                                     } else {
18284:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18285:                                     }
18286:                                 }
18287:                             }
18288:                         }
18289:                     } elsif ($type eq 'notify') {
18290:                         my $numapprove = 0;
18291:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
18292:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
18293:                                 if ($cancreate{'notify'}{'approval'}) {
18294:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
18295:                                     $numapprove ++;
18296:                                 }
18297:                             }
18298:                         }
18299:                         unless ($numapprove) {
18300:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
18301:                         }
18302:                     }
18303:                     if ($chgtext) {
18304:                         $resulttext .= '<li>'.$chgtext.'</li>';
18305:                     }
18306:                 }
18307:             }
18308:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
18309:                 my ($emailrules,$emailruleorder) =
18310:                     &Apache::lonnet::inst_userrules($dom,'email');
18311:                 foreach my $type (@{$changes{'email_rule'}}) {
18312:                     if (ref($email_rule{$type}) eq 'ARRAY') {
18313:                         my $chgtext = '<ul>';
18314:                         foreach my $rule (@{$email_rule{$type}}) {
18315:                             if (ref($emailrules->{$rule}) eq 'HASH') {
18316:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
18317:                             }
18318:                         }
18319:                         $chgtext .= '</ul>';
18320:                         my $typename;
18321:                         if (@types) {
18322:                             if ($type eq 'default') {
18323:                                 $typename = $othertitle;
18324:                             } else {
18325:                                 $typename = $usertypes{$type};
18326:                             } 
18327:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
18328:                         }
18329:                         if (@{$email_rule{$type}} > 0) {
18330:                             $resulttext .= '<li>'.
18331:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
18332:                                                $usertypes{$type}).
18333:                                            $chgtext.
18334:                                            '</li>';
18335:                         } else {
18336:                             $resulttext .= '<li>'.
18337:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
18338:                                            '</li>'.
18339:                                            &mt('(Affiliation: [_1])',$typename);
18340:                         }
18341:                     }
18342:                 }
18343:             }
18344:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
18345:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
18346:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
18347:                         my $chgtext = '<ul>';
18348:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
18349:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
18350:                         }
18351:                         $chgtext .= '</ul>';
18352:                         $resulttext .= '<li>'.
18353:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
18354:                                           $chgtext.
18355:                                        '</li>';
18356:                     } else {
18357:                         $resulttext .= '<li>'.
18358:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
18359:                                        '</li>';
18360:                     }
18361:                 }
18362:             }
18363:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
18364:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
18365:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18366:                 foreach my $type (@{$changes{'selfcreate'}}) {
18367:                     my $typename = $type;
18368:                     if (keys(%usertypes) > 0) {
18369:                         if ($usertypes{$type} ne '') {
18370:                             $typename = $usertypes{$type};
18371:                         }
18372:                     }
18373:                     my @modifiable;
18374:                     $resulttext .= '<li>'.
18375:                                     &mt('Self-creation of account by users with status: [_1]',
18376:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
18377:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
18378:                     foreach my $field (@fields) {
18379:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
18380:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
18381:                         }
18382:                     }
18383:                     if (@modifiable > 0) {
18384:                         $resulttext .= join(', ',@modifiable);
18385:                     } else {
18386:                         $resulttext .= &mt('none');
18387:                     }
18388:                     $resulttext .= '</li>';
18389:                 }
18390:                 $resulttext .= '</ul></li>';
18391:             }
18392:             $resulttext .= '</ul>';
18393:             my $cachetime = 24*60*60;
18394:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
18395:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18396:             if (ref($lastactref) eq 'HASH') {
18397:                 $lastactref->{'domdefaults'} = 1;
18398:             }
18399:         } else {
18400:             $resulttext = &mt('No changes made to self-creation settings');
18401:         }
18402:     } else {
18403:         $resulttext = '<span class="LC_error">'.
18404:             &mt('An error occurred: [_1]',$putresult).'</span>';
18405:     }
18406:     if ($warningmsg ne '') {
18407:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18408:     }
18409:     return $resulttext;
18410: }
18411: 
18412: sub process_captcha {
18413:     my ($container,$changes,$newsettings,$currsettings) = @_;
18414:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
18415:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
18416:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
18417:         $newsettings->{'captcha'} = 'original';
18418:     }
18419:     my %current;
18420:     if (ref($currsettings) eq 'HASH') {
18421:         %current = %{$currsettings};
18422:     }
18423:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
18424:         if ($container eq 'cancreate') {
18425:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18426:                 push(@{$changes->{'cancreate'}},'captcha');
18427:             } elsif (!defined($changes->{'cancreate'})) {
18428:                 $changes->{'cancreate'} = ['captcha'];
18429:             }
18430:         } elsif ($container eq 'passwords') {
18431:             $changes->{'reset'} = 1;
18432:         } else {
18433:             $changes->{'captcha'} = 1;
18434:         }
18435:     }
18436:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
18437:     if ($newsettings->{'captcha'} eq 'recaptcha') {
18438:         $newpub = $env{'form.'.$container.'_recaptchapub'};
18439:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
18440:         $newpub =~ s/[^\w\-]//g;
18441:         $newpriv =~ s/[^\w\-]//g;
18442:         $newsettings->{'recaptchakeys'} = {
18443:                                              public  => $newpub,
18444:                                              private => $newpriv,
18445:                                           };
18446:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
18447:         $newversion =~ s/\D//g;
18448:         if ($newversion ne '2') {
18449:             $newversion = 1;
18450:         }
18451:         $newsettings->{'recaptchaversion'} = $newversion;
18452:     }
18453:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
18454:         $currpub = $current{'recaptchakeys'}{'public'};
18455:         $currpriv = $current{'recaptchakeys'}{'private'};
18456:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
18457:             $newsettings->{'recaptchakeys'} = {
18458:                                                  public  => '',
18459:                                                  private => '',
18460:                                               }
18461:         }
18462:     }
18463:     if ($current{'captcha'} eq 'recaptcha') {
18464:         $currversion = $current{'recaptchaversion'};
18465:         if ($currversion ne '2') {
18466:             $currversion = 1;
18467:         }
18468:     }
18469:     if ($currversion ne $newversion) {
18470:         if ($container eq 'cancreate') {
18471:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18472:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
18473:             } elsif (!defined($changes->{'cancreate'})) {
18474:                 $changes->{'cancreate'} = ['recaptchaversion'];
18475:             }
18476:         } elsif ($container eq 'passwords') {
18477:             $changes->{'reset'} = 1;
18478:         } else {
18479:             $changes->{'recaptchaversion'} = 1;
18480:         }
18481:     }
18482:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
18483:         if ($container eq 'cancreate') {
18484:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18485:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
18486:             } elsif (!defined($changes->{'cancreate'})) {
18487:                 $changes->{'cancreate'} = ['recaptchakeys'];
18488:             }
18489:         } elsif ($container eq 'passwords') {
18490:             $changes->{'reset'} = 1;
18491:         } else {
18492:             $changes->{'recaptchakeys'} = 1;
18493:         }
18494:     }
18495:     return;
18496: }
18497: 
18498: sub modify_usermodification {
18499:     my ($dom,%domconfig) = @_;
18500:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
18501:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18502:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18503:             if ($key eq 'selfcreate') {
18504:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
18505:             } else {  
18506:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
18507:             }
18508:         }
18509:     }
18510:     my @contexts = ('author','course');
18511:     my %context_title = (
18512:                            author => 'In author context',
18513:                            course => 'In course context',
18514:                         );
18515:     my @fields = ('lastname','firstname','middlename','generation',
18516:                   'permanentemail','id');
18517:     my %roles = (
18518:                   author => ['ca','aa'],
18519:                   course => ['st','ep','ta','in','cr'],
18520:                 );
18521:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18522:     foreach my $context (@contexts) {
18523:         foreach my $role (@{$roles{$context}}) {
18524:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
18525:             foreach my $item (@fields) {
18526:                 if (grep(/^\Q$item\E$/,@modifiable)) {
18527:                     $modifyhash{$context}{$role}{$item} = 1;
18528:                 } else {
18529:                     $modifyhash{$context}{$role}{$item} = 0;
18530:                 }
18531:             }
18532:         }
18533:         if (ref($curr_usermodification{$context}) eq 'HASH') {
18534:             foreach my $role (@{$roles{$context}}) {
18535:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
18536:                     foreach my $field (@fields) {
18537:                         if ($modifyhash{$context}{$role}{$field} ne 
18538:                                 $curr_usermodification{$context}{$role}{$field}) {
18539:                             push(@{$changes{$context}},$role);
18540:                             last;
18541:                         }
18542:                     }
18543:                 }
18544:             }
18545:         } else {
18546:             foreach my $context (@contexts) {
18547:                 foreach my $role (@{$roles{$context}}) {
18548:                     push(@{$changes{$context}},$role);
18549:                 }
18550:             }
18551:         }
18552:     }
18553:     my %usermodification_hash =  (
18554:                                    usermodification => \%modifyhash,
18555:                                  );
18556:     my $putresult = &Apache::lonnet::put_dom('configuration',
18557:                                              \%usermodification_hash,$dom);
18558:     if ($putresult eq 'ok') {
18559:         if (keys(%changes) > 0) {
18560:             $resulttext = &mt('Changes made: ').'<ul>';
18561:             foreach my $context (@contexts) {
18562:                 if (ref($changes{$context}) eq 'ARRAY') {
18563:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
18564:                     if (ref($changes{$context}) eq 'ARRAY') {
18565:                         foreach my $role (@{$changes{$context}}) {
18566:                             my $rolename;
18567:                             if ($role eq 'cr') {
18568:                                 $rolename = &mt('Custom');
18569:                             } else {
18570:                                 $rolename = &Apache::lonnet::plaintext($role);
18571:                             }
18572:                             my @modifiable;
18573:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
18574:                             foreach my $field (@fields) {
18575:                                 if ($modifyhash{$context}{$role}{$field}) {
18576:                                     push(@modifiable,$fieldtitles{$field});
18577:                                 }
18578:                             }
18579:                             if (@modifiable > 0) {
18580:                                 $resulttext .= join(', ',@modifiable);
18581:                             } else {
18582:                                 $resulttext .= &mt('none'); 
18583:                             }
18584:                             $resulttext .= '</li>';
18585:                         }
18586:                         $resulttext .= '</ul></li>';
18587:                     }
18588:                 }
18589:             }
18590:             $resulttext .= '</ul>';
18591:         } else {
18592:             $resulttext = &mt('No changes made to user modification settings');
18593:         }
18594:     } else {
18595:         $resulttext = '<span class="LC_error">'.
18596:             &mt('An error occurred: [_1]',$putresult).'</span>';
18597:     }
18598:     return $resulttext;
18599: }
18600: 
18601: sub modify_defaults {
18602:     my ($dom,$lastactref,%domconfig) = @_;
18603:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
18604:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18605:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
18606:                  'portal_def');
18607:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
18608:     foreach my $item (@items) {
18609:         $newvalues{$item} = $env{'form.'.$item};
18610:         if ($item eq 'auth_def') {
18611:             if ($newvalues{$item} ne '') {
18612:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
18613:                     push(@errors,$item);
18614:                 }
18615:             }
18616:         } elsif ($item eq 'lang_def') {
18617:             if ($newvalues{$item} ne '') {
18618:                 if ($newvalues{$item} =~ /^(\w+)/) {
18619:                     my $langcode = $1;
18620:                     if ($langcode ne 'x_chef') {
18621:                         if (code2language($langcode) eq '') {
18622:                             push(@errors,$item);
18623:                         }
18624:                     }
18625:                 } else {
18626:                     push(@errors,$item);
18627:                 }
18628:             }
18629:         } elsif ($item eq 'timezone_def') {
18630:             if ($newvalues{$item} ne '') {
18631:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
18632:                     push(@errors,$item);   
18633:                 }
18634:             }
18635:         } elsif ($item eq 'datelocale_def') {
18636:             if ($newvalues{$item} ne '') {
18637:                 my @datelocale_ids = DateTime::Locale->ids();
18638:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
18639:                     push(@errors,$item);
18640:                 }
18641:             }
18642:         } elsif ($item eq 'portal_def') {
18643:             if ($newvalues{$item} ne '') {
18644:                 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])\/?$/) {
18645:                     push(@errors,$item);
18646:                 }
18647:             }
18648:         }
18649:         if (grep(/^\Q$item\E$/,@errors)) {
18650:             $newvalues{$item} = $domdefaults{$item};
18651:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
18652:             $changes{$item} = 1;
18653:         }
18654:         $domdefaults{$item} = $newvalues{$item};
18655:     }
18656:     my %staticdefaults = (
18657:                            'intauth_cost'   => 10,
18658:                            'intauth_check'  => 0,
18659:                            'intauth_switch' => 0,
18660:                          );
18661:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
18662:         if (exists($domdefaults{$item})) {
18663:             $newvalues{$item} = $domdefaults{$item};
18664:         } else {
18665:             $newvalues{$item} = $staticdefaults{$item};
18666:         }
18667:     }
18668:     my %defaults_hash = (
18669:                          defaults => \%newvalues,
18670:                         );
18671:     my $title = &defaults_titles();
18672: 
18673:     my $currinststatus;
18674:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18675:         $currinststatus = $domconfig{'inststatus'};
18676:     } else {
18677:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18678:         $currinststatus = {
18679:                              inststatustypes => $usertypes,
18680:                              inststatusorder => $types,
18681:                              inststatusguest => [],
18682:                           };
18683:     }
18684:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
18685:     my @allpos;
18686:     my %alltypes;
18687:     my @inststatusguest;
18688:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
18689:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
18690:             unless (grep(/^\Q$type\E$/,@todelete)) {
18691:                 push(@inststatusguest,$type);
18692:             }
18693:         }
18694:     }
18695:     my ($currtitles,$currorder);
18696:     if (ref($currinststatus) eq 'HASH') {
18697:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
18698:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
18699:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
18700:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
18701:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
18702:                     }
18703:                 }
18704:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
18705:                     my $position = $env{'form.inststatus_pos_'.$type};
18706:                     $position =~ s/\D+//g;
18707:                     $allpos[$position] = $type;
18708:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
18709:                     $alltypes{$type} =~ s/`//g;
18710:                 }
18711:             }
18712:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
18713:             $currtitles =~ s/,$//;
18714:         }
18715:     }
18716:     if ($env{'form.addinststatus'}) {
18717:         my $newtype = $env{'form.addinststatus'};
18718:         $newtype =~ s/\W//g;
18719:         unless (exists($alltypes{$newtype})) {
18720:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
18721:             $alltypes{$newtype} =~ s/`//g; 
18722:             my $position = $env{'form.addinststatus_pos'};
18723:             $position =~ s/\D+//g;
18724:             if ($position ne '') {
18725:                 $allpos[$position] = $newtype;
18726:             }
18727:         }
18728:     }
18729:     my @orderedstatus;
18730:     foreach my $type (@allpos) {
18731:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
18732:             push(@orderedstatus,$type);
18733:         }
18734:     }
18735:     foreach my $type (keys(%alltypes)) {
18736:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
18737:             delete($alltypes{$type});
18738:         }
18739:     }
18740:     $defaults_hash{'inststatus'} = {
18741:                                      inststatustypes => \%alltypes,
18742:                                      inststatusorder => \@orderedstatus,
18743:                                      inststatusguest => \@inststatusguest,
18744:                                    };
18745:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
18746:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
18747:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
18748:         }
18749:     }
18750:     if ($currorder ne join(',',@orderedstatus)) {
18751:         $changes{'inststatus'}{'inststatusorder'} = 1;
18752:     }
18753:     my $newtitles;
18754:     foreach my $item (@orderedstatus) {
18755:         $newtitles .= $alltypes{$item}.',';
18756:     }
18757:     $newtitles =~ s/,$//;
18758:     if ($currtitles ne $newtitles) {
18759:         $changes{'inststatus'}{'inststatustypes'} = 1;
18760:     }
18761:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
18762:                                              $dom);
18763:     if ($putresult eq 'ok') {
18764:         if (keys(%changes) > 0) {
18765:             $resulttext = &mt('Changes made:').'<ul>';
18766:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
18767:             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";
18768:             foreach my $item (sort(keys(%changes))) {
18769:                 if ($item eq 'inststatus') {
18770:                     if (ref($changes{'inststatus'}) eq 'HASH') {
18771:                         if (@orderedstatus) {
18772:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
18773:                             foreach my $type (@orderedstatus) { 
18774:                                 $resulttext .= $alltypes{$type}.', ';
18775:                             }
18776:                             $resulttext =~ s/, $//;
18777:                             $resulttext .= '</li>';
18778:                         } else {
18779:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
18780:                         }
18781:                     }
18782:                 } else {
18783:                     my $value = $env{'form.'.$item};
18784:                     if ($value eq '') {
18785:                         $value = &mt('none');
18786:                     } elsif ($item eq 'auth_def') {
18787:                         my %authnames = &authtype_names();
18788:                         my %shortauth = (
18789:                                           internal   => 'int',
18790:                                           krb4       => 'krb4',
18791:                                           krb5       => 'krb5',
18792:                                           localauth  => 'loc',
18793:                                           lti        => 'lti',
18794:                         );
18795:                         $value = $authnames{$shortauth{$value}};
18796:                     }
18797:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
18798:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
18799:                 }
18800:             }
18801:             $resulttext .= '</ul>';
18802:             $mailmsgtext .= "\n";
18803:             my $cachetime = 24*60*60;
18804:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18805:             if (ref($lastactref) eq 'HASH') {
18806:                 $lastactref->{'domdefaults'} = 1;
18807:             }
18808:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
18809:                 my $notify = 1;
18810:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
18811:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
18812:                         $notify = 0;
18813:                     }
18814:                 }
18815:                 if ($notify) {
18816:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
18817:                                                "LON-CAPA Domain Settings Change - $dom",
18818:                                                $mailmsgtext);
18819:                 }
18820:             }
18821:         } else {
18822:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
18823:         }
18824:     } else {
18825:         $resulttext = '<span class="LC_error">'.
18826:             &mt('An error occurred: [_1]',$putresult).'</span>';
18827:     }
18828:     if (@errors > 0) {
18829:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
18830:         foreach my $item (@errors) {
18831:             $resulttext .= ' "'.$title->{$item}.'",';
18832:         }
18833:         $resulttext =~ s/,$//;
18834:     }
18835:     return $resulttext;
18836: }
18837: 
18838: sub modify_scantron {
18839:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18840:     my ($resulttext,%confhash,%changes,$errors);
18841:     my $custom = 'custom.tab';
18842:     my $default = 'default.tab';
18843:     my $servadm = $r->dir_config('lonAdmEMail');
18844:     my ($configuserok,$author_ok,$switchserver) =
18845:         &config_check($dom,$confname,$servadm);
18846:     if ($env{'form.scantronformat.filename'} ne '') {
18847:         my $error;
18848:         if ($configuserok eq 'ok') {
18849:             if ($switchserver) {
18850:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
18851:             } else {
18852:                 if ($author_ok eq 'ok') {
18853:                     my ($result,$scantronurl) =
18854:                         &publishlogo($r,'upload','scantronformat',$dom,
18855:                                      $confname,'scantron','','',$custom);
18856:                     if ($result eq 'ok') {
18857:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
18858:                         $changes{'scantronformat'} = 1;
18859:                     } else {
18860:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
18861:                     }
18862:                 } else {
18863:                     $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);
18864:                 }
18865:             }
18866:         } else {
18867:             $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);
18868:         }
18869:         if ($error) {
18870:             &Apache::lonnet::logthis($error);
18871:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18872:         }
18873:     }
18874:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18875:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
18876:             if ($env{'form.scantronformat_del'}) {
18877:                 $confhash{'scantron'}{'scantronformat'} = '';
18878:                 $changes{'scantronformat'} = 1;
18879:             } else {
18880:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
18881:             }
18882:         }
18883:     }
18884:     my @options = ('hdr','pad','rem');
18885:     my @fields = &scantroncsv_fields();
18886:     my %titles = &scantronconfig_titles();
18887:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
18888:     my ($newdat,$currdat,%newcol,%currcol);
18889:     if (grep(/^dat$/,@formats)) {
18890:         $confhash{'scantron'}{config}{dat} = 1;
18891:         $newdat = 1;
18892:     } else {
18893:         $newdat = 0;
18894:     }
18895:     if (grep(/^csv$/,@formats)) {
18896:         my %bynum;
18897:         foreach my $field (@fields) {
18898:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
18899:                 my $posscol = $1;
18900:                 if (($posscol < 20) && (!$bynum{$posscol})) {
18901:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
18902:                     $bynum{$posscol} = $field;
18903:                     $newcol{$field} = $posscol;
18904:                 }
18905:             }
18906:         }
18907:         if (keys(%newcol)) {
18908:             foreach my $option (@options) {
18909:                 if ($env{'form.scantroncsv_'.$option}) {
18910:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
18911:                 }
18912:             }
18913:         }
18914:     }
18915:     $currdat = 1;
18916:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18917:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
18918:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
18919:                 $currdat = 0;
18920:             }
18921:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18922:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18923:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
18924:                 }
18925:             }
18926:         }
18927:     }
18928:     if ($currdat != $newdat) {
18929:         $changes{'config'} = 1;
18930:     } else {
18931:         foreach my $field (@fields) {
18932:             if ($currcol{$field} ne '') {
18933:                 if ($currcol{$field} ne $newcol{$field}) {
18934:                     $changes{'config'} = 1;
18935:                     last;
18936:                 }
18937:             } elsif ($newcol{$field} ne '') {
18938:                 $changes{'config'} = 1;
18939:                 last;
18940:             }
18941:         }
18942:     }
18943:     if (keys(%confhash) > 0) {
18944:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
18945:                                                  $dom);
18946:         if ($putresult eq 'ok') {
18947:             if (keys(%changes) > 0) {
18948:                 if (ref($confhash{'scantron'}) eq 'HASH') {
18949:                     $resulttext = &mt('Changes made:').'<ul>';
18950:                     if ($changes{'scantronformat'}) {
18951:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
18952:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
18953:                         } else {
18954:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
18955:                         }
18956:                     }
18957:                     if ($changes{'config'}) {
18958:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
18959:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
18960:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
18961:                             }
18962:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18963:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18964:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
18965:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
18966:                                         foreach my $field (@fields) {
18967:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
18968:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
18969:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
18970:                                             }
18971:                                         }
18972:                                         $resulttext .= '</ul></li>';
18973:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
18974:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
18975:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
18976:                                                 foreach my $option (@options) {
18977:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
18978:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
18979:                                                     }
18980:                                                 }
18981:                                                 $resulttext .= '</ul></li>';
18982:                                             }
18983:                                         }
18984:                                     }
18985:                                 }
18986:                             }
18987:                         } else {
18988:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
18989:                         }
18990:                     }
18991:                     $resulttext .= '</ul>';
18992:                 } else {
18993:                     $resulttext = &mt('Changes made to bubblesheet format file.');
18994:                 }
18995:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
18996:                 if (ref($lastactref) eq 'HASH') {
18997:                     $lastactref->{'domainconfig'} = 1;
18998:                 }
18999:             } else {
19000:                 $resulttext = &mt('No changes made to bubblesheet format settings');
19001:             }
19002:         } else {
19003:             $resulttext = '<span class="LC_error">'.
19004:                 &mt('An error occurred: [_1]',$putresult).'</span>';
19005:         }
19006:     } else {
19007:         $resulttext = &mt('No changes made to bubblesheet format file'); 
19008:     }
19009:     if ($errors) {
19010:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
19011:                        $errors.'</ul></p>';
19012:     }
19013:     return $resulttext;
19014: }
19015: 
19016: sub modify_coursecategories {
19017:     my ($dom,$lastactref,%domconfig) = @_;
19018:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
19019:         $cathash);
19020:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
19021:     my @catitems = ('unauth','auth');
19022:     my @cattypes = ('std','domonly','codesrch','none');
19023:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19024:         $cathash = $domconfig{'coursecategories'}{'cats'};
19025:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
19026:             $changes{'togglecats'} = 1;
19027:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
19028:         }
19029:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
19030:             $changes{'categorize'} = 1;
19031:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
19032:         }
19033:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
19034:             $changes{'togglecatscomm'} = 1;
19035:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
19036:         }
19037:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
19038:             $changes{'categorizecomm'} = 1;
19039:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
19040: 
19041:         }
19042:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
19043:             $changes{'togglecatsplace'} = 1;
19044:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
19045:         }
19046:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
19047:             $changes{'categorizeplace'} = 1;
19048:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
19049:         }
19050:         foreach my $item (@catitems) {
19051:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19052:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
19053:                     $changes{$item} = 1;
19054:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19055:                 }
19056:             }
19057:         }
19058:     } else {
19059:         $changes{'togglecats'} = 1;
19060:         $changes{'categorize'} = 1;
19061:         $changes{'togglecatscomm'} = 1;
19062:         $changes{'categorizecomm'} = 1;
19063:         $changes{'togglecatsplace'} = 1;
19064:         $changes{'categorizeplace'} = 1;
19065:         $domconfig{'coursecategories'} = {
19066:                                              togglecats => $env{'form.togglecats'},
19067:                                              categorize => $env{'form.categorize'},
19068:                                              togglecatscomm => $env{'form.togglecatscomm'},
19069:                                              categorizecomm => $env{'form.categorizecomm'},
19070:                                              togglecatsplace => $env{'form.togglecatsplace'},
19071:                                              categorizeplace => $env{'form.categorizeplace'},
19072:                                          };
19073:         foreach my $item (@catitems) {
19074:             if ($env{'form.coursecat_'.$item} ne 'std') {
19075:                 $changes{$item} = 1;
19076:             }
19077:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19078:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19079:             }
19080:         }
19081:     }
19082:     if (ref($cathash) eq 'HASH') {
19083:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
19084:             push (@deletecategory,'instcode::0');
19085:         }
19086:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
19087:             push(@deletecategory,'communities::0');
19088:         }
19089:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
19090:             push(@deletecategory,'placement::0');
19091:         }
19092:     }
19093:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
19094:     if (ref($cathash) eq 'HASH') {
19095:         if (@deletecategory > 0) {
19096:             #FIXME Need to remove category from all courses using a deleted category 
19097:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
19098:             foreach my $item (@deletecategory) {
19099:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
19100:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
19101:                     $deletions{$item} = 1;
19102:                     &recurse_cat_deletes($item,$cathash,\%deletions);
19103:                 }
19104:             }
19105:         }
19106:         foreach my $item (keys(%{$cathash})) {
19107:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19108:             if ($cathash->{$item} ne $env{'form.'.$item}) {
19109:                 $reorderings{$item} = 1;
19110:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
19111:             }
19112:             if ($env{'form.addcategory_name_'.$item} ne '') {
19113:                 my $newcat = $env{'form.addcategory_name_'.$item};
19114:                 my $newdepth = $depth+1;
19115:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19116:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
19117:                 $adds{$newitem} = 1; 
19118:             }
19119:             if ($env{'form.subcat_'.$item} ne '') {
19120:                 my $newcat = $env{'form.subcat_'.$item};
19121:                 my $newdepth = $depth+1;
19122:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19123:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
19124:                 $adds{$newitem} = 1;
19125:             }
19126:         }
19127:     }
19128:     if ($env{'form.instcode'} eq '1') {
19129:         if (ref($cathash) eq 'HASH') {
19130:             my $newitem = 'instcode::0';
19131:             if ($cathash->{$newitem} eq '') {  
19132:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19133:                 $adds{$newitem} = 1;
19134:             }
19135:         } else {
19136:             my $newitem = 'instcode::0';
19137:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19138:             $adds{$newitem} = 1;
19139:         }
19140:     }
19141:     if ($env{'form.communities'} eq '1') {
19142:         if (ref($cathash) eq 'HASH') {
19143:             my $newitem = 'communities::0';
19144:             if ($cathash->{$newitem} eq '') {
19145:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19146:                 $adds{$newitem} = 1;
19147:             }
19148:         } else {
19149:             my $newitem = 'communities::0';
19150:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19151:             $adds{$newitem} = 1;
19152:         }
19153:     }
19154:     if ($env{'form.placement'} eq '1') {
19155:         if (ref($cathash) eq 'HASH') {
19156:             my $newitem = 'placement::0';
19157:             if ($cathash->{$newitem} eq '') {
19158:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19159:                 $adds{$newitem} = 1;
19160:             }
19161:         } else {
19162:             my $newitem = 'placement::0';
19163:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19164:             $adds{$newitem} = 1;
19165:         }
19166:     }
19167:     if ($env{'form.addcategory_name'} ne '') {
19168:         if (($env{'form.addcategory_name'} ne 'instcode') &&
19169:             ($env{'form.addcategory_name'} ne 'communities') &&
19170:             ($env{'form.addcategory_name'} ne 'placement')) {
19171:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
19172:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
19173:             $adds{$newitem} = 1;
19174:         }
19175:     }
19176:     my $putresult;
19177:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19178:         if (keys(%deletions) > 0) {
19179:             foreach my $key (keys(%deletions)) {
19180:                 if ($predelallitems{$key} ne '') {
19181:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
19182:                 }
19183:             }
19184:         }
19185:         my (@chkcats,@chktrails,%chkallitems);
19186:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
19187:         if (ref($chkcats[0]) eq 'ARRAY') {
19188:             my $depth = 0;
19189:             my $chg = 0;
19190:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
19191:                 my $name = $chkcats[0][$i];
19192:                 my $item;
19193:                 if ($name eq '') {
19194:                     $chg ++;
19195:                 } else {
19196:                     $item = &escape($name).'::0';
19197:                     if ($chg) {
19198:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
19199:                     }
19200:                     $depth ++; 
19201:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
19202:                     $depth --;
19203:                 }
19204:             }
19205:         }
19206:     }
19207:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19208:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
19209:         if ($putresult eq 'ok') {
19210:             my %title = (
19211:                          togglecats     => 'Show/Hide a course in catalog',
19212:                          categorize     => 'Assign a category to a course',
19213:                          togglecatscomm => 'Show/Hide a community in catalog',
19214:                          categorizecomm => 'Assign a category to a community',
19215:                         );
19216:             my %level = (
19217:                          dom  => 'set in Domain ("Modify Course/Community")',
19218:                          crs  => 'set in Course ("Course Configuration")',
19219:                          comm => 'set in Community ("Community Configuration")',
19220:                          none     => 'No catalog',
19221:                          std      => 'Standard catalog',
19222:                          domonly  => 'Domain-only catalog',
19223:                          codesrch => 'Code search form',
19224:                         );
19225:             $resulttext = &mt('Changes made:').'<ul>';
19226:             if ($changes{'togglecats'}) {
19227:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
19228:             }
19229:             if ($changes{'categorize'}) {
19230:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
19231:             }
19232:             if ($changes{'togglecatscomm'}) {
19233:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
19234:             }
19235:             if ($changes{'categorizecomm'}) {
19236:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
19237:             }
19238:             if ($changes{'unauth'}) {
19239:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
19240:             }
19241:             if ($changes{'auth'}) {
19242:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
19243:             }
19244:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19245:                 my $cathash;
19246:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19247:                     $cathash = $domconfig{'coursecategories'}{'cats'};
19248:                 } else {
19249:                     $cathash = {};
19250:                 } 
19251:                 my (@cats,@trails,%allitems);
19252:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
19253:                 if (keys(%deletions) > 0) {
19254:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
19255:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
19256:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
19257:                     }
19258:                     $resulttext .= '</ul></li>';
19259:                 }
19260:                 if (keys(%reorderings) > 0) {
19261:                     my %sort_by_trail;
19262:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
19263:                     foreach my $key (keys(%reorderings)) {
19264:                         if ($allitems{$key} ne '') {
19265:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19266:                         }
19267:                     }
19268:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19269:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19270:                     }
19271:                     $resulttext .= '</ul></li>';
19272:                 }
19273:                 if (keys(%adds) > 0) {
19274:                     my %sort_by_trail;
19275:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
19276:                     foreach my $key (keys(%adds)) {
19277:                         if ($allitems{$key} ne '') {
19278:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19279:                         }
19280:                     }
19281:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19282:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19283:                     }
19284:                     $resulttext .= '</ul></li>';
19285:                 }
19286:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
19287:                 if (ref($lastactref) eq 'HASH') {
19288:                     $lastactref->{'cats'} = 1;
19289:                 }
19290:             }
19291:             $resulttext .= '</ul>';
19292:             if ($changes{'unauth'} || $changes{'auth'}) {
19293:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19294:                 if ($changes{'auth'}) {
19295:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
19296:                 }
19297:                 if ($changes{'unauth'}) {
19298:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
19299:                 }
19300:                 my $cachetime = 24*60*60;
19301:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19302:                 if (ref($lastactref) eq 'HASH') {
19303:                     $lastactref->{'domdefaults'} = 1;
19304:                 }
19305:             }
19306:         } else {
19307:             $resulttext = '<span class="LC_error">'.
19308:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19309:         }
19310:     } else {
19311:         $resulttext = &mt('No changes made to course and community categories');
19312:     }
19313:     return $resulttext;
19314: }
19315: 
19316: sub modify_serverstatuses {
19317:     my ($dom,%domconfig) = @_;
19318:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
19319:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
19320:         %currserverstatus = %{$domconfig{'serverstatuses'}};
19321:     }
19322:     my @pages = &serverstatus_pages();
19323:     foreach my $type (@pages) {
19324:         $newserverstatus{$type}{'namedusers'} = '';
19325:         $newserverstatus{$type}{'machines'} = '';
19326:         if (defined($env{'form.'.$type.'_namedusers'})) {
19327:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
19328:             my @okusers;
19329:             foreach my $user (@users) {
19330:                 my ($uname,$udom) = split(/:/,$user);
19331:                 if (($udom =~ /^$match_domain$/) &&   
19332:                     (&Apache::lonnet::domain($udom)) &&
19333:                     ($uname =~ /^$match_username$/)) {
19334:                     if (!grep(/^\Q$user\E/,@okusers)) {
19335:                         push(@okusers,$user);
19336:                     }
19337:                 }
19338:             }
19339:             if (@okusers > 0) {
19340:                  @okusers = sort(@okusers);
19341:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
19342:             }
19343:         }
19344:         if (defined($env{'form.'.$type.'_machines'})) {
19345:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
19346:             my @okmachines;
19347:             foreach my $ip (@machines) {
19348:                 my @parts = split(/\./,$ip);
19349:                 next if (@parts < 4);
19350:                 my $badip = 0;
19351:                 for (my $i=0; $i<4; $i++) {
19352:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
19353:                         $badip = 1;
19354:                         last;
19355:                     }
19356:                 }
19357:                 if (!$badip) {
19358:                     push(@okmachines,$ip);     
19359:                 }
19360:             }
19361:             @okmachines = sort(@okmachines);
19362:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
19363:         }
19364:     }
19365:     my %serverstatushash =  (
19366:                                 serverstatuses => \%newserverstatus,
19367:                             );
19368:     foreach my $type (@pages) {
19369:         foreach my $setting ('namedusers','machines') {
19370:             my (@current,@new);
19371:             if (ref($currserverstatus{$type}) eq 'HASH') {
19372:                 if ($currserverstatus{$type}{$setting} ne '') { 
19373:                     @current = split(/,/,$currserverstatus{$type}{$setting});
19374:                 }
19375:             }
19376:             if ($newserverstatus{$type}{$setting} ne '') {
19377:                 @new = split(/,/,$newserverstatus{$type}{$setting});
19378:             }
19379:             if (@current > 0) {
19380:                 if (@new > 0) {
19381:                     foreach my $item (@current) {
19382:                         if (!grep(/^\Q$item\E$/,@new)) {
19383:                             $changes{$type}{$setting} = 1;
19384:                             last;
19385:                         }
19386:                     }
19387:                     foreach my $item (@new) {
19388:                         if (!grep(/^\Q$item\E$/,@current)) {
19389:                             $changes{$type}{$setting} = 1;
19390:                             last;
19391:                         }
19392:                     }
19393:                 } else {
19394:                     $changes{$type}{$setting} = 1;
19395:                 }
19396:             } elsif (@new > 0) {
19397:                 $changes{$type}{$setting} = 1;
19398:             }
19399:         }
19400:     }
19401:     if (keys(%changes) > 0) {
19402:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
19403:         my $putresult = &Apache::lonnet::put_dom('configuration',
19404:                                                  \%serverstatushash,$dom);
19405:         if ($putresult eq 'ok') {
19406:             $resulttext .= &mt('Changes made:').'<ul>';
19407:             foreach my $type (@pages) {
19408:                 if (ref($changes{$type}) eq 'HASH') {
19409:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
19410:                     if ($changes{$type}{'namedusers'}) {
19411:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
19412:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
19413:                         } else {
19414:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
19415:                         }
19416:                     }
19417:                     if ($changes{$type}{'machines'}) {
19418:                         if ($newserverstatus{$type}{'machines'} eq '') {
19419:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
19420:                         } else {
19421:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
19422:                         }
19423: 
19424:                     }
19425:                     $resulttext .= '</ul></li>';
19426:                 }
19427:             }
19428:             $resulttext .= '</ul>';
19429:         } else {
19430:             $resulttext = '<span class="LC_error">'.
19431:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
19432: 
19433:         }
19434:     } else {
19435:         $resulttext = &mt('No changes made to access to server status pages');
19436:     }
19437:     return $resulttext;
19438: }
19439: 
19440: sub modify_helpsettings {
19441:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19442:     my ($resulttext,$errors,%changes,%helphash);
19443:     my %defaultchecked = ('submitbugs' => 'on');
19444:     my @offon = ('off','on');
19445:     my @toggles = ('submitbugs');
19446:     my %current = ('submitbugs' => '',
19447:                    'adhoc'      => {},
19448:                   );
19449:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
19450:         %current = %{$domconfig{'helpsettings'}};
19451:     }
19452:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19453:     foreach my $item (@toggles) {
19454:         if ($defaultchecked{$item} eq 'on') { 
19455:             if ($current{$item} eq '') {
19456:                 if ($env{'form.'.$item} eq '0') {
19457:                     $changes{$item} = 1;
19458:                 }
19459:             } elsif ($current{$item} ne $env{'form.'.$item}) {
19460:                 $changes{$item} = 1;
19461:             }
19462:         } elsif ($defaultchecked{$item} eq 'off') {
19463:             if ($current{$item} eq '') {
19464:                 if ($env{'form.'.$item} eq '1') {
19465:                     $changes{$item} = 1;
19466:                 }
19467:             } elsif ($current{$item} ne $env{'form.'.$item}) {
19468:                 $changes{$item} = 1;
19469:             }
19470:         }
19471:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
19472:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
19473:         }
19474:     }
19475:     my $maxnum = $env{'form.helproles_maxnum'};
19476:     my $confname = $dom.'-domainconfig';
19477:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
19478:     my (@allpos,%newsettings,%changedprivs,$newrole);
19479:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19480:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
19481:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
19482:     my %lt = &Apache::lonlocal::texthash(
19483:                     s      => 'system',
19484:                     d      => 'domain',
19485:                     order  => 'Display order',
19486:                     access => 'Role usage',
19487:                     all    => 'All with domain helpdesk or helpdesk assistant role',
19488:                     dh     => 'All with domain helpdesk role',
19489:                     da     => 'All with domain helpdesk assistant role',
19490:                     none   => 'None',
19491:                     status => 'Determined based on institutional status',
19492:                     inc    => 'Include all, but exclude specific personnel',
19493:                     exc    => 'Exclude all, but include specific personnel',
19494:     );
19495:     for (my $num=0; $num<=$maxnum; $num++) {
19496:         my ($prefix,$identifier,$rolename,%curr);
19497:         if ($num == $maxnum) {
19498:             next unless ($env{'form.newcusthelp'} == $maxnum);
19499:             $identifier = 'custhelp'.$num;
19500:             $prefix = 'helproles_'.$num;
19501:             $rolename = $env{'form.custhelpname'.$num};
19502:             $rolename=~s/[^A-Za-z0-9]//gs;
19503:             next if ($rolename eq '');
19504:             next if (exists($existing{'rolesdef_'.$rolename}));
19505:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
19506:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
19507:                                                      $newprivs{'c'},$confname,$dom);
19508:             if ($result ne 'ok') {
19509:                 $errors .= '<li><span class="LC_error">'.
19510:                            &mt('An error occurred storing the new custom role: [_1]',
19511:                            $result).'</span></li>';
19512:                 next;
19513:             } else {
19514:                 $changedprivs{$rolename} = \%newprivs;
19515:                 $newrole = $rolename;
19516:             }
19517:         } else {
19518:             $prefix = 'helproles_'.$num;
19519:             $rolename = $env{'form.'.$prefix};
19520:             next if ($rolename eq '');
19521:             next unless (exists($existing{'rolesdef_'.$rolename}));
19522:             $identifier = 'custhelp'.$num;
19523:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
19524:             my %currprivs;
19525:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
19526:                 split(/\_/,$existing{'rolesdef_'.$rolename});
19527:             foreach my $level ('c','d','s') {
19528:                 if ($newprivs{$level} ne $currprivs{$level}) {
19529:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
19530:                                                              $newprivs{'c'},$confname,$dom);
19531:                     if ($result ne 'ok') {
19532:                         $errors .= '<li><span class="LC_error">'.
19533:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
19534:                                        $rolename,$result).'</span></li>';
19535:                     } else {
19536:                         $changedprivs{$rolename} = \%newprivs;
19537:                     }
19538:                     last;
19539:                 }
19540:             }
19541:             if (ref($current{'adhoc'}) eq 'HASH') {
19542:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
19543:                     %curr = %{$current{'adhoc'}{$rolename}};
19544:                 }
19545:             }
19546:         }
19547:         my $newpos = $env{'form.'.$prefix.'_pos'};
19548:         $newpos =~ s/\D+//g;
19549:         $allpos[$newpos] = $rolename;
19550:         my $newdesc = $env{'form.'.$prefix.'_desc'};
19551:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
19552:         if ($curr{'desc'}) {
19553:             if ($curr{'desc'} ne $newdesc) {
19554:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
19555:                 $newsettings{$rolename}{'desc'} = $newdesc;
19556:             }
19557:         } elsif ($newdesc ne '') {
19558:             $changes{'customrole'}{$rolename}{'desc'} = 1;
19559:             $newsettings{$rolename}{'desc'} = $newdesc;
19560:         }
19561:         my $access = $env{'form.'.$prefix.'_access'};
19562:         if (grep(/^\Q$access\E$/,@accesstypes)) {
19563:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
19564:             if ($access eq 'status') {
19565:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
19566:                 if (scalar(@statuses) == 0) {
19567:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
19568:                 } else {
19569:                     my (@shownstatus,$numtypes);
19570:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
19571:                     if (ref($types) eq 'ARRAY') {
19572:                         $numtypes = scalar(@{$types});
19573:                         foreach my $type (sort(@statuses)) {
19574:                             if ($type eq 'default') {
19575:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
19576:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
19577:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
19578:                                 push(@shownstatus,$usertypes->{$type});
19579:                             }
19580:                         }
19581:                     }
19582:                     if (grep(/^default$/,@statuses)) {
19583:                         push(@shownstatus,$othertitle);
19584:                     }
19585:                     if (scalar(@shownstatus) == 1+$numtypes) {
19586:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
19587:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
19588:                     } else {
19589:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
19590:                         if (ref($curr{'status'}) eq 'ARRAY') {
19591:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
19592:                             if (@diffs) {
19593:                                 $changes{'customrole'}{$rolename}{$access} = 1;
19594:                             }
19595:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19596:                             $changes{'customrole'}{$rolename}{$access} = 1;
19597:                         }
19598:                     }
19599:                 }
19600:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
19601:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
19602:                 my @newspecstaff;
19603:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
19604:                 foreach my $person (sort(@personnel)) {
19605:                     if ($domhelpdesk{$person}) {
19606:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
19607:                     }
19608:                 }
19609:                 if (ref($curr{$access}) eq 'ARRAY') {
19610:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
19611:                     if (@diffs) {
19612:                         $changes{'customrole'}{$rolename}{$access} = 1;
19613:                     }
19614:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19615:                     $changes{'customrole'}{$rolename}{$access} = 1;
19616:                 }
19617:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19618:                     my ($uname,$udom) = split(/:/,$person);
19619:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
19620:                 }
19621:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
19622:             }
19623:         } else {
19624:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
19625:         }
19626:         unless ($curr{'access'} eq $access) {
19627:             $changes{'customrole'}{$rolename}{'access'} = 1;
19628:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
19629:         }
19630:     }
19631:     if (@allpos > 0) {
19632:         my $idx = 0;
19633:         foreach my $rolename (@allpos) {
19634:             if ($rolename ne '') {
19635:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
19636:                 if (ref($current{'adhoc'}) eq 'HASH') {
19637:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
19638:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
19639:                             $changes{'customrole'}{$rolename}{'order'} = 1;
19640:                             $newsettings{$rolename}{'order'} = $idx+1;
19641:                         }
19642:                     }
19643:                 }
19644:                 $idx ++;
19645:             }
19646:         }
19647:     }
19648:     my $putresult;
19649:     if (keys(%changes) > 0) {
19650:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
19651:         if ($putresult eq 'ok') {
19652:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
19653:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
19654:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
19655:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
19656:                 }
19657:             }
19658:             my $cachetime = 24*60*60;
19659:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19660:             if (ref($lastactref) eq 'HASH') {
19661:                 $lastactref->{'domdefaults'} = 1;
19662:             }
19663:         } else {
19664:             $errors .= '<li><span class="LC_error">'.
19665:                        &mt('An error occurred storing the settings: [_1]',
19666:                            $putresult).'</span></li>';
19667:         }
19668:     }
19669:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
19670:         $resulttext = &mt('Changes made:').'<ul>';
19671:         my (%shownprivs,@levelorder);
19672:         @levelorder = ('c','d','s');
19673:         if ((keys(%changes)) && ($putresult eq 'ok')) {
19674:             foreach my $item (sort(keys(%changes))) {
19675:                 if ($item eq 'submitbugs') {
19676:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
19677:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
19678:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
19679:                 } elsif ($item eq 'customrole') {
19680:                     if (ref($changes{'customrole'}) eq 'HASH') {
19681:                         my @keyorder = ('order','desc','access','status','exc','inc');
19682:                         my %keytext = &Apache::lonlocal::texthash(
19683:                                                                    order  => 'Order',
19684:                                                                    desc   => 'Role description',
19685:                                                                    access => 'Role usage',
19686:                                                                    status => 'Allowed institutional types',
19687:                                                                    exc    => 'Allowed personnel',
19688:                                                                    inc    => 'Disallowed personnel',
19689:                         );
19690:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
19691:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
19692:                                 if ($role eq $newrole) {
19693:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
19694:                                                               $role).'<ul>';
19695:                                 } else {
19696:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
19697:                                                               $role).'<ul>';
19698:                                 }
19699:                                 foreach my $key (@keyorder) {
19700:                                     if ($changes{'customrole'}{$role}{$key}) {
19701:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
19702:                                                                   $keytext{$key},$newsettings{$role}{$key}).
19703:                                                        '</li>';
19704:                                     }
19705:                                 }
19706:                                 if (ref($changedprivs{$role}) eq 'HASH') {
19707:                                     $shownprivs{$role} = 1;
19708:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
19709:                                     foreach my $level (@levelorder) {
19710:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
19711:                                             next if ($item eq '');
19712:                                             my ($priv) = split(/\&/,$item,2);
19713:                                             if (&Apache::lonnet::plaintext($priv)) {
19714:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
19715:                                                 unless ($level eq 'c') {
19716:                                                     $resulttext .= ' ('.$lt{$level}.')';
19717:                                                 }
19718:                                                 $resulttext .= '</li>';
19719:                                             }
19720:                                         }
19721:                                     }
19722:                                     $resulttext .= '</ul>';
19723:                                 }
19724:                                 $resulttext .= '</ul></li>';
19725:                             }
19726:                         }
19727:                     }
19728:                 }
19729:             }
19730:         }
19731:         if (keys(%changedprivs)) {
19732:             foreach my $role (sort(keys(%changedprivs))) {
19733:                 unless ($shownprivs{$role}) {
19734:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
19735:                                               $role).'<ul>'.
19736:                                    '<li>'.&mt('Privileges set to :').'<ul>';
19737:                     foreach my $level (@levelorder) {
19738:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
19739:                             next if ($item eq '');
19740:                             my ($priv) = split(/\&/,$item,2);
19741:                             if (&Apache::lonnet::plaintext($priv)) {
19742:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
19743:                                 unless ($level eq 'c') {
19744:                                     $resulttext .= ' ('.$lt{$level}.')';
19745:                                 }
19746:                                 $resulttext .= '</li>';
19747:                             }
19748:                         }
19749:                     }
19750:                     $resulttext .= '</ul></li></ul></li>';
19751:                 }
19752:             }
19753:         }
19754:         $resulttext .= '</ul>';
19755:     } else {
19756:         $resulttext = &mt('No changes made to help settings');
19757:     }
19758:     if ($errors) {
19759:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
19760:                                     $errors.'</ul>';
19761:     }
19762:     return $resulttext;
19763: }
19764: 
19765: sub modify_coursedefaults {
19766:     my ($dom,$lastactref,%domconfig) = @_;
19767:     my ($resulttext,$errors,%changes,%defaultshash);
19768:     my %defaultchecked = (
19769:                            'canuse_pdfforms' => 'off',
19770:                            'uselcmath'       => 'on',
19771:                            'usejsme'         => 'on'
19772:                          );
19773:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
19774:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
19775:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
19776:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
19777:                    'mysqltables_textbook','mysqltables_placement');
19778:     my @types = ('official','unofficial','community','textbook','placement');
19779:     my %staticdefaults = (
19780:                            anonsurvey_threshold => 10,
19781:                            uploadquota          => 500,
19782:                            postsubmit           => 60,
19783:                            mysqltables          => 172800,
19784:                          );
19785:     my %texoptions = (
19786:                         MathJax  => 'MathJax',
19787:                         mimetex  => &mt('Convert to Images'),
19788:                         tth      => &mt('TeX to HTML'),
19789:                      );
19790:     $defaultshash{'coursedefaults'} = {};
19791: 
19792:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
19793:         if ($domconfig{'coursedefaults'} eq '') {
19794:             $domconfig{'coursedefaults'} = {};
19795:         }
19796:     }
19797: 
19798:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
19799:         foreach my $item (@toggles) {
19800:             if ($defaultchecked{$item} eq 'on') {
19801:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19802:                     ($env{'form.'.$item} eq '0')) {
19803:                     $changes{$item} = 1;
19804:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19805:                     $changes{$item} = 1;
19806:                 }
19807:             } elsif ($defaultchecked{$item} eq 'off') {
19808:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19809:                     ($env{'form.'.$item} eq '1')) {
19810:                     $changes{$item} = 1;
19811:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19812:                     $changes{$item} = 1;
19813:                 }
19814:             }
19815:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
19816:         }
19817:         foreach my $item (@numbers) {
19818:             my ($currdef,$newdef);
19819:             $newdef = $env{'form.'.$item};
19820:             if ($item eq 'anonsurvey_threshold') {
19821:                 $currdef = $domconfig{'coursedefaults'}{$item};
19822:                 $newdef =~ s/\D//g;
19823:                 if ($newdef eq '' || $newdef < 1) {
19824:                     $newdef = 1;
19825:                 }
19826:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
19827:             } else {
19828:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
19829:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
19830:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
19831:                 }
19832:                 $newdef =~ s/[^\w.\-]//g;
19833:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
19834:             }
19835:             if ($currdef ne $newdef) {
19836:                 if ($item eq 'anonsurvey_threshold') {
19837:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
19838:                         $changes{$item} = 1;
19839:                     }
19840:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
19841:                     my $setting = $1;
19842:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
19843:                         $changes{$setting} = 1;
19844:                     }
19845:                 }
19846:             }
19847:         }
19848:         my $texengine;
19849:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
19850:             $texengine = $env{'form.texengine'};
19851:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
19852:             if ($currdef eq '') {
19853:                 unless ($texengine eq $Apache::lonnet::deftex) {
19854:                     $changes{'texengine'} = 1;
19855:                 }
19856:             } elsif ($currdef ne $texengine) {
19857:                 $changes{'texengine'} = 1;
19858:             }
19859:         }
19860:         if ($texengine ne '') {
19861:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
19862:         }
19863:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
19864:         my @currclonecode;
19865:         if (ref($currclone) eq 'HASH') {
19866:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
19867:                 @currclonecode = @{$currclone->{'instcode'}};
19868:             }
19869:         }
19870:         my $newclone;
19871:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
19872:             $newclone = $env{'form.canclone'};
19873:         }
19874:         if ($newclone eq 'instcode') {
19875:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
19876:             my (%codedefaults,@code_order,@clonecode);
19877:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
19878:                                                     \@code_order);
19879:             foreach my $item (@code_order) {
19880:                 if (grep(/^\Q$item\E$/,@newcodes)) {
19881:                     push(@clonecode,$item);
19882:                 }
19883:             }
19884:             if (@clonecode) {
19885:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
19886:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
19887:                 if (@diffs) {
19888:                     $changes{'canclone'} = 1;
19889:                 }
19890:             } else {
19891:                 $newclone eq '';
19892:             }
19893:         } elsif ($newclone ne '') {
19894:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
19895:         }
19896:         if ($newclone ne $currclone) {
19897:             $changes{'canclone'} = 1;
19898:         }
19899:         my %credits;
19900:         foreach my $type (@types) {
19901:             unless ($type eq 'community') {
19902:                 $credits{$type} = $env{'form.'.$type.'_credits'};
19903:                 $credits{$type} =~ s/[^\d.]+//g;
19904:             }
19905:         }
19906:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
19907:             ($env{'form.coursecredits'} eq '1')) {
19908:             $changes{'coursecredits'} = 1;
19909:             foreach my $type (keys(%credits)) {
19910:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19911:             }
19912:         } else {
19913:             if ($env{'form.coursecredits'} eq '1') {
19914:                 foreach my $type (@types) {
19915:                     unless ($type eq 'community') {
19916:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
19917:                             $changes{'coursecredits'} = 1;
19918:                         }
19919:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19920:                     }
19921:                 }
19922:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19923:                 foreach my $type (@types) {
19924:                     unless ($type eq 'community') {
19925:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
19926:                             $changes{'coursecredits'} = 1;
19927:                             last;
19928:                         }
19929:                     }
19930:                 }
19931:             }
19932:         }
19933:         if ($env{'form.postsubmit'} eq '1') {
19934:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
19935:             my %currtimeout;
19936:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19937:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
19938:                     $changes{'postsubmit'} = 1;
19939:                 }
19940:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19941:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
19942:                 }
19943:             } else {
19944:                 $changes{'postsubmit'} = 1;
19945:             }
19946:             foreach my $type (@types) {
19947:                 my $timeout = $env{'form.'.$type.'_timeout'};
19948:                 $timeout =~ s/\D//g;
19949:                 if ($timeout == $staticdefaults{'postsubmit'}) {
19950:                     $timeout = '';
19951:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
19952:                     $timeout = '0';
19953:                 }
19954:                 unless ($timeout eq '') {
19955:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
19956:                 }
19957:                 if (exists($currtimeout{$type})) {
19958:                     if ($timeout ne $currtimeout{$type}) {
19959:                         $changes{'postsubmit'} = 1;
19960:                     }
19961:                 } elsif ($timeout ne '') {
19962:                     $changes{'postsubmit'} = 1;
19963:                 }
19964:             }
19965:         } else {
19966:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
19967:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19968:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
19969:                     $changes{'postsubmit'} = 1;
19970:                 }
19971:             } else {
19972:                 $changes{'postsubmit'} = 1;
19973:             }
19974:         }
19975:     }
19976:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19977:                                              $dom);
19978:     if ($putresult eq 'ok') {
19979:         if (keys(%changes) > 0) {
19980:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19981:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
19982:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
19983:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
19984:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
19985:                     if ($changes{$item}) {
19986:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
19987:                     }
19988:                 }
19989:                 if ($changes{'coursecredits'}) {
19990:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19991:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
19992:                             $domdefaults{$type.'credits'} =
19993:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
19994:                         }
19995:                     }
19996:                 }
19997:                 if ($changes{'postsubmit'}) {
19998:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19999:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
20000:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20001:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
20002:                                 $domdefaults{$type.'postsubtimeout'} =
20003:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20004:                             }
20005:                         }
20006:                     }
20007:                 }
20008:                 if ($changes{'uploadquota'}) {
20009:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20010:                         foreach my $type (@types) {
20011:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
20012:                         }
20013:                     }
20014:                 }
20015:                 if ($changes{'canclone'}) {
20016:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20017:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20018:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
20019:                             if (@clonecodes) {
20020:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
20021:                             }
20022:                         }
20023:                     } else {
20024:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
20025:                     }
20026:                 }
20027:                 my $cachetime = 24*60*60;
20028:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20029:                 if (ref($lastactref) eq 'HASH') {
20030:                     $lastactref->{'domdefaults'} = 1;
20031:                 }
20032:             }
20033:             $resulttext = &mt('Changes made:').'<ul>';
20034:             foreach my $item (sort(keys(%changes))) {
20035:                 if ($item eq 'canuse_pdfforms') {
20036:                     if ($env{'form.'.$item} eq '1') {
20037:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
20038:                     } else {
20039:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
20040:                     }
20041:                 } elsif ($item eq 'uselcmath') {
20042:                     if ($env{'form.'.$item} eq '1') {
20043:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
20044:                     } else {
20045:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
20046:                     }
20047:                 } elsif ($item eq 'usejsme') {
20048:                     if ($env{'form.'.$item} eq '1') {
20049:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
20050:                     } else {
20051:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
20052:                     }
20053:                 } elsif ($item eq 'texengine') {
20054:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
20055:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
20056:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
20057:                     }
20058:                 } elsif ($item eq 'anonsurvey_threshold') {
20059:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
20060:                 } elsif ($item eq 'uploadquota') {
20061:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20062:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
20063:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
20064:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
20065:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
20066:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
20067:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
20068:                                        '</ul>'.
20069:                                        '</li>';
20070:                     } else {
20071:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
20072:                     }
20073:                 } elsif ($item eq 'mysqltables') {
20074:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
20075:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
20076:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
20077:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
20078:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
20079:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
20080:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
20081:                                        '</ul>'.
20082:                                        '</li>';
20083:                     } else {
20084:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
20085:                     }
20086:                 } elsif ($item eq 'postsubmit') {
20087:                     if ($domdefaults{'postsubmit'} eq 'off') {
20088:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
20089:                     } else {
20090:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
20091:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20092:                             $resulttext .= &mt('durations:').'<ul>';
20093:                             foreach my $type (@types) {
20094:                                 $resulttext .= '<li>';
20095:                                 my $timeout;
20096:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20097:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20098:                                 }
20099:                                 my $display;
20100:                                 if ($timeout eq '0') {
20101:                                     $display = &mt('unlimited');
20102:                                 } elsif ($timeout eq '') {
20103:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
20104:                                 } else {
20105:                                     $display = &mt('[quant,_1,second]',$timeout);
20106:                                 }
20107:                                 if ($type eq 'community') {
20108:                                     $resulttext .= &mt('Communities');
20109:                                 } elsif ($type eq 'official') {
20110:                                     $resulttext .= &mt('Official courses');
20111:                                 } elsif ($type eq 'unofficial') {
20112:                                     $resulttext .= &mt('Unofficial courses');
20113:                                 } elsif ($type eq 'textbook') {
20114:                                     $resulttext .= &mt('Textbook courses');
20115:                                 } elsif ($type eq 'placement') {
20116:                                     $resulttext .= &mt('Placement tests');
20117:                                 }
20118:                                 $resulttext .= ' -- '.$display.'</li>';
20119:                             }
20120:                             $resulttext .= '</ul>';
20121:                         }
20122:                         $resulttext .= '</li>';
20123:                     }
20124:                 } elsif ($item eq 'coursecredits') {
20125:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20126:                         if (($domdefaults{'officialcredits'} eq '') &&
20127:                             ($domdefaults{'unofficialcredits'} eq '') &&
20128:                             ($domdefaults{'textbookcredits'} eq '')) {
20129:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20130:                         } else {
20131:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
20132:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
20133:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
20134:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
20135:                                            '</ul>'.
20136:                                            '</li>';
20137:                         }
20138:                     } else {
20139:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20140:                     }
20141:                 } elsif ($item eq 'canclone') {
20142:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20143:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20144:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
20145:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
20146:                         }
20147:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
20148:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
20149:                     } else {
20150:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
20151:                     }
20152:                 }
20153:             }
20154:             $resulttext .= '</ul>';
20155:         } else {
20156:             $resulttext = &mt('No changes made to course defaults');
20157:         }
20158:     } else {
20159:         $resulttext = '<span class="LC_error">'.
20160:             &mt('An error occurred: [_1]',$putresult).'</span>';
20161:     }
20162:     return $resulttext;
20163: }
20164: 
20165: sub modify_selfenrollment {
20166:     my ($dom,$lastactref,%domconfig) = @_;
20167:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
20168:     my @types = ('official','unofficial','community','textbook','placement');
20169:     my %titles = &tool_titles();
20170:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
20171:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
20172:     $ordered{'default'} = ['types','registered','approval','limit'];
20173: 
20174:     my (%roles,%shown,%toplevel);
20175:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
20176: 
20177:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
20178:         if ($domconfig{'selfenrollment'} eq '') {
20179:             $domconfig{'selfenrollment'} = {};
20180:         }
20181:     }
20182:     %toplevel = (
20183:                   admin      => 'Configuration Rights',
20184:                   default    => 'Default settings',
20185:                   validation => 'Validation of self-enrollment requests',
20186:                 );
20187:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
20188: 
20189:     if (ref($ordered{'admin'}) eq 'ARRAY') {
20190:         foreach my $item (@{$ordered{'admin'}}) {
20191:             foreach my $type (@types) {
20192:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
20193:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
20194:                 } else {
20195:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
20196:                 }
20197:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
20198:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
20199:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
20200:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
20201:                             push(@{$changes{'admin'}{$type}},$item);
20202:                         }
20203:                     } else {
20204:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
20205:                             push(@{$changes{'admin'}{$type}},$item);
20206:                         }
20207:                     }
20208:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
20209:                     push(@{$changes{'admin'}{$type}},$item);
20210:                 }
20211:             }
20212:         }
20213:     }
20214: 
20215:     foreach my $item (@{$ordered{'default'}}) {
20216:         foreach my $type (@types) {
20217:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
20218:             if ($item eq 'types') {
20219:                 unless (($value eq 'all') || ($value eq 'dom')) {
20220:                     $value = '';
20221:                 }
20222:             } elsif ($item eq 'registered') {
20223:                 unless ($value eq '1') {
20224:                     $value = 0;
20225:                 }
20226:             } elsif ($item eq 'approval') {
20227:                 unless ($value =~ /^[012]$/) {
20228:                     $value = 0;
20229:                 }
20230:             } else {
20231:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20232:                     $value = 'none';
20233:                 }
20234:             }
20235:             $selfenrollhash{'default'}{$type}{$item} = $value;
20236:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
20237:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20238:                     if ($selfenrollhash{'default'}{$type}{$item} ne
20239:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
20240:                          push(@{$changes{'default'}{$type}},$item);
20241:                     }
20242:                 } else {
20243:                     push(@{$changes{'default'}{$type}},$item);
20244:                 }
20245:             } else {
20246:                 push(@{$changes{'default'}{$type}},$item);
20247:             }
20248:             if ($item eq 'limit') {
20249:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20250:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
20251:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
20252:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
20253:                     }
20254:                 } else {
20255:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
20256:                 }
20257:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20258:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
20259:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
20260:                          push(@{$changes{'default'}{$type}},'cap');
20261:                     }
20262:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
20263:                     push(@{$changes{'default'}{$type}},'cap');
20264:                 }
20265:             }
20266:         }
20267:     }
20268: 
20269:     foreach my $item (@{$itemsref}) {
20270:         if ($item eq 'fields') {
20271:             my @changed;
20272:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
20273:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
20274:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
20275:             }
20276:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20277:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
20278:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
20279:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
20280:                 } else {
20281:                     @changed = @{$selfenrollhash{'validation'}{$item}};
20282:                 }
20283:             } else {
20284:                 @changed = @{$selfenrollhash{'validation'}{$item}};
20285:             }
20286:             if (@changed) {
20287:                 if ($selfenrollhash{'validation'}{$item}) { 
20288:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
20289:                 } else {
20290:                     $changes{'validation'}{$item} = &mt('None');
20291:                 }
20292:             }
20293:         } else {
20294:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
20295:             if ($item eq 'markup') {
20296:                if ($env{'form.selfenroll_validation_'.$item}) {
20297:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
20298:                }
20299:             }
20300:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20301:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
20302:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
20303:                 }
20304:             }
20305:         }
20306:     }
20307: 
20308:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
20309:                                              $dom);
20310:     if ($putresult eq 'ok') {
20311:         if (keys(%changes) > 0) {
20312:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20313:             $resulttext = &mt('Changes made:').'<ul>';
20314:             foreach my $key ('admin','default','validation') {
20315:                 if (ref($changes{$key}) eq 'HASH') {
20316:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
20317:                     if ($key eq 'validation') {
20318:                         foreach my $item (@{$itemsref}) {
20319:                             if (exists($changes{$key}{$item})) {
20320:                                 if ($item eq 'markup') {
20321:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20322:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
20323:                                 } else {  
20324:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20325:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
20326:                                 }
20327:                             }
20328:                         }
20329:                     } else {
20330:                         foreach my $type (@types) {
20331:                             if ($type eq 'community') {
20332:                                 $roles{'1'} = &mt('Community personnel');
20333:                             } else {
20334:                                 $roles{'1'} = &mt('Course personnel');
20335:                             }
20336:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
20337:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
20338:                                     if ($key eq 'admin') {
20339:                                         my @mgrdc = ();
20340:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20341:                                             foreach my $item (@{$ordered{'admin'}}) {
20342:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
20343:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
20344:                                                         push(@mgrdc,$item);
20345:                                                     }
20346:                                                 }
20347:                                             }
20348:                                             if (@mgrdc) {
20349:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
20350:                                             } else {
20351:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
20352:                                             }
20353:                                         }
20354:                                     } else {
20355:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20356:                                             foreach my $item (@{$ordered{$key}}) {
20357:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20358:                                                     $domdefaults{$type.'selfenroll'.$item} =
20359:                                                         $selfenrollhash{$key}{$type}{$item};
20360:                                                 }
20361:                                             }
20362:                                         }
20363:                                     }
20364:                                 }
20365:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
20366:                                 foreach my $item (@{$ordered{$key}}) {
20367:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20368:                                         $resulttext .= '<li>';
20369:                                         if ($key eq 'admin') {
20370:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
20371:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
20372:                                         } else {
20373:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
20374:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
20375:                                         }
20376:                                         $resulttext .= '</li>';
20377:                                     }
20378:                                 }
20379:                                 $resulttext .= '</ul></li>';
20380:                             }
20381:                         }
20382:                         $resulttext .= '</ul></li>'; 
20383:                     }
20384:                 }
20385:             }
20386:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
20387:                 my $cachetime = 24*60*60;
20388:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20389:                 if (ref($lastactref) eq 'HASH') {
20390:                     $lastactref->{'domdefaults'} = 1;
20391:                 }
20392:             }
20393:             $resulttext .= '</ul>';
20394:         } else {
20395:             $resulttext = &mt('No changes made to self-enrollment settings');
20396:         }
20397:     } else {
20398:         $resulttext = '<span class="LC_error">'.
20399:             &mt('An error occurred: [_1]',$putresult).'</span>';
20400:     }
20401:     return $resulttext;
20402: }
20403: 
20404: sub modify_wafproxy {
20405:     my ($dom,$action,$lastactref,%domconfig) = @_;
20406:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20407:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
20408:         %wafproxy,%changes,%expirecache,%expiresaml);
20409:     foreach my $server (sort(keys(%servers))) {
20410:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20411:         if ($serverhome eq $server) {
20412:             my $serverdom = &Apache::lonnet::host_domain($server);
20413:             if ($serverdom eq $dom) {
20414:                 $canset{$server} = 1;
20415:             }
20416:         }
20417:     }
20418:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
20419:         %{$values{$dom}} = ();
20420:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
20421:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
20422:         }
20423:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
20424:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
20425:         }
20426:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20427:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
20428:         }
20429:     }
20430:     my $output;
20431:     if (keys(%canset)) {
20432:         %{$wafproxy{'alias'}} = ();
20433:         %{$wafproxy{'saml'}} = ();
20434:         foreach my $key (sort(keys(%canset))) {
20435:             if ($env{'form.wafproxy_'.$dom}) {
20436:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
20437:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
20438:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
20439:                     $changes{'alias'} = 1;
20440:                 }
20441:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
20442:                     $wafproxy{'saml'}{$key} = 1;
20443:                 }
20444:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
20445:                     $changes{'saml'} = 1;
20446:                 }
20447:             } else {
20448:                 $wafproxy{'alias'}{$key} = '';
20449:                 $wafproxy{'saml'}{$key} = '';
20450:                 if ($curralias{$key}) {
20451:                     $changes{'alias'} = 1;
20452:                 }
20453:                 if ($currsaml{$key}) {
20454:                     $changes{'saml'} = 1;
20455:                 }
20456:             }
20457:             if ($wafproxy{'alias'}{$key} eq '') {
20458:                 if ($curralias{$key}) {
20459:                     $expirecache{$key} = 1;
20460:                 }
20461:                 delete($wafproxy{'alias'}{$key});
20462:             }
20463:             if ($wafproxy{'saml'}{$key} eq '') {
20464:                 if ($currsaml{$key}) {
20465:                     $expiresaml{$key} = 1;
20466:                 }
20467:                 delete($wafproxy{'saml'}{$key});
20468:             }
20469:         }
20470:         unless (keys(%{$wafproxy{'alias'}})) {
20471:             delete($wafproxy{'alias'});
20472:         }
20473:         unless (keys(%{$wafproxy{'saml'}})) {
20474:             delete($wafproxy{'saml'});
20475:         }
20476:         # Localization for values in %warn occurs in &mt() calls separately.
20477:         my %warn = (
20478:                      trusted => 'trusted IP range(s)',
20479:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
20480:                      vpnext => 'IP range(s) for backend WAF connections',
20481:                    );
20482:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20483:             my $possible = $env{'form.wafproxy_'.$item};
20484:             $possible =~ s/^\s+|\s+$//g;
20485:             if ($possible ne '') {
20486:                 if ($item eq 'remoteip') {
20487:                     if ($possible =~ /^[mhn]$/) {
20488:                         $wafproxy{$item} = $possible;
20489:                     }
20490:                 } elsif ($item eq 'ipheader') {
20491:                     if ($wafproxy{'remoteip'} eq 'h') {
20492:                         $wafproxy{$item} = $possible;
20493:                     }
20494:                 } elsif ($item eq 'sslopt') {
20495:                     if ($possible =~ /^0|1$/) {
20496:                         $wafproxy{$item} = $possible;
20497:                     }
20498:                 } else {
20499:                     my (@ok,$count);
20500:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
20501:                         unless ($env{'form.wafproxy_vpnaccess'}) {
20502:                             $possible = '';
20503:                         }
20504:                     } elsif ($item eq 'trusted') {
20505:                         unless ($wafproxy{'remoteip'} eq 'h') {
20506:                             $possible = '';
20507:                         }
20508:                     }
20509:                     unless ($possible eq '') {
20510:                         $possible =~ s/[\r\n]+/\s/g;
20511:                         $possible =~ s/\s*-\s*/-/g;
20512:                         $possible =~ s/\s+/,/g;
20513:                         $possible =~ s/,+/,/g;
20514:                     }
20515:                     $count = 0;
20516:                     if ($possible ne '') {
20517:                         foreach my $poss (split(/\,/,$possible)) {
20518:                             $count ++;
20519:                             $poss = &validate_ip_pattern($poss);
20520:                             if ($poss ne '') {
20521:                                 push(@ok,$poss);
20522:                             }
20523:                         }
20524:                         my $diff = $count - scalar(@ok);
20525:                         if ($diff) {
20526:                             push(@warnings,'<li>'.
20527:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
20528:                                      $diff,$warn{$item}).
20529:                                  '</li>');
20530:                         }
20531:                         if (@ok) {
20532:                             my @cidr_list;
20533:                             foreach my $item (@ok) {
20534:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
20535:                             }
20536:                             $wafproxy{$item} = join(',',@cidr_list);
20537:                         }
20538:                     }
20539:                 }
20540:                 if ($wafproxy{$item} ne $currvalue{$item}) {
20541:                     $changes{$item} = 1;
20542:                 }
20543:             } elsif ($currvalue{$item}) {
20544:                 $changes{$item} = 1;
20545:             } 
20546:         }
20547:     } else {
20548:         if (keys(%curralias)) {
20549:             $changes{'alias'} = 1;
20550:         }
20551:         if (keys(%currsaml)) {
20552:             $changes{'saml'} = 1;
20553:         } 
20554:         if (keys(%currvalue)) {
20555:             foreach my $key (keys(%currvalue)) {
20556:                 $changes{$key} = 1;
20557:             }
20558:         }
20559:     }
20560:     if (keys(%changes)) {
20561:         my %defaultshash = (
20562:                               wafproxy => \%wafproxy,
20563:                            ); 
20564:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20565:                                                  $dom);
20566:         if ($putresult eq 'ok') {
20567:             my $cachetime = 24*60*60;
20568:             my (%domdefaults,$updatedomdefs);
20569:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
20570:                 if ($changes{$item}) {
20571:                     unless ($updatedomdefs) {
20572:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20573:                         $updatedomdefs = 1;
20574:                     }
20575:                     if ($wafproxy{$item}) {
20576:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
20577:                     } elsif (exists($domdefaults{'waf_'.$item})) {
20578:                         delete($domdefaults{'waf_'.$item});
20579:                     } 
20580:                 }
20581:             }
20582:             if ($updatedomdefs) {
20583:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20584:                 if (ref($lastactref) eq 'HASH') {
20585:                     $lastactref->{'domdefaults'} = 1;
20586:                 }
20587:             }
20588:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
20589:                 my %updates = %expirecache;
20590:                 foreach my $key (keys(%expirecache)) {
20591:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
20592:                 }
20593:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
20594:                     my $cachetime = 24*60*60;
20595:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
20596:                         $updates{$key} = 1;
20597:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
20598:                                                       $cachetime);
20599:                     }
20600:                 }
20601:                 if (ref($lastactref) eq 'HASH') {
20602:                     $lastactref->{'proxyalias'} = \%updates;
20603:                 }
20604:             }
20605:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
20606:                 my %samlupdates = %expiresaml;
20607:                 foreach my $key (keys(%expiresaml)) {
20608:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
20609:                 }
20610:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
20611:                     my $cachetime = 24*60*60;
20612:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
20613:                         $samlupdates{$key} = 1;
20614:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
20615:                                                       $cachetime);
20616:                     }
20617:                 }
20618:                 if (ref($lastactref) eq 'HASH') {
20619:                     $lastactref->{'proxysaml'} = \%samlupdates;
20620:                 }
20621:             }
20622:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
20623:             foreach my $item ('alias','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20624:                 if ($changes{$item}) {
20625:                     if ($item eq 'alias') {
20626:                         my $numaliased = 0;
20627:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
20628:                             my $shown;
20629:                             if (keys(%{$wafproxy{'alias'}})) {
20630:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
20631:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
20632:                                                          &Apache::lonnet::hostname($server),
20633:                                                          $wafproxy{'alias'}{$server}).'</li>';
20634:                                     $numaliased ++;
20635:                                 }
20636:                                 if ($numaliased) {
20637:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
20638:                                                           '<ul>'.$shown.'</ul>').'</li>';
20639:                                 }
20640:                             }
20641:                         }
20642:                         unless ($numaliased) {
20643:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
20644:                         }
20645:                     } elsif ($item eq 'saml') {
20646:                         my $shown; 
20647:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
20648:                             if (keys(%{$wafproxy{'saml'}})) {
20649:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
20650:                             }
20651:                         }
20652:                         if ($shown) {
20653:                             $output .= '<li>'.&mt('Alias used by Shibboleth for: [_1]',
20654:                                                   $shown).'</li>';
20655:                         } else {
20656:                             $output .= '<li>'.&mt('No alias used for Shibboleth').'</li>';
20657:                         }
20658:                     } else {
20659:                         if ($item eq 'remoteip') {
20660:                             my %ip_methods = &remoteip_methods();
20661:                             if ($wafproxy{$item} =~ /^[mh]$/) {
20662:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
20663:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
20664:                             } else {
20665:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
20666:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
20667:                                                '</li>';
20668:                                 } else {
20669:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
20670:                                 }
20671:                             }
20672:                         } elsif ($item eq 'ipheader') {
20673:                             if ($wafproxy{$item}) {
20674:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
20675:                                                       $wafproxy{$item}).'</li>';
20676:                             } else {
20677:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
20678:                             }
20679:                         } elsif ($item eq 'trusted') {
20680:                             if ($wafproxy{$item}) {
20681:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
20682:                                                       $wafproxy{$item}).'</li>';
20683:                             } else {
20684:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
20685:                             }
20686:                         } elsif ($item eq 'vpnint') {
20687:                             if ($wafproxy{$item}) {
20688:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
20689:                                                        $wafproxy{$item}).'</li>';
20690:                             } else {
20691:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
20692:                             }
20693:                         } elsif ($item eq 'vpnext') {
20694:                             if ($wafproxy{$item}) {
20695:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
20696:                                                        $wafproxy{$item}).'</li>';
20697:                             } else {
20698:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
20699:                             }
20700:                         } elsif ($item eq 'sslopt') {
20701:                             if ($wafproxy{$item}) {
20702:                                 $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>';
20703:                             } else {
20704:                                 $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>';
20705:                             }
20706:                         }
20707:                     }
20708:                 }
20709:             }
20710:         } else {
20711:             $output = '<span class="LC_error">'.
20712:                       &mt('An error occurred: [_1]',$putresult).'</span>';
20713:         }
20714:     } elsif (keys(%canset)) {
20715:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
20716:     }
20717:     if (@warnings) {
20718:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
20719:                        join("\n",@warnings).'</ul>';
20720:     }
20721:     return $output;
20722: }
20723: 
20724: sub validate_ip_pattern {
20725:     my ($pattern) = @_;
20726:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
20727:         my ($start,$end) = ($1,$2);
20728:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
20729:             if (($start !~ m{/}) && ($end !~ m{/})) {
20730:                 return $start.'-'.$end;
20731:             }
20732:         }
20733:     } elsif ($pattern ne '') {
20734:         $pattern = &Net::CIDR::cidrvalidate($pattern);
20735:         if ($pattern ne '') {
20736:             return $pattern;
20737:         }
20738:     }
20739:     return;
20740: }
20741: 
20742: sub modify_usersessions {
20743:     my ($dom,$lastactref,%domconfig) = @_;
20744:     my @hostingtypes = ('version','excludedomain','includedomain');
20745:     my @offloadtypes = ('primary','default');
20746:     my %types = (
20747:                   remote => \@hostingtypes,
20748:                   hosted => \@hostingtypes,
20749:                   spares => \@offloadtypes,
20750:                 );
20751:     my @prefixes = ('remote','hosted','spares');
20752:     my @lcversions = &Apache::lonnet::all_loncaparevs();
20753:     my (%by_ip,%by_location,@intdoms,@instdoms);
20754:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20755:     my @locations = sort(keys(%by_location));
20756:     my (%defaultshash,%changes);
20757:     foreach my $prefix (@prefixes) {
20758:         $defaultshash{'usersessions'}{$prefix} = {};
20759:     }
20760:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20761:     my $resulttext;
20762:     my %iphost = &Apache::lonnet::get_iphost();
20763:     foreach my $prefix (@prefixes) {
20764:         next if ($prefix eq 'spares');
20765:         foreach my $type (@{$types{$prefix}}) {
20766:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20767:             if ($type eq 'version') {
20768:                 my $value = $env{'form.'.$prefix.'_'.$type};
20769:                 my $okvalue;
20770:                 if ($value ne '') {
20771:                     if (grep(/^\Q$value\E$/,@lcversions)) {
20772:                         $okvalue = $value;
20773:                     }
20774:                 }
20775:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20776:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20777:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
20778:                             if ($inuse == 0) {
20779:                                 $changes{$prefix}{$type} = 1;
20780:                             } else {
20781:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
20782:                                     $changes{$prefix}{$type} = 1;
20783:                                 }
20784:                                 if ($okvalue ne '') {
20785:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20786:                                 } 
20787:                             }
20788:                         } else {
20789:                             if (($inuse == 1) && ($okvalue ne '')) {
20790:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20791:                                 $changes{$prefix}{$type} = 1;
20792:                             }
20793:                         }
20794:                     } else {
20795:                         if (($inuse == 1) && ($okvalue ne '')) {
20796:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20797:                             $changes{$prefix}{$type} = 1;
20798:                         }
20799:                     }
20800:                 } else {
20801:                     if (($inuse == 1) && ($okvalue ne '')) {
20802:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20803:                         $changes{$prefix}{$type} = 1;
20804:                     }
20805:                 }
20806:             } else {
20807:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20808:                 my @okvals;
20809:                 foreach my $val (@vals) {
20810:                     if ($val =~ /:/) {
20811:                         my @items = split(/:/,$val);
20812:                         foreach my $item (@items) {
20813:                             if (ref($by_location{$item}) eq 'ARRAY') {
20814:                                 push(@okvals,$item);
20815:                             }
20816:                         }
20817:                     } else {
20818:                         if (ref($by_location{$val}) eq 'ARRAY') {
20819:                             push(@okvals,$val);
20820:                         }
20821:                     }
20822:                 }
20823:                 @okvals = sort(@okvals);
20824:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20825:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20826:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20827:                             if ($inuse == 0) {
20828:                                 $changes{$prefix}{$type} = 1; 
20829:                             } else {
20830:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20831:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
20832:                                 if (@changed > 0) {
20833:                                     $changes{$prefix}{$type} = 1;
20834:                                 }
20835:                             }
20836:                         } else {
20837:                             if ($inuse == 1) {
20838:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20839:                                 $changes{$prefix}{$type} = 1;
20840:                             }
20841:                         } 
20842:                     } else {
20843:                         if ($inuse == 1) {
20844:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20845:                             $changes{$prefix}{$type} = 1;
20846:                         }
20847:                     }
20848:                 } else {
20849:                     if ($inuse == 1) {
20850:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20851:                         $changes{$prefix}{$type} = 1;
20852:                     }
20853:                 }
20854:             }
20855:         }
20856:     }
20857: 
20858:     my @alldoms = &Apache::lonnet::all_domains();
20859:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20860:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
20861:     my $savespares;
20862: 
20863:     foreach my $lonhost (sort(keys(%servers))) {
20864:         my $serverhomeID =
20865:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
20866:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
20867:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
20868:         my %spareschg;
20869:         foreach my $type (@{$types{'spares'}}) {
20870:             my @okspares;
20871:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
20872:             foreach my $server (@checked) {
20873:                 if (&Apache::lonnet::hostname($server) ne '') {
20874:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
20875:                         unless (grep(/^\Q$server\E$/,@okspares)) {
20876:                             push(@okspares,$server);
20877:                         }
20878:                     }
20879:                 }
20880:             }
20881:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
20882:             my $newspare;
20883:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
20884:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
20885:                     $newspare = $new;
20886:                 }
20887:             }
20888:             my @spares;
20889:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
20890:                 @spares = sort(@okspares,$newspare);
20891:             } else {
20892:                 @spares = sort(@okspares);
20893:             }
20894:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
20895:             if (ref($spareid{$lonhost}) eq 'HASH') {
20896:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
20897:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
20898:                     if (@diffs > 0) {
20899:                         $spareschg{$type} = 1;
20900:                     }
20901:                 }
20902:             }
20903:         }
20904:         if (keys(%spareschg) > 0) {
20905:             $changes{'spares'}{$lonhost} = \%spareschg;
20906:         }
20907:     }
20908:     $defaultshash{'usersessions'}{'offloadnow'} = {};
20909:     $defaultshash{'usersessions'}{'offloadoth'} = {};
20910:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
20911:     my @okoffload;
20912:     if (@offloadnow) {
20913:         foreach my $server (@offloadnow) {
20914:             if (&Apache::lonnet::hostname($server) ne '') {
20915:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
20916:                     push(@okoffload,$server);
20917:                 }
20918:             }
20919:         }
20920:         if (@okoffload) {
20921:             foreach my $lonhost (@okoffload) {
20922:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
20923:             }
20924:         }
20925:     }
20926:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
20927:     my @okoffloadoth;
20928:     if (@offloadoth) {
20929:         foreach my $server (@offloadoth) {
20930:             if (&Apache::lonnet::hostname($server) ne '') {
20931:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
20932:                     push(@okoffloadoth,$server);
20933:                 }
20934:             }
20935:         }
20936:         if (@okoffloadoth) {
20937:             foreach my $lonhost (@okoffloadoth) {
20938:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
20939:             }
20940:         }
20941:     }
20942:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
20943:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
20944:             if (ref($changes{'spares'}) eq 'HASH') {
20945:                 if (keys(%{$changes{'spares'}}) > 0) {
20946:                     $savespares = 1;
20947:                 }
20948:             }
20949:         } else {
20950:             $savespares = 1;
20951:         }
20952:         foreach my $offload ('offloadnow','offloadoth') {
20953:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
20954:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
20955:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
20956:                         $changes{$offload} = 1;
20957:                         last;
20958:                     }
20959:                 }
20960:                 unless ($changes{$offload}) {
20961:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
20962:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
20963:                             $changes{$offload} = 1;
20964:                             last;
20965:                         }
20966:                     }
20967:                 }
20968:             } else {
20969:                 if (($offload eq 'offloadnow') && (@okoffload)) {
20970:                      $changes{'offloadnow'} = 1;
20971:                 }
20972:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
20973:                     $changes{'offloadoth'} = 1;
20974:                 }
20975:             } 
20976:         }
20977:     } else {
20978:         if (@okoffload) {
20979:             $changes{'offloadnow'} = 1;
20980:         }
20981:         if (@okoffloadoth) {
20982:             $changes{'offloadoth'} = 1;
20983:         }
20984:     }
20985:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
20986:     if ((keys(%changes) > 0) || ($savespares)) {
20987:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20988:                                                  $dom);
20989:         if ($putresult eq 'ok') {
20990:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20991:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
20992:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
20993:                 }
20994:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
20995:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
20996:                 }
20997:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20998:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
20999:                 }
21000:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21001:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
21002:                 }
21003:             }
21004:             my $cachetime = 24*60*60;
21005:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21006:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
21007:             if (ref($lastactref) eq 'HASH') {
21008:                 $lastactref->{'domdefaults'} = 1;
21009:                 $lastactref->{'usersessions'} = 1;
21010:             }
21011:             if (keys(%changes) > 0) {
21012:                 my %lt = &usersession_titles();
21013:                 $resulttext = &mt('Changes made:').'<ul>';
21014:                 foreach my $prefix (@prefixes) {
21015:                     if (ref($changes{$prefix}) eq 'HASH') {
21016:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21017:                         if ($prefix eq 'spares') {
21018:                             if (ref($changes{$prefix}) eq 'HASH') {
21019:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
21020:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
21021:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
21022:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
21023:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21024:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
21025:                                         foreach my $type (@{$types{$prefix}}) {
21026:                                             if ($changes{$prefix}{$lonhost}{$type}) {
21027:                                                 my $offloadto = &mt('None');
21028:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
21029:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
21030:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
21031:                                                     }
21032:                                                 }
21033:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
21034:                                             }
21035:                                         }
21036:                                     }
21037:                                     $resulttext .= '</li>';
21038:                                 }
21039:                             }
21040:                         } else {
21041:                             foreach my $type (@{$types{$prefix}}) {
21042:                                 if (defined($changes{$prefix}{$type})) {
21043:                                     my ($newvalue,$notinuse);
21044:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21045:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
21046:                                             if ($type eq 'version') {
21047:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
21048:                                             } else {
21049:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21050:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
21051:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
21052:                                                     }
21053:                                                 } else {
21054:                                                     $notinuse = 1;
21055:                                                 }
21056:                                             }
21057:                                         }
21058:                                     }
21059:                                     if ($newvalue eq '') {
21060:                                         if ($type eq 'version') {
21061:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
21062:                                         } elsif ($notinuse) {
21063:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21064:                                         } else {
21065:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21066:                                         }
21067:                                     } else {
21068:                                         if ($type eq 'version') {
21069:                                             $newvalue .= ' '.&mt('(or later)');
21070:                                         }
21071:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21072:                                     }
21073:                                 }
21074:                             }
21075:                         }
21076:                         $resulttext .= '</ul>';
21077:                     }
21078:                 }
21079:                 if ($changes{'offloadnow'}) {
21080:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21081:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
21082:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
21083:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
21084:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21085:                             }
21086:                             $resulttext .= '</ul>';
21087:                         } else {
21088:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
21089:                         }
21090:                     } else {
21091:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
21092:                     }
21093:                 }
21094:                 if ($changes{'offloadoth'}) {
21095:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21096:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
21097:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
21098:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
21099:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21100:                             }
21101:                             $resulttext .= '</ul>';
21102:                         } else {
21103:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
21104:                         }
21105:                     } else {
21106:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
21107:                     }
21108:                 }
21109:                 $resulttext .= '</ul>';
21110:             } else {
21111:                 $resulttext = $nochgmsg;
21112:             }
21113:         } else {
21114:             $resulttext = '<span class="LC_error">'.
21115:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21116:         }
21117:     } else {
21118:         $resulttext = $nochgmsg;
21119:     }
21120:     return $resulttext;
21121: }
21122: 
21123: sub modify_ssl {
21124:     my ($dom,$lastactref,%domconfig) = @_;
21125:     my (%by_ip,%by_location,@intdoms,@instdoms);
21126:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21127:     my @locations = sort(keys(%by_location));
21128:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21129:     my (%defaultshash,%changes);
21130:     my $action = 'ssl';
21131:     my @prefixes = ('connto','connfrom','replication');
21132:     foreach my $prefix (@prefixes) {
21133:         $defaultshash{$action}{$prefix} = {};
21134:     }
21135:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21136:     my $resulttext;
21137:     my %iphost = &Apache::lonnet::get_iphost();
21138:     my @reptypes = ('certreq','nocertreq');
21139:     my @connecttypes = ('dom','intdom','other');
21140:     my %types = (
21141:                   connto      => \@connecttypes,
21142:                   connfrom    => \@connecttypes,
21143:                   replication => \@reptypes,
21144:                 );
21145:     foreach my $prefix (sort(keys(%types))) {
21146:         foreach my $type (@{$types{$prefix}}) {
21147:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21148:                 my $value = 'yes';
21149:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
21150:                     $value = $env{'form.'.$prefix.'_'.$type};
21151:                 }
21152:                 if (ref($domconfig{$action}) eq 'HASH') {
21153:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21154:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
21155:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
21156:                                 $changes{$prefix}{$type} = 1;
21157:                             }
21158:                             $defaultshash{$action}{$prefix}{$type} = $value;
21159:                         } else {
21160:                             $defaultshash{$action}{$prefix}{$type} = $value;
21161:                             $changes{$prefix}{$type} = 1;
21162:                         }
21163:                     } else {
21164:                         $defaultshash{$action}{$prefix}{$type} = $value;
21165:                         $changes{$prefix}{$type} = 1;
21166:                     }
21167:                 } else {
21168:                     $defaultshash{$action}{$prefix}{$type} = $value;
21169:                     $changes{$prefix}{$type} = 1;
21170:                 }
21171:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
21172:                     delete($changes{$prefix}{$type});
21173:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
21174:                     delete($changes{$prefix}{$type});
21175:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
21176:                     delete($changes{$prefix}{$type});
21177:                 }
21178:             } elsif ($prefix eq 'replication') {
21179:                 if (@locations > 0) {
21180:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21181:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21182:                     my @okvals;
21183:                     foreach my $val (@vals) {
21184:                         if ($val =~ /:/) {
21185:                             my @items = split(/:/,$val);
21186:                             foreach my $item (@items) {
21187:                                 if (ref($by_location{$item}) eq 'ARRAY') {
21188:                                     push(@okvals,$item);
21189:                                 }
21190:                             }
21191:                         } else {
21192:                             if (ref($by_location{$val}) eq 'ARRAY') {
21193:                                 push(@okvals,$val);
21194:                             }
21195:                         }
21196:                     }
21197:                     @okvals = sort(@okvals);
21198:                     if (ref($domconfig{$action}) eq 'HASH') {
21199:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21200:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
21201:                                 if ($inuse == 0) {
21202:                                     $changes{$prefix}{$type} = 1;
21203:                                 } else {
21204:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21205:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
21206:                                     if (@changed > 0) {
21207:                                         $changes{$prefix}{$type} = 1;
21208:                                     }
21209:                                 }
21210:                             } else {
21211:                                 if ($inuse == 1) {
21212:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21213:                                     $changes{$prefix}{$type} = 1;
21214:                                 }
21215:                             }
21216:                         } else {
21217:                             if ($inuse == 1) {
21218:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
21219:                                 $changes{$prefix}{$type} = 1;
21220:                             }
21221:                         }
21222:                     } else {
21223:                         if ($inuse == 1) {
21224:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
21225:                             $changes{$prefix}{$type} = 1;
21226:                         }
21227:                     }
21228:                 }
21229:             }
21230:         }
21231:     }
21232:     if (keys(%changes)) {
21233:         foreach my $prefix (keys(%changes)) {
21234:             if (ref($changes{$prefix}) eq 'HASH') {
21235:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
21236:                     delete($changes{$prefix});
21237:                 }
21238:             } else {
21239:                 delete($changes{$prefix});
21240:             }
21241:         }
21242:     }
21243:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
21244:     if (keys(%changes) > 0) {
21245:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21246:                                                  $dom);
21247:         if ($putresult eq 'ok') {
21248:             if (ref($defaultshash{$action}) eq 'HASH') {
21249:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
21250:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
21251:                 }
21252:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
21253:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
21254:                 }
21255:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
21256:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
21257:                 }
21258:             }
21259:             my $cachetime = 24*60*60;
21260:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21261:             if (ref($lastactref) eq 'HASH') {
21262:                 $lastactref->{'domdefaults'} = 1;
21263:             }
21264:             if (keys(%changes) > 0) {
21265:                 my %titles = &ssl_titles();
21266:                 $resulttext = &mt('Changes made:').'<ul>';
21267:                 foreach my $prefix (@prefixes) {
21268:                     if (ref($changes{$prefix}) eq 'HASH') {
21269:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
21270:                         foreach my $type (@{$types{$prefix}}) {
21271:                             if (defined($changes{$prefix}{$type})) {
21272:                                 my ($newvalue,$notinuse);
21273:                                 if (ref($defaultshash{$action}) eq 'HASH') {
21274:                                     if (ref($defaultshash{$action}{$prefix})) {
21275:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21276:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
21277:                                         } else {
21278:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
21279:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
21280:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
21281:                                                 }
21282:                                             } else {
21283:                                                 $notinuse = 1;
21284:                                             }
21285:                                         }
21286:                                     }
21287:                                     if ($notinuse) {
21288:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
21289:                                     } elsif ($newvalue eq '') {
21290:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
21291:                                     } else {
21292:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
21293:                                     }
21294:                                 }
21295:                             }
21296:                         }
21297:                         $resulttext .= '</ul>';
21298:                     }
21299:                 }
21300:             } else {
21301:                 $resulttext = $nochgmsg;
21302:             }
21303:         } else {
21304:             $resulttext = '<span class="LC_error">'.
21305:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21306:         }
21307:     } else {
21308:         $resulttext = $nochgmsg;
21309:     }
21310:     return $resulttext;
21311: }
21312: 
21313: sub modify_trust {
21314:     my ($dom,$lastactref,%domconfig) = @_;
21315:     my (%by_ip,%by_location,@intdoms,@instdoms);
21316:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21317:     my @locations = sort(keys(%by_location));
21318:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
21319:     my @types = ('exc','inc');
21320:     my (%defaultshash,%changes);
21321:     foreach my $prefix (@prefixes) {
21322:         $defaultshash{'trust'}{$prefix} = {};
21323:     }
21324:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21325:     my $resulttext;
21326:     foreach my $prefix (@prefixes) {
21327:         foreach my $type (@types) {
21328:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21329:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21330:             my @okvals;
21331:             foreach my $val (@vals) {
21332:                 if ($val =~ /:/) {
21333:                     my @items = split(/:/,$val);
21334:                     foreach my $item (@items) {
21335:                         if (ref($by_location{$item}) eq 'ARRAY') {
21336:                             push(@okvals,$item);
21337:                         }
21338:                     }
21339:                 } else {
21340:                     if (ref($by_location{$val}) eq 'ARRAY') {
21341:                         push(@okvals,$val);
21342:                     }
21343:                 }
21344:             }
21345:             @okvals = sort(@okvals);
21346:             if (ref($domconfig{'trust'}) eq 'HASH') {
21347:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
21348:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21349:                         if ($inuse == 0) {
21350:                             $changes{$prefix}{$type} = 1;
21351:                         } else {
21352:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21353:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
21354:                             if (@changed > 0) {
21355:                                 $changes{$prefix}{$type} = 1;
21356:                             }
21357:                         }
21358:                     } else {
21359:                         if ($inuse == 1) {
21360:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21361:                             $changes{$prefix}{$type} = 1;
21362:                         }
21363:                     }
21364:                 } else {
21365:                     if ($inuse == 1) {
21366:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21367:                         $changes{$prefix}{$type} = 1;
21368:                     }
21369:                 }
21370:             } else {
21371:                 if ($inuse == 1) {
21372:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21373:                     $changes{$prefix}{$type} = 1;
21374:                 }
21375:             }
21376:         }
21377:     }
21378:     my $nochgmsg = &mt('No changes made to trust settings.');
21379:     if (keys(%changes) > 0) {
21380:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21381:                                                  $dom);
21382:         if ($putresult eq 'ok') {
21383:             if (ref($defaultshash{'trust'}) eq 'HASH') {
21384:                 foreach my $prefix (@prefixes) {
21385:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
21386:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
21387:                     }
21388:                 }
21389:             }
21390:             my $cachetime = 24*60*60;
21391:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21392:             if (ref($lastactref) eq 'HASH') {
21393:                 $lastactref->{'domdefaults'} = 1;
21394:             }
21395:             if (keys(%changes) > 0) {
21396:                 my %lt = &trust_titles();
21397:                 $resulttext = &mt('Changes made:').'<ul>';
21398:                 foreach my $prefix (@prefixes) {
21399:                     if (ref($changes{$prefix}) eq 'HASH') {
21400:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21401:                         foreach my $type (@types) {
21402:                             if (defined($changes{$prefix}{$type})) {
21403:                                 my ($newvalue,$notinuse);
21404:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
21405:                                     if (ref($defaultshash{'trust'}{$prefix})) {
21406:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21407:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
21408:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
21409:                                             }
21410:                                         } else {
21411:                                             $notinuse = 1;
21412:                                         }
21413:                                     }
21414:                                 }
21415:                                 if ($notinuse) {
21416:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21417:                                 } elsif ($newvalue eq '') {
21418:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21419:                                 } else {
21420:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21421:                                 }
21422:                             }
21423:                         }
21424:                         $resulttext .= '</ul>';
21425:                     }
21426:                 }
21427:                 $resulttext .= '</ul>';
21428:             } else {
21429:                 $resulttext = $nochgmsg;
21430:             }
21431:         } else {
21432:             $resulttext = '<span class="LC_error">'.
21433:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21434:         }
21435:     } else {
21436:         $resulttext = $nochgmsg;
21437:     }
21438:     return $resulttext;
21439: }
21440: 
21441: sub modify_loadbalancing {
21442:     my ($dom,%domconfig) = @_;
21443:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
21444:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
21445:     my ($othertitle,$usertypes,$types) =
21446:         &Apache::loncommon::sorted_inst_types($dom);
21447:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21448:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
21449:     my @sparestypes = ('primary','default');
21450:     my %typetitles = &sparestype_titles();
21451:     my $resulttext;
21452:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
21453:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
21454:         %existing = %{$domconfig{'loadbalancing'}};
21455:     }
21456:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
21457:                               \%currtargets,\%currrules,\%currcookies);
21458:     my ($saveloadbalancing,%defaultshash,%changes);
21459:     my ($alltypes,$othertypes,$titles) =
21460:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
21461:     my %ruletitles = &offloadtype_text();
21462:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
21463:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
21464:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
21465:         if ($balancer eq '') {
21466:             next;
21467:         }
21468:         if (!exists($servers{$balancer})) {
21469:             if (exists($currbalancer{$balancer})) {
21470:                 push(@{$changes{'delete'}},$balancer);
21471:             }
21472:             next;
21473:         }
21474:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
21475:             push(@{$changes{'delete'}},$balancer);
21476:             next;
21477:         }
21478:         if (!exists($currbalancer{$balancer})) {
21479:             push(@{$changes{'add'}},$balancer);
21480:         }
21481:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
21482:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
21483:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
21484:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
21485:             $saveloadbalancing = 1;
21486:         }
21487:         foreach my $sparetype (@sparestypes) {
21488:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
21489:             my @offloadto;
21490:             foreach my $target (@targets) {
21491:                 if (($servers{$target}) && ($target ne $balancer)) {
21492:                     if ($sparetype eq 'default') {
21493:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
21494:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
21495:                         }
21496:                     }
21497:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
21498:                         push(@offloadto,$target);
21499:                     }
21500:                 }
21501:             }
21502:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
21503:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
21504:                     push(@offloadto,$balancer);
21505:                 }
21506:             }
21507:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
21508:         }
21509:         if ($env{'form.loadbalancing_cookie_'.$i}) {
21510:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
21511:             if (exists($currbalancer{$balancer})) { 
21512:                 unless ($currcookies{$balancer}) {
21513:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
21514:                 }
21515:             }
21516:         } elsif (exists($currbalancer{$balancer})) {
21517:             if ($currcookies{$balancer}) {
21518:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
21519:             }
21520:         }
21521:         if (ref($currtargets{$balancer}) eq 'HASH') {
21522:             foreach my $sparetype (@sparestypes) {
21523:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
21524:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
21525:                     if (@targetdiffs > 0) {
21526:                         $changes{'curr'}{$balancer}{'targets'} = 1;
21527:                     }
21528:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21529:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
21530:                         $changes{'curr'}{$balancer}{'targets'} = 1;
21531:                     }
21532:                 }
21533:             }
21534:         } else {
21535:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
21536:                 foreach my $sparetype (@sparestypes) {
21537:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21538:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
21539:                             $changes{'curr'}{$balancer}{'targets'} = 1;
21540:                         }
21541:                     }
21542:                 }
21543:             }
21544:         }
21545:         my $ishomedom;
21546:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
21547:             $ishomedom = 1;
21548:         }
21549:         if (ref($alltypes) eq 'ARRAY') {
21550:             foreach my $type (@{$alltypes}) {
21551:                 my $rule;
21552:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
21553:                          (!$ishomedom)) {
21554:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
21555:                 }
21556:                 if ($rule eq 'specific') {
21557:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
21558:                     if (exists($servers{$specifiedhost})) {
21559:                         $rule = $specifiedhost;
21560:                     }
21561:                 }
21562:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
21563:                 if (ref($currrules{$balancer}) eq 'HASH') {
21564:                     if ($rule ne $currrules{$balancer}{$type}) {
21565:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
21566:                     }
21567:                 } elsif ($rule ne '') {
21568:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
21569:                 }
21570:             }
21571:         }
21572:     }
21573:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
21574:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
21575:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
21576:             $defaultshash{'loadbalancing'} = {};
21577:         }
21578:         my $putresult = &Apache::lonnet::put_dom('configuration',
21579:                                                  \%defaultshash,$dom);
21580:         if ($putresult eq 'ok') {
21581:             if (keys(%changes) > 0) {
21582:                 my %toupdate;
21583:                 if (ref($changes{'delete'}) eq 'ARRAY') {
21584:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
21585:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
21586:                         $toupdate{$balancer} = 1;
21587:                     }
21588:                 }
21589:                 if (ref($changes{'add'}) eq 'ARRAY') {
21590:                     foreach my $balancer (sort(@{$changes{'add'}})) {
21591:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
21592:                         $toupdate{$balancer} = 1;
21593:                     }
21594:                 }
21595:                 if (ref($changes{'curr'}) eq 'HASH') {
21596:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
21597:                         $toupdate{$balancer} = 1;
21598:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
21599:                             if ($changes{'curr'}{$balancer}{'targets'}) {
21600:                                 my %offloadstr;
21601:                                 foreach my $sparetype (@sparestypes) {
21602:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21603:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
21604:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
21605:                                         }
21606:                                     }
21607:                                 }
21608:                                 if (keys(%offloadstr) == 0) {
21609:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
21610:                                 } else {
21611:                                     my $showoffload;
21612:                                     foreach my $sparetype (@sparestypes) {
21613:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
21614:                                         if (defined($offloadstr{$sparetype})) {
21615:                                             $showoffload .= $offloadstr{$sparetype};
21616:                                         } else {
21617:                                             $showoffload .= &mt('None');
21618:                                         }
21619:                                         $showoffload .= ('&nbsp;'x3);
21620:                                     }
21621:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
21622:                                 }
21623:                             }
21624:                         }
21625:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
21626:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
21627:                                 foreach my $type (@{$alltypes}) {
21628:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
21629:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
21630:                                         my $balancetext;
21631:                                         if ($rule eq '') {
21632:                                             $balancetext =  $ruletitles{'default'};
21633:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
21634:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
21635:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
21636:                                                 foreach my $sparetype (@sparestypes) {
21637:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21638:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
21639:                                                     }
21640:                                                 }
21641:                                                 foreach my $item (@{$alltypes}) {
21642:                                                     next if ($item =~  /^_LC_ipchange/);
21643:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
21644:                                                     if ($hasrule eq 'homeserver') {
21645:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
21646:                                                     } else {
21647:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
21648:                                                             if ($servers{$hasrule}) {
21649:                                                                 $toupdate{$hasrule} = 1;
21650:                                                             }
21651:                                                         }
21652:                                                     }
21653:                                                 }
21654:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
21655:                                                     $balancetext =  $ruletitles{$rule};
21656:                                                 } else {
21657:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
21658:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
21659:                                                     if ($receiver) {
21660:                                                         $toupdate{$receiver};
21661:                                                     }
21662:                                                 }
21663:                                             } else {
21664:                                                 $balancetext =  $ruletitles{$rule};
21665:                                             }
21666:                                         } else {
21667:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
21668:                                         }
21669:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
21670:                                     }
21671:                                 }
21672:                             }
21673:                         }
21674:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
21675:                             if ($currcookies{$balancer}) {
21676:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
21677:                                                           $balancer).'</li>';
21678:                             } else {
21679:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
21680:                                                           $balancer).'</li>';
21681:                             }
21682:                         }
21683:                     }
21684:                 }
21685:                 if (keys(%toupdate)) {
21686:                     my %thismachine;
21687:                     my $updatedhere;
21688:                     my $cachetime = 60*60*24;
21689:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21690:                     foreach my $lonhost (keys(%toupdate)) {
21691:                         if ($thismachine{$lonhost}) {
21692:                             unless ($updatedhere) {
21693:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
21694:                                                               $defaultshash{'loadbalancing'},
21695:                                                               $cachetime);
21696:                                 $updatedhere = 1;
21697:                             }
21698:                         } else {
21699:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
21700:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21701:                         }
21702:                     }
21703:                 }
21704:                 if ($resulttext ne '') {
21705:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
21706:                 } else {
21707:                     $resulttext = $nochgmsg;
21708:                 }
21709:             } else {
21710:                 $resulttext = $nochgmsg;
21711:             }
21712:         } else {
21713:             $resulttext = '<span class="LC_error">'.
21714:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21715:         }
21716:     } else {
21717:         $resulttext = $nochgmsg;
21718:     }
21719:     return $resulttext;
21720: }
21721: 
21722: sub recurse_check {
21723:     my ($chkcats,$categories,$depth,$name) = @_;
21724:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
21725:         my $chg = 0;
21726:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
21727:             my $category = $chkcats->[$depth]{$name}[$j];
21728:             my $item;
21729:             if ($category eq '') {
21730:                 $chg ++;
21731:             } else {
21732:                 my $deeper = $depth + 1;
21733:                 $item = &escape($category).':'.&escape($name).':'.$depth;
21734:                 if ($chg) {
21735:                     $categories->{$item} -= $chg;
21736:                 }
21737:                 &recurse_check($chkcats,$categories,$deeper,$category);
21738:                 $deeper --;
21739:             }
21740:         }
21741:     }
21742:     return;
21743: }
21744: 
21745: sub recurse_cat_deletes {
21746:     my ($item,$coursecategories,$deletions) = @_;
21747:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
21748:     my $subdepth = $depth + 1;
21749:     if (ref($coursecategories) eq 'HASH') {
21750:         foreach my $subitem (keys(%{$coursecategories})) {
21751:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
21752:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
21753:                 delete($coursecategories->{$subitem});
21754:                 $deletions->{$subitem} = 1;
21755:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
21756:             }
21757:         }
21758:     }
21759:     return;
21760: }
21761: 
21762: sub active_dc_picker {
21763:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
21764:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
21765:     my @domcoord = keys(%domcoords);
21766:     if (keys(%currhash)) {
21767:         foreach my $dc (keys(%currhash)) {
21768:             unless (exists($domcoords{$dc})) {
21769:                 push(@domcoord,$dc);
21770:             }
21771:         }
21772:     }
21773:     @domcoord = sort(@domcoord);
21774:     my $numdcs = scalar(@domcoord);
21775:     my $rows = 0;
21776:     my $table;
21777:     if ($numdcs > 1) {
21778:         $table = '<table>';
21779:         for (my $i=0; $i<@domcoord; $i++) {
21780:             my $rem = $i%($numinrow);
21781:             if ($rem == 0) {
21782:                 if ($i > 0) {
21783:                     $table .= '</tr>';
21784:                 }
21785:                 $table .= '<tr>';
21786:                 $rows ++;
21787:             }
21788:             my $check = '';
21789:             if ($inputtype eq 'radio') {
21790:                 if (keys(%currhash) == 0) {
21791:                     if (!$i) {
21792:                         $check = ' checked="checked"';
21793:                     }
21794:                 } elsif (exists($currhash{$domcoord[$i]})) {
21795:                     $check = ' checked="checked"';
21796:                 }
21797:             } else {
21798:                 if (exists($currhash{$domcoord[$i]})) {
21799:                     $check = ' checked="checked"';
21800:                 }
21801:             }
21802:             if ($i == @domcoord - 1) {
21803:                 my $colsleft = $numinrow - $rem;
21804:                 if ($colsleft > 1) {
21805:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
21806:                 } else {
21807:                     $table .= '<td class="LC_left_item">';
21808:                 }
21809:             } else {
21810:                 $table .= '<td class="LC_left_item">';
21811:             }
21812:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
21813:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
21814:             $table .= '<span class="LC_nobreak"><label>'.
21815:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
21816:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
21817:             if ($user ne $dcname.':'.$dcdom) {
21818:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21819:             }
21820:             $table .= '</label></span></td>';
21821:         }
21822:         $table .= '</tr></table>';
21823:     } elsif ($numdcs == 1) {
21824:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
21825:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
21826:         if ($inputtype eq 'radio') {
21827:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
21828:             if ($user ne $dcname.':'.$dcdom) {
21829:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21830:             }
21831:         } else {
21832:             my $check;
21833:             if (exists($currhash{$domcoord[0]})) {
21834:                 $check = ' checked="checked"';
21835:             }
21836:             $table = '<span class="LC_nobreak"><label>'.
21837:                      '<input type="checkbox" name="'.$name.'" '.
21838:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
21839:             if ($user ne $dcname.':'.$dcdom) {
21840:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21841:             }
21842:             $table .= '</label></span>';
21843:             $rows ++;
21844:         }
21845:     }
21846:     return ($numdcs,$table,$rows);
21847: }
21848: 
21849: sub usersession_titles {
21850:     return &Apache::lonlocal::texthash(
21851:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
21852:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
21853:                spares => 'Servers offloaded to, when busy',
21854:                version => 'LON-CAPA version requirement',
21855:                excludedomain => 'Allow all, but exclude specific domains',
21856:                includedomain => 'Deny all, but include specific domains',
21857:                primary => 'Primary (checked first)',
21858:                default => 'Default',
21859:            );
21860: }
21861: 
21862: sub id_for_thisdom {
21863:     my (%servers) = @_;
21864:     my %altids;
21865:     foreach my $server (keys(%servers)) {
21866:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
21867:         if ($serverhome ne $server) {
21868:             $altids{$serverhome} = $server;
21869:         }
21870:     }
21871:     return %altids;
21872: }
21873: 
21874: sub count_servers {
21875:     my ($currbalancer,%servers) = @_;
21876:     my (@spares,$numspares);
21877:     foreach my $lonhost (sort(keys(%servers))) {
21878:         next if ($currbalancer eq $lonhost);
21879:         push(@spares,$lonhost);
21880:     }
21881:     if ($currbalancer) {
21882:         $numspares = scalar(@spares);
21883:     } else {
21884:         $numspares = scalar(@spares) - 1;
21885:     }
21886:     return ($numspares,@spares);
21887: }
21888: 
21889: sub lonbalance_targets_js {
21890:     my ($dom,$types,$servers,$settings) = @_;
21891:     my $select = &mt('Select');
21892:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
21893:     if (ref($servers) eq 'HASH') {
21894:         $alltargets = join("','",sort(keys(%{$servers})));
21895:         my @homedoms;
21896:         foreach my $server (sort(keys(%{$servers}))) {
21897:             if (&Apache::lonnet::host_domain($server) eq $dom) {
21898:                 push(@homedoms,'1');
21899:             } else {
21900:                 push(@homedoms,'0');
21901:             }
21902:         }
21903:         $allishome = join("','",@homedoms);
21904:     }
21905:     if (ref($types) eq 'ARRAY') {
21906:         if (@{$types} > 0) {
21907:             @alltypes = @{$types};
21908:         }
21909:     }
21910:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
21911:     $allinsttypes = join("','",@alltypes);
21912:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
21913:     if (ref($settings) eq 'HASH') {
21914:         %existing = %{$settings};
21915:     }
21916:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
21917:                               \%currtargets,\%currrules,\%currcookies);
21918:     my $balancers = join("','",sort(keys(%currbalancer)));
21919:     return <<"END";
21920: 
21921: <script type="text/javascript">
21922: // <![CDATA[
21923: 
21924: currBalancers = new Array('$balancers');
21925: 
21926: function toggleTargets(balnum) {
21927:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
21928:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
21929:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
21930:     var prevbalancer = prevhostitem.value;
21931:     var baltotal = document.getElementById('loadbalancing_total').value;
21932:     prevhostitem.value = balancer;
21933:     if (prevbalancer != '') {
21934:         var prevIdx = currBalancers.indexOf(prevbalancer);
21935:         if (prevIdx != -1) {
21936:             currBalancers.splice(prevIdx,1);
21937:         }
21938:     }
21939:     if (balancer == '') {
21940:         hideSpares(balnum);
21941:     } else {
21942:         var currIdx = currBalancers.indexOf(balancer);
21943:         if (currIdx == -1) {
21944:             currBalancers.push(balancer);
21945:         }
21946:         var homedoms = new Array('$allishome');
21947:         var ishomedom = homedoms[lonhostitem.selectedIndex];
21948:         showSpares(balancer,ishomedom,balnum);
21949:     }
21950:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
21951:     return;
21952: }
21953: 
21954: function showSpares(balancer,ishomedom,balnum) {
21955:     var alltargets = new Array('$alltargets');
21956:     var insttypes = new Array('$allinsttypes');
21957:     var offloadtypes = new Array('primary','default');
21958: 
21959:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
21960:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
21961:  
21962:     for (var i=0; i<offloadtypes.length; i++) {
21963:         var count = 0;
21964:         for (var j=0; j<alltargets.length; j++) {
21965:             if (alltargets[j] != balancer) {
21966:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
21967:                 item.value = alltargets[j];
21968:                 item.style.textAlign='left';
21969:                 item.style.textFace='normal';
21970:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
21971:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
21972:                     item.disabled = '';
21973:                 } else {
21974:                     item.disabled = 'disabled';
21975:                     item.checked = false;
21976:                 }
21977:                 count ++;
21978:             }
21979:         }
21980:     }
21981:     for (var k=0; k<insttypes.length; k++) {
21982:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
21983:             if (ishomedom == 1) {
21984:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21985:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21986:             } else {
21987:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21988:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21989:             }
21990:         } else {
21991:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21992:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21993:         }
21994:         if ((insttypes[k] != '_LC_external') && 
21995:             ((insttypes[k] != '_LC_internetdom') ||
21996:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
21997:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
21998:             item.options.length = 0;
21999:             item.options[0] = new Option("","",true,true);
22000:             var idx = 0;
22001:             for (var m=0; m<alltargets.length; m++) {
22002:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
22003:                     idx ++;
22004:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22005:                 }
22006:             }
22007:         }
22008:     }
22009:     return;
22010: }
22011: 
22012: function hideSpares(balnum) {
22013:     var alltargets = new Array('$alltargets');
22014:     var insttypes = new Array('$allinsttypes');
22015:     var offloadtypes = new Array('primary','default');
22016: 
22017:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
22018:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
22019: 
22020:     var total = alltargets.length - 1;
22021:     for (var i=0; i<offloadtypes; i++) {
22022:         for (var j=0; j<total; j++) {
22023:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
22024:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
22025:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
22026:         }
22027:     }
22028:     for (var k=0; k<insttypes.length; k++) {
22029:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22030:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22031:         if (insttypes[k] != '_LC_external') {
22032:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
22033:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
22034:         }
22035:     }
22036:     return;
22037: }
22038: 
22039: function checkOffloads(item,balnum,type) {
22040:     var alltargets = new Array('$alltargets');
22041:     var offloadtypes = new Array('primary','default');
22042:     if (item.checked) {
22043:         var total = alltargets.length - 1;
22044:         var other;
22045:         if (type == offloadtypes[0]) {
22046:             other = offloadtypes[1];
22047:         } else {
22048:             other = offloadtypes[0];
22049:         }
22050:         for (var i=0; i<total; i++) {
22051:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
22052:             if (server == item.value) {
22053:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
22054:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
22055:                 }
22056:             }
22057:         }
22058:     }
22059:     return;
22060: }
22061: 
22062: function singleServerToggle(balnum,type) {
22063:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
22064:     if (offloadtoSelIdx == 0) {
22065:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
22066:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22067: 
22068:     } else {
22069:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
22070:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22071:     }
22072:     return;
22073: }
22074: 
22075: function balanceruleChange(formname,balnum,type) {
22076:     if (type == '_LC_external') {
22077:         return;
22078:     }
22079:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
22080:     for (var i=0; i<typesRules.length; i++) {
22081:         if (formname.elements[typesRules[i]].checked) {
22082:             if (formname.elements[typesRules[i]].value != 'specific') {
22083:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
22084:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22085:             } else {
22086:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22087:             }
22088:         }
22089:     }
22090:     return;
22091: }
22092: 
22093: function balancerDeleteChange(balnum) {
22094:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22095:     var baltotal = document.getElementById('loadbalancing_total').value;
22096:     var addtarget;
22097:     var removetarget;
22098:     var action = 'delete';
22099:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
22100:         var lonhost = hostitem.value;
22101:         var currIdx = currBalancers.indexOf(lonhost);
22102:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
22103:             if (currIdx != -1) {
22104:                 currBalancers.splice(currIdx,1);
22105:             }
22106:             addtarget = lonhost;
22107:         } else {
22108:             if (currIdx == -1) {
22109:                 currBalancers.push(lonhost);
22110:             }
22111:             removetarget = lonhost;
22112:             action = 'undelete';
22113:         }
22114:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
22115:     }
22116:     return;
22117: }
22118: 
22119: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
22120:     if (baltotal > 1) {
22121:         var offloadtypes = new Array('primary','default');
22122:         var alltargets = new Array('$alltargets');
22123:         var insttypes = new Array('$allinsttypes');
22124:         for (var i=0; i<baltotal; i++) {
22125:             if (i != balnum) {
22126:                 for (var j=0; j<offloadtypes.length; j++) {
22127:                     var total = alltargets.length - 1;
22128:                     for (var k=0; k<total; k++) {
22129:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
22130:                         var server = serveritem.value;
22131:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
22132:                             if (server == addtarget) {
22133:                                 serveritem.disabled = '';
22134:                             }
22135:                         }
22136:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22137:                             if (server == removetarget) {
22138:                                 serveritem.disabled = 'disabled';
22139:                                 serveritem.checked = false;
22140:                             }
22141:                         }
22142:                     }
22143:                 }
22144:                 for (var j=0; j<insttypes.length; j++) {
22145:                     if (insttypes[j] != '_LC_external') {
22146:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
22147:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
22148:                             var currSel = singleserver.selectedIndex;
22149:                             var currVal = singleserver.options[currSel].value;
22150:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
22151:                                 var numoptions = singleserver.options.length;
22152:                                 var needsnew = 1;
22153:                                 for (var k=0; k<numoptions; k++) {
22154:                                     if (singleserver.options[k] == addtarget) {
22155:                                         needsnew = 0;
22156:                                         break;
22157:                                     }
22158:                                 }
22159:                                 if (needsnew == 1) {
22160:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
22161:                                 }
22162:                             }
22163:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22164:                                 singleserver.options.length = 0;
22165:                                 if ((currVal) && (currVal != removetarget)) {
22166:                                     singleserver.options[0] = new Option("","",false,false);
22167:                                 } else {
22168:                                     singleserver.options[0] = new Option("","",true,true);
22169:                                 }
22170:                                 var idx = 0;
22171:                                 for (var m=0; m<alltargets.length; m++) {
22172:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
22173:                                         idx ++;
22174:                                         if (currVal == alltargets[m]) {
22175:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
22176:                                         } else {
22177:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22178:                                         }
22179:                                     }
22180:                                 }
22181:                             }
22182:                         }
22183:                     }
22184:                 }
22185:             }
22186:         }
22187:     }
22188:     return;
22189: }
22190: 
22191: // ]]>
22192: </script>
22193: 
22194: END
22195: }
22196: 
22197: 
22198: sub new_spares_js {
22199:     my @sparestypes = ('primary','default');
22200:     my $types = join("','",@sparestypes);
22201:     my $select = &mt('Select');
22202:     return <<"END";
22203: 
22204: <script type="text/javascript">
22205: // <![CDATA[
22206: 
22207: function updateNewSpares(formname,lonhost) {
22208:     var types = new Array('$types');
22209:     var include = new Array();
22210:     var exclude = new Array();
22211:     for (var i=0; i<types.length; i++) {
22212:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
22213:         for (var j=0; j<spareboxes.length; j++) {
22214:             if (formname.elements[spareboxes[j]].checked) {
22215:                 exclude.push(formname.elements[spareboxes[j]].value);
22216:             } else {
22217:                 include.push(formname.elements[spareboxes[j]].value);
22218:             }
22219:         }
22220:     }
22221:     for (var i=0; i<types.length; i++) {
22222:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
22223:         var selIdx = newSpare.selectedIndex;
22224:         var currnew = newSpare.options[selIdx].value;
22225:         var okSpares = new Array();
22226:         for (var j=0; j<newSpare.options.length; j++) {
22227:             var possible = newSpare.options[j].value;
22228:             if (possible != '') {
22229:                 if (exclude.indexOf(possible) == -1) {
22230:                     okSpares.push(possible);
22231:                 } else {
22232:                     if (currnew == possible) {
22233:                         selIdx = 0;
22234:                     }
22235:                 }
22236:             }
22237:         }
22238:         for (var k=0; k<include.length; k++) {
22239:             if (okSpares.indexOf(include[k]) == -1) {
22240:                 okSpares.push(include[k]);
22241:             }
22242:         }
22243:         okSpares.sort();
22244:         newSpare.options.length = 0;
22245:         if (selIdx == 0) {
22246:             newSpare.options[0] = new Option("$select","",true,true);
22247:         } else {
22248:             newSpare.options[0] = new Option("$select","",false,false);
22249:         }
22250:         for (var m=0; m<okSpares.length; m++) {
22251:             var idx = m+1;
22252:             var selThis = 0;
22253:             if (selIdx != 0) {
22254:                 if (okSpares[m] == currnew) {
22255:                     selThis = 1;
22256:                 }
22257:             }
22258:             if (selThis == 1) {
22259:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
22260:             } else {
22261:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
22262:             }
22263:         }
22264:     }
22265:     return;
22266: }
22267: 
22268: function checkNewSpares(lonhost,type) {
22269:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
22270:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
22271:     if (chosen != '') {
22272:         var othertype;
22273:         var othernewSpare;
22274:         if (type == 'primary') {
22275:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
22276:         }
22277:         if (type == 'default') {
22278:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
22279:         }
22280:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
22281:             othernewSpare.selectedIndex = 0;
22282:         }
22283:     }
22284:     return;
22285: }
22286: 
22287: // ]]>
22288: </script>
22289: 
22290: END
22291: 
22292: }
22293: 
22294: sub common_domprefs_js {
22295:     return <<"END";
22296: 
22297: <script type="text/javascript">
22298: // <![CDATA[
22299: 
22300: function getIndicesByName(formname,item) {
22301:     var group = new Array();
22302:     for (var i=0;i<formname.elements.length;i++) {
22303:         if (formname.elements[i].name == item) {
22304:             group.push(formname.elements[i].id);
22305:         }
22306:     }
22307:     return group;
22308: }
22309: 
22310: // ]]>
22311: </script>
22312: 
22313: END
22314: 
22315: }
22316: 
22317: sub recaptcha_js {
22318:     my %lt = &captcha_phrases();
22319:     return <<"END";
22320: 
22321: <script type="text/javascript">
22322: // <![CDATA[
22323: 
22324: function updateCaptcha(caller,context) {
22325:     var privitem;
22326:     var pubitem;
22327:     var privtext;
22328:     var pubtext;
22329:     var versionitem;
22330:     var versiontext;
22331:     if (document.getElementById(context+'_recaptchapub')) {
22332:         pubitem = document.getElementById(context+'_recaptchapub');
22333:     } else {
22334:         return;
22335:     }
22336:     if (document.getElementById(context+'_recaptchapriv')) {
22337:         privitem = document.getElementById(context+'_recaptchapriv');
22338:     } else {
22339:         return;
22340:     }
22341:     if (document.getElementById(context+'_recaptchapubtxt')) {
22342:         pubtext = document.getElementById(context+'_recaptchapubtxt');
22343:     } else {
22344:         return;
22345:     }
22346:     if (document.getElementById(context+'_recaptchaprivtxt')) {
22347:         privtext = document.getElementById(context+'_recaptchaprivtxt');
22348:     } else {
22349:         return;
22350:     }
22351:     if (document.getElementById(context+'_recaptchaversion')) {
22352:         versionitem = document.getElementById(context+'_recaptchaversion');
22353:     } else {
22354:         return;
22355:     }
22356:     if (document.getElementById(context+'_recaptchavertxt')) {
22357:         versiontext = document.getElementById(context+'_recaptchavertxt');
22358:     } else {
22359:         return;
22360:     }
22361:     if (caller.checked) {
22362:         if (caller.value == 'recaptcha') {
22363:             pubitem.type = 'text';
22364:             privitem.type = 'text';
22365:             pubitem.size = '40';
22366:             privitem.size = '40';
22367:             pubtext.innerHTML = "$lt{'pub'}";
22368:             privtext.innerHTML = "$lt{'priv'}";
22369:             versionitem.type = 'text';
22370:             versionitem.size = '3';
22371:             versiontext.innerHTML = "$lt{'ver'}";
22372:         } else {
22373:             pubitem.type = 'hidden';
22374:             privitem.type = 'hidden';
22375:             versionitem.type = 'hidden';
22376:             pubtext.innerHTML = '';
22377:             privtext.innerHTML = '';
22378:             versiontext.innerHTML = '';
22379:         }
22380:     }
22381:     return;
22382: }
22383: 
22384: // ]]>
22385: </script>
22386: 
22387: END
22388: 
22389: }
22390: 
22391: sub toggle_display_js {
22392:     return <<"END";
22393: 
22394: <script type="text/javascript">
22395: // <![CDATA[
22396: 
22397: function toggleDisplay(domForm,caller) {
22398:     if (document.getElementById(caller)) {
22399:         var divitem = document.getElementById(caller);
22400:         var optionsElement = domForm.coursecredits;
22401:         var checkval = 1;
22402:         var dispval = 'block';
22403:         var selfcreateRegExp = /^cancreate_emailverified/;
22404:         if (caller == 'emailoptions') {
22405:             optionsElement = domForm.cancreate_email;
22406:         }
22407:         if (caller == 'studentsubmission') {
22408:             optionsElement = domForm.postsubmit;
22409:         }
22410:         if (caller == 'cloneinstcode') {
22411:             optionsElement = domForm.canclone;
22412:             checkval = 'instcode';
22413:         }
22414:         if (selfcreateRegExp.test(caller)) {
22415:             optionsElement = domForm.elements[caller];
22416:             checkval = 'other';
22417:             dispval = 'inline'
22418:         }
22419:         if (optionsElement.length) {
22420:             var currval;
22421:             for (var i=0; i<optionsElement.length; i++) {
22422:                 if (optionsElement[i].checked) {
22423:                    currval = optionsElement[i].value;
22424:                 }
22425:             }
22426:             if (currval == checkval) {
22427:                 divitem.style.display = dispval;
22428:             } else {
22429:                 divitem.style.display = 'none';
22430:             }
22431:         }
22432:     }
22433:     return;
22434: }
22435: 
22436: // ]]>
22437: </script>
22438: 
22439: END
22440: 
22441: }
22442: 
22443: sub captcha_phrases {
22444:     return &Apache::lonlocal::texthash (
22445:                  priv => 'Private key',
22446:                  pub  => 'Public key',
22447:                  original  => 'original (CAPTCHA)',
22448:                  recaptcha => 'successor (ReCAPTCHA)',
22449:                  notused   => 'unused',
22450:                  ver => 'ReCAPTCHA version (1 or 2)',
22451:     );
22452: }
22453: 
22454: sub devalidate_remote_domconfs {
22455:     my ($dom,$cachekeys) = @_;
22456:     return unless (ref($cachekeys) eq 'HASH');
22457:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22458:     my %thismachine;
22459:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22460:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
22461:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml');
22462:     my %cache_by_lonhost;
22463:     if (exists($cachekeys->{'samllanding'})) {
22464:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
22465:             my %landing = %{$cachekeys->{'samllanding'}};
22466:             my %domservers = &Apache::lonnet::get_servers($dom);
22467:             if (keys(%domservers)) {
22468:                 foreach my $server (keys(%domservers)) {
22469:                     my @cached;
22470:                     next if ($thismachine{$server});
22471:                     if ($landing{$server}) {
22472:                         push(@cached,&escape('samllanding').':'.&escape($server));
22473:                     }
22474:                     if (@cached) {
22475:                         $cache_by_lonhost{$server} = \@cached;
22476:                     }
22477:                 }
22478:             }
22479:         }
22480:     }
22481:     if (keys(%servers)) {
22482:         foreach my $server (keys(%servers)) {
22483:             next if ($thismachine{$server});
22484:             my @cached;
22485:             foreach my $name (@posscached) {
22486:                 if ($cachekeys->{$name}) {
22487:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
22488:                         if (ref($cachekeys->{$name}) eq 'HASH') {
22489:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
22490:                                 push(@cached,&escape($name).':'.&escape($key));
22491:                             }
22492:                         }
22493:                     } else {
22494:                         push(@cached,&escape($name).':'.&escape($dom));
22495:                     }
22496:                 }
22497:             }
22498:             if ((exists($cache_by_lonhost{$server})) &&
22499:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
22500:                 push(@cached,@{$cache_by_lonhost{$server}});
22501:             }
22502:             if (@cached) {
22503:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
22504:             }
22505:         }
22506:     }
22507:     return;
22508: }
22509: 
22510: 1;

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